Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions key/src/platform_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ impl<'a> Deserialize<'a> for PlatformAddress {
}
}

impl From<PlatformAddress> for Address {
fn from(address: PlatformAddress) -> Self {
address.address
}
}

struct PlatformAddressVisitor;

impl<'a> Visitor<'a> for PlatformAddressVisitor {
Expand Down
34 changes: 23 additions & 11 deletions rpc/src/v1/impls/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ use ccore::{
AssetClient, BlockId, EngineInfo, ExecuteClient, MinerService, MiningBlockChainClient, RegularKey, Shard,
SignedParcel, UnverifiedParcel,
};
use ckey::{Address, NetworkId, Public};
use ckey::{NetworkId, PlatformAddress, Public};
use cstate::{AssetScheme, AssetSchemeAddress, OwnedAsset};
use ctypes::invoice::{ParcelInvoice, TransactionInvoice};
use ctypes::parcel::Action;
use ctypes::{BlockNumber, ShardId, WorldId};
use primitives::{H160, H256, U256};
use primitives::{H256, U256};
use rlp::{DecoderError, UntrustedRlp};

use jsonrpc_core::Result;
Expand Down Expand Up @@ -135,17 +135,17 @@ where
self.client.is_asset_spent(transaction_hash, index, shard_id, block_id).map_err(errors::parcel_state)
}

fn get_nonce(&self, address: H160, block_number: Option<u64>) -> Result<Option<U256>> {
fn get_nonce(&self, address: PlatformAddress, block_number: Option<u64>) -> Result<Option<U256>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.nonce(&address.into(), block_id))
}

fn get_balance(&self, address: H160, block_number: Option<u64>) -> Result<Option<U256>> {
fn get_balance(&self, address: PlatformAddress, block_number: Option<u64>) -> Result<Option<U256>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.balance(&address.into(), block_id.into()))
}

fn get_regular_key(&self, address: H160, block_number: Option<u64>) -> Result<Option<Public>> {
fn get_regular_key(&self, address: PlatformAddress, block_number: Option<u64>) -> Result<Option<Public>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.regular_key(&address.into(), block_id.into()))
}
Expand Down Expand Up @@ -177,34 +177,46 @@ where
}

fn get_block_by_number(&self, block_number: u64) -> Result<Option<Block>> {
Ok(self.client.block(BlockId::Number(block_number)).map(|block| block.decode().into()))
Ok(self
.client
.block(BlockId::Number(block_number))
.map(|block| Block::from_core(block.decode(), self.client.common_params().network_id)))
}

fn get_block_by_hash(&self, block_hash: H256) -> Result<Option<Block>> {
Ok(self.client.block(BlockId::Hash(block_hash)).map(|block| block.decode().into()))
Ok(self
.client
.block(BlockId::Hash(block_hash))
.map(|block| Block::from_core(block.decode(), self.client.common_params().network_id)))
}

fn get_pending_parcels(&self) -> Result<Vec<Parcel>> {
Ok(self.client.ready_parcels().into_iter().map(|signed| signed.into()).collect())
}

