Skip to content

Commit

Permalink
Initial support of executor gossip (#219)
Browse files Browse the repository at this point in the history
* Initial support of executor gossip

Now the gossip message for bundle and execution receipt can be sent and
received, but still a lot of works to do.

* Some cleanups

* Impl the execution receipt stream

`OpaqueExecutionReceipt` is introduced as `ExecutionReceipt` has both
concrete type(`primary_hash`) and generic type `Hash`, used by the
client submitting the external ER to runtime.

* Validate the gossip message on receiving

`GossipMessageHandler` has been changed from `async` to sync due to the
`Validator` trait of network-gossip is not async and it's non-trivial to
submit a patch to the upstream.

* Custom rebroadcast timeout

* Clean up the messages that have been rebroadcasted

* Add a TODO

* Extract `gossip_bundle` and `gossip_execution_receipt`

* Nit

* Change HandlerOutcome to HandlerResult

* Remove HandlerResult

Fixes #219 (comment)

* Apply the code review

Fixes #219 (comment)
Fixes #219 (comment)

* Add a TODO
  • Loading branch information
liuchengxu authored Dec 31, 2021
1 parent a91a40d commit dbfcd24
Show file tree
Hide file tree
Showing 20 changed files with 706 additions and 120 deletions.
38 changes: 38 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 @@ -7,6 +7,7 @@ members = [
"cumulus/client/cirrus-executor",
"cumulus/client/consensus/common",
"cumulus/client/consensus/relay-chain",
"cumulus/client/executor-gossip",
"cumulus/parachain-template/node",
"cumulus/parachain-template/runtime",
"cumulus/primitives",
Expand Down
12 changes: 6 additions & 6 deletions crates/cirrus-node-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize};
use sp_application_crypto::KeyTypeId;
use sp_consensus_slots::Slot;
use sp_core::bytes;
use sp_executor::{ExecutionReceipt, OpaqueBundle};
use sp_executor::{OpaqueBundle, OpaqueExecutionReceipt};
use sp_runtime::traits::Hash as HashT;
use std::pin::Pin;
use subspace_core_primitives::{Randomness, Tag};
Expand Down Expand Up @@ -94,14 +94,14 @@ impl BundleResult {
}

/// Result of the [`ProcessorFn`] invocation.
pub struct ProcessorResult<H = Hash> {
/// The execution receipt that was built.
pub execution_receipt: ExecutionReceipt<H>,
pub struct ProcessorResult {
/// The opaque execution receipt that was built.
pub opaque_execution_receipt: OpaqueExecutionReceipt,
}

impl ProcessorResult {
pub fn to_execution_receipt(self) -> ExecutionReceipt<Hash> {
self.execution_receipt
pub fn to_opaque_execution_receipt(self) -> OpaqueExecutionReceipt {
self.opaque_execution_receipt
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/pallet-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod pallet {
head_hash: T::Hash,
},
/// A new candidate receipt was backed.
ExecutionReceiptStored { receipt_hash: T::Hash },
ExecutionReceiptStored { receipt_hash: H256 },
/// A transaction bundle was included.
TransactionBundleStored { bundle_hash: H256 },
/// A fraud proof was processed.
Expand Down Expand Up @@ -224,6 +224,7 @@ mod pallet {
.build()
}
Call::submit_fraud_proof { fraud_proof } => {
// TODO: prevent the spamming of fraud proof transaction.
if let Err(e) = Self::check_fraud_proof(fraud_proof) {
log::error!(
target: "runtime::subspace::executor",
Expand Down
23 changes: 17 additions & 6 deletions crates/sp-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<Extrinsic: sp_runtime::traits::Extrinsic + Encode> From<Bundle<Extrinsic>>
#[derive(Decode, Encode, TypeInfo, PartialEq, Eq, Clone, RuntimeDebug)]
pub struct ExecutionReceipt<Hash> {
/// Primary block hash.
pub primary_hash: Hash,
pub primary_hash: H256,
/// Secondary block hash?
pub secondary_hash: Hash,
/// State root after finishing the execution.
Expand All @@ -104,10 +104,21 @@ pub struct ExecutionReceipt<Hash> {
pub state_transition_root: Hash,
}

impl<Hash: Copy> ExecutionReceipt<Hash> {
/// TODO: hash of ER?
pub fn hash(&self) -> Hash {
self.primary_hash
impl<Hash: Encode> ExecutionReceipt<Hash> {
/// Returns the hash of this execution receipt.
pub fn hash(&self) -> H256 {
BlakeTwo256::hash_of(self)
}
}

// TODO: this might be unneccessary, ideally we could interact with the runtime using `ExecutionReceipt` directly.
// Refer to the comment https://github.com/subspace/subspace/pull/219#discussion_r776749767
#[derive(Decode, Encode, TypeInfo, PartialEq, Eq, Clone, RuntimeDebug)]
pub struct OpaqueExecutionReceipt(Vec<u8>);

impl<Hash: Encode> From<ExecutionReceipt<Hash>> for OpaqueExecutionReceipt {
fn from(inner: ExecutionReceipt<Hash>) -> Self {
Self(inner.encode())
}
}

Expand All @@ -129,7 +140,7 @@ sp_api::decl_runtime_apis! {

/// Submits the execution receipt via an unsigned extrinsic.
fn submit_execution_receipt_unsigned(
execution_receipt: ExecutionReceipt<<Block as BlockT>::Hash>,
opaque_execution_receipt: OpaqueExecutionReceipt,
) -> Option<()>;

/// Submits the transaction bundle via an unsigned extrinsic.
Expand Down
10 changes: 8 additions & 2 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,15 @@ impl_runtime_apis! {
}

fn submit_execution_receipt_unsigned(
execution_receipt: sp_executor::ExecutionReceipt<<Block as BlockT>::Hash>,
opaque_execution_receipt: sp_executor::OpaqueExecutionReceipt,
) -> Option<()> {
Executor::submit_execution_receipt_unsigned(execution_receipt).ok()
<sp_executor::ExecutionReceipt<<Block as BlockT>::Hash>>::decode(
&mut opaque_execution_receipt.encode().as_slice(),
)
.ok()
.and_then(|execution_receipt| {
Executor::submit_execution_receipt_unsigned(execution_receipt).ok()
})
}

fn submit_transaction_bundle_unsigned(opaque_bundle: OpaqueBundle) -> Option<()> {
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 @@ -8,6 +8,7 @@ edition = "2021"
# Substrate dependencies
sc-client-api = { git = "https://github.com/paritytech/substrate", rev = "5bd5b842d4ea520d281b1398e1f54907c9862fcd" }
sc-transaction-pool-api = { git = "https://github.com/paritytech/substrate", rev = "5bd5b842d4ea520d281b1398e1f54907c9862fcd" }
sc-utils = { git = "https://github.com/paritytech/substrate", rev = "5bd5b842d4ea520d281b1398e1f54907c9862fcd" }
sp-runtime = { git = "https://github.com/paritytech/substrate", rev = "5bd5b842d4ea520d281b1398e1f54907c9862fcd" }
sp-core = { git = "https://github.com/paritytech/substrate", rev = "5bd5b842d4ea520d281b1398e1f54907c9862fcd" }
sp-consensus = { git = "https://github.com/paritytech/substrate", rev = "5bd5b842d4ea520d281b1398e1f54907c9862fcd" }
Expand All @@ -30,6 +31,7 @@ tracing = "0.1.25"
polkadot-overseer = { path = "../../../polkadot/node/overseer" }
polkadot-node-subsystem = { path = "../../../polkadot/node/subsystem" }

cirrus-client-executor-gossip = { path = "../executor-gossip" }
cirrus-node-primitives = { path = "../../../crates/cirrus-node-primitives" }
cirrus-primitives = { path = "../../primitives" }
sp-executor = { path = "../../../crates/sp-executor" }
Expand Down
Loading

0 comments on commit dbfcd24

Please sign in to comment.