Skip to content

Commit 4292086

Browse files
foriequal0mergify[bot]
authored andcommitted
Refactor term_common_params method
1 parent 89c9b40 commit 4292086

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

core/src/block.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::client::{EngineInfo, TermInfo};
3333
use crate::consensus::CodeChainEngine;
3434
use crate::error::{BlockError, Error};
3535
use crate::transaction::{SignedTransaction, UnverifiedTransaction};
36+
use crate::BlockId;
3637

3738
/// A block, encoded as it is on the block chain.
3839
#[derive(Debug, Clone, PartialEq)]
@@ -493,16 +494,7 @@ pub fn enact<C: ChainTimeInfo + EngineInfo + FindActionHandler + TermInfo>(
493494
b.populate_from(header);
494495
b.push_transactions(transactions, client, parent.number(), parent.timestamp())?;
495496

496-
let term_common_params = {
497-
let block_number = client
498-
.last_term_finished_block_num((*header.parent_hash()).into())
499-
.expect("The block of the parent hash should exist");
500-
if block_number == 0 {
501-
None
502-
} else {
503-
Some(client.common_params((block_number).into()).expect("Common params should exist"))
504-
}
505-
};
497+
let term_common_params = client.term_common_params(BlockId::Hash(*header.parent_hash()));
506498
b.close_and_lock(parent, term_common_params.as_ref())
507499
}
508500

core/src/client/client.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,15 @@ impl TermInfo for Client {
813813
.map(|state| state.metadata().unwrap().expect("Metadata always exist"))
814814
.map(|metadata| metadata.current_term_id())
815815
}
816+
817+
fn term_common_params(&self, id: BlockId) -> Option<CommonParams> {
818+
let block_number = self.last_term_finished_block_num(id).expect("The block of the parent hash should exist");
819+
if block_number == 0 {
820+
None
821+
} else {
822+
Some(self.common_params((block_number).into()).expect("Common params should exist"))
823+
}
824+
}
816825
}
817826

818827
impl AccountData for Client {

core/src/client/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub trait ConsensusClient: BlockChainClient + EngineClient + EngineInfo + TermIn
119119
pub trait TermInfo {
120120
fn last_term_finished_block_num(&self, id: BlockId) -> Option<BlockNumber>;
121121
fn current_term_id(&self, id: BlockId) -> Option<u64>;
122+
fn term_common_params(&self, id: BlockId) -> Option<CommonParams>;
122123
}
123124

124125
/// Provides methods to access account info

core/src/client/test_client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,10 @@ impl TermInfo for TestBlockChainClient {
679679
fn current_term_id(&self, _id: BlockId) -> Option<u64> {
680680
self.term_id
681681
}
682+
683+
fn term_common_params(&self, _id: BlockId) -> Option<CommonParams> {
684+
None
685+
}
682686
}
683687

684688
impl StateInfo for TestBlockChainClient {

core/src/miner/miner.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -603,16 +603,7 @@ impl Miner {
603603
let parent_header = chain.block_header(&parent_hash.into()).expect("Parent header MUST exist");
604604
(parent_header.decode(), parent_hash)
605605
};
606-
let term_common_params = {
607-
let block_number = chain
608-
.last_term_finished_block_num(parent_hash.into())
609-
.expect("The block of the parent hash should exist");
610-
if block_number == 0 {
611-
None
612-
} else {
613-
Some(chain.common_params((block_number).into()).expect("Common params should exist"))
614-
}
615-
};
606+
let term_common_params = chain.term_common_params(parent_hash.into());
616607
let block = open_block.close(&parent_header, term_common_params.as_ref())?;
617608

618609
let fetch_seq = |p: &Public| {

0 commit comments

Comments
 (0)