From 7aef364ccc6c701ae0a9fc1f0b053e882962c923 Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 23 May 2022 10:38:48 +0800 Subject: [PATCH 01/11] chore: logging produce block process --- crates/block-producer/src/block_producer.rs | 43 ++++++++++++++++----- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/crates/block-producer/src/block_producer.rs b/crates/block-producer/src/block_producer.rs index ce24f82a8..9d6cea192 100644 --- a/crates/block-producer/src/block_producer.rs +++ b/crates/block-producer/src/block_producer.rs @@ -254,6 +254,7 @@ impl BlockProducer { // try issue next block let mut retry_count = 0; while retry_count <= MAX_BLOCK_OUTPUT_PARAM_RETRY_COUNT { + let t = Instant::now(); let (block_number, tx, next_global_state) = match self .compose_next_block_submit_tx(rollup_input_since, rollup_cell.clone(), retry_count) .await @@ -262,20 +263,23 @@ impl BlockProducer { Err(err) if err.downcast_ref::().is_some() => { // Wait next l1 tip block median time log::debug!( - "[produce block] block timestamp is greater than rollup input since, wait next median time" + target: "produce-block", + "block timestamp is greater than rollup input since, wait next median time" ); return Ok(()); } Err(err) => { retry_count += 1; log::warn!( - "[produce block] retry compose next block submit tx, retry: {}, reason: {}", + target: "produce-block", + "retry compose next block submit tx, retry: {}, reason: {}", retry_count, err ); continue; } }; + log::debug!(target: "produce-block", "Produce l2block #{} ({}ms)", block_number, t.elapsed().as_millis()); let expected_next_block_number = global_state.block().count().unpack(); if expected_next_block_number != block_number { @@ -287,8 +291,11 @@ impl BlockProducer { } let submitted_tx_hash = tx.hash(); + let t = Instant::now(); match self.submit_block_tx(block_number, tx).await { Ok(SubmitResult::Submitted) => { + log::debug!(target: "produce-block", "Submitted l2block #{} in {} ({}ms)", + block_number, hex::encode(&submitted_tx_hash), t.elapsed().as_millis()); self.last_committed_l2_block = LastCommittedL2Block { committed_tip_block_hash: l2_tip_block_hash, committed_at: Instant::now(), @@ -300,7 +307,8 @@ impl BlockProducer { Err(err) => { retry_count += 1; log::warn!( - "[produce block] retry submit block tx , retry: {}, reason: {}", + target: "produce-block", + "retry submit block tx , retry: {}, reason: {}", retry_count, err ); @@ -335,13 +343,19 @@ impl BlockProducer { let (opt_finalized_custodians, block_param) = { let (mem_block, post_block_state) = { let t = Instant::now(); - log::debug!("[compose_next_block_submit_tx] acquire mem-pool",); + log::debug!(target: "produce-block", "acquire mem-pool",); let mem_pool = self.mem_pool.lock().await; log::debug!( - "[compose_next_block_submit_tx] unlock mem-pool {}ms", + target: "produce-block", "unlock mem-pool {}ms", + t.elapsed().as_millis() + ); + let t = Instant::now(); + let r = mem_pool.output_mem_block(&OutputParam::new(retry_count)); + log::debug!( + target: "produce-block", "output mem block {}ms", t.elapsed().as_millis() ); - mem_pool.output_mem_block(&OutputParam::new(retry_count)) + r }; let t = Instant::now(); @@ -363,12 +377,14 @@ impl BlockProducer { query.await?.expect_any() }; log::debug!( + target: "produce-block", "finalized custodians {:?}", finalized_custodians.cells_info.len() ); log::debug!( - "[compose_next_block_submit_tx] generate produce block param {}ms", + target: "produce-block", + "generate produce block param {}ms", t.elapsed().as_millis() ); (Some(finalized_custodians), produce_block_param) @@ -389,7 +405,13 @@ impl BlockProducer { block_param, }; let db = self.store.begin_transaction(); + let t = Instant::now(); let block_result = produce_block(&db, &self.generator, param)?; + log::debug!( + target: "produce-block", + "produce block {}ms", + t.elapsed().as_millis() + ); let ProduceBlockResult { mut block, mut global_state, @@ -416,6 +438,7 @@ impl BlockProducer { let block_txs = block.transactions().len(); let block_withdrawals = block.withdrawals().len(); log::info!( + target: "produce-block", "produce new block #{} (txs: {}, deposits: {}, withdrawals: {})", number, block_txs, @@ -453,14 +476,16 @@ impl BlockProducer { Ok(tx) => tx, Err(err) => { log::error!( - "[produce_next_block] Failed to composite submitting transaction: {}", + target: "produce-block", + "Failed to composite submitting transaction: {}", err ); return Err(err); } }; log::debug!( - "[compose_next_block_submit_tx] complete tx skeleton {}ms", + target: "produce-block", + "complete tx skeleton {}ms", t.elapsed().as_millis() ); if tx.as_slice().len() <= MAX_BLOCK_BYTES as usize From b862d43b5c5586f90521bf9bfe0ddc3c24ce80d3 Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 23 May 2022 16:11:12 +0800 Subject: [PATCH 02/11] chore: logging sync-block process --- crates/chain/src/chain.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/chain/src/chain.rs b/crates/chain/src/chain.rs index f8ba5e1f8..33e3a7351 100644 --- a/crates/chain/src/chain.rs +++ b/crates/chain/src/chain.rs @@ -259,12 +259,12 @@ impl Chain { log::debug!("[complete_initial_syncing] acquire mem-pool",); let t = Instant::now(); let mut mem_pool = mem_pool.lock().await; - mem_pool.notify_new_tip(tip_block_hash).await?; - mem_pool.mem_pool_state().set_completed_initial_syncing(); log::debug!( "[complete_initial_syncing] unlock mem-pool {}ms", t.elapsed().as_millis() ); + mem_pool.notify_new_tip(tip_block_hash).await?; + mem_pool.mem_pool_state().set_completed_initial_syncing(); } } self.complete_initial_syncing = true; @@ -825,8 +825,11 @@ impl Chain { } // update layer1 actions - for action in param.updates { + log::debug!(target: "sync-block", "sync {} actions", param.updates.len()); + for (i, action) in param.updates.into_iter().enumerate() { + let t = Instant::now(); self.update_l1action(&db, action)?; + log::debug!(target: "sync-block", "process {}th action cost {}ms", i, t.elapsed().as_millis()); match self.last_sync_event() { SyncEvent::Success => (), _ => db.commit()?, @@ -834,7 +837,6 @@ impl Chain { } db.commit()?; - log::debug!("commit db after sync"); let tip_block_hash: H256 = self.local_state.tip.hash().into(); if let Some(mem_pool) = &self.mem_pool { @@ -842,10 +844,13 @@ impl Chain { && (is_revert_happend || self.complete_initial_syncing) { // update mem pool state - log::debug!("[sync] acquire mem-pool",); + log::debug!(target: "sync-block", "acquire mem-pool",); let t = Instant::now(); - mem_pool.lock().await.notify_new_tip(tip_block_hash).await?; - log::debug!("[sync] unlock mem-pool {}ms", t.elapsed().as_millis()); + let mut mem_pool = mem_pool.lock().await; + log::debug!(target: "sync-block", "unlock mem-pool {}ms", t.elapsed().as_millis()); + let t = Instant::now(); + mem_pool.notify_new_tip(tip_block_hash).await?; + log::debug!(target: "sync-block", "notify mem-pool new tip cost {}ms", t.elapsed().as_millis()); } } @@ -870,6 +875,7 @@ impl Chain { "check account tree" ); + log::debug!(target: "sync-block", "Complete"); Ok(()) } From a6c87badb0e1127aef957aa8a752dee65ac7b1fc Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 23 May 2022 18:16:47 +0800 Subject: [PATCH 03/11] chore: logging more produce-block code --- crates/block-producer/src/block_producer.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/block-producer/src/block_producer.rs b/crates/block-producer/src/block_producer.rs index 9d6cea192..9ff354c1f 100644 --- a/crates/block-producer/src/block_producer.rs +++ b/crates/block-producer/src/block_producer.rs @@ -519,6 +519,7 @@ impl BlockProducer { let cycles = match self.rpc_client.dry_run_transaction(&tx).await { Ok(cycles) => { log::info!( + target: "produce-block", "Tx({}) L2 block #{} execution cycles: {}", block_number, hex::encode(tx.hash()), @@ -535,7 +536,10 @@ impl BlockProducer { log::error!("[contracts dep] refresh failed {}", err); } - log::info!("Skip submitting l2 block since CKB can't resolve tx, previous block may haven't been processed by CKB"); + log::info!( + target: "produce-block", + "Skip submitting l2 block since CKB can't resolve tx, previous block may haven't been processed by CKB" + ); return Ok(SubmitResult::Skip); } else { if err_str.contains(TRANSACTION_SCRIPT_ERROR) @@ -558,12 +562,14 @@ impl BlockProducer { } }; log::debug!( - "[compose_next_block_submit_tx] dry run {}ms", + target: "produce-block", + "dry run {}ms", t.elapsed().as_millis() ); if cycles > self.debug_config.expected_l1_tx_upper_bound_cycles { log::warn!( + target: "produce-block", "Submitting l2 block is cost unexpected cycles: {:?}, expected upper bound: {}", cycles, self.debug_config.expected_l1_tx_upper_bound_cycles @@ -580,6 +586,7 @@ impl BlockProducer { match self.rpc_client.send_transaction(&tx).await { Ok(tx_hash) => { log::info!( + target: "produce-block", "Submitted l2 block {} in tx {}", block_number, hex::encode(tx_hash.as_slice()) @@ -587,7 +594,7 @@ impl BlockProducer { Ok(SubmitResult::Submitted) } Err(err) => { - log::error!("Submitting l2 block error: {}", err); + log::error!(target: "produce-block", "Submitting l2 block error: {}", err); // dumping script error transactions let err_str = err.to_string(); @@ -611,6 +618,7 @@ impl BlockProducer { .as_secs(); if since_last_committed_secs < WAIT_PRODUCE_BLOCK_SECONDS { log::debug!( + target: "produce-block", "last committed is {}s ago, dump tx", since_last_committed_secs ); From 3069c136945347d2cd1b752311eea9e61cb8eb51 Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 23 May 2022 18:17:23 +0800 Subject: [PATCH 04/11] fix: filter packaged withdrawl requests --- crates/mem-pool/src/pool.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/mem-pool/src/pool.rs b/crates/mem-pool/src/pool.rs index 5a8024024..29d9205d3 100644 --- a/crates/mem-pool/src/pool.rs +++ b/crates/mem-pool/src/pool.rs @@ -918,7 +918,19 @@ impl MemPool { if withdrawals.is_empty() { for entry in self.pending().values() { if !entry.withdrawals.is_empty() && withdrawals.len() < MAX_MEM_BLOCK_WITHDRAWALS { - withdrawals.push(entry.withdrawals.first().unwrap().clone()); + let withdrawal = entry.withdrawals.first().unwrap(); + let id = state + .get_account_id_by_script_hash( + &withdrawal.raw().account_script_hash().unpack(), + )? + .expect("get id of withdrawal account"); + let nonce = state.get_nonce(id)?; + let expected_nonce: u32 = withdrawal.raw().nonce().unpack(); + // ignore withdrawal mismatch the nonce + if nonce != expected_nonce { + continue; + } + withdrawals.push(withdrawal.clone()); } } } From 6cf364bffeaf7c004e454db6619297a7e9734f98 Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 23 May 2022 18:17:47 +0800 Subject: [PATCH 05/11] chore: increase MAX_MEM_BLOCK_DEPOSITS and MAX_MEM_BLOCK_WITHDRAWALS to 100 --- crates/mem-pool/src/constants.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/mem-pool/src/constants.rs b/crates/mem-pool/src/constants.rs index f8ca651e9..cbee79927 100644 --- a/crates/mem-pool/src/constants.rs +++ b/crates/mem-pool/src/constants.rs @@ -1,7 +1,7 @@ /// MAX deposits in the mem block -pub const MAX_MEM_BLOCK_DEPOSITS: usize = 50; +pub const MAX_MEM_BLOCK_DEPOSITS: usize = 100; /// MAX withdrawals in the mem block -pub const MAX_MEM_BLOCK_WITHDRAWALS: usize = 50; +pub const MAX_MEM_BLOCK_WITHDRAWALS: usize = 100; /// MAX withdrawals in the mem block pub const MAX_MEM_BLOCK_TXS: usize = 1000; /// MIN CKB deposit capacity, calculated from custodian cell size From bceb9c2b7ced2fd2d5be69167577fd28b228729e Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 24 May 2022 11:35:19 +0800 Subject: [PATCH 06/11] fix: filter withdrawals in prepare_next_mem_block --- crates/mem-pool/src/pool.rs | 42 ++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/crates/mem-pool/src/pool.rs b/crates/mem-pool/src/pool.rs index 29d9205d3..5879243d1 100644 --- a/crates/mem-pool/src/pool.rs +++ b/crates/mem-pool/src/pool.rs @@ -905,7 +905,7 @@ impl MemPool { async fn finalize_withdrawals( &mut self, state: &mut MemStateTree<'_>, - mut withdrawals: Vec, + withdrawals: Vec, ) -> Result<()> { // check mem block state assert!(self.mem_block.withdrawals().is_empty()); @@ -914,23 +914,35 @@ impl MemPool { assert!(self.mem_block.finalized_custodians().is_none()); assert!(self.mem_block.txs().is_empty()); + fn filter_withdrawals( + state: &MemStateTree<'_>, + withdrawal: &WithdrawalRequestExtra, + ) -> bool { + let id = state + .get_account_id_by_script_hash(&withdrawal.raw().account_script_hash().unpack()) + .expect("get id") + .expect("id exist"); + let nonce = state.get_nonce(id).expect("get nonce"); + let expected_nonce: u32 = withdrawal.raw().nonce().unpack(); + // ignore withdrawal mismatch the nonce + nonce == expected_nonce + } + // find withdrawals from pending - if withdrawals.is_empty() { + let mut withdrawals: Vec<_> = withdrawals + .into_iter() + .filter(|withdrawal| filter_withdrawals(state, withdrawal)) + .collect(); + // package withdrawals + if withdrawals.len() < MAX_MEM_BLOCK_WITHDRAWALS { for entry in self.pending().values() { - if !entry.withdrawals.is_empty() && withdrawals.len() < MAX_MEM_BLOCK_WITHDRAWALS { - let withdrawal = entry.withdrawals.first().unwrap(); - let id = state - .get_account_id_by_script_hash( - &withdrawal.raw().account_script_hash().unpack(), - )? - .expect("get id of withdrawal account"); - let nonce = state.get_nonce(id)?; - let expected_nonce: u32 = withdrawal.raw().nonce().unpack(); - // ignore withdrawal mismatch the nonce - if nonce != expected_nonce { - continue; + if let Some(withdrawal) = entry.withdrawals.first() { + if filter_withdrawals(state, withdrawal) { + withdrawals.push(withdrawal.clone()); + } + if withdrawals.len() >= MAX_MEM_BLOCK_WITHDRAWALS { + break; } - withdrawals.push(withdrawal.clone()); } } } From b682b7a718c241d96b4b55db45a2981295fc112b Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 24 May 2022 12:28:09 +0800 Subject: [PATCH 07/11] chore: increase default EXPECTED_TX_UPPER_BOUND_CYCLES to 350000000 --- crates/config/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/config/src/config.rs b/crates/config/src/config.rs index fcf78765d..02f7dc156 100644 --- a/crates/config/src/config.rs +++ b/crates/config/src/config.rs @@ -221,7 +221,7 @@ fn default_enable_debug_rpc() -> bool { impl Default for DebugConfig { fn default() -> Self { - const EXPECTED_TX_UPPER_BOUND_CYCLES: u64 = 70000000u64; + const EXPECTED_TX_UPPER_BOUND_CYCLES: u64 = 350000000u64; const DEFAULT_DEBUG_TX_DUMP_PATH: &str = "debug-tx-dump"; Self { From 42a990b5afbda322075a5308b0931e6f39486c38 Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 24 May 2022 18:14:23 +0800 Subject: [PATCH 08/11] chore: use Vec#retain to filter withdrawals --- crates/mem-pool/src/pool.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/mem-pool/src/pool.rs b/crates/mem-pool/src/pool.rs index 5879243d1..2d58e4be1 100644 --- a/crates/mem-pool/src/pool.rs +++ b/crates/mem-pool/src/pool.rs @@ -905,7 +905,7 @@ impl MemPool { async fn finalize_withdrawals( &mut self, state: &mut MemStateTree<'_>, - withdrawals: Vec, + mut withdrawals: Vec, ) -> Result<()> { // check mem block state assert!(self.mem_block.withdrawals().is_empty()); @@ -929,10 +929,7 @@ impl MemPool { } // find withdrawals from pending - let mut withdrawals: Vec<_> = withdrawals - .into_iter() - .filter(|withdrawal| filter_withdrawals(state, withdrawal)) - .collect(); + withdrawals.retain(|withdrawal| filter_withdrawals(state, withdrawal)); // package withdrawals if withdrawals.len() < MAX_MEM_BLOCK_WITHDRAWALS { for entry in self.pending().values() { From 93f19bb4775d16f99ab276422eca5dd94843f791 Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 24 May 2022 18:16:47 +0800 Subject: [PATCH 09/11] chore: remove unused method MemPool#try_to_recovery_from_invalid_state --- crates/mem-pool/src/pool.rs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/crates/mem-pool/src/pool.rs b/crates/mem-pool/src/pool.rs index 2d58e4be1..cd9f5eb31 100644 --- a/crates/mem-pool/src/pool.rs +++ b/crates/mem-pool/src/pool.rs @@ -422,27 +422,6 @@ impl MemPool { Ok(()) } - /// FIXME - /// This function is a temporary mechanism - /// Try to recovery from invalid state by drop txs & deposit - pub fn try_to_recovery_from_invalid_state(&mut self) -> Result<()> { - log::warn!("[mem-pool] try to recovery from invalid state by drop txs & deposits"); - log::warn!("[mem-pool] drop mem-block"); - log::warn!( - "[mem-pool] drop withdrawals: {}", - self.mem_block.withdrawals().len() - ); - log::warn!("[mem-pool] drop txs: {}", self.mem_block.txs().len()); - for tx_hash in self.mem_block.txs() { - log::warn!("[mem-pool] drop tx: {}", hex::encode(tx_hash.as_slice())); - } - self.mem_block.clear(); - log::warn!("[mem-pool] drop pending: {}", self.pending.len()); - self.pending.clear(); - log::warn!("[mem-pool] try_to_recovery - done"); - Ok(()) - } - /// output mem block #[instrument(skip_all, fields(retry_count = output_param.retry_count))] pub fn output_mem_block(&self, output_param: &OutputParam) -> (MemBlock, AccountMerkleState) { From f9896aff1f63c0beb98928080c4a338f54fa2cfa Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 25 May 2022 16:51:57 +0800 Subject: [PATCH 10/11] fix: stop package more withdrawals if mem pool is recovering --- crates/mem-pool/src/pool.rs | 102 +++++++++--------- crates/tests/src/testing_tool/chain.rs | 13 ++- .../restore_mem_pool_pending_withdrawal.rs | 6 +- .../src/tests/unlock_withdrawal_to_owner.rs | 4 +- 4 files changed, 70 insertions(+), 55 deletions(-) diff --git a/crates/mem-pool/src/pool.rs b/crates/mem-pool/src/pool.rs index cd9f5eb31..1d25e49d2 100644 --- a/crates/mem-pool/src/pool.rs +++ b/crates/mem-pool/src/pool.rs @@ -408,7 +408,9 @@ impl MemPool { #[instrument(skip_all)] pub async fn notify_new_tip(&mut self, new_tip: H256) -> Result<()> { // reset pool state - self.reset(Some(self.current_tip.0), Some(new_tip)).await?; + if self.current_tip.0 != new_tip { + self.reset(Some(self.current_tip.0), Some(new_tip)).await?; + } Ok(()) } @@ -613,15 +615,51 @@ impl MemPool { self.remove_unexecutables(&mut mem_state, &db).await?; log::info!("[mem-pool] reset reinject txs: {} mem-block txs: {} reinject withdrawals: {} mem-block withdrawals: {}", reinject_txs.len(), mem_block_txs.len(), reinject_withdrawals.len(), mem_block_withdrawals.len()); - // re-inject withdrawals - let withdrawals_iter = reinject_withdrawals - .into_iter() - .chain(mem_block_withdrawals); - // re-inject txs - let txs_iter = reinject_txs.into_iter().chain(mem_block_txs); - if self.node_mode != NodeMode::ReadOnly { - self.prepare_next_mem_block(&db, &mut mem_state, withdrawals_iter, txs_iter) + // re-inject txs + let txs = reinject_txs.into_iter().chain(mem_block_txs).collect(); + let is_mem_pool_recovery = old_tip.is_none(); + + // re-inject withdrawals + let mut withdrawals: Vec<_> = reinject_withdrawals.into_iter().collect(); + if is_mem_pool_recovery { + // recovery mem block withdrawals + withdrawals.extend(mem_block_withdrawals); + } else { + // packages more withdrawals + fn filter_withdrawals( + state: &MemStateTree<'_>, + withdrawal: &WithdrawalRequestExtra, + ) -> bool { + let id = state + .get_account_id_by_script_hash( + &withdrawal.raw().account_script_hash().unpack(), + ) + .expect("get id") + .expect("id exist"); + let nonce = state.get_nonce(id).expect("get nonce"); + let expected_nonce: u32 = withdrawal.raw().nonce().unpack(); + // ignore withdrawal mismatch the nonce + nonce == expected_nonce + } + withdrawals.retain(|w| filter_withdrawals(&mem_state, w)); + + // package withdrawals + if withdrawals.len() < MAX_MEM_BLOCK_WITHDRAWALS { + for entry in self.pending().values() { + if let Some(withdrawal) = entry.withdrawals.first() { + if filter_withdrawals(&mem_state, withdrawal) { + withdrawals.push(withdrawal.clone()); + } + if withdrawals.len() >= MAX_MEM_BLOCK_WITHDRAWALS { + break; + } + } + } + } + } + + self.prepare_next_mem_block(&db, &mut mem_state, withdrawals, txs) .await?; } @@ -681,21 +719,18 @@ impl MemPool { } /// Prepare for next mem block - #[instrument(skip_all, fields(withdrawals_count = withdrawals.size_hint().1, txs_count = txs.size_hint().1))] - async fn prepare_next_mem_block< - WithdrawalIter: Iterator, - TxIter: Iterator + Clone, - >( + #[instrument(skip_all, fields(withdrawals_count = withdrawals.len(), txs_count = txs.len()))] + async fn prepare_next_mem_block( &mut self, db: &StoreTransaction, state: &mut MemStateTree<'_>, - withdrawals: WithdrawalIter, - txs: TxIter, + withdrawals: Vec, + txs: Vec, ) -> Result<()> { // check order of inputs { let mut id_to_nonce: HashMap = HashMap::default(); - for tx in txs.clone() { + for tx in &txs { let id: u32 = tx.raw().from_id().unpack(); let nonce: u32 = tx.raw().nonce().unpack(); if let Some(&prev_nonce) = id_to_nonce.get(&id) { @@ -712,7 +747,6 @@ impl MemPool { } // Handle state before txs // withdrawal - let withdrawals: Vec = withdrawals.collect(); self.finalize_withdrawals(state, withdrawals.clone()) .await?; // deposits @@ -884,7 +918,7 @@ impl MemPool { async fn finalize_withdrawals( &mut self, state: &mut MemStateTree<'_>, - mut withdrawals: Vec, + withdrawals: Vec, ) -> Result<()> { // check mem block state assert!(self.mem_block.withdrawals().is_empty()); @@ -893,36 +927,6 @@ impl MemPool { assert!(self.mem_block.finalized_custodians().is_none()); assert!(self.mem_block.txs().is_empty()); - fn filter_withdrawals( - state: &MemStateTree<'_>, - withdrawal: &WithdrawalRequestExtra, - ) -> bool { - let id = state - .get_account_id_by_script_hash(&withdrawal.raw().account_script_hash().unpack()) - .expect("get id") - .expect("id exist"); - let nonce = state.get_nonce(id).expect("get nonce"); - let expected_nonce: u32 = withdrawal.raw().nonce().unpack(); - // ignore withdrawal mismatch the nonce - nonce == expected_nonce - } - - // find withdrawals from pending - withdrawals.retain(|withdrawal| filter_withdrawals(state, withdrawal)); - // package withdrawals - if withdrawals.len() < MAX_MEM_BLOCK_WITHDRAWALS { - for entry in self.pending().values() { - if let Some(withdrawal) = entry.withdrawals.first() { - if filter_withdrawals(state, withdrawal) { - withdrawals.push(withdrawal.clone()); - } - if withdrawals.len() >= MAX_MEM_BLOCK_WITHDRAWALS { - break; - } - } - } - } - let max_withdrawal_capacity = std::u128::MAX; let finalized_custodians = { // query withdrawals from ckb-indexer diff --git a/crates/tests/src/testing_tool/chain.rs b/crates/tests/src/testing_tool/chain.rs index 7bcf711db..43bd5d016 100644 --- a/crates/tests/src/testing_tool/chain.rs +++ b/crates/tests/src/testing_tool/chain.rs @@ -446,7 +446,7 @@ pub async fn construct_block( mem_pool: &mut MemPool, deposit_requests: Vec, ) -> anyhow::Result { - construct_block_with_timestamp(chain, mem_pool, deposit_requests, 0).await + construct_block_with_timestamp(chain, mem_pool, deposit_requests, 0, true).await } pub async fn construct_block_with_timestamp( @@ -454,7 +454,14 @@ pub async fn construct_block_with_timestamp( mem_pool: &mut MemPool, deposit_requests: Vec, timestamp: u64, + refresh_mem_pool: bool, ) -> anyhow::Result { + if !refresh_mem_pool { + assert!( + deposit_requests.is_empty(), + "skip refresh mem pool, bug deposits isn't empty" + ) + } let stake_cell_owner_lock_hash = H256::zero(); let db = chain.store().begin_transaction(); let generator = chain.generator(); @@ -520,7 +527,9 @@ pub async fn construct_block_with_timestamp( }; mem_pool.set_provider(Box::new(provider)); // refresh mem block - mem_pool.reset_mem_block().await?; + if refresh_mem_pool { + mem_pool.reset_mem_block().await?; + } let provider = DummyMemPoolProvider { deposit_cells: Vec::default(), fake_blocktime: Duration::from_millis(0), diff --git a/crates/tests/src/tests/restore_mem_pool_pending_withdrawal.rs b/crates/tests/src/tests/restore_mem_pool_pending_withdrawal.rs index d0002e763..a21dc2148 100644 --- a/crates/tests/src/tests/restore_mem_pool_pending_withdrawal.rs +++ b/crates/tests/src/tests/restore_mem_pool_pending_withdrawal.rs @@ -1,7 +1,9 @@ use std::collections::HashMap; use std::time::Duration; -use crate::testing_tool::chain::{build_sync_tx, construct_block, restart_chain, setup_chain}; +use crate::testing_tool::chain::{ + build_sync_tx, construct_block, construct_block_with_timestamp, restart_chain, setup_chain, +}; use crate::testing_tool::common::random_always_success_script; use crate::testing_tool::mem_pool_provider::DummyMemPoolProvider; @@ -188,7 +190,7 @@ async fn test_restore_mem_pool_pending_withdrawal() { let block_result = { let mem_pool = chain.mem_pool().as_ref().unwrap(); let mut mem_pool = mem_pool.lock().await; - construct_block(&chain, &mut mem_pool, vec![]) + construct_block_with_timestamp(&chain, &mut mem_pool, vec![], 0, false) .await .unwrap() }; diff --git a/crates/tests/src/tests/unlock_withdrawal_to_owner.rs b/crates/tests/src/tests/unlock_withdrawal_to_owner.rs index 8feba5e15..e394a32a0 100644 --- a/crates/tests/src/tests/unlock_withdrawal_to_owner.rs +++ b/crates/tests/src/tests/unlock_withdrawal_to_owner.rs @@ -297,7 +297,7 @@ async fn test_build_unlock_to_owner_tx() { let withdrawal_block_result = { let mem_pool = chain.mem_pool().as_ref().unwrap(); let mut mem_pool = mem_pool.lock().await; - construct_block_with_timestamp(&chain, &mut mem_pool, vec![], BLOCK_TIMESTAMP) + construct_block_with_timestamp(&chain, &mut mem_pool, vec![], BLOCK_TIMESTAMP, true) .await .unwrap() }; @@ -546,7 +546,7 @@ async fn test_build_unlock_to_owner_tx() { let provider = DummyMemPoolProvider::default(); mem_pool.set_provider(Box::new(provider)); mem_pool.reset_mem_block().await.unwrap(); - construct_block_with_timestamp(&chain, &mut mem_pool, vec![], BLOCK_TIMESTAMP2) + construct_block_with_timestamp(&chain, &mut mem_pool, vec![], BLOCK_TIMESTAMP2, true) .await .unwrap() }; From 253b1dc8785ab924dc6f5e61fdcf8fe366d2ec2a Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 26 May 2022 11:20:17 +0800 Subject: [PATCH 11/11] fix: correct typo --- crates/tests/src/testing_tool/chain.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tests/src/testing_tool/chain.rs b/crates/tests/src/testing_tool/chain.rs index 43bd5d016..fad064b11 100644 --- a/crates/tests/src/testing_tool/chain.rs +++ b/crates/tests/src/testing_tool/chain.rs @@ -459,7 +459,7 @@ pub async fn construct_block_with_timestamp( if !refresh_mem_pool { assert!( deposit_requests.is_empty(), - "skip refresh mem pool, bug deposits isn't empty" + "skip refresh mem pool, but deposits isn't empty" ) } let stake_cell_owner_lock_hash = H256::zero();