Skip to content

Commit

Permalink
Merge pull request #2106 from subspace/fraud_proof/invalid_bundle_uns…
Browse files Browse the repository at this point in the history
…igned_extrinsic

Domains: Mark bundles that contain inherent extrinsics such as timestamp and set_code as Invalid
  • Loading branch information
vedhavyas authored Oct 20, 2023
2 parents a66e924 + b034587 commit d7f9265
Show file tree
Hide file tree
Showing 27 changed files with 529 additions and 224 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/sp-domains-fraud-proof/src/host_functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{FraudProofVerificationInfoRequest, FraudProofVerificationInfoResponse};
use codec::{Decode, Encode};
use domain_block_preprocessor::runtime_api::InherentExtrinsicConstructor;
use domain_block_preprocessor::runtime_api::TimestampExtrinsicConstructor;
use domain_block_preprocessor::runtime_api_light::RuntimeApiLight;
use sc_executor::RuntimeVersionOf;
use sp_api::{BlockT, ProvideRuntimeApi};
Expand Down Expand Up @@ -107,7 +107,7 @@ where
let domain_runtime_api_light =
RuntimeApiLight::new(self.executor.clone(), runtime_code.into());

InherentExtrinsicConstructor::<DomainBlock>::construct_timestamp_inherent_extrinsic(
TimestampExtrinsicConstructor::<DomainBlock>::construct_timestamp_extrinsic(
&domain_runtime_api_light,
// We do not care about the domain hash since this is stateless call into
// domain runtime,
Expand Down
6 changes: 5 additions & 1 deletion crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,18 +729,21 @@ pub enum InvalidBundleType {
IllegalTx(u32),
/// Transaction is an invalid XDM
InvalidXDM(u32),
/// Transaction is an inherent extrinsic.
InherentExtrinsic(u32),
}

impl InvalidBundleType {
// Return the checking order of the invalid type
pub fn checking_order(&self) -> u8 {
// Use explicit number as the order instead of the enum discriminant
// to avoid chenging the order accidentally
// to avoid changing the order accidentally
match self {
Self::UndecodableTx(_) => 1,
Self::OutOfRangeTx(_) => 2,
Self::IllegalTx(_) => 3,
Self::InvalidXDM(_) => 4,
Self::InherentExtrinsic(_) => 5,
}
}

Expand All @@ -750,6 +753,7 @@ impl InvalidBundleType {
Self::OutOfRangeTx(i) => *i,
Self::IllegalTx(i) => *i,
Self::InvalidXDM(i) => *i,
Self::InherentExtrinsic(i) => *i,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/subspace-fraud-proof/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ async fn execution_proof_creation_and_verification_should_work() {
digest,
&*alice.backend,
test_txs.clone().into_iter().map(Into::into).collect(),
Default::default(),
)
.unwrap()
};
Expand Down Expand Up @@ -687,6 +688,7 @@ async fn invalid_execution_proof_should_not_work() {
},
&*alice.backend,
test_txs.clone().into_iter().map(Into::into).collect(),
Default::default(),
)
.unwrap()
};
Expand Down
31 changes: 22 additions & 9 deletions domains/client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use sp_blockchain::{ApplyExtrinsicFailed, Error};
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{Block as BlockT, Hash, HashingFor, Header as HeaderT, NumberFor, One};
use sp_runtime::Digest;
use std::collections::VecDeque;

/// Used as parameter to [`BlockBuilderProvider`] to express if proof recording should be enabled.
///
Expand Down Expand Up @@ -127,7 +128,7 @@ where

/// Utility for building new (valid) blocks from a stream of extrinsics.
pub struct BlockBuilder<'a, Block: BlockT, A: ProvideRuntimeApi<Block>, B> {
extrinsics: Vec<Block::Extrinsic>,
extrinsics: VecDeque<Block::Extrinsic>,
api: ApiRef<'a, A::Api>,
parent_hash: Block::Hash,
backend: &'a B,
Expand All @@ -147,14 +148,16 @@ where
/// While proof recording is enabled, all accessed trie nodes are saved.
/// These recorded trie nodes can be used by a third party to prove the
/// output of this block builder without having access to the full storage.
#[allow(clippy::too_many_arguments)]
pub fn new(
api: &'a A,
parent_hash: Block::Hash,
parent_number: NumberFor<Block>,
record_proof: RecordProof,
inherent_digests: Digest,
backend: &'a B,
extrinsics: Vec<Block::Extrinsic>,
mut extrinsics: VecDeque<Block::Extrinsic>,
maybe_inherent_data: Option<sp_inherents::InherentData>,
) -> Result<Self, Error> {
let header = <<Block as BlockT>::Header as HeaderT>::new(
parent_number + One::one(),
Expand All @@ -174,6 +177,13 @@ where

api.initialize_block(parent_hash, &header)?;

if let Some(inherent_data) = maybe_inherent_data {
let inherent_extrinsics = Self::create_inherents(parent_hash, &api, inherent_data)?;
for inherent_extrinsic in inherent_extrinsics {
extrinsics.push_front(inherent_extrinsic)
}
}

Ok(Self {
parent_hash,
extrinsics,
Expand Down Expand Up @@ -275,7 +285,7 @@ where
let storage_changes = self.collect_storage_changes()?;

Ok(BuiltBlock {
block: <Block as BlockT>::new(header, self.extrinsics),
block: <Block as BlockT>::new(header, self.extrinsics.into()),
storage_changes,
proof,
})
Expand All @@ -285,17 +295,18 @@ where
///
/// Returns the inherents created by the runtime or an error if something failed.
pub fn create_inherents(
&mut self,
parent_hash: Block::Hash,
api: &ApiRef<A::Api>,
inherent_data: sp_inherents::InherentData,
) -> Result<Vec<Block::Extrinsic>, Error> {
let parent_hash = self.parent_hash;
self.api
) -> Result<VecDeque<Block::Extrinsic>, Error> {
let exts = api
.execute_in_transaction(move |api| {
// `create_inherents` should not change any state, to ensure this we always rollback
// the transaction.
TransactionOutcome::Rollback(api.inherent_extrinsics(parent_hash, inherent_data))
})
.map_err(|e| Error::Application(Box::new(e)))
.map_err(|e| Error::Application(Box::new(e)))?;
Ok(VecDeque::from(exts))
}

/// Estimate the size of the block in the current state.
Expand Down Expand Up @@ -324,6 +335,7 @@ mod tests {
use sp_core::Blake2Hasher;
use sp_state_machine::Backend;
// TODO: Remove `substrate_test_runtime_client` dependency for faster build time
use std::collections::VecDeque;
use substrate_test_runtime_client::{DefaultTestClientBuilderExt, TestClientBuilderExt};

// TODO: Unlock this test, it got broken in https://github.com/subspace/subspace/pull/1548 and
Expand All @@ -341,7 +353,8 @@ mod tests {
RecordProof::Yes,
Default::default(),
&*backend,
vec![],
VecDeque::new(),
Default::default(),
)
.unwrap()
.build()
Expand Down
3 changes: 2 additions & 1 deletion domains/client/block-preprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include = [

[dependencies]
async-trait = { version = "0.1.57" }
codec = { package = "parity-scale-codec", version = "3.6.5", features = [ "derive" ] }
codec = { package = "parity-scale-codec", version = "3.6.5", features = ["derive"] }
domain-runtime-primitives = { version = "0.1.0", path = "../../primitives/runtime" }
sc-client-api = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "892bf8e938c6bd2b893d3827d1093cd81baa59a1" }
sc-executor = { version = "0.10.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "892bf8e938c6bd2b893d3827d1093cd81baa59a1" }
Expand All @@ -22,6 +22,7 @@ sp-api = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sd
sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "892bf8e938c6bd2b893d3827d1093cd81baa59a1" }
sp-core = { version = "21.0.0", git = "https://github.com/subspace/polkadot-sdk", rev = "892bf8e938c6bd2b893d3827d1093cd81baa59a1" }
sp-domains = { version = "0.1.0", path = "../../../crates/sp-domains" }
sp-executive = { version = "0.1.0", path = "../../primitives/executive" }
sp-inherents = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "892bf8e938c6bd2b893d3827d1093cd81baa59a1" }
sp-messenger = { version = "0.1.0", path = "../../primitives/messenger" }
sp-runtime = { version = "24.0.0", git = "https://github.com/subspace/polkadot-sdk", rev = "892bf8e938c6bd2b893d3827d1093cd81baa59a1" }
Expand Down
Loading

0 comments on commit d7f9265

Please sign in to comment.