Skip to content

Commit

Permalink
feat: use since to provide l1 time
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Nov 9, 2022
1 parent 3e96828 commit 2034831
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 257 deletions.
62 changes: 2 additions & 60 deletions crates/block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use gw_mem_pool::{
pool::{MemPool, OutputParam},
};
use gw_rpc_client::{contract::ContractsCellDepManager, rpc_client::RPCClient};
use gw_store::traits::chain_store::ChainStore;
use gw_store::Store;
use gw_types::core::Timepoint;
use gw_types::offchain::{global_state_from_slice, CompatibleFinalizedTimepoint};
Expand All @@ -35,9 +34,8 @@ use gw_types::{
prelude::*,
};
use gw_utils::{
fee::fill_tx_fee_with_local, genesis_info::CKBGenesisInfo, get_l1_confirmed_header,
local_cells::LocalCellsManager, query_l1_header_by_timestamp, query_rollup_cell, since::Since,
transaction_skeleton::TransactionSkeleton, wallet::Wallet,
fee::fill_tx_fee_with_local, genesis_info::CKBGenesisInfo, local_cells::LocalCellsManager,
query_rollup_cell, since::Since, transaction_skeleton::TransactionSkeleton, wallet::Wallet,
};
use std::{collections::HashSet, sync::Arc, time::Instant};
use tokio::sync::Mutex;
Expand Down Expand Up @@ -180,41 +178,10 @@ impl BlockProducer {
smt.root().to_owned()
};

// By applying finality mechanism based on L1 timestamp, we assign the L1 timestamp,
// obtained from a L1 header_dep, to GlobalState.last_finalized_timepoint.
//
// But L1 chain rollback makes things difficult. It must be a relation between
// GlobalState or l2block and L1 header_dep, while L1 rollback, the relation should
// be updated in time.
//
// Currently, we choose a L1 confirmed block header to be L1 header_dep, to avoid
// L1 reorg issue.
//
// For deep reorg, we have to recover Godwoken by manually reverting L2.
let prev_global_state = self
.store
.get_block_post_global_state(&block_param.parent_block.hash().into())?
.expect("get parent post global state");
let prev_timestamp = match Timepoint::from_full_value(
prev_global_state.last_finalized_block_number().unpack(),
) {
Timepoint::BlockNumber(_) => 0,
Timepoint::Timestamp(ts) => {
ts + self
.generator
.rollup_context()
.rollup_config
.finality_as_duration()
}
};
let l1_confirmed_header = get_l1_confirmed_header(&self.rpc_client, prev_timestamp).await?;
let l1_confirmed_header_timestamp = l1_confirmed_header.timestamp();

