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!: omni rollup cell lock #608

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
8 changes: 7 additions & 1 deletion crates/block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,9 @@ impl BlockProducer {
} = args;

let rollup_context = self.generator.rollup_context();
let mut tx_skeleton = TransactionSkeleton::default();
let omni_lock_code_hash = self.contracts_dep_manager.load_scripts().omni_lock.hash();
let mut tx_skeleton = TransactionSkeleton::new(omni_lock_code_hash.0);

// rollup cell
tx_skeleton.inputs_mut().push(InputCellInfo {
input: CellInput::new_builder()
Expand Down Expand Up @@ -693,6 +695,10 @@ impl BlockProducer {
tx_skeleton
.cell_deps_mut()
.push(self.ckb_genesis_info.sighash_dep());
// omni lock
tx_skeleton
.cell_deps_mut()
.push(contracts_dep.omni_lock.clone().into());

// Package pending revert withdrawals and custodians
let db = { self.chain.lock().await.store().begin_transaction() };
Expand Down
9 changes: 6 additions & 3 deletions crates/block-producer/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ impl Challenger {
let challenge_output = enter_challenge.build_output();

// Build challenge transaction
let mut tx_skeleton = TransactionSkeleton::default();
let omni_lock_code_hash = self.contracts_dep_manager.load_scripts().omni_lock.hash();
let mut tx_skeleton = TransactionSkeleton::new(omni_lock_code_hash.0);
let contracts_dep = self.contracts_dep_manager.load();

// Rollup
Expand Down Expand Up @@ -420,7 +421,8 @@ impl Challenger {
let revert_output = revert.build_output()?;

// Build revert transaction
let mut tx_skeleton = TransactionSkeleton::default();
let omni_lock_code_hash = self.contracts_dep_manager.load_scripts().omni_lock.hash();
let mut tx_skeleton = TransactionSkeleton::new(omni_lock_code_hash.0);
let contracts_dep = self.contracts_dep_manager.load();

// Rollup
Expand Down Expand Up @@ -505,7 +507,8 @@ impl Challenger {
challenge_input: InputCellInfo,
verifier_context: VerifierContext,
) -> Result<Transaction> {
let mut tx_skeleton = TransactionSkeleton::default();
let omni_lock_code_hash = self.contracts_dep_manager.load_scripts().omni_lock.hash();
let mut tx_skeleton = TransactionSkeleton::new(omni_lock_code_hash.0);
let contracts_dep = self.contracts_dep_manager.load();

// Rollup
Expand Down
17 changes: 4 additions & 13 deletions crates/block-producer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ pub struct BaseInitComponents {
}

impl BaseInitComponents {
#[allow(deprecated)]
pub async fn init(config: &Config, skip_config_check: bool) -> Result<Self> {
let rollup_config: RollupConfig = config.genesis.rollup_config.clone().into();
let rollup_context = RollupContext {
Expand All @@ -303,20 +302,12 @@ impl BaseInitComponents {

let opt_block_producer_config = config.block_producer.as_ref();
let mut contracts_dep_manager = None;
if let Some(block_producer_config) = opt_block_producer_config {
use gw_rpc_client::contract::{check_script, query_type_script_from_old_config};
let mut script_config = config.consensus.contract_type_scripts.clone();
if opt_block_producer_config.is_some() {
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;

if check_script(&script_config, &rollup_config, rollup_type_script).is_err() {
let now = Instant::now();
script_config =
query_type_script_from_old_config(&rpc_client, block_producer_config).await?;
log::trace!("[contracts dep] old config {}ms", now.elapsed().as_millis());

check_script(&script_config, &rollup_config, rollup_type_script)?;
}

check_script(&script_config, &rollup_config, rollup_type_script)?;
contracts_dep_manager =
Some(ContractsCellDepManager::build(rpc_client.clone(), script_config).await?);
}
Expand Down
7 changes: 6 additions & 1 deletion crates/challenge/src/offchain/mock_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ pub fn mock_cancel_challenge_tx(
let verifier_context = VerifierContext::mock_from(&mut cancel_output, &contracts_dep)?;
let contracts_dep = mock_rollup.contracts_dep_manager.load();

let mut tx_skeleton = TransactionSkeleton::default();
let omni_lock_code_hash = {
let scripts = mock_rollup.contracts_dep_manager.load_scripts();
scripts.omni_lock.hash()
};
let mut tx_skeleton = TransactionSkeleton::new(omni_lock_code_hash.0);
let mut cell_deps = Vec::new();
let mut inputs = Vec::new();

Expand All @@ -90,6 +94,7 @@ pub fn mock_cancel_challenge_tx(
let rollup_deps = vec![
contracts_dep.rollup_cell_type.clone().into(),
mock_rollup.config.rollup_config_cell_dep.clone().into(),
contracts_dep.omni_lock.clone().into(),
];
let rollup_output = (
rollup_input.cell.output.clone(),
Expand Down
108 changes: 2 additions & 106 deletions crates/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub struct ContractTypeScriptConfig {
pub withdrawal_lock: Script,
pub challenge_lock: Script,
pub l1_sudt: Script,
pub omni_lock: Script,
pub allowed_eoa_scripts: HashMap<H256, Script>,
pub allowed_contract_scripts: HashMap<H256, Script>,
}
Expand All @@ -134,6 +135,7 @@ pub struct ContractsCellDep {
pub withdrawal_cell_lock: CellDep,
pub challenge_cell_lock: CellDep,
pub l1_sudt_type: CellDep,
pub omni_lock: CellDep,
pub allowed_eoa_locks: HashMap<H256, CellDep>,
pub allowed_contract_types: HashMap<H256, CellDep>,
}
Expand All @@ -143,51 +145,12 @@ pub struct ConsensusConfig {
pub contract_type_scripts: ContractTypeScriptConfig,
}

// TODO: remove deprecated fields
// NOTE: deprecated cell dep fields are used to fetch contract type scripts.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct BlockProducerConfig {
pub account_id: u32,
#[serde(default = "default_check_mem_block_before_submit")]
pub check_mem_block_before_submit: bool,
// cell deps
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub rollup_cell_type_dep: CellDep,
pub rollup_config_cell_dep: CellDep,
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub deposit_cell_lock_dep: CellDep,
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub stake_cell_lock_dep: CellDep,
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub custodian_cell_lock_dep: CellDep,
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub withdrawal_cell_lock_dep: CellDep,
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub challenge_cell_lock_dep: CellDep,
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub l1_sudt_type_dep: CellDep,
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub allowed_eoa_deps: HashMap<H256, CellDep>,
#[deprecated]
#[serde(skip_serializing)]
#[serde(default)]
pub allowed_contract_deps: HashMap<H256, CellDep>,
pub challenger_config: ChallengerConfig,
pub wallet_config: WalletConfig,
#[serde(default = "default_withdrawal_unlocker_wallet")]
Expand Down Expand Up @@ -412,70 +375,3 @@ pub struct DynamicConfig {
pub fee_config: FeeConfig,
pub rpc_config: RPCConfig,
}

#[cfg(test)]
mod test {
use std::collections::HashMap;

use ckb_fixed_hash::H256;
use gw_jsonrpc_types::blockchain::{CellDep, OutPoint};
use serde::{Deserialize, Serialize};

use crate::{BlockProducerConfig, ChallengerConfig, WalletConfig};

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct OldBlockProducerConfig {
pub account_id: u32,
#[serde(default = "default_check_mem_block_before_submit")]
pub check_mem_block_before_submit: bool,
// cell deps
pub rollup_cell_type_dep: CellDep,
pub rollup_config_cell_dep: CellDep,
pub deposit_cell_lock_dep: CellDep,
pub stake_cell_lock_dep: CellDep,
pub custodian_cell_lock_dep: CellDep,
pub withdrawal_cell_lock_dep: CellDep,
pub challenge_cell_lock_dep: CellDep,
pub l1_sudt_type_dep: CellDep,
pub allowed_eoa_deps: HashMap<H256, CellDep>,
pub allowed_contract_deps: HashMap<H256, CellDep>,
pub challenger_config: ChallengerConfig,
pub wallet_config: WalletConfig,
}

fn default_check_mem_block_before_submit() -> bool {
false
}

#[allow(deprecated)]
#[test]
fn test_block_producer_config_serde() {
// Reading from old config
let expected_rollup_cell_type_dep = CellDep {
out_point: OutPoint {
tx_hash: H256([1u8; 32]),
..Default::default()
},
..Default::default()
};

let old_config = OldBlockProducerConfig {
rollup_cell_type_dep: expected_rollup_cell_type_dep.clone(),
..Default::default()
};

let toml_config = toml::to_string(&old_config).unwrap();
let config: BlockProducerConfig = toml::from_str(&toml_config).unwrap();
assert_eq!(config.rollup_cell_type_dep, expected_rollup_cell_type_dep);

// Serialize from new config should skip deprecated fields
let new_toml_config = toml::to_string(&config).unwrap();
let err = toml::from_str::<OldBlockProducerConfig>(&new_toml_config).unwrap_err();
let expected_err_msg = "missing field `rollup_cell_type_dep`";
assert!(err.to_string().contains(expected_err_msg));

// Reading from new config
let new_config: BlockProducerConfig = toml::from_str(&new_toml_config).unwrap();
assert_eq!(new_config.rollup_config_cell_dep, CellDep::default()); // deprecated fields are skipped
}
}
4 changes: 2 additions & 2 deletions crates/mem-pool/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// MAX deposits in the mem block
pub const MAX_MEM_BLOCK_DEPOSITS: usize = 20;
pub const MAX_MEM_BLOCK_DEPOSITS: usize = 50;
/// MAX withdrawals in the mem block
pub const MAX_MEM_BLOCK_WITHDRAWALS: usize = 20;
pub const MAX_MEM_BLOCK_WITHDRAWALS: usize = 50;
/// MAX withdrawals in the mem block
pub const MAX_MEM_BLOCK_TXS: usize = 1000;
/// MAX tx size 50 KB
Expand Down
52 changes: 7 additions & 45 deletions crates/rpc-client/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Instant;
use anyhow::{anyhow, bail, Result};
use arc_swap::ArcSwap;
use async_jsonrpc_client::Params as ClientParams;
use gw_config::{BlockProducerConfig, ContractTypeScriptConfig, ContractsCellDep};
use gw_config::{ContractTypeScriptConfig, ContractsCellDep};
use gw_jsonrpc_types::blockchain::{CellDep, Script};
use gw_types::packed::RollupConfig;
use gw_types::prelude::Pack;
Expand Down Expand Up @@ -41,6 +41,10 @@ impl ContractsCellDepManager {
self.deps.load()
}

pub fn load_scripts(&self) -> &ContractTypeScriptConfig {
&self.scripts
}

pub async fn refresh(&self) -> Result<()> {
log::info!("[contracts dep] refresh");

Expand Down Expand Up @@ -119,6 +123,7 @@ pub async fn query_cell_deps(
let withdrawal_cell_lock = query("withdraw", script_config.withdrawal_lock.clone()).await?;
let challenge_cell_lock = query("challenge", script_config.challenge_lock.clone()).await?;
let l1_sudt_type = query("l1 sudt", script_config.l1_sudt.clone()).await?;
let omni_lock = query("omni", script_config.omni_lock.clone()).await?;

let mut allowed_eoa_locks = HashMap::with_capacity(script_config.allowed_eoa_scripts.len());
for (eoa_hash, eoa_script) in script_config.allowed_eoa_scripts.iter() {
Expand All @@ -141,55 +146,12 @@ pub async fn query_cell_deps(
withdrawal_cell_lock,
challenge_cell_lock,
l1_sudt_type,
omni_lock,
allowed_eoa_locks,
allowed_contract_types,
})
}

// For old config compatibility
#[allow(deprecated)]
#[deprecated]
pub async fn query_type_script_from_old_config(
rpc_client: &RPCClient,
config: &BlockProducerConfig,
) -> Result<ContractTypeScriptConfig> {
let query = |contract: &'static str, cell_dep: CellDep| -> _ {
rpc_client.ckb.query_type_script(contract, cell_dep)
};

let state_validator = query("state validator", config.rollup_cell_type_dep.clone()).await?;
let deposit_lock = query("deposit lock", config.deposit_cell_lock_dep.clone()).await?;
let stake_lock = query("stake lock", config.stake_cell_lock_dep.clone()).await?;
let custodian_lock = query("custodian lock", config.custodian_cell_lock_dep.clone()).await?;
let withdrawal_lock = query("withdrawal lock", config.withdrawal_cell_lock_dep.clone()).await?;
let challenge_lock = query("challenge lock", config.challenge_cell_lock_dep.clone()).await?;
let l1_sudt = query("l1 sudt", config.l1_sudt_type_dep.clone()).await?;

let mut allowed_eoa_scripts = HashMap::with_capacity(config.allowed_eoa_deps.len());
for (type_hash, cell_dep) in config.allowed_eoa_deps.iter() {
let eoa_type_script = query("eoa", cell_dep.clone()).await?;
allowed_eoa_scripts.insert(type_hash.to_owned(), eoa_type_script);
}

let mut allowed_contract_scripts = HashMap::with_capacity(config.allowed_contract_deps.len());
for (type_hash, cell_dep) in config.allowed_contract_deps.iter() {
let contract_type_script = query("contract", cell_dep.clone()).await?;
allowed_contract_scripts.insert(type_hash.to_owned(), contract_type_script);
}

Ok(ContractTypeScriptConfig {
state_validator,
deposit_lock,
stake_lock,
custodian_lock,
withdrawal_lock,
challenge_lock,
l1_sudt,
allowed_eoa_scripts,
allowed_contract_scripts,
})
}

async fn query_by_type_script(
rpc_client: &RPCClient,
contract: &'static str,
Expand Down
Loading