Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize Godwoken finality mechanism #836

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/godwoken-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Godwoken Tests

on:
push:
branches: [develop, master, ci, 'v1*']
tags: ["v*.*.*"]
pull_request:

jobs:
godwoken-tests:
uses: godwokenrises/godwoken-tests/.github/workflows/reusable-integration-test-v1.yml@develop
with:
extra_github_env: |
MANUAL_BUILD_GODWOKEN=true
GODWOKEN_GIT_URL="https://github.com/${{ github.repository }}"
GODWOKEN_GIT_CHECKOUT=${{ github.ref }}
MANUAL_BUILD_SCRIPTS=true
SCRIPTS_GIT_URL="https://github.com/${{ github.repository }}"
SCRIPTS_GIT_CHECKOUT=${{ github.ref }}
31 changes: 23 additions & 8 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},
Expand Down Expand Up @@ -59,9 +61,20 @@ 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
.fork_config
.use_timestamp_as_timepoint(block_number)
{
let block_timestamp = block.raw().timestamp().unpack();
Timepoint::from_timestamp(block_timestamp)
} else {
Timepoint::from_block_number(block_number)
}
};
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 @@ -168,6 +181,7 @@ impl BlockProducer {
let smt = db.reverted_block_smt()?;
smt.root().to_owned()
};

let param = ProduceBlockParam {
stake_cell_owner_lock_hash: self.wallet.lock_script().hash().into(),
reverted_block_root,
Expand Down Expand Up @@ -332,16 +346,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 @@ -350,7 +365,7 @@ impl BlockProducer {
local_cells_manager,
rpc_client,
finalized_custodians,
last_finalized_block_number,
&prev_compatible_finalized_timepoint,
)
.await?
.expect_any();
Expand Down
3 changes: 1 addition & 2 deletions crates/block-producer/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,8 @@ impl Challenger {
};
let prev_state = rollup_state.get_state().to_owned();
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
32 changes: 18 additions & 14 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 @@ -20,12 +22,12 @@ use tracing::instrument;

pub const MAX_CUSTODIANS: usize = 50;

#[instrument(skip_all, fields(last_finalized_block_number = last_finalized_block_number))]
keroro520 marked this conversation as resolved.
Show resolved Hide resolved
#[instrument(skip_all, err(Debug), fields(timepoint = ?compatible_finalized_timepoint))]
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 +37,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 +48,17 @@ 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))]
#[instrument(skip_all, err(Debug), fields(timepoint = ?compatible_finalized_timepoint))]
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 +69,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 +79,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 @@ -136,9 +138,11 @@ 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_timepoint()
.unpack(),
)) {
continue;
}

