Skip to content

Commit

Permalink
Merge pull request #2125 from subspace/verify_domain_hash
Browse files Browse the repository at this point in the history
Fraud proof: Invalid domain block hash
  • Loading branch information
vedhavyas authored Oct 18, 2023
2 parents 6bfc122 + a9cbb81 commit d89bdea
Show file tree
Hide file tree
Showing 18 changed files with 454 additions and 97 deletions.
2 changes: 1 addition & 1 deletion crates/pallet-domains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ subspace-runtime-primitives = { version = "0.1.0", default-features = false, pat
[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "74509ce016358e7f56ed2825fd75c324f8c22ef9" }
pallet-timestamp = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "74509ce016358e7f56ed2825fd75c324f8c22ef9" }
sp-externalities = { version = "0.19.0", git = "https://github.com/subspace/polkadot-sdk", rev = "74509ce016358e7f56ed2825fd75c324f8c22ef9" }
sp-state-machine = { version = "0.28.0", git = "https://github.com/subspace/polkadot-sdk", rev = "74509ce016358e7f56ed2825fd75c324f8c22ef9" }
sp-trie = { version = "22.0.0", git = "https://github.com/subspace/polkadot-sdk", rev = "74509ce016358e7f56ed2825fd75c324f8c22ef9" }
sp-externalities = { version = "0.19.0", git = "https://github.com/subspace/polkadot-sdk", rev = "74509ce016358e7f56ed2825fd75c324f8c22ef9" }

[features]
default = ["std"]
Expand Down
13 changes: 7 additions & 6 deletions crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ mod benchmarks {
let (_, operator_id) =
register_helper_operator::<T>(domain_id, T::Currency::minimum_balance());

let mut receipt = BlockTree::<T>::get::<_, T::DomainNumber>(domain_id, Zero::zero())
.first()
.and_then(DomainBlocks::<T>::get)
.expect("genesis receipt must exist")
.execution_receipt;
let mut receipt =
BlockTree::<T>::get::<_, DomainBlockNumberFor<T>>(domain_id, Zero::zero())
.first()
.and_then(DomainBlocks::<T>::get)
.expect("genesis receipt must exist")
.execution_receipt;
for i in 1..=(block_tree_pruning_depth + 1) {
let consensus_block_number = i.into();
let domain_block_number = i.into();
Expand Down Expand Up @@ -243,7 +244,7 @@ mod benchmarks {
assert_eq!(domain_obj.owner_account_id, creator);
assert!(DomainStakingSummary::<T>::get(domain_id).is_some());
assert_eq!(
BlockTree::<T>::get::<_, T::DomainNumber>(domain_id, Zero::zero()).len(),
BlockTree::<T>::get::<_, DomainBlockNumberFor<T>>(domain_id, Zero::zero()).len(),
1
);
assert_eq!(NextDomainId::<T>::get(), domain_id + 1.into());
Expand Down
6 changes: 3 additions & 3 deletions crates/pallet-domains/src/block_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use crate::pallet::StateRoots;
use crate::{
BalanceOf, BlockTree, Config, ConsensusBlockHash, DomainBlockDescendants, DomainBlocks,
ExecutionInbox, ExecutionReceiptOf, HeadReceiptNumber, InboxedBundleAuthor,
BalanceOf, BlockTree, Config, ConsensusBlockHash, DomainBlockDescendants, DomainBlockNumberFor,
DomainBlocks, ExecutionInbox, ExecutionReceiptOf, HeadReceiptNumber, InboxedBundleAuthor,
};
use codec::{Decode, Encode};
use frame_support::{ensure, PalletError};
Expand Down Expand Up @@ -227,7 +227,7 @@ pub(crate) struct ConfirmedDomainBlockInfo<DomainNumber, Balance> {
}

pub(crate) type ProcessExecutionReceiptResult<T> =
Result<Option<ConfirmedDomainBlockInfo<<T as Config>::DomainNumber, BalanceOf<T>>>, Error>;
Result<Option<ConfirmedDomainBlockInfo<DomainBlockNumberFor<T>, BalanceOf<T>>>, Error>;

/// Process the execution receipt to add it to the block tree
/// Returns the domain block number that was pruned, if any
Expand Down
23 changes: 19 additions & 4 deletions crates/pallet-domains/src/domain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::block_tree::import_genesis_receipt;
use crate::pallet::DomainStakingSummary;
use crate::staking::StakingSummary;
use crate::{
Config, DomainRegistry, ExecutionReceiptOf, HoldIdentifier, NextDomainId, RuntimeRegistry,
Config, DomainHashingFor, DomainRegistry, ExecutionReceiptOf, HoldIdentifier, NextDomainId,
RuntimeRegistry,
};
use alloc::string::String;
use codec::{Decode, Encode};
Expand All @@ -15,7 +16,10 @@ use frame_support::{ensure, PalletError};
use frame_system::pallet_prelude::*;
use scale_info::TypeInfo;
use sp_core::Get;
use sp_domains::{DomainId, DomainsDigestItem, OperatorAllowList, ReceiptHash, RuntimeId};
use sp_domains::{
derive_domain_block_hash, DomainId, DomainsDigestItem, OperatorAllowList, ReceiptHash,
RuntimeId,
};
use sp_runtime::traits::{CheckedAdd, Zero};
use sp_runtime::DigestItem;
use sp_std::collections::btree_map::BTreeMap;
Expand Down Expand Up @@ -126,9 +130,20 @@ pub(crate) fn do_instantiate_domain<T: Config>(

let state_version = runtime_obj.version.state_version();
let raw_genesis = runtime_obj.into_complete_raw_genesis(domain_id);
let state_root = raw_genesis.state_root::<T::DomainHashing>(state_version);
let state_root = raw_genesis.state_root::<DomainHashingFor<T>>(state_version);
let genesis_block_hash = derive_domain_block_hash::<T::DomainHeader>(
Zero::zero(),
sp_domains::EMPTY_EXTRINSIC_ROOT.into(),
state_root,
Default::default(),
Default::default(),
);

ExecutionReceiptOf::<T>::genesis(state_root)
ExecutionReceiptOf::<T>::genesis(
state_root,
sp_domains::EMPTY_EXTRINSIC_ROOT,
genesis_block_hash,
)
};
let genesis_receipt_hash = genesis_receipt.hash();

Expand Down
Loading

0 comments on commit d89bdea

Please sign in to comment.