From a28abd672b58d979f9e8ae47e757903826faa2ad Mon Sep 17 00:00:00 2001 From: EthanYuan Date: Thu, 18 Jul 2024 22:42:27 +0800 Subject: [PATCH 1/2] remove dao in genesis in dev spec. --- resource/specs/dev.toml | 4 - spec/src/consensus.rs | 5 +- spec/src/lib.rs | 11 +- test/src/main.rs | 5 - test/src/specs/dao/dao_tx.rs | 82 ---- test/src/specs/dao/dao_user.rs | 253 ---------- test/src/specs/dao/dao_verifier.rs | 431 ------------------ test/src/specs/dao/dao_verify.rs | 17 - test/src/specs/dao/mod.rs | 10 - test/src/specs/dao/satoshi_dao_occupied.rs | 161 ------- test/src/specs/dao/utils.rs | 30 -- test/src/specs/mod.rs | 2 - util/app-config/src/lib.rs | 17 +- .../src/tests/transaction_verifier.rs | 365 +-------------- verification/src/transaction_verifier.rs | 17 +- 15 files changed, 18 insertions(+), 1392 deletions(-) delete mode 100644 test/src/specs/dao/dao_tx.rs delete mode 100644 test/src/specs/dao/dao_user.rs delete mode 100644 test/src/specs/dao/dao_verifier.rs delete mode 100644 test/src/specs/dao/dao_verify.rs delete mode 100644 test/src/specs/dao/mod.rs delete mode 100644 test/src/specs/dao/satoshi_dao_occupied.rs delete mode 100644 test/src/specs/dao/utils.rs diff --git a/resource/specs/dev.toml b/resource/specs/dev.toml index b5d6584444..facc03f332 100644 --- a/resource/specs/dev.toml +++ b/resource/specs/dev.toml @@ -25,10 +25,6 @@ file = { bundled = "specs/cells/secp256k1_blake160_sighash_all" } create_type_id = true capacity = 100_000_0000_0000 [[genesis.system_cells]] -file = { bundled = "specs/cells/dao" } -create_type_id = true -capacity = 16_000_0000_0000 -[[genesis.system_cells]] file = { bundled = "specs/cells/secp256k1_data" } create_type_id = false capacity = 1_048_617_0000_0000 diff --git a/spec/src/consensus.rs b/spec/src/consensus.rs index e9fa6364f6..6c24e2f9d0 100644 --- a/spec/src/consensus.rs +++ b/spec/src/consensus.rs @@ -9,8 +9,7 @@ use crate::{ self, Deployment, DeploymentPos, ThresholdState, Versionbits, VersionbitsCache, VersionbitsConditionChecker, VersionbitsIndexer, }, - OUTPUT_INDEX_DAO, OUTPUT_INDEX_SECP256K1_BLAKE160_MULTISIG_ALL, - OUTPUT_INDEX_SECP256K1_BLAKE160_SIGHASH_ALL, + OUTPUT_INDEX_SECP256K1_BLAKE160_MULTISIG_ALL, OUTPUT_INDEX_SECP256K1_BLAKE160_SIGHASH_ALL, }; use ckb_constant::{ consensus::TAU, @@ -345,7 +344,7 @@ impl ConsensusBuilder { "genesis block must contain the witness for cellbase" ); - self.inner.dao_type_hash = self.get_type_hash(OUTPUT_INDEX_DAO).unwrap_or_default(); + self.inner.dao_type_hash = Byte32::default(); self.inner.secp256k1_blake160_sighash_all_type_hash = self.get_type_hash(OUTPUT_INDEX_SECP256K1_BLAKE160_SIGHASH_ALL); self.inner.secp256k1_blake160_multisig_all_type_hash = diff --git a/spec/src/lib.rs b/spec/src/lib.rs index 747d69e591..379c54e52a 100644 --- a/spec/src/lib.rs +++ b/spec/src/lib.rs @@ -22,8 +22,8 @@ use ckb_hash::{blake2b_256, new_blake2b}; use ckb_jsonrpc_types::Script; use ckb_pow::{Pow, PowEngine}; use ckb_resource::{ - Resource, CODE_HASH_DAO, CODE_HASH_SECP256K1_BLAKE160_MULTISIG_ALL, - CODE_HASH_SECP256K1_BLAKE160_SIGHASH_ALL, CODE_HASH_SECP256K1_DATA, + Resource, CODE_HASH_SECP256K1_BLAKE160_MULTISIG_ALL, CODE_HASH_SECP256K1_BLAKE160_SIGHASH_ALL, + CODE_HASH_SECP256K1_DATA, }; use ckb_types::{ bytes::Bytes, @@ -59,12 +59,10 @@ const SPECIAL_CELL_PRIVKEY: H256 = /// The output index of SECP256K1/blake160 script in the genesis no.0 transaction pub const OUTPUT_INDEX_SECP256K1_BLAKE160_SIGHASH_ALL: u64 = 1; -/// The output index of DAO script in the genesis no.0 transaction -pub const OUTPUT_INDEX_DAO: u64 = 2; /// The output data index of SECP256K1 in the genesis no.0 transaction -pub const OUTPUT_INDEX_SECP256K1_DATA: u64 = 3; +pub const OUTPUT_INDEX_SECP256K1_DATA: u64 = 2; /// The output index of SECP256K1/multisig script in the genesis no.0 transaction -pub const OUTPUT_INDEX_SECP256K1_BLAKE160_MULTISIG_ALL: u64 = 4; +pub const OUTPUT_INDEX_SECP256K1_BLAKE160_MULTISIG_ALL: u64 = 3; /// The CKB block chain specification #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] @@ -737,7 +735,6 @@ impl ChainSpec { OUTPUT_INDEX_SECP256K1_BLAKE160_SIGHASH_ALL as usize, &CODE_HASH_SECP256K1_BLAKE160_SIGHASH_ALL, )?; - check_cells_data_hash(0, OUTPUT_INDEX_DAO as usize, &CODE_HASH_DAO)?; check_cells_data_hash( 0, OUTPUT_INDEX_SECP256K1_DATA as usize, diff --git a/test/src/main.rs b/test/src/main.rs index cef9c5a673..bdb02450b6 100644 --- a/test/src/main.rs +++ b/test/src/main.rs @@ -417,10 +417,6 @@ fn all_specs() -> Vec> { Box::new(ForksContainSameUncle), Box::new(SendConflictTxToRelay), Box::new(SendConflictTxToRelayRBF), - Box::new(WithdrawDAO), - Box::new(WithdrawDAOWithOverflowCapacity), - Box::new(DAOWithSatoshiCellOccupied), - Box::new(SpendSatoshiCell::new()), Box::new(MiningBasic), Box::new(BlockTemplates), Box::new(BootstrapCellbase), @@ -554,7 +550,6 @@ fn all_specs() -> Vec> { Box::new(ConflictInProposed), Box::new(RemoveConflictFromPending), Box::new(SubmitConflict), - Box::new(DAOVerify), Box::new(AvoidDuplicatedProposalsWithUncles), Box::new(BlockSyncRelayerCollaboration), Box::new(RpcGetBlockTemplate), diff --git a/test/src/specs/dao/dao_tx.rs b/test/src/specs/dao/dao_tx.rs deleted file mode 100644 index 64eec88332..0000000000 --- a/test/src/specs/dao/dao_tx.rs +++ /dev/null @@ -1,82 +0,0 @@ -use crate::specs::dao::dao_user::DAOUser; -use crate::specs::dao::dao_verifier::DAOVerifier; -use crate::specs::dao::utils::{ensure_committed, goto_target_point}; -use crate::utils::{assert_send_transaction_fail, generate_utxo_set}; -use crate::{Node, Spec}; - -use ckb_types::core::EpochNumberWithFraction; -use ckb_types::{core::Capacity, prelude::*}; - -pub struct WithdrawDAO; - -impl Spec for WithdrawDAO { - fn modify_chain_spec(&self, spec: &mut ckb_chain_spec::ChainSpec) { - spec.params.genesis_epoch_length = Some(2); - spec.params.epoch_duration_target = Some(16); - spec.params.permanent_difficulty_in_dummy = Some(true); - } - - fn run(&self, nodes: &mut Vec) { - let node = &nodes[0]; - let utxos = generate_utxo_set(node, 21); - let mut user = DAOUser::new(node, utxos); - - ensure_committed(node, &user.deposit()); - node.mine(20); // Time makes interest - ensure_committed(node, &user.prepare()); - - let withdrawal = user.withdraw(); - let since = EpochNumberWithFraction::from_full_value( - withdrawal.inputs().get(0).unwrap().since().unpack(), - ); - goto_target_point(node, since); - ensure_committed(node, &withdrawal); - DAOVerifier::init(node).verify(); - } -} - -pub struct WithdrawDAOWithOverflowCapacity; - -impl Spec for WithdrawDAOWithOverflowCapacity { - fn modify_chain_spec(&self, spec: &mut ckb_chain_spec::ChainSpec) { - spec.params.genesis_epoch_length = Some(2); - spec.params.epoch_duration_target = Some(16); - spec.params.permanent_difficulty_in_dummy = Some(true); - } - - fn run(&self, nodes: &mut Vec) { - let node = &nodes[0]; - let utxos = generate_utxo_set(node, 21); - let mut user = DAOUser::new(node, utxos); - - ensure_committed(node, &user.deposit()); - node.mine(20); // Time makes interest - ensure_committed(node, &user.prepare()); - - let withdrawal = user.withdraw(); - let invalid_withdrawal = { - let outputs: Vec<_> = withdrawal - .outputs() - .into_iter() - .map(|cell_output| { - let old_capacity: Capacity = cell_output.capacity().unpack(); - let new_capacity = old_capacity.safe_add(Capacity::one()).unwrap(); - cell_output - .as_builder() - .capacity(new_capacity.pack()) - .build() - }) - .collect(); - withdrawal - .as_advanced_builder() - .set_outputs(outputs) - .build() - }; - let since = EpochNumberWithFraction::from_full_value( - withdrawal.inputs().get(0).unwrap().since().unpack(), - ); - goto_target_point(node, since); - assert_send_transaction_fail(node, &invalid_withdrawal, "Overflow"); - ensure_committed(node, &withdrawal); - } -} diff --git a/test/src/specs/dao/dao_user.rs b/test/src/specs/dao/dao_user.rs deleted file mode 100644 index aa729e8476..0000000000 --- a/test/src/specs/dao/dao_user.rs +++ /dev/null @@ -1,253 +0,0 @@ -use crate::utils::since_from_absolute_epoch_number; -use crate::{Node, TXOSet, TXO}; -use ckb_chain_spec::OUTPUT_INDEX_DAO; -use ckb_types::core::{EpochNumberWithFraction, HeaderView}; -use ckb_types::packed::WitnessArgs; -use ckb_types::{ - bytes::Bytes, - core::{ScriptHashType, TransactionBuilder, TransactionView}, - packed::{CellDep, CellInput, CellOutput, OutPoint, Script}, - prelude::*, -}; -use std::collections::HashSet; - -// https://github.com/nervosnetwork/ckb-system-scripts/blob/1fd4cd3e2ab7e5ffbafce1f60119b95937b3c6eb/c/dao.c#L81 -pub const LOCK_PERIOD_EPOCHS: u64 = 180; - -pub struct DAOUser<'a> { - node: &'a Node, - always_utxo: TXOSet, - deposit_utxo: TXOSet, - prepare_utxo: TXOSet, - withdraw_utxo: TXOSet, -} - -impl<'a> DAOUser<'a> { - pub fn new(node: &'a Node, always_utxo: TXOSet) -> Self { - Self { - node, - always_utxo, - deposit_utxo: Default::default(), - prepare_utxo: Default::default(), - withdraw_utxo: Default::default(), - } - } - - pub fn deposit(&mut self) -> TransactionView { - assert!(!self.always_utxo.is_empty()); - let node = self.node; - let inputs = self - .always_utxo - .iter() - .map(|txo| CellInput::new(txo.out_point(), 0)) - .collect::>(); - let output_data = Bytes::from(&[0u8; 8][..]).pack(); - let outputs = { - // TRICK: When we change the always_outputs to deposit_outputs, the always_output's - // capacity will be insufficient. So here uses part of always_outputs' capacity - // as the "capacity filler". - let outputs_len = self.always_utxo.len() / 2; - let capacity = self.always_utxo.total_capacity() / outputs_len as u64; - (0..outputs_len) - .map(|_| { - CellOutput::new_builder() - .capacity(capacity.pack()) - .lock(node.always_success_script()) - .type_(Some(self.dao_type_script()).pack()) - .build() - }) - .collect::>() - }; - let outputs_data = outputs - .iter() - .map(|_| output_data.clone()) - .collect::>(); - let cell_deps = vec![node.always_success_cell_dep(), self.dao_cell_dep()]; - let tx = TransactionBuilder::default() - .cell_deps(cell_deps) - .inputs(inputs) - .outputs(outputs) - .outputs_data(outputs_data) - .witness(Default::default()) - .build(); - self.deposit_utxo = TXOSet::from(&tx); - tx - } - - pub fn prepare(&mut self) -> TransactionView { - assert!(!self.deposit_utxo.is_empty()); - let node = self.node; - let deposit_utxo_headers = self.utxo_headers(&self.deposit_utxo); - let inputs = deposit_utxo_headers - .iter() - .map(|(txo, _)| CellInput::new(txo.out_point(), 0)); - let outputs = deposit_utxo_headers.iter().map(|(txo, _)| { - CellOutput::new_builder() - .capacity(txo.capacity().pack()) - .lock(txo.lock()) - .type_(txo.type_()) - .build() - }); - let outputs_data = deposit_utxo_headers.iter().map(|(_, header)| { - let deposit_number = header.number(); - Bytes::from(deposit_number.to_le_bytes().to_vec()).pack() - }); - let cell_deps = vec![node.always_success_cell_dep(), self.dao_cell_dep()]; - // NOTE: dao.c uses `deposit_header` to ensure the prepare_output.capacity == deposit_output.capacity - let header_deps = deposit_utxo_headers - .iter() - .map(|(_, header)| header.hash()) - .collect::>() - .into_iter() - .collect::>(); - let witnesses = deposit_utxo_headers - .iter() - .map(|(_, header)| { - let index = header_deps - .iter() - .position(|hash| hash == &header.hash()) - .unwrap() as u64; - WitnessArgs::new_builder() - .input_type(Some(Bytes::from(index.to_le_bytes().to_vec())).pack()) - .build() - .as_bytes() - .pack() - }) - .collect::>(); - let tx = TransactionBuilder::default() - .inputs(inputs) - .outputs(outputs) - .cell_deps(cell_deps) - .header_deps(header_deps) - .witnesses(witnesses) - .outputs_data(outputs_data) - .build(); - self.prepare_utxo = TXOSet::from(&tx); - tx - } - - pub fn withdraw(&mut self) -> TransactionView { - assert!(!self.prepare_utxo.is_empty()); - let node = self.node; - let deposit_utxo_headers = self.utxo_headers(&self.deposit_utxo); - let prepare_utxo_headers = self.utxo_headers(&self.prepare_utxo); - let inputs = prepare_utxo_headers.iter().map(|(txo, _)| { - let minimal_unlock_point = self.minimal_unlock_point(&txo.out_point()); - let since = since_from_absolute_epoch_number(minimal_unlock_point); - CellInput::new(txo.out_point(), since) - }); - let output_capacity = deposit_utxo_headers - .iter() - .zip(prepare_utxo_headers.iter()) - .map(|((deposit_txo, _), (_, prepare_header))| { - let balance = node.rpc_client().calculate_dao_maximum_withdraw( - deposit_txo.out_point().into(), - prepare_header.hash(), - ); - balance.as_u64() - }) - .sum::(); - let output = CellOutput::new_builder() - .capacity(output_capacity.pack()) - .lock(node.always_success_script()) - .build(); - let cell_deps = vec![node.always_success_cell_dep(), self.dao_cell_dep()]; - let header_deps = deposit_utxo_headers - .iter() - .chain(prepare_utxo_headers.iter()) - .map(|(_, header)| header.hash()) - .collect::>() - .into_iter() - .collect::>(); - let witnesses = deposit_utxo_headers - .iter() - .map(|(_, header)| { - let index = header_deps - .iter() - .position(|hash| hash == &header.hash()) - .unwrap() as u64; - WitnessArgs::new_builder() - .input_type(Some(Bytes::from(index.to_le_bytes().to_vec())).pack()) - .build() - .as_bytes() - .pack() - }) - .collect::>(); - let tx = TransactionBuilder::default() - .inputs(inputs) - .output(output) - .cell_deps(cell_deps) - .header_deps(header_deps) - .witnesses(witnesses) - .output_data(Default::default()) - .build(); - self.withdraw_utxo = TXOSet::from(&tx); - tx - } - - pub fn dao_type_script(&self) -> Script { - Script::new_builder() - .code_hash(self.node.consensus().dao_type_hash()) - .hash_type(ScriptHashType::Type.into()) - .build() - } - - fn dao_cell_dep(&self) -> CellDep { - let node = self.node; - CellDep::new_builder() - .out_point(OutPoint::new( - node.consensus() - .genesis_block() - .transaction(0) - .unwrap() - .hash(), - OUTPUT_INDEX_DAO as u32, - )) - .build() - } - - fn utxo_headers(&self, utxo: &TXOSet) -> Vec<(TXO, HeaderView)> { - utxo.iter() - .map(|txo| { - let tx_hash = txo.out_point().tx_hash(); - let block_hash = self - .node - .rpc_client() - .get_transaction(tx_hash) - .tx_status - .block_hash - .expect("get utxo transaction block hash"); - let header = self - .node - .rpc_client() - .get_header(block_hash.pack()) - .expect("get utxo transaction block header") - .into(); - (txo, header) - }) - .collect() - } - - fn minimal_unlock_point(&self, out_point: &OutPoint) -> EpochNumberWithFraction { - let node = self.node; - let tx_hash = out_point.tx_hash(); - let deposit_point = { - let deposit_hash = node - .rpc_client() - .get_transaction(tx_hash) - .tx_status - .block_hash - .expect("get deposit transaction block hash"); - let deposit_header = node - .rpc_client() - .get_header(deposit_hash.pack()) - .expect("get deposit transaction block header"); - EpochNumberWithFraction::from_full_value(deposit_header.inner.epoch.value()) - }; - EpochNumberWithFraction::new( - deposit_point.number() + LOCK_PERIOD_EPOCHS, - deposit_point.index(), - deposit_point.length(), - ) - } -} diff --git a/test/src/specs/dao/dao_verifier.rs b/test/src/specs/dao/dao_verifier.rs deleted file mode 100644 index b2b1fab86a..0000000000 --- a/test/src/specs/dao/dao_verifier.rs +++ /dev/null @@ -1,431 +0,0 @@ -use crate::Node; -use byteorder::{ByteOrder, LittleEndian}; -use ckb_chain_spec::consensus::Consensus; -use ckb_dao_utils::extract_dao_data; -use ckb_jsonrpc_types::EpochView; -use ckb_types::core::{BlockEconomicState, BlockNumber, BlockView, Capacity, TransactionView}; -use ckb_types::packed::{Byte32, CellOutput, OutPoint}; -use ckb_types::prelude::Unpack; -use ckb_util::Mutex; -use std::collections::HashMap; - -#[derive(Default)] -#[allow(non_snake_case)] -pub struct DAOVerifier { - consensus: Consensus, - tip_number: BlockNumber, - blocks: Vec, - transactions: HashMap, - epochs: Vec, - blocks_reward: Vec>, - - cache_C: Mutex>, - cache_S: Mutex>, - cache_U: Mutex>, - cache_ar: Mutex>, -} - -impl DAOVerifier { - pub fn init(node: &Node) -> Self { - let consensus = node.consensus().clone(); - let tip_number = node.get_tip_block_number(); - let mut blocks = Vec::new(); - let mut transactions = HashMap::new(); - let mut epochs = Vec::new(); - let mut blocks_reward = Vec::new(); - for number in 0..=node.get_tip_block_number() { - blocks.push(node.get_block_by_number(number)) - } - for block in blocks.iter() { - for transaction in block.transactions() { - transactions.insert(transaction.hash(), (block.number(), transaction)); - } - } - for number in 0..=node.rpc_client().get_current_epoch().number.value() { - epochs.push(node.rpc_client().get_epoch_by_number(number).unwrap()); - } - for block in blocks.iter() { - blocks_reward.push( - node.rpc_client() - .get_block_economic_state(block.hash()) - .map(Into::into), - ); - } - Self { - consensus, - tip_number, - blocks, - transactions, - epochs, - blocks_reward, - ..Default::default() - } - } - - pub fn ar0(&self) -> u64 { - let ar0 = 10u64.pow(16); - self.cache_ar.lock().insert(0, ar0); - ar0 - } - - pub fn p(&self, i: BlockNumber) -> u64 { - for epoch in self.epochs.iter() { - if epoch.start_number.value() <= i - && i < epoch.start_number.value() + epoch.length.value() - { - let epoch_primary_reward = - self.consensus.primary_epoch_reward(epoch.number.value()); - if i - epoch.start_number.value() - < epoch_primary_reward.as_u64() % epoch.length.value() - { - return epoch_primary_reward.as_u64() / epoch.length.value() + 1; - } else { - return epoch_primary_reward.as_u64() / epoch.length.value(); - } - } - } - unreachable!() - } - - pub fn s(&self, i: BlockNumber) -> u64 { - for epoch in self.epochs.iter() { - if epoch.start_number.value() <= i - && i < epoch.start_number.value() + epoch.length.value() - { - let epoch_secondary_reward = self.consensus.secondary_epoch_reward(); - if i - epoch.start_number.value() - < epoch_secondary_reward.as_u64() % epoch.length.value() - { - return epoch_secondary_reward.as_u64() / epoch.length.value() + 1; - } else { - return epoch_secondary_reward.as_u64() / epoch.length.value(); - } - } - } - unreachable!() - } - - pub fn ar(&self, i: BlockNumber) -> u64 { - { - if let Some(ar) = self.cache_ar.lock().get(&i) { - return *ar; - } - } - if i == 0 { - return self.ar0(); - } - - let ar = self.ar(i - 1) - + u64::try_from( - u128::from(self.ar(i - 1)) * u128::from(self.s(i)) / u128::from(self.C(i - 1)), - ) - .unwrap(); - self.cache_ar.lock().insert(i, ar); - ar - } - - #[allow(non_snake_case)] - pub fn U_in(&self, i: BlockNumber) -> u64 { - let mut sum = 0u64; - for tx in self.blocks[i as usize].transactions() { - for o in tx.input_pts_iter() { - if !o.is_null() { - sum += self.get_output_occupied_capacity(&o); - } - } - } - sum - } - - #[allow(non_snake_case)] - pub fn U_out(&self, i: BlockNumber) -> u64 { - let satoshi_cell_occupied_ratio = self.consensus.satoshi_cell_occupied_ratio; - let satoshi_pubkey_hash = &self.consensus.satoshi_pubkey_hash; - let mut sum = 0u64; - for (tx_index, tx) in self.blocks[i as usize].transactions().iter().enumerate() { - for (out_point, output) in tx.output_pts().iter().zip(tx.outputs().into_iter()) { - if i == 0 - && tx_index == 0 - && output.lock().args().raw_data() == satoshi_pubkey_hash.0[..] - { - sum += Unpack::::unpack(&output.capacity()) - .safe_mul_ratio(satoshi_cell_occupied_ratio) - .unwrap() - .as_u64(); - } else { - sum += self.get_output_occupied_capacity(out_point); - } - } - } - sum - } - - #[allow(non_snake_case)] - pub fn C_in(&self, i: BlockNumber) -> u64 { - let mut sum = 0u64; - for tx in self.blocks[i as usize].transactions() { - for o in tx.input_pts_iter() { - if !o.is_null() { - sum += self.get_output_capacity(&o); - } - } - } - sum - } - - #[allow(non_snake_case)] - pub fn C_out(&self, i: BlockNumber) -> u64 { - let mut sum = 0u64; - for tx in self.blocks[i as usize].transactions() { - for o in tx.output_pts() { - sum += self.get_output_capacity(&o); - } - } - sum - } - - #[allow(non_snake_case)] - pub fn C0(&self) -> u64 { - let C0 = self.C_out(0) - self.C_in(0) + self.p(0) + self.s(0); - self.cache_C.lock().insert(0, C0); - C0 - } - - #[allow(non_snake_case)] - pub fn U0(&self) -> u64 { - let U0 = self.U_out(0) - self.U_in(0); - self.cache_U.lock().insert(0, U0); - U0 - } - - #[allow(non_snake_case)] - pub fn S0(&self) -> u64 { - let S0 = self.s(0); - self.cache_S.lock().insert(0, S0); - S0 - } - - #[allow(non_snake_case)] - pub fn I(&self, i: BlockNumber) -> u64 { - let mut sum = 0u64; - for tx in self.blocks[i as usize].transactions() { - for o in tx.input_pts_iter() { - if o.is_null() { - continue; - } - - if self.is_dao_prepare_input(&o) { - // `o` is prepare point, then `o`'s same-position input is deposit point - let prepare_tx = self.get_transaction(&o.tx_hash()); - let deposit_out_point = prepare_tx - .inputs() - .get(o.index().unpack()) - .unwrap() - .previous_output(); - let deposit_header_number = - self.get_tx_block_number(&deposit_out_point.tx_hash()); - let prepare_header_number = self.get_tx_block_number(&prepare_tx.hash()); - let deposit_ar = self.ar(deposit_header_number); - let prepare_ar = self.ar(prepare_header_number); - let deposit_counted_capacity = self.get_output_capacity(&deposit_out_point) - - self.get_output_occupied_capacity(&o); - let prepare_capacity = u64::try_from( - u128::from(deposit_counted_capacity) * u128::from(prepare_ar) - / u128::from(deposit_ar), - ) - .unwrap(); - let interest = prepare_capacity - deposit_counted_capacity; - sum += interest - } - } - } - sum - } - - fn is_dao_prepare_input(&self, out_point: &OutPoint) -> bool { - let input_tx = self.get_transaction(&out_point.tx_hash()); - let input_data = input_tx - .outputs_data() - .get(out_point.index().unpack()) - .unwrap(); - if input_data.len() != 8 { - return false; - } - - let deposited_number = LittleEndian::read_u64(&input_data.raw_data()[0..8]); - if deposited_number == 0 { - return false; - } - - let dao_type_hash = self.consensus.dao_type_hash(); - self.get_output(out_point) - .type_() - .to_opt() - .map(|script| script.code_hash() == dao_type_hash) - .unwrap_or(false) - } - - #[allow(non_snake_case)] - pub fn C(&self, i: BlockNumber) -> u64 { - { - if let Some(C) = self.cache_C.lock().get(&i) { - return *C; - } - } - if i == 0 { - return self.C0(); - } - - let C = self.C(i - 1) + self.p(i) + self.s(i); - self.cache_C.lock().insert(i, C); - C - } - - #[allow(non_snake_case)] - pub fn S(&self, i: BlockNumber) -> u64 { - { - if let Some(S) = self.cache_S.lock().get(&i) { - return *S; - } - } - if i == 0 { - return self.S0(); - } - - let S = self.S(i - 1) - self.I(i) + self.s(i) - - u64::try_from( - u128::from(self.s(i)) * u128::from(self.U(i - 1)) / u128::from(self.C(i - 1)), - ) - .unwrap(); - self.cache_S.lock().insert(i, S); - S - } - - #[allow(non_snake_case)] - pub fn U(&self, i: BlockNumber) -> u64 { - { - if let Some(U) = self.cache_U.lock().get(&i) { - return *U; - } - } - if i == 0 { - return self.U0(); - } - - let U = self.U(i - 1) + self.U_out(i) - self.U_in(i); - self.cache_U.lock().insert(i, U); - U - } - - fn get_tx_block_number(&self, tx_hash: &Byte32) -> BlockNumber { - self.transactions - .get(tx_hash) - .map(|(i, _)| *i) - .expect("exist") - } - - fn get_transaction(&self, tx_hash: &Byte32) -> &TransactionView { - self.transactions - .get(tx_hash) - .map(|(_, tx)| tx) - .expect("exist") - } - - fn get_output(&self, out_point: &OutPoint) -> CellOutput { - self.get_transaction(&out_point.tx_hash()) - .output(out_point.index().unpack()) - .expect("exist") - } - - fn get_output_capacity(&self, out_point: &OutPoint) -> u64 { - self.get_output(out_point).capacity().unpack() - } - - fn get_output_occupied_capacity(&self, out_point: &OutPoint) -> u64 { - let satoshi_pubkey_hash = &self.consensus.satoshi_pubkey_hash; - let satoshi_cell_occupied_ratio = self.consensus.satoshi_cell_occupied_ratio; - let (output, data) = self - .get_transaction(&out_point.tx_hash()) - .output_with_data(out_point.index().unpack()) - .expect("exist"); - if Unpack::::unpack(&out_point.index()) == 0 - && output.lock().args().raw_data() == satoshi_pubkey_hash.0[..] - { - Unpack::::unpack(&output.capacity()) - .safe_mul_ratio(satoshi_cell_occupied_ratio) - .unwrap() - .as_u64() - } else { - output - .occupied_capacity(Capacity::bytes(data.len()).unwrap()) - .unwrap() - .as_u64() - } - } - - pub fn verify(&self) { - self.blocks.iter().for_each(|block| { - assert_eq!( - self.C(block.number()), - extract_dao_data(block.dao()).1.as_u64(), - "assert C. expected_dao_field: {}", - self.expected_dao_field(block.number()), - ); - }); - self.blocks.iter().for_each(|block| { - assert_eq!( - self.U(block.number()), - extract_dao_data(block.dao()).3.as_u64(), - "assert U. expected_dao_field: {}", - self.expected_dao_field(block.number()), - ); - }); - self.blocks.iter().for_each(|block| { - let finalization_delay_length = self.consensus.finalization_delay_length(); - let i = block.number(); - let reward = &self.blocks_reward[i as usize]; - if i == 0 || i + finalization_delay_length > self.tip_number { - assert!( - reward.is_none(), - "assert block_reward_{i}. Non finalized block should has not economic state. actual: {reward:?}", - ); - } else { - assert_eq!( - Some(self.p(i)), - reward.as_ref().map(|reward| reward.issuance.primary.as_u64()), - "assert block_reward_{i}" - ); - } - }); - self.blocks.iter().for_each(|block| { - assert_eq!( - self.ar(block.number()), - extract_dao_data(block.dao()).0, - "assert ar. expected_dao_field: {}", - self.expected_dao_field(block.number()), - ); - }); - self.blocks.iter().for_each(|block| { - assert_eq!( - self.S(block.number()), - extract_dao_data(block.dao()).2.as_u64(), - "assert S. expected_dao_field: {}", - self.expected_dao_field(block.number()), - ); - }); - } - - fn expected_dao_field(&self, i: BlockNumber) -> String { - format!( - "C_{}: {}, S_{}: {}, U_{}: {}, ar_{}: {}", - i, - self.C(i), - i, - self.S(i), - i, - self.U(i), - i, - self.ar(i), - ) - } -} diff --git a/test/src/specs/dao/dao_verify.rs b/test/src/specs/dao/dao_verify.rs deleted file mode 100644 index 3e9abfb559..0000000000 --- a/test/src/specs/dao/dao_verify.rs +++ /dev/null @@ -1,17 +0,0 @@ -use crate::specs::dao::dao_verifier::DAOVerifier; -use crate::{Node, Spec}; - -pub struct DAOVerify; - -impl Spec for DAOVerify { - fn modify_chain_spec(&self, spec: &mut ckb_chain_spec::ChainSpec) { - spec.params.genesis_epoch_length = Some(20); - } - - fn run(&self, nodes: &mut Vec) { - let node = &nodes[0]; - let genesis_epoch_length = node.consensus().genesis_epoch_ext().length(); - node.mine(genesis_epoch_length * 5); - DAOVerifier::init(node).verify(); - } -} diff --git a/test/src/specs/dao/mod.rs b/test/src/specs/dao/mod.rs deleted file mode 100644 index e0e4346a60..0000000000 --- a/test/src/specs/dao/mod.rs +++ /dev/null @@ -1,10 +0,0 @@ -mod dao_tx; -mod dao_user; -mod dao_verifier; -mod dao_verify; -mod satoshi_dao_occupied; -mod utils; - -pub use dao_tx::*; -pub use dao_verify::*; -pub use satoshi_dao_occupied::*; diff --git a/test/src/specs/dao/satoshi_dao_occupied.rs b/test/src/specs/dao/satoshi_dao_occupied.rs deleted file mode 100644 index 5756d25ceb..0000000000 --- a/test/src/specs/dao/satoshi_dao_occupied.rs +++ /dev/null @@ -1,161 +0,0 @@ -use crate::specs::dao::dao_user::DAOUser; -use crate::specs::dao::dao_verifier::DAOVerifier; -use crate::specs::dao::utils::{ensure_committed, goto_target_point}; -use crate::util::check::is_transaction_committed; -use crate::utils::generate_utxo_set; -use crate::{Node, Spec}; -use ckb_chain_spec::IssuedCell; -use ckb_crypto::secp::{Generator, Privkey, Pubkey}; -use ckb_dao_utils::extract_dao_data; -use ckb_test_chain_utils::always_success_cell; -use ckb_types::core::{EpochNumberWithFraction, TransactionBuilder}; -use ckb_types::packed::{CellInput, CellOutput, OutPoint}; -use ckb_types::{ - bytes::Bytes, - core::{Capacity, Ratio}, - h160, - prelude::*, - H160, -}; - -const SATOSHI_CELL_CAPACITY: Capacity = Capacity::shannons(10_000_000_000_000_000); -const SATOSHI_PUBKEY_HASH: H160 = h160!("0x62e907b15cbf27d5425399ebf6f0fb50ebb88f18"); -const CELLBASE_USED_BYTES: usize = 41; - -pub struct DAOWithSatoshiCellOccupied; - -impl Spec for DAOWithSatoshiCellOccupied { - fn run(&self, nodes: &mut Vec) { - let node = &nodes[0]; - let utxos = generate_utxo_set(node, 10); - let mut user = DAOUser::new(node, utxos); - - ensure_committed(node, &user.deposit()); - ensure_committed(node, &user.prepare()); - - let withdrawal = user.withdraw(); - let since = EpochNumberWithFraction::from_full_value( - withdrawal.inputs().get(0).unwrap().since().unpack(), - ); - goto_target_point(node, since); - ensure_committed(node, &withdrawal); - DAOVerifier::init(node).verify(); - } - - fn modify_chain_spec(&self, spec: &mut ckb_chain_spec::ChainSpec) { - let satoshi_cell = issue_satoshi_cell(); - spec.genesis.issued_cells.push(satoshi_cell); - spec.params.genesis_epoch_length = Some(2); - spec.params.epoch_duration_target = Some(16); - spec.params.permanent_difficulty_in_dummy = Some(true); - } -} - -pub struct SpendSatoshiCell { - privkey: Privkey, - pubkey: Pubkey, - satoshi_cell_occupied_ratio: Ratio, -} - -impl Default for SpendSatoshiCell { - fn default() -> Self { - Self::new() - } -} - -impl SpendSatoshiCell { - pub fn new() -> Self { - let (privkey, pubkey) = Generator::random_keypair(); - let satoshi_cell_occupied_ratio = Ratio::new(6, 10); - - SpendSatoshiCell { - privkey, - pubkey, - satoshi_cell_occupied_ratio, - } - } -} - -impl Spec for SpendSatoshiCell { - fn run(&self, nodes: &mut Vec) { - let node0 = &nodes[0]; - let satoshi_cell_occupied = SATOSHI_CELL_CAPACITY - .safe_mul_ratio(node0.consensus().satoshi_cell_occupied_ratio) - .unwrap(); - // check genesis blocks dao - let genesis = node0.get_block_by_number(0); - let (_ar, _c, _s, u) = extract_dao_data(genesis.header().dao()); - // u - used capacity should includes virtual occupied - assert!(u > satoshi_cell_occupied); - - // Build tx to spent virtual occupied capacity - let cellbase = &genesis.transactions()[0]; - let satoshi_input = CellInput::new( - OutPoint::new(cellbase.hash(), (cellbase.outputs().len() - 1) as u32), - 0, - ); - let output = CellOutput::new_builder() - .capacity(satoshi_cell_occupied.pack()) - .lock(always_success_cell().2.clone()) - .build(); - - let transaction = TransactionBuilder::default() - .cell_deps(vec![node0.always_success_cell_dep()]) - .input(satoshi_input) - .output(output) - .output_data(Bytes::new().pack()) - .build(); - let tx_hash = transaction.hash(); - let sig = self - .privkey - .sign_recoverable(&tx_hash.unpack()) - .expect("sign"); - let mut witness_vec = sig.serialize(); - witness_vec.extend_from_slice(&self.pubkey.serialize()); - let witness = Bytes::from(witness_vec); - let transaction = transaction - .as_advanced_builder() - .witness(witness.pack()) - .build(); - - node0.mine(1); - node0 - .rpc_client() - .send_transaction(transaction.data().into()); - node0.mine_until_transaction_confirm(&tx_hash); - // cellbase occupied capacity minus satoshi cell - let cellbase_used_capacity = Capacity::bytes(CELLBASE_USED_BYTES).unwrap(); - assert!(is_transaction_committed(node0, &transaction)); - - let tip = node0.get_tip_block(); - // check tip dao, expect u correct - let (_ar, _c, _s, new_u) = extract_dao_data(tip.header().dao()); - assert_eq!( - Ok(new_u), - u.safe_sub(satoshi_cell_occupied) - .and_then(|c| c.safe_add(cellbase_used_capacity)) - ); - } - - fn modify_chain_spec(&self, spec: &mut ckb_chain_spec::ChainSpec) { - let satoshi_cell_occupied_ratio = self.satoshi_cell_occupied_ratio; - spec.genesis.issued_cells.push(issue_satoshi_cell()); - spec.genesis.satoshi_gift.satoshi_cell_occupied_ratio = satoshi_cell_occupied_ratio; - spec.params.genesis_epoch_length = Some(2); - spec.params.epoch_duration_target = Some(16); - spec.params.permanent_difficulty_in_dummy = Some(true); - } -} - -fn issue_satoshi_cell() -> IssuedCell { - let lock = always_success_cell() - .2 - .clone() - .as_builder() - .args(Bytes::from(&SATOSHI_PUBKEY_HASH.0[..]).pack()) - .build(); - IssuedCell { - capacity: SATOSHI_CELL_CAPACITY, - lock: lock.into(), - } -} diff --git a/test/src/specs/dao/utils.rs b/test/src/specs/dao/utils.rs deleted file mode 100644 index 331aaea5e0..0000000000 --- a/test/src/specs/dao/utils.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::util::check::is_transaction_committed; -use crate::Node; -use ckb_types::core::EpochNumberWithFraction; -use ckb_types::{core::TransactionView, packed::OutPoint}; - -/// Send the given transaction and make it committed -pub(crate) fn ensure_committed(node: &Node, transaction: &TransactionView) -> OutPoint { - let closest = node.consensus().tx_proposal_window().closest(); - let tx_hash = transaction.hash(); - node.rpc_client() - .send_transaction(transaction.data().into()); - node.mine_until_transaction_confirm_with_windows(&tx_hash, closest); - assert!(is_transaction_committed(node, transaction)); - OutPoint::new(tx_hash, 0) -} - -/// A helper function keep the node growing until into the target EpochNumberWithFraction. -pub(crate) fn goto_target_point(node: &Node, target_point: EpochNumberWithFraction) { - loop { - let tip_epoch = node.rpc_client().get_tip_header().inner.epoch; - let tip_point = EpochNumberWithFraction::from_full_value(tip_epoch.value()); - - // Here is our target EpochNumberWithFraction. - if tip_point >= target_point { - break; - } - - node.mine(1); - } -} diff --git a/test/src/specs/mod.rs b/test/src/specs/mod.rs index 5e9d9fc569..d16a4cf288 100644 --- a/test/src/specs/mod.rs +++ b/test/src/specs/mod.rs @@ -1,6 +1,5 @@ mod alert; mod consensus; -mod dao; mod hardfork; mod mining; mod p2p; @@ -11,7 +10,6 @@ mod tx_pool; pub use alert::*; pub use consensus::*; -pub use dao::*; pub use hardfork::*; pub use mining::*; pub use p2p::*; diff --git a/util/app-config/src/lib.rs b/util/app-config/src/lib.rs index 02110816aa..52df1593d4 100644 --- a/util/app-config/src/lib.rs +++ b/util/app-config/src/lib.rs @@ -31,9 +31,6 @@ use ckb_types::{u256, H256, U256}; use clap::ArgMatches; use std::{path::PathBuf, str::FromStr}; -// 500_000 total difficulty -const MIN_CHAIN_WORK_500K: U256 = u256!("0x3314412053c82802a7"); - /// A struct including all the information to start the ckb process. pub struct Setup { /// Subcommand name. @@ -73,19 +70,7 @@ impl Setup { let consensus = self.consensus()?; let chain_spec_hash = self.chain_spec()?.hash; let mut config = self.config.into_ckb()?; - - let mainnet_genesis = ckb_chain_spec::ChainSpec::load_from( - &ckb_resource::Resource::bundled("specs/mainnet.toml".to_string()), - ) - .expect("load mainnet spec fail") - .build_genesis() - .expect("build mainnet genesis fail"); - config.network.sync.min_chain_work = - if consensus.genesis_block.hash() == mainnet_genesis.hash() { - MIN_CHAIN_WORK_500K - } else { - u256!("0x0") - }; + config.network.sync.min_chain_work = u256!("0x0"); let arg_assume_valid_target = matches.get_one::(cli::ARG_ASSUME_VALID_TARGET); diff --git a/verification/src/tests/transaction_verifier.rs b/verification/src/tests/transaction_verifier.rs index 16f05fe1fe..a5ab3c4c1a 100644 --- a/verification/src/tests/transaction_verifier.rs +++ b/verification/src/tests/transaction_verifier.rs @@ -1,14 +1,10 @@ use super::super::transaction_verifier::{ - CapacityVerifier, DaoScriptSizeVerifier, DuplicateDepsVerifier, EmptyVerifier, - MaturityVerifier, OutputsDataVerifier, Since, SinceVerifier, SizeVerifier, VersionVerifier, + CapacityVerifier, DuplicateDepsVerifier, EmptyVerifier, MaturityVerifier, OutputsDataVerifier, + Since, SinceVerifier, SizeVerifier, VersionVerifier, }; use crate::error::TransactionErrorSource; use crate::{TransactionError, TxVerifyEnv}; -use ckb_chain_spec::{ - build_genesis_type_id_script, - consensus::{Consensus, ConsensusBuilder}, - OUTPUT_INDEX_DAO, -}; +use ckb_chain_spec::consensus::ConsensusBuilder; use ckb_error::{assert_error_eq, Error}; use ckb_test_chain_utils::{MockMedianTime, MOCK_MEDIAN_TIME_COUNT}; use ckb_traits::CellDataProvider; @@ -19,11 +15,11 @@ use ckb_types::{ capacity_bytes, cell::{CellMetaBuilder, ResolvedTransaction}, hardfork::HardForks, - BlockNumber, Capacity, EpochNumber, EpochNumberWithFraction, HeaderView, ScriptHashType, + BlockNumber, Capacity, EpochNumber, EpochNumberWithFraction, HeaderView, TransactionBuilder, TransactionInfo, TransactionView, }, h256, - packed::{Byte32, CellDep, CellInput, CellOutput, OutPoint, Script}, + packed::{Byte32, CellDep, CellInput, CellOutput, OutPoint}, prelude::*, }; use std::sync::Arc; @@ -103,8 +99,7 @@ pub fn test_capacity_outofbound() { .build()], resolved_dep_groups: vec![], }); - let dao_type_hash = build_genesis_type_id_script(OUTPUT_INDEX_DAO).calc_script_hash(); - let verifier = CapacityVerifier::new(rtx, dao_type_hash); + let verifier = CapacityVerifier::new(rtx); assert_error_eq!( verifier.verify().unwrap_err(), @@ -117,30 +112,6 @@ pub fn test_capacity_outofbound() { ); } -#[test] -pub fn test_skip_dao_capacity_check() { - let dao_type_script = build_genesis_type_id_script(OUTPUT_INDEX_DAO); - let transaction = TransactionBuilder::default() - .output( - CellOutput::new_builder() - .capacity(capacity_bytes!(500).pack()) - .type_(Some(dao_type_script.clone()).pack()) - .build(), - ) - .output_data(Bytes::new().pack()) - .build(); - - let rtx = Arc::new(ResolvedTransaction { - transaction, - resolved_cell_deps: Vec::new(), - resolved_inputs: vec![], - resolved_dep_groups: vec![], - }); - let verifier = CapacityVerifier::new(rtx, dao_type_script.calc_script_hash()); - - assert!(verifier.verify().is_ok()); -} - // inputs immature verify #[test] pub fn test_inputs_cellbase_maturity() { @@ -328,8 +299,7 @@ pub fn test_capacity_invalid() { ], resolved_dep_groups: vec![], }); - let dao_type_hash = build_genesis_type_id_script(OUTPUT_INDEX_DAO).calc_script_hash(); - let verifier = CapacityVerifier::new(rtx, dao_type_hash); + let verifier = CapacityVerifier::new(rtx); assert_error_eq!( verifier.verify().unwrap_err(), @@ -796,324 +766,3 @@ impl CellDataProvider for EmptyDataProvider { None } } - -fn build_consensus_with_dao_limiting_block(block_number: u64) -> (Arc, Script) { - let dao_script = build_genesis_type_id_script(OUTPUT_INDEX_DAO); - let mut consensus = ConsensusBuilder::default() - .starting_block_limiting_dao_withdrawing_lock(block_number) - .build(); - - // Default consensus built this way only has one dummy output in the - // cellbase transaction from genesis block, meaning it will be missing - // the dao script. For simplicity, we are hacking consensus here with - // a dao_type_hash value, a proper way should be creating a proper genesis - // block here, but we will leave it till we really need it. - consensus.dao_type_hash = dao_script.calc_script_hash(); - - let dao_type_script = Script::new_builder() - .code_hash(dao_script.calc_script_hash()) - .hash_type(ScriptHashType::Type.into()) - .build(); - - (Arc::new(consensus), dao_type_script) -} - -#[test] -fn test_dao_disables_different_lock_script_size() { - let (consensus, dao_type_script) = build_consensus_with_dao_limiting_block(20000); - - let transaction = TransactionBuilder::default() - .outputs(vec![ - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - CellOutput::new_builder() - .capacity(capacity_bytes!(200).pack()) - .lock( - Script::new_builder() - .args(Bytes::from(vec![1; 20]).pack()) - .build(), - ) - .type_(Some(dao_type_script.clone()).pack()) - .build(), - ]) - .outputs_data(vec![Bytes::new().pack(); 2]) - .build(); - - let rtx = Arc::new(ResolvedTransaction { - transaction, - resolved_cell_deps: Vec::new(), - resolved_inputs: vec![ - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - Bytes::new(), - ) - .transaction_info(mock_transaction_info( - 20010, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(201).pack()) - .lock(Script::new_builder().args(Bytes::new().pack()).build()) - .type_(Some(dao_type_script).pack()) - .build(), - Bytes::from(vec![0; 8]), - ) - .transaction_info(mock_transaction_info( - 20011, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - ], - resolved_dep_groups: vec![], - }); - let verifier = DaoScriptSizeVerifier::new(rtx, consensus, EmptyDataProvider {}); - - assert_error_eq!( - verifier.verify().unwrap_err(), - TransactionError::DaoLockSizeMismatch { index: 1 }, - ); -} - -#[test] -fn test_dao_disables_different_lock_script_size_before_limiting_block() { - let (consensus, dao_type_script) = build_consensus_with_dao_limiting_block(21000); - - let transaction = TransactionBuilder::default() - .outputs(vec![ - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - CellOutput::new_builder() - .capacity(capacity_bytes!(200).pack()) - .lock( - Script::new_builder() - .args(Bytes::from(vec![1; 20]).pack()) - .build(), - ) - .type_(Some(dao_type_script.clone()).pack()) - .build(), - ]) - .outputs_data(vec![Bytes::new().pack(); 2]) - .build(); - - let rtx = Arc::new(ResolvedTransaction { - transaction, - resolved_cell_deps: Vec::new(), - resolved_inputs: vec![ - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - Bytes::new(), - ) - .transaction_info(mock_transaction_info( - 20010, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(201).pack()) - .lock(Script::new_builder().args(Bytes::new().pack()).build()) - .type_(Some(dao_type_script).pack()) - .build(), - Bytes::from(vec![0; 8]), - ) - .transaction_info(mock_transaction_info( - 20011, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - ], - resolved_dep_groups: vec![], - }); - let verifier = DaoScriptSizeVerifier::new(rtx, consensus, EmptyDataProvider {}); - - assert!(verifier.verify().is_ok()); -} - -#[test] -fn test_non_dao_allows_lock_script_size() { - let (consensus, _dao_type_script) = build_consensus_with_dao_limiting_block(20000); - - let transaction = TransactionBuilder::default() - .outputs(vec![ - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - CellOutput::new_builder() - .capacity(capacity_bytes!(200).pack()) - .lock( - Script::new_builder() - .args(Bytes::from(vec![1; 20]).pack()) - .build(), - ) - .build(), - ]) - .outputs_data(vec![Bytes::new().pack(); 2]) - .build(); - - let rtx = Arc::new(ResolvedTransaction { - transaction, - resolved_cell_deps: Vec::new(), - resolved_inputs: vec![ - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - Bytes::new(), - ) - .transaction_info(mock_transaction_info( - 20010, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(201).pack()) - .lock(Script::new_builder().args(Bytes::new().pack()).build()) - .build(), - Bytes::from(vec![0; 8]), - ) - .transaction_info(mock_transaction_info( - 20011, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - ], - resolved_dep_groups: vec![], - }); - let verifier = DaoScriptSizeVerifier::new(rtx, consensus, EmptyDataProvider {}); - - assert!(verifier.verify().is_ok()); -} - -#[test] -fn test_dao_allows_different_lock_script_size_in_withdraw_phase_2() { - let (consensus, dao_type_script) = build_consensus_with_dao_limiting_block(20000); - - let transaction = TransactionBuilder::default() - .outputs(vec![ - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - CellOutput::new_builder() - .capacity(capacity_bytes!(200).pack()) - .lock( - Script::new_builder() - .args(Bytes::from(vec![1; 20]).pack()) - .build(), - ) - .type_(Some(dao_type_script.clone()).pack()) - .build(), - ]) - .outputs_data(vec![Bytes::new().pack(); 2]) - .build(); - - let rtx = Arc::new(ResolvedTransaction { - transaction, - resolved_cell_deps: Vec::new(), - resolved_inputs: vec![ - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - Bytes::new(), - ) - .transaction_info(mock_transaction_info( - 20010, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(201).pack()) - .lock(Script::new_builder().args(Bytes::new().pack()).build()) - .type_(Some(dao_type_script).pack()) - .build(), - Bytes::from(vec![1; 8]), - ) - .transaction_info(mock_transaction_info( - 20011, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - ], - resolved_dep_groups: vec![], - }); - let verifier = DaoScriptSizeVerifier::new(rtx, consensus, EmptyDataProvider {}); - - assert!(verifier.verify().is_ok()); -} - -#[test] -fn test_dao_allows_different_lock_script_size_using_normal_cells_in_withdraw_phase_2() { - let (consensus, dao_type_script) = build_consensus_with_dao_limiting_block(20000); - - let transaction = TransactionBuilder::default() - .outputs(vec![ - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - CellOutput::new_builder() - .capacity(capacity_bytes!(200).pack()) - .lock( - Script::new_builder() - .args(Bytes::from(vec![1; 20]).pack()) - .build(), - ) - .type_(Some(dao_type_script).pack()) - .build(), - ]) - .outputs_data(vec![]) - .build(); - - let rtx = Arc::new(ResolvedTransaction { - transaction, - resolved_cell_deps: Vec::new(), - resolved_inputs: vec![ - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(50).pack()) - .build(), - Bytes::new(), - ) - .transaction_info(mock_transaction_info( - 20010, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - CellMetaBuilder::from_cell_output( - CellOutput::new_builder() - .capacity(capacity_bytes!(201).pack()) - .lock(Script::new_builder().args(Bytes::new().pack()).build()) - .build(), - Bytes::from(vec![1; 8]), - ) - .transaction_info(mock_transaction_info( - 20011, - EpochNumberWithFraction::new(10, 0, 10), - 0, - )) - .build(), - ], - resolved_dep_groups: vec![], - }); - let verifier = DaoScriptSizeVerifier::new(rtx, consensus, EmptyDataProvider {}); - - assert!(verifier.verify().is_ok()); -} diff --git a/verification/src/transaction_verifier.rs b/verification/src/transaction_verifier.rs index c22d74b359..0a5f6c9e8c 100644 --- a/verification/src/transaction_verifier.rs +++ b/verification/src/transaction_verifier.rs @@ -149,7 +149,7 @@ where Arc::clone(&consensus), Arc::clone(&tx_env), ), - capacity: CapacityVerifier::new(Arc::clone(&rtx), consensus.dao_type_hash()), + capacity: CapacityVerifier::new(Arc::clone(&rtx)), fee_calculator: FeeCalculator::new(rtx, consensus, data_loader), } } @@ -453,15 +453,13 @@ impl<'a> DuplicateDepsVerifier<'a> { /// Perform inputs and outputs `capacity` field related verification pub struct CapacityVerifier { resolved_transaction: Arc, - dao_type_hash: Byte32, } impl CapacityVerifier { /// Create a new `CapacityVerifier` - pub fn new(resolved_transaction: Arc, dao_type_hash: Byte32) -> Self { + pub fn new(resolved_transaction: Arc) -> Self { CapacityVerifier { resolved_transaction, - dao_type_hash, } } @@ -472,7 +470,7 @@ impl CapacityVerifier { // withdraw transactions. // cellbase's outputs are verified by RewardVerifier // DAO withdraw transaction is verified via the type script of DAO cells - if !(self.resolved_transaction.is_cellbase() || self.valid_dao_withdraw_transaction()) { + if !(self.resolved_transaction.is_cellbase()) { let inputs_sum = self.resolved_transaction.inputs_capacity()?; let outputs_sum = self.resolved_transaction.outputs_capacity()?; @@ -505,13 +503,6 @@ impl CapacityVerifier { Ok(()) } - - fn valid_dao_withdraw_transaction(&self) -> bool { - self.resolved_transaction - .resolved_inputs - .iter() - .any(|cell_meta| cell_uses_dao_type_script(&cell_meta.cell_output, &self.dao_type_hash)) - } } fn cell_uses_dao_type_script(cell_output: &CellOutput, dao_type_hash: &Byte32) -> bool { @@ -874,7 +865,7 @@ where data_loader.clone(), tx_env, ), - capacity: CapacityVerifier::new(Arc::clone(&rtx), consensus.dao_type_hash()), + capacity: CapacityVerifier::new(Arc::clone(&rtx)), fee_calculator: FeeCalculator::new(rtx, consensus, data_loader), } } From d2e17c56a3c5d1d2a5e07671d04b28ab6f80d320 Mon Sep 17 00:00:00 2001 From: EthanYuan Date: Thu, 18 Jul 2024 22:43:17 +0800 Subject: [PATCH 2/2] disable init mainnet and testnet. --- resource/src/template.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resource/src/template.rs b/resource/src/template.rs index 319b09bf95..ca3f133e28 100644 --- a/resource/src/template.rs +++ b/resource/src/template.rs @@ -1,7 +1,7 @@ /// Default chain spec. -pub const DEFAULT_SPEC: &str = "mainnet"; +pub const DEFAULT_SPEC: &str = "dev"; /// The list of bundled chain specs. -pub const AVAILABLE_SPECS: &[&str] = &["mainnet", "testnet", "staging", "dev"]; +pub const AVAILABLE_SPECS: &[&str] = &["dev"]; /// The default RPC listen port *8114*. pub const DEFAULT_RPC_PORT: &str = "8114"; /// The default P2P listen port *8115*.