Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ fn submit_fraud_proof_should_work() {
pre_state_root: H256::random(),
post_state_root: H256::random(),
proof: StorageProof::empty(),
execution_phase: ExecutionPhase::FinalizeBlock,
execution_phase: ExecutionPhase::FinalizeBlock {
total_extrinsics: 0,
},
})
};

Expand Down
44 changes: 21 additions & 23 deletions crates/sp-domains/src/fraud_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ use sp_trie::StorageProof;
use subspace_core_primitives::BlockNumber;
use subspace_runtime_primitives::AccountId;

/// Execution phase along with an optional encoded call data.
///
/// Each execution phase has a different method for the runtime call.
/// A phase of a block's execution.
#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
pub enum ExecutionPhase {
/// Executes the `initialize_block` hook.
InitializeBlock { call_data: Vec<u8> },
InitializeBlock,
/// Executes some extrinsic.
/// TODO: maybe optimized to not include the whole extrinsic blob in the future.
ApplyExtrinsic { call_data: Vec<u8> },
ApplyExtrinsic(u32),
/// Executes the `finalize_block` hook.
FinalizeBlock,
FinalizeBlock { total_extrinsics: u32 },
}

impl ExecutionPhase {
Expand All @@ -29,9 +26,9 @@ impl ExecutionPhase {
match self {
// TODO: Replace `DomainCoreApi_initialize_block_with_post_state_root` with `Core_initalize_block`
// Should be a same issue with https://github.com/paritytech/substrate/pull/10922#issuecomment-1068997467
Self::InitializeBlock { .. } => "DomainCoreApi_initialize_block_with_post_state_root",
Self::ApplyExtrinsic { .. } => "BlockBuilder_apply_extrinsic",
Self::FinalizeBlock => "BlockBuilder_finalize_block",
Self::InitializeBlock => "DomainCoreApi_initialize_block_with_post_state_root",
Self::ApplyExtrinsic(_) => "BlockBuilder_apply_extrinsic",
Self::FinalizeBlock { .. } => "BlockBuilder_finalize_block",
}
}

Expand All @@ -42,17 +39,9 @@ impl ExecutionPhase {
/// result of execution reported in [`FraudProof`] is expected or not.
pub fn verifying_method(&self) -> &'static str {
match self {
Self::InitializeBlock { .. } => "DomainCoreApi_initialize_block_with_post_state_root",
Self::ApplyExtrinsic { .. } => "DomainCoreApi_apply_extrinsic_with_post_state_root",
Self::FinalizeBlock => "BlockBuilder_finalize_block",
}
}

/// Returns the call data used to generate and verify the proof.
pub fn call_data(&self) -> &[u8] {
match self {
Self::InitializeBlock { call_data } | Self::ApplyExtrinsic { call_data } => call_data,
Self::FinalizeBlock => Default::default(),
Self::InitializeBlock => "DomainCoreApi_initialize_block_with_post_state_root",
Self::ApplyExtrinsic(_) => "DomainCoreApi_apply_extrinsic_with_post_state_root",
Self::FinalizeBlock { .. } => "BlockBuilder_finalize_block",
}
}

Expand All @@ -62,13 +51,13 @@ impl ExecutionPhase {
execution_result: Vec<u8>,
) -> Result<Header::Hash, VerificationError> {
match self {
ExecutionPhase::InitializeBlock { .. } | ExecutionPhase::ApplyExtrinsic { .. } => {
Self::InitializeBlock | Self::ApplyExtrinsic(_) => {
let encoded_storage_root = Vec::<u8>::decode(&mut execution_result.as_slice())
.map_err(VerificationError::InitializeBlockOrApplyExtrinsicDecode)?;
Header::Hash::decode(&mut encoded_storage_root.as_slice())
.map_err(VerificationError::StorageRootDecode)
}
ExecutionPhase::FinalizeBlock => {
Self::FinalizeBlock { .. } => {
let new_header = Header::decode(&mut execution_result.as_slice())
.map_err(VerificationError::HeaderDecode)?;
Ok(*new_header.state_root())
Expand All @@ -84,6 +73,15 @@ pub enum VerificationError {
/// `pre_state_root` in the invalid state transition proof is invalid.
#[cfg_attr(feature = "thiserror", error("invalid `pre_state_root`"))]
InvalidPreStateRoot,
/// `post_state_root` not found in the state.
#[cfg_attr(feature = "thiserror", error("`post_state_root` not found"))]
PostStateRootNotFound,
/// `post_state_root` is same as the one stored on chain.
#[cfg_attr(
feature = "thiserror",
error("`post_state_root` is same as the one on chain")
)]
SamePostStateRoot,
/// Failed to pass the execution proof check.
#[cfg_attr(
feature = "thiserror",
Expand Down
2 changes: 2 additions & 0 deletions crates/sp-receipts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ codec = { package = "parity-scale-codec", version = "3.1.2", default-features =
sp-api = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/substrate", rev = "456cfad45a178617f6886ec400c312f2fea59232" }
sp-core = { version = "7.0.0", default-features = false, git = "https://github.com/subspace/substrate", rev = "456cfad45a178617f6886ec400c312f2fea59232" }
sp-domains = { version = "0.1.0", default-features = false, path = "../sp-domains" }
sp-runtime = { version = "7.0.0", default-features = false, git = "https://github.com/subspace/substrate", rev = "456cfad45a178617f6886ec400c312f2fea59232" }
sp-std = { version = "5.0.0", default-features = false, git = "https://github.com/subspace/substrate", rev = "456cfad45a178617f6886ec400c312f2fea59232" }

[features]
Expand All @@ -25,5 +26,6 @@ std = [
"sp-api/std",
"sp-core/std",
"sp-domains/std",
"sp-runtime/std",
"sp-std/std",
]
8 changes: 8 additions & 0 deletions crates/sp-receipts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@
use codec::{Decode, Encode};
use sp_core::H256;
use sp_domains::DomainId;
use sp_runtime::traits::NumberFor;
use sp_std::vec::Vec;

sp_api::decl_runtime_apis! {
pub trait ReceiptsApi<DomainHash: Encode + Decode> {
/// Returns the trace of given domain receipt hash.
fn execution_trace(domain_id: DomainId, receipt_hash: H256) -> Vec<DomainHash>;

/// Returns the state root of given domain block.
fn state_root(
domain_id: DomainId,
domain_block_number: NumberFor<Block>,
domain_block_hash: Block::Hash,
) -> Option<DomainHash>;
}
}
Loading