Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
refactor: remove finality_as_blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Nov 2, 2022
1 parent c3b1c00 commit 47e3bda
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 23 deletions.
19 changes: 4 additions & 15 deletions contracts/gw-utils/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions contracts/state-validator/src/verifications/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions contracts/state-validator/src/verifications/submit_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 47e3bda

Please sign in to comment.