Skip to content

Commit cbb3c06

Browse files
committed
Refactor term_common_params method
1 parent f83de3d commit cbb3c06

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)]
@@ -504,16 +505,7 @@ pub fn enact<C: ChainTimeInfo + EngineInfo + FindActionHandler + TermInfo>(
504505
b.push_transactions(transactions, client, parent.number(), parent.timestamp())?;
505506

506507
let parent_common_params = client.common_params((*header.parent_hash()).into()).unwrap();
507-
let term_common_params = {
508-
let block_number = client
509-
.last_term_finished_block_num((*header.parent_hash()).into())
510-
.expect("The block of the parent hash should exist");
511-
if block_number == 0 {
512-
None
513-
} else {
514-
Some(client.common_params((block_number).into()).expect("Common params should exist"))
515-
}
516-
};
508+
let term_common_params = client.term_common_params(BlockId::Hash(*header.parent_hash()));
517509
b.close_and_lock(parent, &parent_common_params, term_common_params.as_ref())
518510
}
519511

core/src/client/client.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,15 @@ impl TermInfo for Client {
826826
.map(|state| state.metadata().unwrap().expect("Metadata always exist"))
827827
.map(|metadata| metadata.current_term_id())
828828
}
829+
830+
fn term_common_params(&self, id: BlockId) -> Option<CommonParams> {
831+
let block_number = self.last_term_finished_block_num(id).expect("The block of the parent hash should exist");
832+
if block_number == 0 {
833+
None
834+
} else {
835+
Some(self.common_params((block_number).into()).expect("Common params should exist"))
836+
}
837+
}
829838
}
830839

831840
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
@@ -604,16 +604,7 @@ impl Miner {
604604
(parent_header.decode(), parent_hash)
605605
};
606606
let parent_common_params = chain.common_params(parent_hash.into()).unwrap();
607-
let term_common_params = {
608-
let block_number = chain
609-
.last_term_finished_block_num(parent_hash.into())
610-
.expect("The block of the parent hash should exist");
611-
if block_number == 0 {
612-
None
613-
} else {
614-
Some(chain.common_params((block_number).into()).expect("Common params should exist"))
615-
}
616-
};
607+
let term_common_params = chain.term_common_params(parent_hash.into());
617608
let block = open_block.close(&parent_header, &parent_common_params, term_common_params.as_ref())?;
618609

619610
let fetch_seq = |p: &Public| {

0 commit comments

Comments
 (0)