Skip to content

Commit

Permalink
Refactor term_common_params method
Browse files Browse the repository at this point in the history
  • Loading branch information
foriequal0 authored and mergify[bot] committed Dec 7, 2019
1 parent 89c9b40 commit 4292086
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
12 changes: 2 additions & 10 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::client::{EngineInfo, TermInfo};
use crate::consensus::CodeChainEngine;
use crate::error::{BlockError, Error};
use crate::transaction::{SignedTransaction, UnverifiedTransaction};
use crate::BlockId;

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

let term_common_params = {
let block_number = client
.last_term_finished_block_num((*header.parent_hash()).into())
.expect("The block of the parent hash should exist");
if block_number == 0 {
None
} else {
Some(client.common_params((block_number).into()).expect("Common params should exist"))
}
};
let term_common_params = client.term_common_params(BlockId::Hash(*header.parent_hash()));
b.close_and_lock(parent, term_common_params.as_ref())
}

Expand Down
9 changes: 9 additions & 0 deletions core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,15 @@ impl TermInfo for Client {
.map(|state| state.metadata().unwrap().expect("Metadata always exist"))
.map(|metadata| metadata.current_term_id())
}

fn term_common_params(&self, id: BlockId) -> Option<CommonParams> {
let block_number = self.last_term_finished_block_num(id).expect("The block of the parent hash should exist");
if block_number == 0 {
None
} else {
Some(self.common_params((block_number).into()).expect("Common params should exist"))
}
}
}

impl AccountData for Client {
Expand Down
1 change: 1 addition & 0 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub trait ConsensusClient: BlockChainClient + EngineClient + EngineInfo + TermIn
pub trait TermInfo {
fn last_term_finished_block_num(&self, id: BlockId) -> Option<BlockNumber>;
fn current_term_id(&self, id: BlockId) -> Option<u64>;
fn term_common_params(&self, id: BlockId) -> Option<CommonParams>;
}

/// Provides methods to access account info
Expand Down
4 changes: 4 additions & 0 deletions core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,10 @@ impl TermInfo for TestBlockChainClient {
fn current_term_id(&self, _id: BlockId) -> Option<u64> {
self.term_id
}

fn term_common_params(&self, _id: BlockId) -> Option<CommonParams> {
None
}
}

impl StateInfo for TestBlockChainClient {
Expand Down
11 changes: 1 addition & 10 deletions core/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,16 +603,7 @@ impl Miner {
let parent_header = chain.block_header(&parent_hash.into()).expect("Parent header MUST exist");
(parent_header.decode(), parent_hash)
};
let term_common_params = {
let block_number = chain
.last_term_finished_block_num(parent_hash.into())
.expect("The block of the parent hash should exist");
if block_number == 0 {
None
} else {
Some(chain.common_params((block_number).into()).expect("Common params should exist"))
}
};
let term_common_params = chain.term_common_params(parent_hash.into());
let block = open_block.close(&parent_header, term_common_params.as_ref())?;

let fetch_seq = |p: &Public| {
Expand Down

0 comments on commit 4292086

Please sign in to comment.