diff --git a/chain/src/chain.rs b/chain/src/chain.rs index c671b6dbf2..6c56c087b8 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -209,9 +209,9 @@ impl ChainService { self.shared.store().save_with_batch(|batch| { self.shared.store().insert_block(batch, &block); - if &cannon_total_difficulty > ¤t_total_difficulty - || (¤t_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", diff --git a/nodes_template/default.toml b/nodes_template/default.toml index 6ddb9619bd..cfe3039f22 100644 --- a/nodes_template/default.toml +++ b/nodes_template/default.toml @@ -1,5 +1,4 @@ data_dir = "default" -txs_verify_cache_size = 100000 [db] path = "default/db" @@ -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" diff --git a/shared/src/shared.rs b/shared/src/shared.rs index 21fca594d6..ab7048ce40 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -106,8 +106,8 @@ impl Shared { 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(); @@ -357,7 +357,6 @@ impl BlockMedianTimeContext for Shared { pub struct SharedBuilder { db: Option, consensus: Option, - txs_verify_cache_size: Option, tx_pool_config: Option, } @@ -366,7 +365,6 @@ impl Default for SharedBuilder { SharedBuilder { db: None, consensus: None, - txs_verify_cache_size: None, tx_pool_config: None, } } @@ -377,7 +375,6 @@ impl SharedBuilder { SharedBuilder { db: Some(MemoryKeyValueDB::open(COLUMNS as usize)), consensus: None, - txs_verify_cache_size: None, tx_pool_config: None, } } @@ -411,7 +408,9 @@ impl SharedBuilder { } 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 } @@ -419,9 +418,11 @@ impl SharedBuilder { 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, + ) } } diff --git a/shared/src/tx_pool/types.rs b/shared/src/tx_pool/types.rs index beaf062ce0..83866c07f9 100644 --- a/shared/src/tx_pool/types.rs +++ b/shared/src/tx_pool/types.rs @@ -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 { @@ -26,6 +28,7 @@ pub struct TxPoolConfig { pub max_cache_size: usize, pub max_pending_size: usize, pub trace: Option, + pub txs_verify_cache_size: usize, } impl Default for TxPoolConfig { @@ -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, } } } diff --git a/shared/src/tx_proposal_table.rs b/shared/src/tx_proposal_table.rs index 87c59fbe6a..70f1141943 100644 --- a/shared/src/tx_proposal_table.rs +++ b/shared/src/tx_proposal_table.rs @@ -46,8 +46,8 @@ impl TxProposalTable { } pub fn reconstruct(&mut self, number: BlockNumber) -> Vec { - 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); diff --git a/spec/src/consensus.rs b/spec/src/consensus.rs index 0d49f6911f..d2955fdf68 100644 --- a/spec/src/consensus.rs +++ b/spec/src/consensus.rs @@ -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; @@ -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, diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index 6ba5464031..8f0f6538ed 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -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")); diff --git a/src/setup.rs b/src/setup.rs index c9c664b500..acb1e4ed22 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -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 { diff --git a/verification/src/block_verifier.rs b/verification/src/block_verifier.rs index b39a968f2d..e473a1174b 100644 --- a/verification/src/block_verifier.rs +++ b/verification/src/block_verifier.rs @@ -456,10 +456,9 @@ impl CommitVerifier { 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 diff --git a/verification/src/tests/commit_verifier.rs b/verification/src/tests/commit_verifier.rs index f7ab0954ac..9daf3091fb 100644 --- a/verification/src/tests/commit_verifier.rs +++ b/verification/src/tests/commit_verifier.rs @@ -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(); @@ -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!( @@ -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(())); @@ -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(); @@ -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!( @@ -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(()));