Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Nov 6, 2022
1 parent d9108a2 commit cba5aef
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 50 deletions.
32 changes: 19 additions & 13 deletions crates/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand All @@ -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")?
Expand All @@ -644,6 +649,7 @@ impl Chain {
block_number,
&finalized_custodians.pack().as_reader(),
)?;

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion crates/mem-pool/src/custodian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub async fn query_finalized_custodians<WithdrawalIter: Iterator<Item = Withdraw
rollup_context,
&total_withdrawal_amount,
total_change_capacity,
&compatible_finalized_timepoint,
compatible_finalized_timepoint,
None,
MAX_CUSTODIANS,
)
Expand Down
16 changes: 4 additions & 12 deletions crates/mem-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl MemPool {
tip_global_state,
)
};
let tip_hash = tip.0.clone();
let tip_hash = tip.0;

let mut mem_block = MemBlock::with_block_producer(block_producer);
let mut pending_deposits = vec![];
Expand Down Expand Up @@ -384,10 +384,6 @@ impl MemPool {
return Ok(Default::default());
}
let snap = self.store.get_snapshot();
println!(
"bilibili collect_finalized_custodian_capacity tip: {:?}",
self.current_tip
);
let mut c: FinalizedCustodianCapacity = snap
.get_block_post_finalized_custodian_capacity(self.current_tip.1)
.ok_or_else(|| anyhow!("failed to get last block post finalized custodian capacity"))?
Expand All @@ -397,13 +393,9 @@ impl MemPool {
let finalizing_range = snap
.get_block_finalizing_range(&self.current_tip.0)
.context("get tip 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 {
return Ok(c);
}

for finalizing_number in finalizing_from_number + 1..finalizing_to_number + 1 {
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 finalizing_deposits = snap
.get_block_deposit_info_vec(finalizing_number)
.context("get last finalized block deposit")?;
Expand Down
2 changes: 0 additions & 2 deletions crates/mem-pool/src/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ impl<'a> 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)
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/store/src/traits/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub trait ChainStore: KVStoreRead {
}

fn get_block_finalizing_range(&self, block_hash: &H256) -> Option<packed::FinalizingRange> {
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))
}

Expand Down
19 changes: 8 additions & 11 deletions crates/store/src/transaction/store_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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> {
Expand Down
24 changes: 23 additions & 1 deletion crates/tests/src/tests/restore_mem_pool_pending_withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions crates/tests/src/tests/unlock_withdrawal_to_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions crates/utils/src/calc_finalizing_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions crates/utils/src/query_l1_header_by_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down

0 comments on commit cba5aef

Please sign in to comment.