Skip to content

Commit

Permalink
refactor: move txs_verify_cache_size to tx_pool_config
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Mar 8, 2019
1 parent d51c197 commit 06a0b3c
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 30 deletions.
6 changes: 3 additions & 3 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
self.shared.store().save_with_batch(|batch| {
self.shared.store().insert_block(batch, &block);

if &cannon_total_difficulty > &current_total_difficulty
|| (&current_total_difficulty == &cannon_total_difficulty
&& block.header().hash() < tip_hash)
if (cannon_total_difficulty > current_total_difficulty)
|| ((current_total_difficulty == cannon_total_difficulty)
&& (block.header().hash() < tip_hash))
{
debug!(
target: "chain",
Expand Down
2 changes: 1 addition & 1 deletion nodes_template/default.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
data_dir = "default"
txs_verify_cache_size = 100000

[db]
path = "default/db"
Expand Down Expand Up @@ -42,6 +41,7 @@ max_proposal_size = 10000
max_cache_size = 1000
max_pending_size = 10000
trace = 100
txs_verify_cache_size = 100000

[block_assembler]
type_hash = "0x0da2fe99fe549e082d4ed483c2e968a89ea8d11aabf5d79e5cbf06522de6e674"
21 changes: 11 additions & 10 deletions shared/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ impl<CI: ChainIndex> Shared<CI> {
tip_number: u64,
) -> TxProposalTable {
let mut proposal_ids = TxProposalTable::new(proposal_window);
let proposal_start = tip_number.saturating_sub(proposal_window.1);
let proposal_end = tip_number.saturating_sub(proposal_window.0);
let proposal_start = tip_number.saturating_sub(proposal_window.start());
let proposal_end = tip_number.saturating_sub(proposal_window.end());
for bn in proposal_start..=proposal_end {
if let Some(hash) = store.get_block_hash(bn) {
let mut ids_set = FnvHashSet::default();
Expand Down Expand Up @@ -357,7 +357,6 @@ impl<CI: ChainIndex> BlockMedianTimeContext for Shared<CI> {
pub struct SharedBuilder<DB: KeyValueDB> {
db: Option<DB>,
consensus: Option<Consensus>,
txs_verify_cache_size: Option<usize>,
tx_pool_config: Option<TxPoolConfig>,
}

Expand All @@ -366,7 +365,6 @@ impl<DB: KeyValueDB> Default for SharedBuilder<DB> {
SharedBuilder {
db: None,
consensus: None,
txs_verify_cache_size: None,
tx_pool_config: None,
}
}
Expand All @@ -377,7 +375,6 @@ impl SharedBuilder<MemoryKeyValueDB> {
SharedBuilder {
db: Some(MemoryKeyValueDB::open(COLUMNS as usize)),
consensus: None,
txs_verify_cache_size: None,
tx_pool_config: None,
}
}
Expand Down Expand Up @@ -411,17 +408,21 @@ impl<DB: 'static + KeyValueDB> SharedBuilder<DB> {
}

pub fn txs_verify_cache_size(mut self, value: usize) -> Self {
self.txs_verify_cache_size = Some(value);
if let Some(c) = self.tx_pool_config.as_mut() {
c.txs_verify_cache_size = value;
};
self
}

pub fn build(self) -> Shared<ChainKVStore<DB>> {
let store = ChainKVStore::new(self.db.unwrap());
let consensus = self.consensus.unwrap_or_else(Consensus::default);
let tx_pool_config = self.tx_pool_config.unwrap_or_else(Default::default);
let txs_verify_cache_size =
std::cmp::max(MIN_TXS_VERIFY_CACHE_SIZE, self.txs_verify_cache_size)
.expect("txs_verify_cache_size MUST not be none");
Shared::new(store, consensus, txs_verify_cache_size, tx_pool_config)
Shared::new(
store,
consensus,
tx_pool_config.txs_verify_cache_size,
tx_pool_config,
)
}
}
4 changes: 4 additions & 0 deletions shared/src/tx_pool/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter::ExactSizeIterator;

pub const MIN_TXS_VERIFY_CACHE_SIZE: usize = 100;

/// Transaction pool configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TxPoolConfig {
Expand All @@ -26,6 +28,7 @@ pub struct TxPoolConfig {
pub max_cache_size: usize,
pub max_pending_size: usize,
pub trace: Option<usize>,
pub txs_verify_cache_size: usize,
}

impl Default for TxPoolConfig {
Expand All @@ -37,6 +40,7 @@ impl Default for TxPoolConfig {
max_cache_size: 1000,
max_pending_size: 10000,
trace: Some(100),
txs_verify_cache_size: MIN_TXS_VERIFY_CACHE_SIZE,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions shared/src/tx_proposal_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl TxProposalTable {
}

pub fn reconstruct(&mut self, number: BlockNumber) -> Vec<ProposalShortId> {
let proposal_start = number.saturating_sub(self.proposal_window.1);
let proposal_end = number.saturating_sub(self.proposal_window.0);
let proposal_start = number.saturating_sub(self.proposal_window.start());
let proposal_end = number.saturating_sub(self.proposal_window.end());

let mut left = self.table.split_off(&proposal_start);
::std::mem::swap(&mut self.table, &mut left);
Expand Down
16 changes: 14 additions & 2 deletions spec/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use ckb_pow::{Pow, PowEngine};
use numext_fixed_uint::U256;
use std::sync::Arc;

pub type ProposalWindow = (BlockNumber, BlockNumber);
pub(crate) const DEFAULT_BLOCK_REWARD: Capacity = 5_000;
pub(crate) const MAX_UNCLE_NUM: usize = 2;
pub(crate) const MAX_UNCLE_AGE: usize = 6;
pub(crate) const TX_PROPOSAL_WINDOW: ProposalWindow = (2, 10);
pub(crate) const TX_PROPOSAL_WINDOW: ProposalWindow = ProposalWindow(2, 10);
pub(crate) const CELLBASE_MATURITY: usize = 100;
// TODO: should adjust this value based on CKB average block time
pub(crate) const MEDIAN_TIME_BLOCK_COUNT: usize = 11;
Expand All @@ -24,6 +23,19 @@ pub(crate) const MAX_BLOCK_CYCLES: Cycle = 100_000_000;
pub(crate) const MAX_BLOCK_BYTES: u64 = 10_000_000; // 10mb
pub(crate) const BLOCK_VERSION: u32 = 0;

#[derive(Default, Clone, PartialEq, Debug, Eq, Copy)]
pub struct ProposalWindow(BlockNumber, BlockNumber);

impl ProposalWindow {
pub fn end(&self) -> BlockNumber {
self.0
}

pub fn start(&self) -> BlockNumber {
self.1
}
}

#[derive(Clone, PartialEq, Debug)]
pub struct Consensus {
pub id: String,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/run_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn run(setup: Setup) {
.consensus(consensus)
.db(&setup.configs.db)
.tx_pool_config(setup.configs.tx_pool.clone())
.txs_verify_cache_size(setup.configs.txs_verify_cache_size)
.txs_verify_cache_size(setup.configs.tx_pool.txs_verify_cache_size)
.build();

let notify = NotifyService::default().start(Some("notify"));
Expand Down
1 change: 0 additions & 1 deletion src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub struct Configs {
pub block_assembler: BlockAssemblerConfig,
pub sync: SyncConfig,
pub tx_pool: TxPoolConfig,
pub txs_verify_cache_size: usize,
}

pub fn get_config_path(matches: &ArgMatches) -> PathBuf {
Expand Down
7 changes: 3 additions & 4 deletions verification/src/block_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,9 @@ impl<CP: ChainProvider + Clone> CommitVerifier<CP> {
return Ok(());
}
let block_number = block.header().number();
let (proposal_window_start, proposal_window_end) =
self.provider.consensus().tx_proposal_window();
let proposal_start = block_number.saturating_sub(proposal_window_end);
let mut proposal_end = block_number.saturating_sub(proposal_window_start);
let proposal_window = self.provider.consensus().tx_proposal_window();
let proposal_start = block_number.saturating_sub(proposal_window.start());
let mut proposal_end = block_number.saturating_sub(proposal_window.end());

let mut block_hash = self
.provider
Expand Down
12 changes: 6 additions & 6 deletions verification/src/tests/commit_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn test_proposal() {
prev_tx_hash = tx.hash().clone();
}

let (proposal_start, proposal_end) = shared.consensus().tx_proposal_window();
let proposal_window = shared.consensus().tx_proposal_window();

let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap();

Expand All @@ -152,7 +152,7 @@ fn test_proposal() {
parent = block.header().clone();

//commit in proposal gap is invalid
for _ in (proposed + 1)..(proposed + proposal_start) {
for _ in (proposed + 1)..(proposed + proposal_window.end()) {
let block: Block = gen_block(&parent, txs20.clone(), vec![], vec![]);
let verifier = CommitVerifier::new(shared.clone());
assert_eq!(
Expand All @@ -169,7 +169,7 @@ fn test_proposal() {
}

//commit in proposal window
for _ in 0..(proposal_end - proposal_start) {
for _ in 0..(proposal_window.start() - proposal_window.end()) {
let block: Block = gen_block(&parent, txs20.clone(), vec![], vec![]);
let verifier = CommitVerifier::new(shared.clone());
assert_eq!(verifier.verify(&block), Ok(()));
Expand Down Expand Up @@ -199,7 +199,7 @@ fn test_uncle_proposal() {
prev_tx_hash = tx.hash().clone();
}

let (proposal_start, proposal_end) = shared.consensus().tx_proposal_window();
let proposal_window = shared.consensus().tx_proposal_window();

let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap();

Expand All @@ -214,7 +214,7 @@ fn test_uncle_proposal() {
parent = block.header().clone();

//commit in proposal gap is invalid
for _ in (proposed + 1)..(proposed + proposal_start) {
for _ in (proposed + 1)..(proposed + proposal_window.end()) {
let block: Block = gen_block(&parent, txs20.clone(), vec![], vec![]);
let verifier = CommitVerifier::new(shared.clone());
assert_eq!(
Expand All @@ -231,7 +231,7 @@ fn test_uncle_proposal() {
}

//commit in proposal window
for _ in 0..(proposal_end - proposal_start) {
for _ in 0..(proposal_window.start() - proposal_window.end()) {
let block: Block = gen_block(&parent, txs20.clone(), vec![], vec![]);
let verifier = CommitVerifier::new(shared.clone());
assert_eq!(verifier.verify(&block), Ok(()));
Expand Down

0 comments on commit 06a0b3c

Please sign in to comment.