Skip to content

Commit

Permalink
Use genesis state to read network_id
Browse files Browse the repository at this point in the history
  • Loading branch information
foriequal0 committed Dec 10, 2019
1 parent 6152f37 commit 0d6c653
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 58 deletions.
6 changes: 3 additions & 3 deletions codechain/run_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use std::time::{SystemTime, UNIX_EPOCH};

use ccore::snapshot_notify;
use ccore::{
AccountProvider, AccountProviderError, BlockId, ChainNotify, Client, ClientConfig, ClientService, EngineInfo,
EngineType, Miner, MinerService, Scheme, Stratum, StratumConfig, StratumError, NUM_COLUMNS,
AccountProvider, AccountProviderError, ChainNotify, Client, ClientConfig, ClientService, EngineInfo, EngineType,
Miner, MinerService, Scheme, Stratum, StratumConfig, StratumError, NUM_COLUMNS,
};
use cdiscovery::{Config, Discovery};
use ckey::{Address, NetworkId, PlatformAddress};
Expand Down Expand Up @@ -286,7 +286,7 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {
let network_config = config.network_config()?;
// XXX: What should we do if the network id has been changed.
let c = client.client();
let network_id = c.common_params(BlockId::Number(0)).unwrap().network_id();
let network_id = c.network_id();
let routing_table = RoutingTable::new();
let service = network_start(network_id, timer_loop, &network_config, Arc::clone(&routing_table))?;

Expand Down
13 changes: 6 additions & 7 deletions core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ impl StateInfo for Client {
}

impl EngineInfo for Client {
fn network_id(&self) -> NetworkId {
self.common_params(BlockId::Earliest).expect("Genesis state should exist").network_id()
}

fn common_params(&self, block_id: BlockId) -> Option<CommonParams> {
self.state_info(block_id.into()).map(|state| {
state
Expand Down Expand Up @@ -528,7 +532,7 @@ impl EngineInfo for Client {
}

fn possible_authors(&self, block_number: Option<u64>) -> Result<Option<Vec<PlatformAddress>>, EngineError> {
let network_id = self.common_params(BlockId::Latest).unwrap().network_id();
let network_id = self.network_id();
if block_number == Some(0) {
let genesis_author = self.block_header(&0.into()).expect("genesis block").author();
return Ok(Some(vec![PlatformAddress::new_v1(network_id, genesis_author)]))
Expand Down Expand Up @@ -594,8 +598,7 @@ impl BlockChainTrait for Client {
}

fn genesis_accounts(&self) -> Vec<PlatformAddress> {
// XXX: What should we do if the network id has been changed
let network_id = self.common_params(BlockId::Latest).unwrap().network_id();
let network_id = self.network_id();
self.genesis_accounts.iter().map(|addr| PlatformAddress::new_v1(network_id, *addr)).collect()
}

Expand Down Expand Up @@ -945,10 +948,6 @@ impl MiningBlockChainClient for Client {
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
self.importer.miner.register_immune_users(immune_user_vec)
}

fn get_network_id(&self) -> NetworkId {
self.common_params(BlockId::Latest).unwrap().network_id()
}
}

impl ChainTimeInfo for Client {
Expand Down
4 changes: 1 addition & 3 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub trait BlockChainTrait {
}

pub trait EngineInfo: Send + Sync {
fn network_id(&self) -> NetworkId;
fn common_params(&self, block_id: BlockId) -> Option<CommonParams>;
fn metadata_seq(&self, block_id: BlockId) -> Option<u64>;
fn block_reward(&self, block_number: u64) -> u64;
Expand Down Expand Up @@ -300,9 +301,6 @@ pub trait MiningBlockChainClient: BlockChainClient + BlockProducer + FindActionH

/// Append designated users to the immune user list.
fn register_immune_users(&self, immune_user_vec: Vec<Address>);

/// Returns network id.
fn get_network_id(&self) -> NetworkId;
}

/// Provides methods to access database.
Expand Down
8 changes: 4 additions & 4 deletions core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,6 @@ impl MiningBlockChainClient for TestBlockChainClient {
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
self.miner.register_immune_users(immune_user_vec)
}

fn get_network_id(&self) -> NetworkId {
NetworkId::default()
}
}

impl AccountData for TestBlockChainClient {
Expand Down Expand Up @@ -657,6 +653,10 @@ impl super::EngineClient for TestBlockChainClient {
}

impl EngineInfo for TestBlockChainClient {
fn network_id(&self) -> NetworkId {
self.scheme.engine.machine().genesis_common_params().network_id()
}

fn common_params(&self, _block_id: BlockId) -> Option<CommonParams> {
Some(*self.scheme.engine.machine().genesis_common_params())
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/consensus/tendermint/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ impl Worker {
}

fn report_double_vote(&self, double: &DoubleVote) {
let network_id = self.client().common_params(BlockId::Latest).unwrap().network_id();
let network_id = self.client().network_id();
let seq = match self.signer.address() {
Some(address) => self.client().latest_seq(address),
None => {
Expand Down
18 changes: 8 additions & 10 deletions rpc/src/v1/impls/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use std::convert::TryInto;
use std::sync::Arc;
use std::time::Duration;

use ccore::{AccountData, AccountProvider, BlockId, EngineInfo, MinerService, MiningBlockChainClient, TermInfo};
use ckey::{NetworkId, Password, PlatformAddress, Signature};
use ccore::{AccountData, AccountProvider, EngineInfo, MinerService, MiningBlockChainClient, TermInfo};
use ckey::{Password, PlatformAddress, Signature};
use ctypes::transaction::IncompleteTransaction;
use jsonrpc_core::Result;
use parking_lot::Mutex;
Expand All @@ -46,11 +46,6 @@ where
miner,
}
}

fn network_id(&self) -> NetworkId {
// XXX: What should we do if the network id has been changed
self.client.common_params(BlockId::Latest).unwrap().network_id()
}
}

impl<C, M> Account for AccountClient<C, M>
Expand All @@ -62,21 +57,24 @@ where
self.account_provider
.get_list()
.map(|addresses| {
addresses.into_iter().map(|address| PlatformAddress::new_v1(self.network_id(), address)).collect()
addresses
.into_iter()
.map(|address| PlatformAddress::new_v1(self.client.network_id(), address))
.collect()
})
.map_err(account_provider)
}

fn create_account(&self, passphrase: Option<Password>) -> Result<PlatformAddress> {
let (address, _) =
self.account_provider.new_account_and_public(&passphrase.unwrap_or_default()).map_err(account_provider)?;
Ok(PlatformAddress::new_v1(self.network_id(), address))
Ok(PlatformAddress::new_v1(self.client.network_id(), address))
}

fn create_account_from_secret(&self, secret: H256, passphrase: Option<Password>) -> Result<PlatformAddress> {
self.account_provider
.insert_account(secret.into(), &passphrase.unwrap_or_default())
.map(|address| PlatformAddress::new_v1(self.network_id(), address))
.map(|address| PlatformAddress::new_v1(self.client.network_id(), address))
.map_err(account_provider)
}

Expand Down
36 changes: 11 additions & 25 deletions rpc/src/v1/impls/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ where
return Ok(None)
}
let block_id = block_number.map(BlockId::from).unwrap_or(BlockId::Latest);
Ok(self.client.get_text(transaction_hash, block_id).map_err(errors::transaction_state)?.map(|text| {
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
Text::from_core(text, self.client.common_params(parent_block_id).unwrap().network_id())
}))
Ok(self
.client
.get_text(transaction_hash, block_id)
.map_err(errors::transaction_state)?
.map(|text| Text::from_core(text, self.client.network_id())))
}

fn get_asset(
Expand Down Expand Up @@ -178,8 +179,7 @@ where
fn get_regular_key_owner(&self, public: Public, block_number: Option<u64>) -> Result<Option<PlatformAddress>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.regular_key_owner(&public_to_address(&public), block_id.into()).and_then(|address| {
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
let network_id = self.client.common_params(parent_block_id).unwrap().network_id();
let network_id = self.client.network_id();
Some(PlatformAddress::new_v1(network_id, address))
}))
}
Expand All @@ -206,17 +206,15 @@ where
fn get_shard_owners(&self, shard_id: ShardId, block_number: Option<u64>) -> Result<Option<Vec<PlatformAddress>>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.shard_owners(shard_id, block_id.into()).map(|owners| {
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
let network_id = self.client.common_params(parent_block_id).unwrap().network_id();
let network_id = self.client.network_id();
owners.into_iter().map(|owner| PlatformAddress::new_v1(network_id, owner)).collect()
}))
}

fn get_shard_users(&self, shard_id: ShardId, block_number: Option<u64>) -> Result<Option<Vec<PlatformAddress>>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.shard_users(shard_id, block_id.into()).map(|users| {
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
let network_id = self.client.common_params(parent_block_id).unwrap().network_id();
let network_id = self.client.network_id();
users.into_iter().map(|user| PlatformAddress::new_v1(network_id, user)).collect()
}))
}
Expand All @@ -239,26 +237,14 @@ where

fn get_block_by_number(&self, block_number: u64) -> Result<Option<Block>> {
let id = BlockId::Number(block_number);
Ok(self.client.block(&id).map(|block| {
let block_id_to_read_params = if block_number == 0 {
0.into()
} else {
(block_number - 1).into()
};
Block::from_core(block.decode(), self.client.common_params(block_id_to_read_params).unwrap().network_id())
}))
Ok(self.client.block(&id).map(|block| Block::from_core(block.decode(), self.client.network_id())))
}

fn get_block_by_hash(&self, block_hash: BlockHash) -> Result<Option<Block>> {
let id = BlockId::Hash(block_hash);
Ok(self.client.block(&id).map(|block| {
let block = block.decode();
let block_id_to_read_params = if block.header.number() == 0 {
0.into()
} else {
(*block.header.parent_hash()).into()
};
Block::from_core(block, self.client.common_params(block_id_to_read_params).unwrap().network_id())
Block::from_core(block, self.client.network_id())
}))
}

Expand Down Expand Up @@ -301,7 +287,7 @@ where
}

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

