Skip to content

Commit 6a2e87e

Browse files
committed
Refactor term_common_params method
1 parent fcaebf6 commit 6a2e87e

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

core/src/block.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ use primitives::{Bytes, H256};
2929
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
3030

3131
use super::invoice::Invoice;
32+
use crate::client::TermInfoExt;
3233
use crate::client::{EngineInfo, TermInfo};
3334
use crate::consensus::CodeChainEngine;
3435
use crate::error::{BlockError, Error};
3536
use crate::transaction::{SignedTransaction, UnverifiedTransaction};
37+
use crate::BlockId;
3638

3739
/// A block, encoded as it is on the block chain.
3840
#[derive(Debug, Clone, PartialEq)]
@@ -504,16 +506,7 @@ pub fn enact<C: ChainTimeInfo + EngineInfo + FindActionHandler + TermInfo>(
504506
b.push_transactions(transactions, client, parent.number(), parent.timestamp())?;
505507

506508
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-
};
509+
let term_common_params = client.term_common_params(BlockId::Hash(*header.parent_hash()));
517510
b.close_and_lock(parent, &parent_common_params, term_common_params.as_ref())
518511
}
519512

core/src/client/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub trait EngineClient: Sync + Send + BlockChainTrait + ImportBlock {
115115
fn get_kvdb(&self) -> Arc<dyn KeyValueDB>;
116116
}
117117

118-
pub trait ConsensusClient: BlockChainClient + EngineClient + EngineInfo + TermInfo + StateInfo {}
118+
pub trait ConsensusClient: BlockChainClient + EngineClient + EngineInfo + TermInfo + StateInfo + TermInfoExt {}
119119

120120
pub trait TermInfo {
121121
fn last_term_finished_block_num(&self, id: BlockId) -> Option<BlockNumber>;
@@ -351,3 +351,22 @@ pub trait StateInfo {
351351
pub trait SnapshotClient {
352352
fn notify_snapshot(&self, id: BlockId);
353353
}
354+
355+
356+
pub trait TermInfoExt {
357+
fn term_common_params(&self, id: BlockId) -> Option<CommonParams>;
358+
}
359+
360+
impl<C> TermInfoExt for C
361+
where
362+
C: TermInfo + EngineInfo,
363+
{
364+
fn term_common_params(&self, id: BlockId) -> Option<CommonParams> {
365+
let block_number = self.last_term_finished_block_num(id).expect("The block of the parent hash should exist");
366+
if block_number == 0 {
367+
None
368+
} else {
369+
Some(self.common_params(block_number.into()).expect("Common params should exist"))
370+
}
371+
}
372+
}

core/src/consensus/tendermint/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl ConsensusEngine for Tendermint {
345345
}
346346
}
347347

348-
fn block_number_if_term_changed(
348+
pub(crate) fn block_number_if_term_changed(
349349
header: &Header,
350350
parent_header: &Header,
351351
common_params: &CommonParams,

core/src/miner/miner.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use crate::account_provider::{AccountProvider, Error as AccountProviderError};
4141
use crate::block::{Block, ClosedBlock, IsBlock};
4242
use crate::client::{
4343
AccountData, BlockChainTrait, BlockProducer, Client, EngineInfo, ImportBlock, MiningBlockChainClient, TermInfo,
44+
TermInfoExt,
4445
};
4546
use crate::codechain_machine::CodeChainMachine;
4647
use crate::consensus::{CodeChainEngine, EngineType};
@@ -599,16 +600,7 @@ impl Miner {
599600
(parent_header.decode(), parent_hash)
600601
};
601602
let parent_common_params = chain.common_params(parent_hash.into()).unwrap();
602-
let term_common_params = {
603-
let block_number = chain
604-
.last_term_finished_block_num(parent_hash.into())
605-
.expect("The block of the parent hash should exist");
606-
if block_number == 0 {
607-
None
608-
} else {
609-
Some(chain.common_params((block_number).into()).expect("Common params should exist"))
610-
}
611-
};
603+
let term_common_params = chain.term_common_params(parent_hash.into());
612604
let block = open_block.close(&parent_header, &parent_common_params, term_common_params.as_ref())?;
613605

614606
let fetch_seq = |p: &Public| {

0 commit comments

Comments
 (0)