Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request paritytech#288 from subspace/execution-proof-impl
Browse files Browse the repository at this point in the history
Implement the creation of FraudProof
  • Loading branch information
liuchengxu authored Mar 22, 2022
2 parents 9485bf4 + 7b4932f commit 99ecf82
Show file tree
Hide file tree
Showing 15 changed files with 819 additions and 33 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"cumulus/client/consensus/common",
"cumulus/client/consensus/relay-chain",
"cumulus/client/executor-gossip",
"cumulus/client/fraud-proof",
"cumulus/pallets/executive",
"cumulus/parachain-template/node",
"cumulus/parachain-template/runtime",
Expand Down
76 changes: 69 additions & 7 deletions cumulus/client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ where
})
}

/// Create a new instance of builder with given extrinsics.
pub fn with_extrinsics(
api: &'a A,
parent_hash: Block::Hash,
parent_number: NumberFor<Block>,
record_proof: RecordProof,
inherent_digests: Digest,
backend: &'a B,
extrinsics: Vec<Block::Extrinsic>,
) -> Result<Self, Error> {
let mut block_builder =
Self::new(api, parent_hash, parent_number, record_proof, inherent_digests, backend)?;
block_builder.extrinsics = extrinsics;
Ok(block_builder)
}

/// Sets the extrinsics.
pub fn set_extrinsics(&mut self, extrinsics: Vec<Block::Extrinsic>) {
self.extrinsics = extrinsics;
Expand Down Expand Up @@ -223,6 +239,58 @@ where
Ok(())
}

fn collect_storage_changes(
&self,
) -> Result<sp_api::StorageChanges<backend::StateBackendFor<B, Block>, Block>, Error> {
let state = self.backend.state_at(self.block_id)?;
let parent_hash = self.parent_hash;
self.api
.into_storage_changes(&state, parent_hash)
.map_err(sp_blockchain::Error::StorageChanges)
}

/// Returns the state before executing the extrinsic at given extrinsic index.
pub fn prepare_storage_changes_before(
&self,
extrinsic_index: usize,
) -> Result<sp_api::StorageChanges<backend::StateBackendFor<B, Block>, Block>, Error> {
for (index, xt) in self.extrinsics.iter().enumerate() {
if index == extrinsic_index {
return Ok(self.collect_storage_changes()?)
}

// TODO: rethink what to do if an error occurs when executing the transaction.
self.api.execute_in_transaction(|api| {
let res = api.apply_extrinsic_with_context(
&self.block_id,
ExecutionContext::BlockConstruction,
xt.clone(),
);
match res {
Ok(Ok(_)) => TransactionOutcome::Commit(Ok(())),
Ok(Err(tx_validity)) => TransactionOutcome::Rollback(Err(
ApplyExtrinsicFailed::Validity(tx_validity).into(),
)),
Err(e) => TransactionOutcome::Rollback(Err(Error::from(e))),
}
})?;
}

Err(Error::Execution(Box::new(format!(
"Invalid extrinsic index, got: {}, max: {}",
extrinsic_index,
self.extrinsics.len()
))))
}

/// Returns the state before finalizing the block.
pub fn prepare_storage_changes_before_finalize_block(
&self,
) -> Result<sp_api::StorageChanges<backend::StateBackendFor<B, Block>, Block>, Error> {
self.execute_extrinsics()?;
self.collect_storage_changes()
}

/// Consume the builder to build a valid `Block` containing all pushed extrinsics.
///
/// Returns the build `Block`, the changes to the storage and an optional `StorageProof`
Expand All @@ -245,13 +313,7 @@ where

let proof = self.api.extract_proof();

let state = self.backend.state_at(self.block_id)?;
let parent_hash = self.parent_hash;

let storage_changes = self
.api
.into_storage_changes(&state, parent_hash)
.map_err(|e| sp_blockchain::Error::StorageChanges(e))?;
let storage_changes = self.collect_storage_changes()?;

Ok(BuiltBlock {
block: <Block as BlockT>::new(header, self.extrinsics),
Expand Down
2 changes: 2 additions & 0 deletions cumulus/client/cirrus-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ polkadot-node-subsystem = { path = "../../../polkadot/node/subsystem" }
cirrus-block-builder = { path = "../block-builder" }
cirrus-client-executor-gossip = { path = "../executor-gossip" }
cirrus-node-primitives = { path = "../../../crates/cirrus-node-primitives" }
cirrus-fraud-proof = { path = "../fraud-proof" }
cirrus-primitives = { path = "../../primitives" }
sp-executor = { path = "../../../crates/sp-executor" }
subspace-core-primitives = { path = "../../../crates/subspace-core-primitives" }
Expand All @@ -59,6 +60,7 @@ version = "0.10.0"

[dev-dependencies]
cirrus-test-service = { path = "../../test/service" }
pallet-balances = { git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998" }
sc-cli = { git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998" }
sp-keyring = { git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998" }
substrate-test-runtime = { path = "../../../substrate/substrate-test-runtime" }
Expand Down
Loading

0 comments on commit 99ecf82

Please sign in to comment.