fn get_common_params(&self, block_number: Option<u64>) -> Result<Option<Params>> {
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/v1/impls/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
Ok(None)
} else {
// XXX: What should we do if the network id has been changed
let network_id = self.client.common_params(BlockId::Latest).unwrap().network_id();
let network_id = self.client.network_id();
Ok(Some(PlatformAddress::new_v1(network_id, author)))
}
}
Expand Down
8 changes: 4 additions & 4 deletions rpc/src/v1/impls/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::sync::Arc;

use ccore::{BlockChainClient, MiningBlockChainClient, SignedTransaction};
use ccore::{BlockChainClient, EngineInfo, MiningBlockChainClient, SignedTransaction};
use cjson::bytes::Bytes;
use ckey::{Address, PlatformAddress};
use ctypes::{Tracker, TxHash};
Expand All @@ -42,7 +42,7 @@ impl<C> MempoolClient<C> {

impl<C> Mempool for MempoolClient<C>
where
C: BlockChainClient + MiningBlockChainClient + 'static,
C: BlockChainClient + MiningBlockChainClient + EngineInfo + 'static,
{
fn send_signed_transaction(&self, raw: Bytes) -> Result<TxHash> {
Rlp::new(&raw.into_vec())
Expand Down Expand Up @@ -82,7 +82,7 @@ where

fn get_banned_accounts(&self) -> Result<Vec<PlatformAddress>> {
let malicious_user_vec = self.client.get_malicious_users();
let network_id = self.client.get_network_id();
let network_id = self.client.network_id();
Ok(malicious_user_vec.into_iter().map(|address| PlatformAddress::new_v1(network_id, address)).collect())
}

Expand All @@ -102,7 +102,7 @@ where

fn get_immune_accounts(&self) -> Result<Vec<PlatformAddress>> {
let immune_user_vec = self.client.get_immune_users();
let network_id = self.client.get_network_id();
let network_id = self.client.network_id();
Ok(immune_user_vec.into_iter().map(|address| PlatformAddress::new_v1(network_id, address)).collect())
}

Expand Down

0 comments on commit 0d6c653

Please sign in to comment.