Skip to content

Commit

Permalink
all in one
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Nov 1, 2022
1 parent a17ea54 commit d0d2336
Show file tree
Hide file tree
Showing 40 changed files with 514 additions and 227 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build/
.tmp/
crates/benches/smt_data/db/
crates/tests/mem_block/
*.swp
2 changes: 2 additions & 0 deletions crates/benches/benches/benchmarks/smt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl BenchExecutionEnvironment {
let rollup_context = RollupContext {
rollup_config: genesis_config.rollup_config.clone().into(),
rollup_script_hash: ROLLUP_TYPE_HASH.into(),
..Default::default()
};

let backend_manage = {
Expand Down Expand Up @@ -414,6 +415,7 @@ impl BenchExecutionEnvironment {
Vec::new(),
Default::default(),
Vec::new(),
Default::default(),
)
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions crates/benches/benches/benchmarks/sudt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn run_contract_get_result<S: State + CodeStore>(
let rollup_ctx = RollupContext {
rollup_config: rollup_config.clone(),
rollup_script_hash: [42u8; 32].into(),
..Default::default()
};
let generator = Generator::new(
backend_manage,
Expand Down
73 changes: 63 additions & 10 deletions crates/block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use gw_mem_pool::{
};
use gw_rpc_client::{contract::ContractsCellDepManager, rpc_client::RPCClient};
use gw_store::Store;
use gw_types::core::Timepoint;
use gw_types::offchain::{global_state_from_slice, CompatibleFinalizedTimepoint};
use gw_types::{
bytes::Bytes,
offchain::{DepositInfo, InputCellInfo, RollupContext},
Expand All @@ -32,8 +34,9 @@ use gw_types::{
prelude::*,
};
use gw_utils::{
fee::fill_tx_fee_with_local, genesis_info::CKBGenesisInfo, local_cells::LocalCellsManager,
query_rollup_cell, since::Since, transaction_skeleton::TransactionSkeleton, wallet::Wallet,
fee::fill_tx_fee_with_local, genesis_info::CKBGenesisInfo, get_confirmed_header_timestamp,
local_cells::LocalCellsManager, query_l1_header_by_timestamp, query_rollup_cell, since::Since,
transaction_skeleton::TransactionSkeleton, wallet::Wallet,
};
use std::{collections::HashSet, sync::Arc, time::Instant};
use tokio::sync::Mutex;
Expand All @@ -48,9 +51,17 @@ fn generate_custodian_cells(
deposit_cells: &[DepositInfo],
) -> Vec<(CellOutput, Bytes)> {
let block_hash: H256 = block.hash().into();
let block_number = block.raw().number().unpack();
let block_timepoint = {
let block_number = block.raw().number().unpack();
if rollup_context.determine_global_state_version(block_number) < 2 {
Timepoint::from_block_number(block_number)
} else {
let block_timestamp = block.raw().timestamp().unpack();
Timepoint::from_timestamp(block_timestamp)
}
};
let to_custodian = |deposit| -> _ {
to_custodian_cell(rollup_context, &block_hash, block_number, deposit)
to_custodian_cell(rollup_context, &block_hash, &block_timepoint, deposit)
.expect("sanitized deposit")
};

Expand Down Expand Up @@ -157,10 +168,24 @@ impl BlockProducer {
let smt = db.reverted_block_smt()?;
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
// rollback.
let l1_confirmed_header_timestamp =
get_confirmed_header_timestamp(&self.rpc_client).await?;

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 @@ -220,6 +245,33 @@ 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) {
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?
.unwrap_or_else(|| {
panic!(
"get_l1_header_by_timestamp l1_timestamp={}, l2block={:#x}",
l1_confirmed_timestamp, block_number
)
});
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 Expand Up @@ -321,16 +373,17 @@ impl BlockProducer {
.outputs_mut()
.push((generated_stake.output, generated_stake.output_data));

let last_finalized_block_number = self
.generator
.rollup_context()
.last_finalized_block_number(block.raw().number().unpack() - 1);
let prev_global_state = global_state_from_slice(&rollup_cell.data)?;
let prev_compatible_finalized_timepoint = CompatibleFinalizedTimepoint::from_global_state(
&prev_global_state,
rollup_context.rollup_config.finality_blocks().unpack(),
);
let finalized_custodians = gw_mem_pool::custodian::query_finalized_custodians(
rpc_client,
&self.store.get_snapshot(),
withdrawal_extras.iter().map(|w| w.request()),
rollup_context,
last_finalized_block_number,
&prev_compatible_finalized_timepoint,
local_cells_manager,
)
.await?
Expand All @@ -339,7 +392,7 @@ impl BlockProducer {
local_cells_manager,
rpc_client,
finalized_custodians,
last_finalized_block_number,
&prev_compatible_finalized_timepoint,
)
.await?
.expect_any();
Expand Down
2 changes: 1 addition & 1 deletion crates/block-producer/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ impl Challenger {
let burn_lock = self.config.challenger_config.burn_lock.clone().into();

let revert = Revert::new(
&self.rollup_context,
self.rollup_context.clone(),
prev_state,
&challenge_cell,
&stake_cells,
Expand Down
29 changes: 14 additions & 15 deletions crates/block-producer/src/custodian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use gw_rpc_client::{
indexer_types::{Order, SearchKey, SearchKeyFilter},
rpc_client::{QueryResult, RPCClient},
};
use gw_types::core::Timepoint;
use gw_types::offchain::CompatibleFinalizedTimepoint;
use gw_types::{
core::ScriptHashType,
offchain::{CellInfo, CollectedCustodianCells},
Expand All @@ -16,16 +18,14 @@ use gw_types::{
use gw_utils::local_cells::{
collect_local_and_indexer_cells, CollectLocalAndIndexerCursor, LocalCellsManager,
};
use tracing::instrument;

pub const MAX_CUSTODIANS: usize = 50;

#[instrument(skip_all, fields(last_finalized_block_number = last_finalized_block_number))]
pub async fn query_mergeable_custodians(
local_cells_manager: &LocalCellsManager,
rpc_client: &RPCClient,
collected_custodians: CollectedCustodianCells,
last_finalized_block_number: u64,
compatible_finalized_timepoint: &CompatibleFinalizedTimepoint,
) -> Result<QueryResult<CollectedCustodianCells>> {
if collected_custodians.cells_info.len() >= MAX_CUSTODIANS {
return Ok(QueryResult::Full(collected_custodians));
Expand All @@ -35,7 +35,7 @@ pub async fn query_mergeable_custodians(
local_cells_manager,
rpc_client,
collected_custodians,
last_finalized_block_number,
compatible_finalized_timepoint,
MAX_CUSTODIANS,
)
.await?;
Expand All @@ -46,17 +46,16 @@ pub async fn query_mergeable_custodians(
query_mergeable_sudt_custodians(
rpc_client,
query_result.expect_any(),
last_finalized_block_number,
compatible_finalized_timepoint,
local_cells_manager,
)
.await
}

#[instrument(skip_all, fields(last_finalized_block_number = last_finalized_block_number))]
async fn query_mergeable_sudt_custodians(
rpc_client: &RPCClient,
collected: CollectedCustodianCells,
last_finalized_block_number: u64,
compatible_finalized_timepoint: &CompatibleFinalizedTimepoint,
local_cells_manager: &LocalCellsManager,
) -> Result<QueryResult<CollectedCustodianCells>> {
if collected.cells_info.len() >= MAX_CUSTODIANS {
Expand All @@ -67,7 +66,7 @@ async fn query_mergeable_sudt_custodians(
local_cells_manager,
rpc_client,
collected,
last_finalized_block_number,
compatible_finalized_timepoint,
MAX_CUSTODIANS,
)
.await
Expand All @@ -77,7 +76,7 @@ async fn query_mergeable_ckb_custodians(
local_cells_manager: &LocalCellsManager,
rpc_client: &RPCClient,
mut collected: CollectedCustodianCells,
last_finalized_block_number: u64,
compatible_finalized_timepoint: &CompatibleFinalizedTimepoint,
max_cells: usize,
) -> Result<QueryResult<CollectedCustodianCells>> {
const MIN_MERGE_CELLS: usize = 5;
Expand Down Expand Up @@ -135,9 +134,9 @@ async fn query_mergeable_ckb_custodians(
Ok(r) => r,
Err(_) => continue,
};
if custodian_lock_args_reader.deposit_block_number().unpack()
> last_finalized_block_number
{
if !compatible_finalized_timepoint.is_finalized(&Timepoint::from_full_value(
custodian_lock_args_reader.deposit_block_number().unpack(),
)) {
continue;
}

Expand Down Expand Up @@ -171,7 +170,7 @@ pub async fn query_mergeable_sudt_custodians_cells(
local_cells_manager: &LocalCellsManager,
rpc_client: &RPCClient,
mut collected: CollectedCustodianCells,
last_finalized_block_number: u64,
compatible_finalized_timepoint: &CompatibleFinalizedTimepoint,
max_cells: usize,
) -> Result<QueryResult<CollectedCustodianCells>> {
const MAX_MERGE_SUDTS: usize = 5;
Expand Down Expand Up @@ -233,7 +232,7 @@ pub async fn query_mergeable_sudt_custodians_cells(
};

let sudt_type_scripts = rpc_client
.query_random_sudt_type_script(last_finalized_block_number, MAX_MERGE_SUDTS)
.query_random_sudt_type_script(compatible_finalized_timepoint, MAX_MERGE_SUDTS)
.await?;
log::info!("merge {} random sudt type scripts", sudt_type_scripts.len());
let mut collected_set: HashSet<_> = {
Expand All @@ -247,7 +246,7 @@ pub async fn query_mergeable_sudt_custodians_cells(
let query_result = rpc_client
.query_mergeable_sudt_custodians_cells_by_sudt_type_script(
&sudt_type_script,
last_finalized_block_number,
compatible_finalized_timepoint,
remain,
&collected_set,
)
Expand Down
19 changes: 15 additions & 4 deletions crates/block-producer/src/produce_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use gw_store::{
state::state_db::StateContext, traits::chain_store::ChainStore, transaction::StoreTransaction,
Store,
};
use gw_types::core::Timepoint;
use gw_types::{
core::Status,
offchain::{BlockParam, DepositInfo, FinalizedCustodianCapacity},
Expand All @@ -41,6 +42,7 @@ 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 @@ -58,6 +60,7 @@ 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 @@ -155,18 +158,26 @@ pub fn produce_block(
.count(block_count.pack())
.build()
};
let last_finalized_block_number =
number.saturating_sub(rollup_context.rollup_config.finality_blocks().unpack());

let last_finalized_timepoint = if rollup_context.determine_global_state_version(number) < 2 {
let finality_as_blocks = rollup_context.rollup_config.finality_as_blocks();
Timepoint::from_block_number(number.saturating_sub(finality_as_blocks))
} else {
let finality_as_duration = rollup_context.rollup_config.finality_as_duration();
Timepoint::from_timestamp(
l1_confirmed_header_timestamp.saturating_sub(finality_as_duration),
)
};
let global_state = GlobalState::new_builder()
.account(post_merkle_state)
.block(post_block)
.tip_block_hash(block.hash().pack())
.tip_block_timestamp(block.raw().timestamp())
.last_finalized_block_number(last_finalized_block_number.pack())
.last_finalized_block_number(last_finalized_timepoint.full_value().pack())
.reverted_block_root(Into::<[u8; 32]>::into(reverted_block_root).pack())
.rollup_config_hash(rollup_config_hash.pack())
.status((Status::Running as u8).into())
.version(1u8.into())
.version(rollup_context.determine_global_state_version(number).into())
.build();
Ok(ProduceBlockResult {
block,
Expand Down
3 changes: 2 additions & 1 deletion crates/block-producer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ impl BaseInitComponents {
let rollup_script_hash: [u8; 32] = config.genesis.rollup_type_hash.clone().into();
rollup_script_hash.into()
},
timestamp_based_finality_fork_block: Some(config.fork_blocks.timestamp_based_finality),
};
let rollup_type_script: Script = config.chain.rollup_type_script.clone().into();
let rpc_client = {
Expand Down Expand Up @@ -574,7 +575,7 @@ pub async fn run(config: Config, skip_config_check: bool) -> Result<()> {
}
let chain = Arc::new(Mutex::new(
Chain::create(
&rollup_config,
rollup_config.clone(),
&config.chain.rollup_type_script.clone().into(),
&config.chain,
store.clone(),
Expand Down
Loading

0 comments on commit d0d2336

Please sign in to comment.