Skip to content

Commit

Permalink
Merge pull request #4186 from eval-exec/exec/remove-dao_type_hash-Opt…
Browse files Browse the repository at this point in the history
…ion-wrapper

Remove `Consensus.dao_type_hash`'s `Option` wrapper
  • Loading branch information
zhangsoledad authored Oct 12, 2023
2 parents 385bc89 + aff040d commit ba31616
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 39 deletions.
4 changes: 2 additions & 2 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ Response
"result": {
"block_version": "0x0",
"cellbase_maturity": "0x10000000000",
"dao_type_hash": null,
"dao_type_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"epoch_duration_target": "0x3840",
"genesis_hash": "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed",
"hardfork_features": [
Expand Down Expand Up @@ -5703,7 +5703,7 @@ Consensus defines various parameters that influence chain consensus

* `genesis_hash`: [`H256`](#type-h256) - The genesis block hash

* `dao_type_hash`: [`H256`](#type-h256) `|` `null` - The dao type hash
* `dao_type_hash`: [`H256`](#type-h256) - The dao type hash

* `secp256k1_blake160_sighash_all_type_hash`: [`H256`](#type-h256) `|` `null` - The secp256k1_blake160_sighash_all_type_hash

Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ pub trait ChainRpc {
/// "result": {
/// "block_version": "0x0",
/// "cellbase_maturity": "0x10000000000",
/// "dao_type_hash": null,
/// "dao_type_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
/// "epoch_duration_target": "0x3840",
/// "genesis_hash": "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed",
/// "hardfork_features": [
Expand Down
4 changes: 1 addition & 3 deletions rpc/src/module/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,7 @@ impl<'a> WellKnownScriptsOnlyValidator<'a> {
Some(script) => {
if !script.is_hash_type_type() {
Err(DefaultOutputsValidatorError::HashType)
} else if script.code_hash()
!= self.consensus.dao_type_hash().expect("No dao system cell")
{
} else if script.code_hash() != self.consensus.dao_type_hash() {
Err(DefaultOutputsValidatorError::CodeHash)
} else if output.lock().args().len() == BLAKE160_LEN + SINCE_LEN {
// https://github.com/nervosnetwork/ckb/wiki/Common-Gotchas#nervos-dao
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/tests/module/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn test_default_outputs_validator() {

// invalid code hash
let tx = build_tx(
&consensus.dao_type_hash().unwrap(),
&consensus.dao_type_hash(),
core::ScriptHashType::Type,
vec![1; 20],
);
Expand Down Expand Up @@ -76,7 +76,7 @@ fn test_default_outputs_validator() {
let lock_type_hash = consensus
.secp256k1_blake160_multisig_all_type_hash()
.unwrap();
let type_type_hash = consensus.dao_type_hash().unwrap();
let type_type_hash = consensus.dao_type_hash();
// valid output lock
let tx = build_tx_with_type(
&lock_type_hash,
Expand Down
10 changes: 5 additions & 5 deletions spec/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl ConsensusBuilder {
median_time_block_count: MEDIAN_TIME_BLOCK_COUNT,
max_block_cycles: MAX_BLOCK_CYCLES,
max_block_bytes: MAX_BLOCK_BYTES,
dao_type_hash: None,
dao_type_hash: Byte32::default(),
secp256k1_blake160_sighash_all_type_hash: None,
secp256k1_blake160_multisig_all_type_hash: None,
genesis_epoch_ext,
Expand Down Expand Up @@ -347,7 +347,7 @@ impl ConsensusBuilder {
"genesis block must contain the witness for cellbase"
);

self.inner.dao_type_hash = self.get_type_hash(OUTPUT_INDEX_DAO);
self.inner.dao_type_hash = self.get_type_hash(OUTPUT_INDEX_DAO).unwrap_or_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 =
Expand Down Expand Up @@ -514,7 +514,7 @@ pub struct Consensus {
/// The dao type hash
///
/// [nervos-dao](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0024-ckb-genesis-script-list/0024-ckb-genesis-script-list.md#nervos-dao)
pub dao_type_hash: Option<Byte32>,
pub dao_type_hash: Byte32,
/// The secp256k1_blake160_sighash_all_type_hash
///
/// [SECP256K1/blake160](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0024-ckb-genesis-script-list/0024-ckb-genesis-script-list.md#secp256k1blake160)
Expand Down Expand Up @@ -626,7 +626,7 @@ impl Consensus {
/// The dao type hash
///
/// [nervos-dao](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0024-ckb-genesis-script-list/0024-ckb-genesis-script-list.md#nervos-dao)
pub fn dao_type_hash(&self) -> Option<Byte32> {
pub fn dao_type_hash(&self) -> Byte32 {
self.dao_type_hash.clone()
}

Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl From<Consensus> for ckb_jsonrpc_types::Consensus {
Self {
id: consensus.id,
genesis_hash: consensus.genesis_hash.unpack(),
dao_type_hash: consensus.dao_type_hash.map(|h| h.unpack()),
dao_type_hash: consensus.dao_type_hash.unpack(),
secp256k1_blake160_sighash_all_type_hash: consensus
.secp256k1_blake160_sighash_all_type_hash
.map(|h| h.unpack()),
Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/dao/dao_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<'a> DAOUser<'a> {

pub fn dao_type_script(&self) -> Script {
Script::new_builder()
.code_hash(self.node.consensus().dao_type_hash().unwrap())
.code_hash(self.node.consensus().dao_type_hash())
.hash_type(ScriptHashType::Type.into())
.build()
}
Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/dao/dao_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl DAOVerifier {
return false;
}

let dao_type_hash = self.consensus.dao_type_hash().unwrap();
let dao_type_hash = self.consensus.dao_type_hash();
self.get_output(out_point)
.type_()
.to_opt()
Expand Down
3 changes: 1 addition & 2 deletions util/dao/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ impl<'a, DL: CellDataProvider + EpochProvider + HeaderProvider> DaoCalculator<'a
let is_dao_type_script = |type_script: Script| {
Into::<u8>::into(type_script.hash_type())
== Into::<u8>::into(ScriptHashType::Type)
&& type_script.code_hash()
== self.consensus.dao_type_hash().expect("No dao system cell")
&& type_script.code_hash() == self.consensus.dao_type_hash()
};
let is_withdrawing_input =
|cell_meta: &CellMeta| match self.data_loader.load_cell_data(cell_meta) {
Expand Down
2 changes: 1 addition & 1 deletion util/jsonrpc-types/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ pub struct Consensus {
/// The genesis block hash
pub genesis_hash: H256,
/// The dao type hash
pub dao_type_hash: Option<H256>,
pub dao_type_hash: H256,
/// The secp256k1_blake160_sighash_all_type_hash
pub secp256k1_blake160_sighash_all_type_hash: Option<H256>,
/// The secp256k1_blake160_multisig_all_type_hash
Expand Down
8 changes: 4 additions & 4 deletions verification/src/tests/transaction_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn test_capacity_outofbound() {
resolved_dep_groups: vec![],
});
let dao_type_hash = build_genesis_type_id_script(OUTPUT_INDEX_DAO).calc_script_hash();
let verifier = CapacityVerifier::new(rtx, Some(dao_type_hash));
let verifier = CapacityVerifier::new(rtx, dao_type_hash);

assert_error_eq!(
verifier.verify().unwrap_err(),
Expand Down Expand Up @@ -136,7 +136,7 @@ pub fn test_skip_dao_capacity_check() {
resolved_inputs: vec![],
resolved_dep_groups: vec![],
});
let verifier = CapacityVerifier::new(rtx, Some(dao_type_script.calc_script_hash()));
let verifier = CapacityVerifier::new(rtx, dao_type_script.calc_script_hash());

assert!(verifier.verify().is_ok());
}
Expand Down Expand Up @@ -329,7 +329,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, Some(dao_type_hash));
let verifier = CapacityVerifier::new(rtx, dao_type_hash);

assert_error_eq!(
verifier.verify().unwrap_err(),
Expand Down Expand Up @@ -808,7 +808,7 @@ fn build_consensus_with_dao_limiting_block(block_number: u64) -> (Arc<Consensus>
// 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 = Some(dao_script.calc_script_hash());
consensus.dao_type_hash = dao_script.calc_script_hash();

let dao_type_script = Script::new_builder()
.code_hash(dao_script.calc_script_hash())
Expand Down
22 changes: 5 additions & 17 deletions verification/src/transaction_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,12 @@ impl<'a> DuplicateDepsVerifier<'a> {
/// Perform inputs and outputs `capacity` field related verification
pub struct CapacityVerifier {
resolved_transaction: Arc<ResolvedTransaction>,
// It's Option because special genesis block do not have dao system cell
dao_type_hash: Option<Byte32>,
dao_type_hash: Byte32,
}

impl CapacityVerifier {
/// Create a new `CapacityVerifier`
pub fn new(
resolved_transaction: Arc<ResolvedTransaction>,
dao_type_hash: Option<Byte32>,
) -> Self {
pub fn new(resolved_transaction: Arc<ResolvedTransaction>, dao_type_hash: Byte32) -> Self {
CapacityVerifier {
resolved_transaction,
dao_type_hash,
Expand Down Expand Up @@ -584,12 +580,7 @@ impl CapacityVerifier {
self.resolved_transaction
.resolved_inputs
.iter()
.any(|cell_meta| {
cell_uses_dao_type_script(
&cell_meta.cell_output,
self.dao_type_hash.as_ref().expect("No dao system cell"),
)
})
.any(|cell_meta| cell_uses_dao_type_script(&cell_meta.cell_output, &self.dao_type_hash))
}
}

Expand Down Expand Up @@ -990,17 +981,14 @@ impl<DL: CellDataProvider> DaoScriptSizeVerifier<DL> {
}
}

fn dao_type_hash(&self) -> Option<Byte32> {
fn dao_type_hash(&self) -> Byte32 {
self.consensus.dao_type_hash()
}

/// Verifies that for all Nervos DAO transactions, withdrawing cells must use lock scripts
/// of the same size as corresponding deposit cells
pub fn verify(&self) -> Result<(), Error> {
if self.dao_type_hash().is_none() {
return Ok(());
}
let dao_type_hash = self.dao_type_hash().unwrap();
let dao_type_hash = self.dao_type_hash();
for (i, (input_meta, cell_output)) in self
.resolved_transaction
.resolved_inputs
Expand Down

0 comments on commit ba31616

Please sign in to comment.