From cba5aeff5d8d3a838fa69f5aef7a403a5848c854 Mon Sep 17 00:00:00 2001 From: keroro Date: Sun, 6 Nov 2022 23:22:21 +0800 Subject: [PATCH] wip --- crates/chain/src/chain.rs | 32 +++++++++++-------- crates/mem-pool/src/custodian.rs | 2 +- crates/mem-pool/src/pool.rs | 16 +++------- crates/mem-pool/src/withdrawal.rs | 2 -- crates/store/src/traits/chain_store.rs | 2 +- .../src/transaction/store_transaction.rs | 19 +++++------ .../restore_mem_pool_pending_withdrawal.rs | 24 +++++++++++++- .../src/tests/unlock_withdrawal_to_owner.rs | 5 +-- crates/utils/src/calc_finalizing_range.rs | 6 ++-- .../utils/src/query_l1_header_by_timestamp.rs | 10 +++--- 10 files changed, 68 insertions(+), 50 deletions(-) diff --git a/crates/chain/src/chain.rs b/crates/chain/src/chain.rs index 0785c12df..faf82e0b9 100644 --- a/crates/chain/src/chain.rs +++ b/crates/chain/src/chain.rs @@ -577,31 +577,35 @@ impl Chain { /// Calculate and store the finalized_custodian_capacity for block block_number. /// /// Initialize by the block parent's finalized_custodian_capacity; - /// increase with finalizing deposit requests after applying this block; - /// decrease with the block's withdrawals. + /// Update with finalizing deposit requests and block's withdrawals. pub fn calculate_and_store_finalized_custodians( &mut self, db: &StoreTransaction, block_number: u64, ) -> Result<(), anyhow::Error> { - let block_hash = db - .get_block_hash_by_number(block_number)? - .context("get block hash")?; - let finalizing_range = db - .get_block_finalizing_range(&block_hash) - .context("get block finalizing range")?; - let finalizing_from_number: u64 = finalizing_range.from_block_number().unpack(); - let finalizing_to_number: u64 = finalizing_range.to_block_number().unpack(); - if finalizing_from_number == finalizing_to_number { + if block_number == 0 { return Ok(()); } - let mut finalized_custodians = db + let block_hash = db + .get_block_hash_by_number(block_number)? + .context("get block hash")?; + let parent_finalized_custodians = db .get_block_post_finalized_custodian_capacity(block_number - 1) .context("get parent block remaining finalized custodians")? .as_reader() .unpack(); - for finalizing_number in finalizing_from_number + 1..finalizing_to_number + 1 { + let mut finalized_custodians = parent_finalized_custodians; + + // Update finalized_custodians with the finalizing deposit requests for the current block + // + // The finalizing range is represents in the form of `(from, to]`. + let finalizing_range = db + .get_block_finalizing_range(&block_hash) + .context("get block finalizing range")?; + let finalizing_from: u64 = finalizing_range.from_block_number().unpack(); + let finalizing_to: u64 = finalizing_range.to_block_number().unpack(); + for finalizing_number in finalizing_from + 1..=finalizing_to { let deposits = db .get_block_deposit_info_vec(finalizing_number) .context("get finalizing block deposit info vec")?; @@ -621,6 +625,7 @@ impl Chain { } } + // Update finalized_custodians with the withdrawals of the current block let withdrawals = db .get_block(&block_hash)? .context("get block")? @@ -644,6 +649,7 @@ impl Chain { block_number, &finalized_custodians.pack().as_reader(), )?; + Ok(()) } diff --git a/crates/mem-pool/src/custodian.rs b/crates/mem-pool/src/custodian.rs index d68a7311c..0f9174dbf 100644 --- a/crates/mem-pool/src/custodian.rs +++ b/crates/mem-pool/src/custodian.rs @@ -124,7 +124,7 @@ pub async fn query_finalized_custodians Generator<'a> { .determine_global_state_version(block_number) < 2 { - println!("bilibili : Timepoint::from_block_number {}", block_number); Timepoint::from_block_number(block_number) } else { let block_timestamp = block.raw().timestamp().unpack(); - println!("bilibili : Timepoint::from_timestamp {}", block_timestamp); Timepoint::from_timestamp(block_timestamp) } }; diff --git a/crates/store/src/traits/chain_store.rs b/crates/store/src/traits/chain_store.rs index c9cc562d1..8e89af3b3 100644 --- a/crates/store/src/traits/chain_store.rs +++ b/crates/store/src/traits/chain_store.rs @@ -154,7 +154,7 @@ pub trait ChainStore: KVStoreRead { } fn get_block_finalizing_range(&self, block_hash: &H256) -> Option { - let data = self.get(COLUMN_BLOCK_FINALIZING_RANGE, &block_hash.as_slice())?; + let data = self.get(COLUMN_BLOCK_FINALIZING_RANGE, block_hash.as_slice())?; Some(from_box_should_be_ok!(packed::FinalizingRangeReader, data)) } diff --git a/crates/store/src/transaction/store_transaction.rs b/crates/store/src/transaction/store_transaction.rs index 2ab20358a..48036fa40 100644 --- a/crates/store/src/transaction/store_transaction.rs +++ b/crates/store/src/transaction/store_transaction.rs @@ -7,17 +7,14 @@ use crate::traits::kv_store::{KVStore, KVStoreWrite}; use gw_common::h256_ext::H256Ext; use gw_common::{merkle_utils::calculate_state_checkpoint, smt::SMT, H256}; use gw_db::schema::{ - Col, COLUMN_ACCOUNT_SMT_BRANCH, COLUMN_ACCOUNT_SMT_LEAF, COLUMN_ASSET_SCRIPT, COLUMN_BAD_BLOCK, - COLUMN_BAD_BLOCK_CHALLENGE_TARGET, COLUMN_BLOCK, COLUMN_BLOCK_DEPOSIT_INFO_VEC, - COLUMN_BLOCK_FINALIZING_RANGE, COLUMN_BLOCK_GLOBAL_STATE, - COLUMN_BLOCK_POST_FINALIZED_CUSTODIAN_CAPACITY, COLUMN_BLOCK_SMT_BRANCH, COLUMN_BLOCK_SMT_LEAF, - COLUMN_BLOCK_STATE_RECORD, COLUMN_BLOCK_STATE_REVERSE_RECORD, COLUMN_BLOCK_SUBMIT_TX, + Col, COLUMN_ASSET_SCRIPT, COLUMN_BAD_BLOCK, COLUMN_BAD_BLOCK_CHALLENGE_TARGET, COLUMN_BLOCK, + COLUMN_BLOCK_DEPOSIT_INFO_VEC, COLUMN_BLOCK_FINALIZING_RANGE, COLUMN_BLOCK_GLOBAL_STATE, + COLUMN_BLOCK_POST_FINALIZED_CUSTODIAN_CAPACITY, COLUMN_BLOCK_SUBMIT_TX, COLUMN_BLOCK_SUBMIT_TX_HASH, COLUMN_INDEX, COLUMN_MEM_POOL_TRANSACTION, COLUMN_MEM_POOL_TRANSACTION_RECEIPT, COLUMN_MEM_POOL_WITHDRAWAL, COLUMN_META, - COLUMN_REVERTED_BLOCK_SMT_BRANCH, COLUMN_REVERTED_BLOCK_SMT_LEAF, - COLUMN_REVERTED_BLOCK_SMT_ROOT, COLUMN_TRANSACTION, COLUMN_TRANSACTION_INFO, - COLUMN_TRANSACTION_RECEIPT, COLUMN_WITHDRAWAL, COLUMN_WITHDRAWAL_INFO, META_BLOCK_SMT_ROOT_KEY, - META_CHAIN_ID_KEY, META_LAST_CONFIRMED_BLOCK_NUMBER_HASH_KEY, + COLUMN_REVERTED_BLOCK_SMT_LEAF, COLUMN_REVERTED_BLOCK_SMT_ROOT, COLUMN_TRANSACTION, + COLUMN_TRANSACTION_INFO, COLUMN_TRANSACTION_RECEIPT, COLUMN_WITHDRAWAL, COLUMN_WITHDRAWAL_INFO, + META_BLOCK_SMT_ROOT_KEY, META_CHAIN_ID_KEY, META_LAST_CONFIRMED_BLOCK_NUMBER_HASH_KEY, META_LAST_SUBMITTED_BLOCK_NUMBER_HASH_KEY, META_LAST_VALID_TIP_BLOCK_HASH_KEY, META_REVERTED_BLOCK_SMT_ROOT_KEY, META_TIP_BLOCK_HASH_KEY, }; @@ -367,14 +364,14 @@ impl StoreTransaction { ) -> Result<(), Error> { self.insert_raw( COLUMN_BLOCK_FINALIZING_RANGE, - &block_hash.as_slice(), + block_hash.as_slice(), finalizing_range.as_slice(), )?; Ok(()) } pub fn delete_block_finalizing_range(&self, block_hash: &H256) -> Result<(), Error> { - self.delete(COLUMN_BLOCK_FINALIZING_RANGE, &block_hash.as_slice()) + self.delete(COLUMN_BLOCK_FINALIZING_RANGE, block_hash.as_slice()) } pub fn set_reverted_block_smt_root(&self, root: H256) -> Result<(), Error> { 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 8d4aeb72d..a8375f71f 100644 --- a/crates/tests/src/tests/restore_mem_pool_pending_withdrawal.rs +++ b/crates/tests/src/tests/restore_mem_pool_pending_withdrawal.rs @@ -77,7 +77,29 @@ async fn test_restore_mem_pool_pending_withdrawal() { assert!(chain.last_sync_event().is_success()); for _ in 0..DEFAULT_FINALITY_BLOCKS { - produce_empty_block(&mut chain).await.unwrap(); + 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, Default::default()) + .await + .unwrap() + }; + let empty_l1action = L1Action { + context: L1ActionContext::SubmitBlock { + l2block: block_result.block.clone(), + deposit_info_vec: Default::default(), + deposit_asset_scripts: Default::default(), + withdrawals: Default::default(), + }, + transaction: build_sync_tx(rollup_cell.clone(), block_result), + }; + let param = SyncParam { + updates: vec![empty_l1action], + reverts: Default::default(), + }; + chain.sync(param).await.unwrap(); + chain.notify_new_tip().await.unwrap(); + assert!(chain.last_sync_event().is_success()); } // Generate withdrawals diff --git a/crates/tests/src/tests/unlock_withdrawal_to_owner.rs b/crates/tests/src/tests/unlock_withdrawal_to_owner.rs index e755a47c6..262564873 100644 --- a/crates/tests/src/tests/unlock_withdrawal_to_owner.rs +++ b/crates/tests/src/tests/unlock_withdrawal_to_owner.rs @@ -539,9 +539,10 @@ async fn test_build_unlock_to_owner_tx() { verify_tx(tx_with_context, 700_000_000_u64).expect("pass"); // Simulate rpc client filter no owner lock withdrawal cells + let finality_as_blocks = rollup_config.finality_blocks().unpack(); let compatible_finalized_timepoint = CompatibleFinalizedTimepoint::from_block_number( - withdrawal_block_result.block.raw().number().unpack(), - rollup_config.finality_blocks().unpack(), + withdrawal_block_result.block.raw().number().unpack() + finality_as_blocks, + finality_as_blocks, ); let unlockable_random_withdrawals: Vec<_> = random_withdrawal_cells .into_iter() diff --git a/crates/utils/src/calc_finalizing_range.rs b/crates/utils/src/calc_finalizing_range.rs index 2f8f375cd..801c0419d 100644 --- a/crates/utils/src/calc_finalizing_range.rs +++ b/crates/utils/src/calc_finalizing_range.rs @@ -51,20 +51,22 @@ pub fn calc_finalizing_range( let older_global_state = db .get_block_post_global_state(&older_block_hash)? .expect("get finalizing block global state"); + + // NOTE: It is determined which finality rule to apply based on the global_state.version of + // older block, but not the version of the current block. let older_global_state_version: u8 = older_global_state.version().into(); let older_timepoint = if older_global_state_version < 2 { Timepoint::from_block_number(older_block_number) } else { Timepoint::from_timestamp(older_global_state.tip_block_timestamp().unpack()) }; - if !compatible_finalized_timepoint.is_finalized(&older_timepoint) { break; } // This `order_block` just went from unfinalized to finalized for `current_block`. // Iterate the next order block. - to_number = to_number + 1; + to_number += 1; } Ok(FinalizingRange::new_builder() diff --git a/crates/utils/src/query_l1_header_by_timestamp.rs b/crates/utils/src/query_l1_header_by_timestamp.rs index 4cd5d8859..3de4ce4c6 100644 --- a/crates/utils/src/query_l1_header_by_timestamp.rs +++ b/crates/utils/src/query_l1_header_by_timestamp.rs @@ -51,14 +51,14 @@ pub async fn query_l1_header_by_timestamp( let start_time = Instant::now(); let mut last_warning_time = Instant::now(); let ret = loop { - if start_time.elapsed() > Duration::from_secs(60) { - if last_warning_time.elapsed() > Duration::from_secs(5) { - last_warning_time = Instant::now(); - log::warn!( + if start_time.elapsed() > Duration::from_secs(60) + && last_warning_time.elapsed() > Duration::from_secs(5) + { + last_warning_time = Instant::now(); + log::warn!( "[query_l1_header_by_timestamp] has been a long time, target_timestamp: {}, cursor_number: {}, elapsed: {:?}", target_timestamp, cursor, start_time.elapsed() ); - } } let header = match rpc_client.get_header_by_number(cursor).await? {