From 47e3bda8c313c0225cd4bee7aa17dff76ee74aed Mon Sep 17 00:00:00 2001 From: keroro Date: Wed, 2 Nov 2022 19:06:32 +0800 Subject: [PATCH] refactor: remove finality_as_blocks --- contracts/gw-utils/src/finality.rs | 19 ++++--------------- .../src/verifications/revert.rs | 6 ++---- .../src/verifications/submit_block.rs | 8 ++++---- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/contracts/gw-utils/src/finality.rs b/contracts/gw-utils/src/finality.rs index 97f4a76..dc28485 100644 --- a/contracts/gw-utils/src/finality.rs +++ b/contracts/gw-utils/src/finality.rs @@ -67,27 +67,16 @@ pub fn is_block_number_finalized( match Timepoint::from_full_value(prev_global_state.last_finalized_block_number().unpack()) { Timepoint::BlockNumber(finalized) => block_number <= finalized, Timepoint::Timestamp(_) => { - let finality = finality_as_blocks(rollup_config); + let finality_blocks: u64 = rollup_config.finality_blocks().unpack(); let tip_number: u64 = prev_global_state.block().count().unpack().saturating_sub(1); - block_number.saturating_add(finality) <= tip_number + block_number.saturating_add(finality_blocks) <= tip_number } } } pub fn finality_as_duration(rollup_config: &RollupConfig) -> u64 { - match Timepoint::from_full_value(rollup_config.finality_blocks().unpack()) { - Timepoint::BlockNumber(block_number) => { - block_number.saturating_mul(BLOCK_INTERVAL_IN_MILLISECONDS) - } - Timepoint::Timestamp(timestamp) => timestamp, - } -} - -pub fn finality_as_blocks(rollup_config: &RollupConfig) -> u64 { - match Timepoint::from_full_value(rollup_config.finality_blocks().unpack()) { - Timepoint::BlockNumber(block_number) => block_number, - Timepoint::Timestamp(timestamp) => timestamp / BLOCK_INTERVAL_IN_MILLISECONDS, - } + let finality_blocks = rollup_config.finality_blocks().unpack(); + finality_blocks.saturating_mul(BLOCK_INTERVAL_IN_MILLISECONDS) } /// Obtain the max timestamp of the header-deps diff --git a/contracts/state-validator/src/verifications/revert.rs b/contracts/state-validator/src/verifications/revert.rs index 469d97c..afe2e40 100644 --- a/contracts/state-validator/src/verifications/revert.rs +++ b/contracts/state-validator/src/verifications/revert.rs @@ -8,9 +8,7 @@ use gw_types::{ packed::{BlockMerkleState, Byte32, GlobalState, RawL2Block, RollupConfig}, prelude::*, }; -use gw_utils::finality::{ - finality_as_blocks, finality_as_duration, obtain_max_timestamp_of_header_deps, -}; +use gw_utils::finality::{finality_as_duration, obtain_max_timestamp_of_header_deps}; use gw_utils::gw_types; use gw_utils::{ cells::{ @@ -255,7 +253,7 @@ fn check_reverted_blocks( let tip_number: u64 = reverted_blocks[0].number().unpack(); let finalized_number = tip_number .saturating_sub(1) - .saturating_sub(finality_as_blocks(&config)); + .saturating_sub(config.finality_blocks().unpack()); Timepoint::from_block_number(finalized_number) } else { let l1_timestamp = match obtain_max_timestamp_of_header_deps() { diff --git a/contracts/state-validator/src/verifications/submit_block.rs b/contracts/state-validator/src/verifications/submit_block.rs index b9e2bc4..6d68786 100644 --- a/contracts/state-validator/src/verifications/submit_block.rs +++ b/contracts/state-validator/src/verifications/submit_block.rs @@ -15,9 +15,7 @@ use gw_utils::gw_types::packed::{L2BlockReader, WithdrawalRequestReader}; // https://nervosnetwork.github.io/ckb-std/riscv64imac-unknown-none-elf/doc/ckb_std/index.html use crate::ckb_std::{ckb_constants::Source, debug}; use gw_state::kv_state::KVState; -use gw_utils::finality::{ - finality_as_blocks, finality_as_duration, is_finalized, obtain_max_timestamp_of_header_deps, -}; +use gw_utils::finality::{finality_as_duration, is_finalized, obtain_max_timestamp_of_header_deps}; use gw_utils::gw_common::{self, ckb_decimal::CKBCapacity}; use gw_utils::gw_types::{self, U256}; @@ -844,7 +842,9 @@ pub fn verify( let version: u8 = post_global_state.version().into(); let last_finalized = if version < 2 { - let finalized_number = context.number.saturating_sub(finality_as_blocks(&config)); + let finalized_number = context + .number + .saturating_sub(config.finality_blocks().unpack()); Timepoint::from_block_number(finalized_number) } else { let l1_timestamp = match obtain_max_timestamp_of_header_deps() {