Skip to content

Commit

Permalink
feat: calc finalizing range via binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Nov 29, 2022
1 parent ba19213 commit d269603
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 306 deletions.
22 changes: 8 additions & 14 deletions crates/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ impl Chain {
let block_hash = db
.get_block_hash_by_number(block_number)?
.context("get block hash")?;
let block = db.get_block(&block_hash)?.context("get block")?;
let parent_finalized_custodians = db
.get_block_post_finalized_custodian_capacity(block_number - 1)
.context("get parent block remaining finalized custodians")?
Expand All @@ -600,10 +601,13 @@ impl Chain {
// 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")?;
for finalizing_number in finalizing_range.range() {
let finalizing_range = calc_finalizing_range(
&self.rollup_config,
self.generator.fork_config(),
&db,
&block,
)?;
for finalizing_number in finalizing_range {
let deposits = db
.get_block_deposit_info_vec(finalizing_number)
.context("get finalizing block deposit info vec")?;
Expand Down Expand Up @@ -1067,16 +1071,6 @@ impl Chain {
)?;
db.insert_asset_scripts(deposit_asset_scripts)?;
db.attach_block(l2block.clone())?;
db.set_block_finalizing_range(
&l2block.hash().into(),
&calc_finalizing_range(
&self.rollup_config,
self.generator.fork_config(),
db,
&l2block,
)?
.as_reader(),
)?;

self.local_state.tip = l2block;
self.local_state.last_global_state = global_state;
Expand Down
4 changes: 1 addition & 3 deletions crates/db/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// Column families alias type
pub type Col = u8;
/// Total column number
pub const COLUMNS: u32 = 38;
pub const COLUMNS: u32 = 37;
/// Column store meta data
pub const COLUMN_META: Col = 0;
/// Column store chain index
Expand Down Expand Up @@ -79,8 +79,6 @@ pub const COLUMN_BLOCK_SUBMIT_TX_HASH: Col = 7;
pub const COLUMN_BLOCK_DEPOSIT_INFO_VEC: Col = 16;
/// block number (in big endian) -> FinalizedCustodianCapacity.
pub const COLUMN_BLOCK_POST_FINALIZED_CUSTODIAN_CAPACITY: Col = 36;
/// block hash -> finalizing block number range
pub const COLUMN_BLOCK_FINALIZING_RANGE: Col = 37;

/// chain id
pub const META_CHAIN_ID_KEY: &[u8] = b"CHAIN_ID";
Expand Down
16 changes: 12 additions & 4 deletions crates/mem-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use gw_types::{
},
prelude::{Builder, Entity, Pack, PackVec, Unpack},
};
use gw_utils::calc_finalizing_range;
use gw_utils::local_cells::LocalCellsManager;
use std::{
cmp::{max, min},
Expand Down Expand Up @@ -388,16 +389,23 @@ impl MemPool {
return Ok(Default::default());
}
let snap = self.store.get_snapshot();
let block: L2Block = snap
.get_block(&self.current_tip.0)
.context("get block")?
.ok_or_else(|| anyhow!("failed to get last block"))?;
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"))?
.as_reader()
.unpack();

let finalizing_range = snap
.get_block_finalizing_range(&self.current_tip.0)
.context("get tip block finalizing range")?;
for finalizing_number in finalizing_range.range() {
let finalizing_range = calc_finalizing_range(
&self.generator.rollup_context().rollup_config,
&self.generator.rollup_context().fork_config,
&snap,
&block,
)?;
for finalizing_number in finalizing_range {
let finalizing_deposits = snap
.get_block_deposit_info_vec(finalizing_number)
.context("get last finalized block deposit")?;
Expand Down
7 changes: 1 addition & 6 deletions crates/store/src/traits/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use gw_common::H256;
use gw_db::error::Error;
use gw_db::schema::{
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_DEPOSIT_INFO_VEC, 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,
Expand Down Expand Up @@ -153,11 +153,6 @@ 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())?;
Some(from_box_should_be_ok!(packed::FinalizingRangeReader, data))
}

/// Get tip block hash. It may be a bad block.
fn get_tip_block_hash(&self) -> Result<H256, Error> {
let slice = self
Expand Down
15 changes: 1 addition & 14 deletions crates/store/src/transaction/store_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use gw_common::h256_ext::H256Ext;
use gw_common::{merkle_utils::calculate_state_checkpoint, smt::SMT, H256};
use gw_db::schema::{
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_DEPOSIT_INFO_VEC, 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,
Expand Down Expand Up @@ -357,19 +357,6 @@ impl StoreTransaction {
)
}

pub fn set_block_finalizing_range(
&self,
block_hash: &H256,
finalizing_range: &packed::FinalizingRangeReader,
) -> Result<(), Error> {
self.insert_raw(
COLUMN_BLOCK_FINALIZING_RANGE,
block_hash.as_slice(),
finalizing_range.as_slice(),
)?;
Ok(())
}

pub fn set_reverted_block_smt_root(&self, root: H256) -> Result<(), Error> {
self.insert_raw(
COLUMN_META,
Expand Down
48 changes: 32 additions & 16 deletions crates/tests/src/tests/calc_finalizing_range.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::testing_tool::chain::setup_chain;
use gw_config::ForkConfig;
use gw_db::schema::{COLUMN_BLOCK_GLOBAL_STATE, COLUMN_INDEX};
use gw_store::traits::chain_store::ChainStore;
use gw_db::schema::{COLUMN_BLOCK, COLUMN_BLOCK_GLOBAL_STATE, COLUMN_INDEX};
use gw_store::traits::kv_store::KVStoreWrite;
use gw_types::core::Timepoint;
use gw_types::packed::{BlockMerkleState, L2Block, RawL2Block};
Expand Down Expand Up @@ -102,34 +101,27 @@ async fn test_calc_finalizing_range() {
.unwrap();
db.insert_raw(COLUMN_INDEX, raw.number().as_slice(), &block.hash())
.unwrap();

let finalizing_range =
calc_finalizing_range(&rollup_config, &fork_config, db, block).unwrap();
db.set_block_finalizing_range(&block.hash().into(), &finalizing_range.as_reader())
db.insert_raw(COLUMN_BLOCK, &block.hash(), block.as_slice())
.unwrap();
db.commit().unwrap();
}

// ## Assert
let mut last_range_end = None;
let fork_height = fork_config.upgrade_global_state_version_to_v2.unwrap();
let finality_as_blocks = rollup_config.finality_blocks().unpack();
let finality_time_in_mss = rollup_config.finality_time_in_ms();
for i in 1..blocks.len() {
let block = &blocks[i];
let block_number = block.raw().number().unpack();
let block_timestamp = block.raw().timestamp().unpack();
let range = chain
.store()
.get_block_finalizing_range(&block.hash().into())
.unwrap();
let from_block_number = range.from_block_number().unpack();
let to_block_number = range.to_block_number().unpack();
let range =
calc_finalizing_range(&rollup_config, &fork_config, chain.store(), block).unwrap();

if block_number <= finality_as_blocks {
assert_eq!(0, from_block_number);
assert_eq!(from_block_number, to_block_number);
if block_number < finality_as_blocks {
assert!(range.is_empty());
} else {
for nb in range.range() {
for nb in range.clone() {
if nb < fork_height {
assert_eq!(nb, block_number.saturating_sub(finality_as_blocks));
} else {
Expand All @@ -138,5 +130,29 @@ async fn test_calc_finalizing_range() {
}
}
}

if !range.is_empty() {
assert!(range.start < range.end);
if last_range_end.is_none() {
assert_eq!(
range,
(1..2),
"block_number: {}, range: {:?}, last_range_end: {:?}",
block_number,
range,
last_range_end
);
} else {
assert_eq!(
last_range_end.unwrap(),
range.start,
"block_number: {}, range: {:?}, last_range_end: {:?}",
block_number,
range,
last_range_end
);
}
last_range_end = Some(range.end);
}
}
}
10 changes: 0 additions & 10 deletions crates/types/schemas/mem_block.mol
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ table FinalizedCustodianCapacity {
sudt: SudtCustodianVec,
}

// Ensure that from_block_number <= to_block_number
//
// This represents a block number range `(from_block_number, to_block_number]`:
// - when from_block_number < to_block_number, blocks {from_block_number+1, from_block_number+2, ..., to_block_number} are finalizing;
// - when from_block_number = to_block_number, no any blocks are finalizing
struct FinalizingRange {
from_block_number: Uint64,
to_block_number: Uint64,
}

vector AccountMerkleStateVec <AccountMerkleState>;

table CompactMemBlock {
Expand Down
Loading

0 comments on commit d269603

Please sign in to comment.