let param = ProduceBlockParam {
stake_cell_owner_lock_hash: self.wallet.lock_script().hash().into(),
reverted_block_root,
rollup_config_hash: self.rollup_config_hash,
l1_confirmed_header_timestamp,
block_param,
};
let db = self.store.begin_transaction();
Expand Down Expand Up @@ -274,31 +241,6 @@ impl BlockProducer {
tx_skeleton
.cell_deps_mut()
.push(contracts_dep.omni_lock.clone().into());
// header_deps, used for obtaining l1 time in the state-validator-script
let block_number = block.raw().number().unpack();
if 2 <= rollup_context.determine_global_state_version(block_number) {
// Recover the l1_confirmed_header based on l1_confimed_timestamp.
// The l1_confirmed_header is referenced when producing l2block and GlobalState.
//
// The block producer would reference confirmed headers to avoid L1 reorg issue.
let finality_as_duration = rollup_context.rollup_config.finality_as_duration();
let last_finalized_timestamp = match Timepoint::from_full_value(
global_state.last_finalized_block_number().unpack(),
) {
Timepoint::Timestamp(last_finalized_timestamp) => last_finalized_timestamp,
Timepoint::BlockNumber(_) => {
unreachable!("2 <= global_state_version, last_finalized_timepoint should be timestamp-based");
}
};
let l1_confirmed_timestamp =
last_finalized_timestamp.saturating_add(finality_as_duration);
let l1_confirmed_header =
query_l1_header_by_timestamp(&self.rpc_client, l1_confirmed_timestamp).await?;

tx_skeleton
.header_deps_mut()
.push(ckb_types::prelude::Unpack::unpack(&l1_confirmed_header.hash()).pack());
}

// Package pending revert withdrawals and custodians
let db = { self.chain.lock().await.store().begin_transaction() };
Expand Down
16 changes: 1 addition & 15 deletions crates/block-producer/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use gw_jsonrpc_types::test_mode::TestModePayload;
use gw_rpc_client::contract::ContractsCellDepManager;
use gw_rpc_client::rpc_client::RPCClient;
use gw_types::bytes::Bytes;
use gw_types::core::{ChallengeTargetType, Status, Timepoint};
use gw_types::core::{ChallengeTargetType, Status};
use gw_types::offchain::{
global_state_from_slice, CellInfo, InputCellInfo, RollupContext, TxStatus,
};
Expand All @@ -39,7 +39,6 @@ use gw_utils::wallet::Wallet;
use tokio::sync::Mutex;
use tracing::instrument;

use gw_utils::get_l1_confirmed_header;
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::sync::Arc;
Expand Down Expand Up @@ -451,23 +450,13 @@ impl Challenger {
};
let prev_state = rollup_state.get_state().to_owned();
let burn_lock = self.config.challenger_config.burn_lock.clone().into();

let prev_timestamp =
match Timepoint::from_full_value(prev_state.last_finalized_block_number().unpack()) {
Timepoint::BlockNumber(_) => 0,
Timepoint::Timestamp(ts) => {
ts + self.rollup_context.rollup_config.finality_as_duration()
}
};
let l1_confirmed_header = get_l1_confirmed_header(&self.rpc_client, prev_timestamp).await?;
let revert = Revert::new(
self.rollup_context.clone(),
prev_state,
&challenge_cell,
&stake_cells,
burn_lock,
context,
l1_confirmed_header,
);
let revert_output = revert.build_output()?;

Expand All @@ -492,9 +481,6 @@ impl Challenger {
tx_skeleton.inputs_mut().push(rollup_state.rollup_input());
tx_skeleton.outputs_mut().push(rollup_output);
tx_skeleton.witnesses_mut().push(rollup_witness);
tx_skeleton
.header_deps_mut()
.extend(revert_output.header_deps);

// Challenge
let challenge_input = to_input_cell_info_with_since(challenge_cell, since);
Expand Down
8 changes: 5 additions & 3 deletions crates/block-producer/src/produce_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub struct ProduceBlockParam {
pub stake_cell_owner_lock_hash: H256,
pub reverted_block_root: H256,
pub rollup_config_hash: H256,
pub l1_confirmed_header_timestamp: u64,
pub block_param: BlockParam,
}

Expand All @@ -62,7 +61,6 @@ pub fn produce_block(
stake_cell_owner_lock_hash,
reverted_block_root,
rollup_config_hash,
l1_confirmed_header_timestamp,
block_param:
BlockParam {
number,
Expand Down Expand Up @@ -167,7 +165,11 @@ pub fn produce_block(
} else {
let finality_as_duration = rollup_context.rollup_config.finality_as_duration();
Timepoint::from_timestamp(
l1_confirmed_header_timestamp.saturating_sub(finality_as_duration),
block
.raw()
.timestamp()
.unpack()
.saturating_sub(finality_as_duration),
)
};
let global_state = GlobalState::new_builder()
Expand Down
17 changes: 7 additions & 10 deletions crates/challenge/src/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use gw_types::packed::ChallengeLockArgsReader;
use gw_types::packed::RawL2Block;
use gw_types::packed::RollupRevert;
use gw_types::packed::{
Byte32, CellOutput, ChallengeLockArgs, GlobalState, RollupAction, RollupActionUnion, Script,
CellOutput, ChallengeLockArgs, GlobalState, RollupAction, RollupActionUnion, Script,
WitnessArgs,
};
use gw_types::{
Expand All @@ -30,15 +30,13 @@ pub struct Revert<'a> {
burn_lock: Script,
post_reverted_block_root: [u8; 32],
revert_witness: RevertWitness,
l1_confirmed_header: ckb_types::core::HeaderView,
}

pub struct RevertOutput {
pub post_global_state: GlobalState,
pub reward_cells: Vec<(CellOutput, Bytes)>,
pub burn_cells: Vec<(CellOutput, Bytes)>,
pub rollup_witness: WitnessArgs,
pub header_deps: Vec<Byte32>,
}

impl<'a> Revert<'a> {
Expand All @@ -49,7 +47,6 @@ impl<'a> Revert<'a> {
stake_cells: &'a [CellInfo],
burn_lock: Script,
revert_context: RevertContext,
l1_confirmed_header: ckb_types::core::HeaderView,
) -> Self {
let reward_burn_rate = rollup_context.rollup_config.reward_burn_rate().into();
let finality_blocks = rollup_context.rollup_config.finality_blocks().unpack();
Expand All @@ -64,7 +61,6 @@ impl<'a> Revert<'a> {
reward_burn_rate,
post_reverted_block_root: revert_context.post_reverted_block_root.into(),
revert_witness: revert_context.revert_witness,
l1_confirmed_header,
}
}

Expand Down Expand Up @@ -115,7 +111,12 @@ impl<'a> Revert<'a> {
.saturating_sub(self.finality_blocks),
)
} else {
Timepoint::from_timestamp(self.l1_confirmed_header.timestamp())
Timepoint::Timestamp(
first_reverted_block
.timestamp()
.unpack()
.saturating_sub(self.rollup_context.rollup_config.finality_as_duration()),
)
};
let running_status: u8 = Status::Running.into();

Expand Down Expand Up @@ -147,15 +148,11 @@ impl<'a> Revert<'a> {
.output_type(Some(rollup_action.as_bytes()).pack())
.build();

let header_deps =
vec![ckb_types::prelude::Unpack::unpack(&self.l1_confirmed_header.hash()).pack()];

Ok(RevertOutput {
post_global_state,
reward_cells: rewards_output.reward_cells,
burn_cells: rewards_output.burn_cells,
rollup_witness,
header_deps,
})
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/tests/src/testing_tool/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,10 @@ pub async fn construct_block_with_timestamp(
let remaining_capacity = mem_block.take_finalized_custodians_capacity();
let block_param = generate_produce_block_param(chain.store(), mem_block, post_merkle_state)?;
let reverted_block_root = db.get_reverted_block_smt_root().unwrap();
let l1_confirmed_header_timestamp = 0;
let param = ProduceBlockParam {
stake_cell_owner_lock_hash,
rollup_config_hash,
reverted_block_root,
l1_confirmed_header_timestamp,
block_param,
};
produce_block(db, generator, param).map(|mut r| {
Expand Down
2 changes: 0 additions & 2 deletions crates/tests/src/tests/mem_block_repackage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ async fn test_repackage_mem_block() {
let smt = db.reverted_block_smt().unwrap();
smt.root().to_owned()
};
let l1_confirmed_header_timestamp = 0;
let param = ProduceBlockParam {
stake_cell_owner_lock_hash: random_always_success_script(&rollup_script_hash)
.hash()
.into(),
reverted_block_root,
rollup_config_hash: rollup_context.rollup_config.hash().into(),
l1_confirmed_header_timestamp,
block_param,
};
let store = chain.store();
Expand Down
2 changes: 0 additions & 2 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod genesis_info;
pub mod liveness;
pub mod local_cells;
pub mod polyjuice_parser;
mod query_l1_header_by_timestamp;
mod query_rollup_cell;
pub mod script_log;
pub mod since;
Expand All @@ -17,5 +16,4 @@ pub mod wallet;
pub mod withdrawal;

pub use calc_finalizing_range::calc_finalizing_range;
pub use query_l1_header_by_timestamp::{get_l1_confirmed_header, query_l1_header_by_timestamp};
pub use query_rollup_cell::query_rollup_cell;
Loading

0 comments on commit 2034831

Please sign in to comment.