fn get_coinbase(&self) -> Result<Option<Address>> {
fn get_coinbase(&self) -> Result<Option<PlatformAddress>> {
if self.miner.author().is_zero() {
Ok(None)
} else {
Ok(Some(self.miner.author()))
const VERSION: u8 = 0;
let network_id = self.client.common_params().network_id;
Ok(Some(PlatformAddress::create(VERSION, network_id, self.miner.author())))
}
}

fn get_network_id(&self) -> Result<NetworkId> {
Ok(self.client.common_params().network_id)
}

fn execute_change_shard_state(&self, transactions: Vec<Transaction>, sender: Address) -> Result<Vec<ChangeShard>> {
fn execute_change_shard_state(
&self,
transactions: Vec<Transaction>,
sender: PlatformAddress,
) -> Result<Vec<ChangeShard>> {
let transaction_types: Vec<_> = transactions.into_iter().map(From::from).collect();
Ok(self
.client
.execute_transactions(&transaction_types, &sender)
.execute_transactions(&transaction_types, &sender.into())
.map_err(errors::core)?
.into_iter()
.map(From::from)
Expand Down
14 changes: 7 additions & 7 deletions rpc/src/v1/traits/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use ckey::{Address, NetworkId, Public};
use ckey::{NetworkId, PlatformAddress, Public};
use cstate::{AssetScheme, OwnedAsset};
use ctypes::invoice::{ParcelInvoice, TransactionInvoice};
use ctypes::{BlockNumber, ShardId, WorldId};
use primitives::{H160, H256, U256};
use primitives::{H256, U256};

use jsonrpc_core::Result;

Expand Down Expand Up @@ -64,15 +64,15 @@ build_rpc_trait! {

/// Gets nonce with given account.
# [rpc(name = "chain_getNonce")]
fn get_nonce(&self, H160, Option<u64>) -> Result<Option<U256>>;
fn get_nonce(&self, PlatformAddress, Option<u64>) -> Result<Option<U256>>;

/// Gets balance with given account.
# [rpc(name = "chain_getBalance")]
fn get_balance(&self, H160, Option<u64>) -> Result<Option<U256>>;
fn get_balance(&self, PlatformAddress, Option<u64>) -> Result<Option<U256>>;

/// Gets regular key with given account
# [rpc(name = "chain_getRegularKey")]
fn get_regular_key(&self, H160, Option<u64>) -> Result<Option<Public>>;
fn get_regular_key(&self, PlatformAddress, Option<u64>) -> Result<Option<Public>>;

/// Gets the number of shards
# [rpc(name = "chain_getNumberOfShards")]
Expand Down Expand Up @@ -108,14 +108,14 @@ build_rpc_trait! {

/// Gets coinbase's account id
# [rpc(name = "chain_getCoinbase")]
fn get_coinbase(&self) -> Result<Option<Address>>;
fn get_coinbase(&self) -> Result<Option<PlatformAddress>>;

/// Return the network id that is used in this chain.
# [rpc(name = "chain_getNetworkId")]
fn get_network_id(&self) -> Result<NetworkId>;

/// Execute Transactions
# [rpc(name = "chain_executeTransactions")]
fn execute_change_shard_state(&self, Vec<Transaction>, Address) -> Result<Vec<ChangeShard>>;
fn execute_change_shard_state(&self, Vec<Transaction>, PlatformAddress) -> Result<Vec<ChangeShard>>;
}
}
25 changes: 13 additions & 12 deletions rpc/src/v1/types/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use ckey::{Address, Public, Signature};
use ckey::{NetworkId, PlatformAddress, Public, Signature};
use ctypes::parcel::{Action as ActionType, ChangeShard as ChangeShardType};
use ctypes::ShardId;
use primitives::{Bytes, H256, U256};
Expand All @@ -39,7 +39,7 @@ pub enum Action {
signatures: Vec<Signature>,
},
Payment {
receiver: Address,
receiver: PlatformAddress,
/// Transferred amount.
amount: U256,
},
Expand All @@ -49,11 +49,11 @@ pub enum Action {
CreateShard,
ChangeShardOwners {
shard_id: ShardId,
owners: Vec<Address>,
owners: Vec<PlatformAddress>,
},
ChangeShardUsers {
shard_id: ShardId,
users: Vec<Address>,
users: Vec<PlatformAddress>,
},
Custom(Bytes),
}
Expand All @@ -68,8 +68,9 @@ impl From<ChangeShardType> for ChangeShard {
}
}

impl From<ActionType> for Action {
fn from(from: ActionType) -> Self {
impl Action {
pub fn from_core(from: ActionType, network_id: NetworkId) -> Self {
const VERSION: u8 = 0;
match from {
ActionType::ChangeShardState {
transactions,
Expand All @@ -84,7 +85,7 @@ impl From<ActionType> for Action {
receiver,
amount,
} => Action::Payment {
receiver,
receiver: PlatformAddress::create(VERSION, network_id, receiver),
amount,
},
ActionType::SetRegularKey {
Expand All @@ -98,14 +99,14 @@ impl From<ActionType> for Action {
owners,
} => Action::ChangeShardOwners {
shard_id,
owners,
owners: owners.into_iter().map(|owner| PlatformAddress::create(VERSION, network_id, owner)).collect(),
},
ActionType::ChangeShardUsers {
shard_id,
users,
} => Action::ChangeShardUsers {
shard_id,
users,
users: users.into_iter().map(|user| PlatformAddress::create(VERSION, network_id, user)).collect(),
},
ActionType::Custom(bytes) => Action::Custom(bytes),
}
Expand Down Expand Up @@ -138,7 +139,7 @@ impl From<Action> for ActionType {
receiver,
amount,
} => ActionType::Payment {
receiver,
receiver: receiver.into(),
amount,
},
Action::SetRegularKey {
Expand All @@ -152,14 +153,14 @@ impl From<Action> for ActionType {
owners,
} => ActionType::ChangeShardOwners {
shard_id,
owners,
owners: owners.into_iter().map(Into::into).collect(),
},
Action::ChangeShardUsers {
shard_id,
users,
} => ActionType::ChangeShardUsers {
shard_id,
users,
users: users.into_iter().map(Into::into).collect(),
},
Action::Custom(bytes) => ActionType::Custom(bytes),
}
Expand Down
18 changes: 10 additions & 8 deletions rpc/src/v1/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use ccore::Block as CoreBlock;
use ckey::Address;
use ckey::{NetworkId, PlatformAddress};
use ctypes::BlockNumber;
use primitives::{H256, U256};

use super::Parcel;
use super::{Action, Parcel};

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Block {
parent_hash: H256,
timestamp: u64,
number: u64,
author: Address,
author: PlatformAddress,

extra_data: Vec<u8>,

Expand All @@ -42,15 +42,16 @@ pub struct Block {
parcels: Vec<Parcel>,
}

impl From<CoreBlock> for Block {
fn from(block: CoreBlock) -> Self {
impl Block {
pub fn from_core(block: CoreBlock, network_id: NetworkId) -> Self {
let block_number = block.header.number();
let block_hash = block.header.hash();
const VERSION: u8 = 0;
Block {
parent_hash: block.header.parent_hash().clone(),
timestamp: block.header.timestamp(),
number: block.header.number(),
author: block.header.author().clone(),
author: PlatformAddress::create(VERSION, network_id, block.header.author().clone()),

extra_data: block.header.extra_data().clone(),

Expand All @@ -68,14 +69,15 @@ impl From<CoreBlock> for Block {
.enumerate()
.map(|(i, unverified)| {
let sig = unverified.signature();
let network_id = unverified.as_unsigned().network_id;
Parcel {
block_number: Some(block_number),
block_hash: Some(block_hash),
parcel_index: Some(i),
nonce: unverified.as_unsigned().nonce.clone(),
fee: unverified.as_unsigned().fee.clone(),
network_id: unverified.as_unsigned().network_id,
action: unverified.as_unsigned().action.clone().into(),
network_id,
action: Action::from_core(unverified.as_unsigned().action.clone(), network_id),
hash: unverified.hash(),
sig: sig.into(),
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/v1/types/parcel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl From<LocalizedParcel> for Parcel {
nonce: p.nonce,
fee: p.fee,
network_id: p.network_id,
action: p.action.clone().into(),
action: Action::from_core(p.as_unsigned().action.clone(), p.network_id),
hash: p.hash(),
sig: sig.into(),
}
Expand All @@ -61,7 +61,7 @@ impl From<SignedParcel> for Parcel {
nonce: p.nonce,
fee: p.fee,
network_id: p.network_id,
action: p.action.clone().into(),
action: Action::from_core(p.as_unsigned().action.clone(), p.network_id),
hash: p.hash(),
sig: sig.into(),
}
Expand Down
Loading