Skip to content

Commit 0d6c653

Browse files
committed
Use genesis state to read network_id
1 parent 6152f37 commit 0d6c653

File tree

9 files changed

+39
-58
lines changed

9 files changed

+39
-58
lines changed

codechain/run_node.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use std::time::{SystemTime, UNIX_EPOCH};
2121

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

core/src/client/client.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ impl StateInfo for Client {
491491
}
492492

493493
impl EngineInfo for Client {
494+
fn network_id(&self) -> NetworkId {
495+
self.common_params(BlockId::Earliest).expect("Genesis state should exist").network_id()
496+
}
497+
494498
fn common_params(&self, block_id: BlockId) -> Option<CommonParams> {
495499
self.state_info(block_id.into()).map(|state| {
496500
state
@@ -528,7 +532,7 @@ impl EngineInfo for Client {
528532
}
529533

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

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

@@ -945,10 +948,6 @@ impl MiningBlockChainClient for Client {
945948
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
946949
self.importer.miner.register_immune_users(immune_user_vec)
947950
}
948-
949-
fn get_network_id(&self) -> NetworkId {
950-
self.common_params(BlockId::Latest).unwrap().network_id()
951-
}
952951
}
953952

954953
impl ChainTimeInfo for Client {

core/src/client/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub trait BlockChainTrait {
8989
}
9090

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

301302
/// Append designated users to the immune user list.
302303
fn register_immune_users(&self, immune_user_vec: Vec<Address>);
303-
304-
/// Returns network id.
305-
fn get_network_id(&self) -> NetworkId;
306304
}
307305

308306
/// Provides methods to access database.

core/src/client/test_client.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,6 @@ impl MiningBlockChainClient for TestBlockChainClient {
381381
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
382382
self.miner.register_immune_users(immune_user_vec)
383383
}
384-
385-
fn get_network_id(&self) -> NetworkId {
386-
NetworkId::default()
387-
}
388384
}
389385

390386
impl AccountData for TestBlockChainClient {
@@ -657,6 +653,10 @@ impl super::EngineClient for TestBlockChainClient {
657653
}
658654

659655
impl EngineInfo for TestBlockChainClient {
656+
fn network_id(&self) -> NetworkId {
657+
self.scheme.engine.machine().genesis_common_params().network_id()
658+
}
659+
660660
fn common_params(&self, _block_id: BlockId) -> Option<CommonParams> {
661661
Some(*self.scheme.engine.machine().genesis_common_params())
662662
}

core/src/consensus/tendermint/worker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ impl Worker {
14771477
}
14781478

14791479
fn report_double_vote(&self, double: &DoubleVote) {
1480-
let network_id = self.client().common_params(BlockId::Latest).unwrap().network_id();
1480+
let network_id = self.client().network_id();
14811481
let seq = match self.signer.address() {
14821482
Some(address) => self.client().latest_seq(address),
14831483
None => {

rpc/src/v1/impls/account.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use std::convert::TryInto;
1818
use std::sync::Arc;
1919
use std::time::Duration;
2020

21-
use ccore::{AccountData, AccountProvider, BlockId, EngineInfo, MinerService, MiningBlockChainClient, TermInfo};
22-
use ckey::{NetworkId, Password, PlatformAddress, Signature};
21+
use ccore::{AccountData, AccountProvider, EngineInfo, MinerService, MiningBlockChainClient, TermInfo};
22+
use ckey::{Password, PlatformAddress, Signature};
2323
use ctypes::transaction::IncompleteTransaction;
2424
use jsonrpc_core::Result;
2525
use parking_lot::Mutex;
@@ -46,11 +46,6 @@ where
4646
miner,
4747
}
4848
}
49-
50-
fn network_id(&self) -> NetworkId {
51-
// XXX: What should we do if the network id has been changed
52-
self.client.common_params(BlockId::Latest).unwrap().network_id()
53-
}
5449
}
5550

5651
impl<C, M> Account for AccountClient<C, M>
@@ -62,21 +57,24 @@ where
6257
self.account_provider
6358
.get_list()
6459
.map(|addresses| {
65-
addresses.into_iter().map(|address| PlatformAddress::new_v1(self.network_id(), address)).collect()
60+
addresses
61+
.into_iter()
62+
.map(|address| PlatformAddress::new_v1(self.client.network_id(), address))
63+
.collect()
6664
})
6765
.map_err(account_provider)
6866
}
6967

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

7674
fn create_account_from_secret(&self, secret: H256, passphrase: Option<Password>) -> Result<PlatformAddress> {
7775
self.account_provider
7876
.insert_account(secret.into(), &passphrase.unwrap_or_default())
79-
.map(|address| PlatformAddress::new_v1(self.network_id(), address))
77+
.map(|address| PlatformAddress::new_v1(self.client.network_id(), address))
8078
.map_err(account_provider)
8179
}
8280

rpc/src/v1/impls/chain.rs

+11-25
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ where
128128
return Ok(None)
129129
}
130130
let block_id = block_number.map(BlockId::from).unwrap_or(BlockId::Latest);
131-
Ok(self.client.get_text(transaction_hash, block_id).map_err(errors::transaction_state)?.map(|text| {
132-
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
133-
Text::from_core(text, self.client.common_params(parent_block_id).unwrap().network_id())
134-
}))
131+
Ok(self
132+
.client
133+
.get_text(transaction_hash, block_id)
134+
.map_err(errors::transaction_state)?
135+
.map(|text| Text::from_core(text, self.client.network_id())))
135136
}
136137

137138
fn get_asset(
@@ -178,8 +179,7 @@ where
178179
fn get_regular_key_owner(&self, public: Public, block_number: Option<u64>) -> Result<Option<PlatformAddress>> {
179180
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
180181
Ok(self.client.regular_key_owner(&public_to_address(&public), block_id.into()).and_then(|address| {
181-
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
182-
let network_id = self.client.common_params(parent_block_id).unwrap().network_id();
182+
let network_id = self.client.network_id();
183183
Some(PlatformAddress::new_v1(network_id, address))
184184
}))
185185
}
@@ -206,17 +206,15 @@ where
206206
fn get_shard_owners(&self, shard_id: ShardId, block_number: Option<u64>) -> Result<Option<Vec<PlatformAddress>>> {
207207
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
208208
Ok(self.client.shard_owners(shard_id, block_id.into()).map(|owners| {
209-
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
210-
let network_id = self.client.common_params(parent_block_id).unwrap().network_id();
209+
let network_id = self.client.network_id();
211210
owners.into_iter().map(|owner| PlatformAddress::new_v1(network_id, owner)).collect()
212211
}))
213212
}
214213

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

240238
fn get_block_by_number(&self, block_number: u64) -> Result<Option<Block>> {
241239
let id = BlockId::Number(block_number);
242-
Ok(self.client.block(&id).map(|block| {
243-
let block_id_to_read_params = if block_number == 0 {
244-
0.into()
245-
} else {
246-
(block_number - 1).into()
247-
};
248-
Block::from_core(block.decode(), self.client.common_params(block_id_to_read_params).unwrap().network_id())
249-
}))
240+
Ok(self.client.block(&id).map(|block| Block::from_core(block.decode(), self.client.network_id())))
250241
}
251242

252243
fn get_block_by_hash(&self, block_hash: BlockHash) -> Result<Option<Block>> {
253244
let id = BlockId::Hash(block_hash);
254245
Ok(self.client.block(&id).map(|block| {
255246
let block = block.decode();
256-
let block_id_to_read_params = if block.header.number() == 0 {
257-
0.into()
258-
} else {
259-
(*block.header.parent_hash()).into()
260-
};
261-
Block::from_core(block, self.client.common_params(block_id_to_read_params).unwrap().network_id())
247+
Block::from_core(block, self.client.network_id())
262248
}))
263249
}
264250

@@ -301,7 +287,7 @@ where
301287
}
302288

303289
fn get_network_id(&self) -> Result<NetworkId> {
304-
Ok(self.client.common_params(BlockId::Latest).unwrap().network_id())
290+
Ok(self.client.network_id())
305291
}
306292

307293
fn get_common_params(&self, block_number: Option<u64>) -> Result<Option<Params>> {

rpc/src/v1/impls/engine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ where
6262
Ok(None)
6363
} else {
6464
// XXX: What should we do if the network id has been changed
65-
let network_id = self.client.common_params(BlockId::Latest).unwrap().network_id();
65+
let network_id = self.client.network_id();
6666
Ok(Some(PlatformAddress::new_v1(network_id, author)))
6767
}
6868
}

rpc/src/v1/impls/mempool.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use std::sync::Arc;
1818

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

4343
impl<C> Mempool for MempoolClient<C>
4444
where
45-
C: BlockChainClient + MiningBlockChainClient + 'static,
45+
C: BlockChainClient + MiningBlockChainClient + EngineInfo + 'static,
4646
{
4747
fn send_signed_transaction(&self, raw: Bytes) -> Result<TxHash> {
4848
Rlp::new(&raw.into_vec())
@@ -82,7 +82,7 @@ where
8282

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

@@ -102,7 +102,7 @@ where
102102

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

0 commit comments

Comments
 (0)