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

Commit

Permalink
fix: use prev_global_state as standard
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Oct 26, 2022
1 parent 3054f76 commit 74ebd43
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion contracts/custodian-lock/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn main() -> Result<(), Error> {
&config,
&global_state,
&Timepoint::from_full_value(lock_args.deposit_block_number().unpack()),
)?;
);
if is_finalized {
// this custodian lock is already finalized, rollup will handle the logic
return Ok(());
Expand Down
51 changes: 26 additions & 25 deletions contracts/gw-utils/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
//!
//! **IMPORTANT NOTE: Offchain and onchain checking logic must be consistent.**
//!
//! - Entity is number-based, post_global_state.last_finalized_block_number is number-based
//! - Entity is number-based, prev_global_state.last_finalized_block_number is number-based
//!
//! Assert entity's timepoint <= post_global_state.last_finalized_block_number
//! Assert entity's timepoint <= prev_global_state.last_finalized_block_number
//!
//! - Entity is number-based, post_global_state.last_finalized_block_number is timestamp-based
//! - Entity is number-based, prev_global_state.last_finalized_block_number is timestamp-based
//!
//! Instead of using post_global_state.last_finalized_block_number, we choose post_global_state.block.count as finality standard.
//! Instead of using prev_global_state.last_finalized_block_number, we choose prev_global_state.block.count as finality standard.
//!
//! Assert entity's timepoint <= post_global_state.block.count - 1 + FINALITY_REQUIREMENT
//! Assert entity's timepoint <= prev_global_state.block.count - 1 + FINALITY_REQUIREMENT
//!
//! - Entity is timestamp-based, post_global_state.last_finalized_block_number is number-based
//! - Entity is timestamp-based, prev_global_state.last_finalized_block_number is number-based
//!
//! Invalid
//! Currently swtiching version from v1 to v2, the entity is sure unfinalized.
//!
//! - Entity is timestamp-based, post_global_state.last_finalized_block_number is timestamp-based
//! - Entity is timestamp-based, prev_global_state.last_finalized_block_number is timestamp-based
//!
//! Assert entity's timepoint <= post_global_state.last_finalized_block_number
//! Assert entity's timepoint <= prev_global_state.last_finalized_block_number
use crate::error::Error;
use ckb_std::{
ckb_constants::Source,
debug,
high_level::{load_header, QueryIter},
};
use gw_types::core::Timepoint;
Expand All @@ -38,36 +38,37 @@ const BLOCK_INTERVAL_IN_MILLISECONDS: u64 = 36000;

pub fn is_finalized(
rollup_config: &RollupConfig,
global_state: &GlobalState,
prev_global_state: &GlobalState,
timepoint: &Timepoint,
) -> Result<bool, Error> {
) -> bool {
match timepoint {
Timepoint::BlockNumber(block_number) => Ok(is_block_number_finalized(
rollup_config,
global_state,
*block_number,
)),
Timepoint::Timestamp(timestamp) => is_timestamp_finalized(global_state, *timestamp),
Timepoint::BlockNumber(block_number) => {
is_block_number_finalized(rollup_config, prev_global_state, *block_number)
}
Timepoint::Timestamp(timestamp) => is_timestamp_finalized(prev_global_state, *timestamp),
}
}

pub fn is_timestamp_finalized(global_state: &GlobalState, timestamp: u64) -> Result<bool, Error> {
match Timepoint::from_full_value(global_state.last_finalized_block_number().unpack()) {
Timepoint::BlockNumber(_) => Err(Error::InvalidPostGlobalState),
Timepoint::Timestamp(finalized) => Ok(timestamp <= finalized),
pub fn is_timestamp_finalized(prev_global_state: &GlobalState, timestamp: u64) -> bool {
match Timepoint::from_full_value(prev_global_state.last_finalized_block_number().unpack()) {
Timepoint::BlockNumber(_) => {
debug!("[is_timestamp_finalized] switching version, prev_global_state.last_finalized_block_number is number-based");
false
}
Timepoint::Timestamp(finalized) => timestamp <= finalized,
}
}

pub fn is_block_number_finalized(
rollup_config: &RollupConfig,
global_state: &GlobalState,
prev_global_state: &GlobalState,
block_number: u64,
) -> bool {
match Timepoint::from_full_value(global_state.last_finalized_block_number().unpack()) {
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 tip_number: u64 = global_state.block().count().unpack().saturating_sub(1);
let tip_number: u64 = prev_global_state.block().count().unpack().saturating_sub(1);
block_number.saturating_add(finality) <= tip_number
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/stake-lock/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn main() -> Result<(), Error> {
&config,
&global_state,
&Timepoint::from_full_value(lock_args.stake_block_number().unpack()),
)?;
);
if is_finalized
&& search_lock_hash(&lock_args.owner_lock_hash().unpack(), Source::Input).is_some()
{
Expand Down
2 changes: 1 addition & 1 deletion contracts/state-validator/src/verifications/challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn verify_enter_challenge(
challenged_block.number().unpack(),
)
} else {
is_timestamp_finalized(post_global_state, challenged_block.timestamp().unpack())?
is_timestamp_finalized(post_global_state, challenged_block.timestamp().unpack())
};
if is_block_finalized {
debug!("cannot challenge a finalized block");
Expand Down
2 changes: 0 additions & 2 deletions contracts/state-validator/src/verifications/submit_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ fn check_input_custodian_cells(
global_state,
&Timepoint::from_full_value(cell.args.deposit_block_number().unpack()),
)
.unwrap_or(false)
});
// check unfinalized custodian cells == reverted deposit requests
let mut reverted_deposit_cells =
Expand Down Expand Up @@ -191,7 +190,6 @@ fn check_output_custodian_cells(
global_state,
&Timepoint::from_full_value(cell.args.deposit_block_number().unpack()),
)
.unwrap_or(false)
});
// check deposits request cells == unfinalized custodian cells
// check l2block's timepoint == unfinalized custodian cells' deposit_block_number
Expand Down
2 changes: 1 addition & 1 deletion contracts/withdrawal-lock/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn main() -> Result<(), Error> {
&config,
&global_state,
&Timepoint::from_full_value(lock_args.withdrawal_block_number().unpack()),
)?;
);
if !is_finalized {
return Err(Error::NotFinalized);
}
Expand Down

0 comments on commit 74ebd43

Please sign in to comment.