Expand Down Expand Up @@ -172,7 +176,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 @@ -234,7 +238,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 @@ -248,7 +252,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
24 changes: 20 additions & 4 deletions crates/block-producer/src/produce_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use gw_store::{
transaction::StoreTransaction,
Store,
};
use gw_types::core::Timepoint;
use gw_types::{
core::Status,
offchain::{BlockParam, DepositInfo, FinalizedCustodianCapacity},
Expand Down Expand Up @@ -157,18 +158,33 @@ 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
.fork_config
.use_timestamp_as_timepoint(number)
{
let finality_time_in_ms = rollup_context.rollup_config.finality_time_in_ms();
Timepoint::from_timestamp(
block
.raw()
.timestamp()
.unpack()
.saturating_sub(finality_time_in_ms),
)
} else {
let finality_as_blocks = rollup_context.rollup_config.finality_blocks().unpack();
Timepoint::from_block_number(number.saturating_sub(finality_as_blocks))
};
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.global_state_version(number).into())
.build();
Ok(ProduceBlockResult {
block,
Expand Down
17 changes: 12 additions & 5 deletions crates/block-producer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,23 @@ impl BaseInitComponents {
)
};

let opt_block_producer_config = config.block_producer.as_ref();
let mut contracts_dep_manager = None;
if opt_block_producer_config.is_some() {
let opt_block_producer_config = config.block_producer.as_ref();
if let Some(block_producer_config) = opt_block_producer_config {
use gw_rpc_client::contract::check_script;
let script_config = config.consensus.contract_type_scripts.clone();
let rollup_type_script = &config.chain.rollup_type_script;
let rollup_config_cell_dep = block_producer_config.rollup_config_cell_dep.clone();

check_script(&script_config, &rollup_config, rollup_type_script)?;
contracts_dep_manager =
Some(ContractsCellDepManager::build(rpc_client.clone(), script_config).await?);
contracts_dep_manager = Some(
ContractsCellDepManager::build(
rpc_client.clone(),
script_config,
rollup_config_cell_dep,
)
.await?,
);
}

if !skip_config_check {
Expand Down Expand Up @@ -550,7 +557,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
32 changes: 23 additions & 9 deletions crates/block-producer/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use gw_rpc_client::{
indexer_types::{Order, SearchKey, SearchKeyFilter},
rpc_client::RPCClient,
};
use gw_types::core::Timepoint;
use gw_types::offchain::CompatibleFinalizedTimepoint;
use gw_types::{
core::ScriptHashType,
offchain::{CellInfo, InputCellInfo},
Expand Down Expand Up @@ -37,12 +39,23 @@ pub async fn generate(
local_cells_manager: &LocalCellsManager,
) -> Result<GeneratedStake> {
let owner_lock_hash = lock_script.hash();
let stake_block_timepoint = {
let block_number: u64 = block.raw().number().unpack();
if rollup_context
.fork_config
.use_timestamp_as_timepoint(block_number)
{
let block_timestamp: u64 = block.raw().timestamp().unpack();
Timepoint::from_timestamp(block_timestamp)
} else {
Timepoint::from_block_number(block_number)
}
};
let lock_args: Bytes = {
let stake_lock_args = StakeLockArgs::new_builder()
.owner_lock_hash(owner_lock_hash.pack())
.stake_block_number(block.raw().number())
.stake_block_timepoint(stake_block_timepoint.full_value().pack())
.build();

let rollup_type_hash = rollup_context.rollup_script_hash.as_slice().iter();
rollup_type_hash
.chain(stake_lock_args.as_slice().iter())
Expand Down Expand Up @@ -128,14 +141,14 @@ pub async fn generate(

/// query stake
///
/// return cell which stake_block_number is less than last_finalized_block_number if the args isn't none
/// otherwise return stake cell randomly
/// Returns a finalized stake_state_cell if `compatible_finalize_timepoint_opt` is some,
/// otherwise returns a random stake_state_cell.
pub async fn query_stake(
client: &CKBIndexerClient,
rollup_context: &RollupContext,
owner_lock_hash: [u8; 32],
required_staking_capacity: u64,
last_finalized_block_number: Option<u64>,
compatible_finalize_timepoint_opt: Option<CompatibleFinalizedTimepoint>,
local_cells_manager: &LocalCellsManager,
) -> Result<Option<CellInfo>> {
let lock = Script::new_builder()
Expand Down Expand Up @@ -170,10 +183,11 @@ pub async fn query_stake(
Ok(r) => r,
Err(_) => return false,
};
match last_finalized_block_number {
Some(last_finalized_block_number) => {
stake_lock_args.stake_block_number().unpack() <= last_finalized_block_number
&& stake_lock_args.owner_lock_hash().as_slice() == owner_lock_hash
match &compatible_finalize_timepoint_opt {
Some(compatible_finalized_timepoint) => {
compatible_finalized_timepoint.is_finalized(&Timepoint::from_full_value(
stake_lock_args.stake_block_timepoint().unpack(),
)) && stake_lock_args.owner_lock_hash().as_slice() == owner_lock_hash
}
None => stake_lock_args.owner_lock_hash().as_slice() == owner_lock_hash,
}
Expand Down
Loading