Skip to content

Commit

Permalink
ParachainConsensusInherentProvider (#40)
Browse files Browse the repository at this point in the history
* adds inherent support to ismp_parachain

* initial work for inherent provider

* introduce ismp-parachain-runtime-api

* fix ParachainConsensusClient

* refactor verify_consensus

* add parachains list to GenesisBuild

* optional inherent

* integration tests works
  • Loading branch information
seunlanlege authored May 23, 2023
1 parent dbd8d33 commit 0e73b69
Show file tree
Hide file tree
Showing 12 changed files with 547 additions and 198 deletions.
30 changes: 28 additions & 2 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[workspace]
resolver = "2"

members = [
"pallet-ismp/rpc",
"pallet-ismp/runtime-api",
"pallet-ismp/primitives",
"pallet-ismp",
"parachain-consensus",
"parachain/inherent",
"parachain/runtime-api",
"parachain",
"ismp-assets"
]
136 changes: 73 additions & 63 deletions pallet-ismp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@ pub use mmr::utils::NodesUtils;
use crate::host::Host;
use codec::{Decode, Encode};
use core::time::Duration;
use frame_support::{log::debug, traits::Get, RuntimeDebug};
use frame_support::{dispatch::DispatchResult, log::debug, traits::Get, RuntimeDebug};
use ismp_rs::{
consensus::{ConsensusClientId, StateMachineId},
handlers::{handle_incoming_message, MessageResult},
host::StateMachine,
messaging::CreateConsensusClient,
router::{Request, Response},
};
use sp_core::{offchain::StorageKind, H256};
// Re-export pallet items so that they can be accessed from the crate namespace.
use crate::primitives::{IsmpDispatch, IsmpMessage};
use crate::{
errors::HandlingError,
mmr::mmr::Mmr,
primitives::{IsmpDispatch, IsmpMessage},
};
use ismp_primitives::{
mmr::{DataOrHash, Leaf, LeafIndex, NodeIndex},
LeafIndexQuery,
};
use ismp_rs::{host::ISMPHost, router::ISMPRouter};
use mmr::mmr::Mmr;
use ismp_rs::{host::ISMPHost, messaging::Message, router::ISMPRouter};
pub use pallet::*;
use sp_std::prelude::*;

Expand All @@ -74,7 +78,7 @@ pub mod pallet {
use ismp_primitives::mmr::{LeafIndex, NodeIndex};
use ismp_rs::{
consensus::{ConsensusClientId, StateCommitment, StateMachineHeight, StateMachineId},
handlers::{self, handle_incoming_message, MessageResult},
handlers::{self},
host::StateMachine,
messaging::Message,
router::ISMPRouter,
Expand Down Expand Up @@ -253,65 +257,8 @@ pub mod pallet {
#[frame_support::transactional]
pub fn handle(origin: OriginFor<T>, messages: Vec<Message>) -> DispatchResult {
let _ = ensure_signed(origin)?;
// Define a host
let host = Host::<T>::default();
let mut errors: Vec<HandlingError> = vec![];

for message in messages {
match handle_incoming_message(&host, message) {
Ok(MessageResult::ConsensusMessage(res)) => {
// check if this is a trusted state machine
let is_trusted_state_machine = host
.challenge_period(res.consensus_client_id.clone()) ==
Duration::from_secs(0);

if is_trusted_state_machine {
for (_, latest_height) in res.state_updates.into_iter() {
Self::deposit_event(Event::<T>::StateMachineUpdated {
state_machine_id: latest_height.id,
latest_height: latest_height.height,
})
}
} else {
if let Some(pending_updates) =
ConsensusUpdateResults::<T>::get(res.consensus_client_id)
{
for (_, latest_height) in pending_updates.into_iter() {
Self::deposit_event(Event::<T>::StateMachineUpdated {
state_machine_id: latest_height.id,
latest_height: latest_height.height,
})
}
}

Self::deposit_event(Event::<T>::ChallengePeriodStarted {
consensus_client_id: res.consensus_client_id,
state_machines: res.state_updates.clone(),
});

// Store the new update result that have just entered the challenge
// period
ConsensusUpdateResults::<T>::insert(
res.consensus_client_id,
res.state_updates,
);
}
}
Ok(_) => {
// Do nothing, event should have been deposited by the ismp router
}
Err(err) => {
errors.push(err.into());
}
}
}

if !errors.is_empty() {
debug!(target: "ismp-rust", "Handling Errors {:?}", errors);
Self::deposit_event(Event::<T>::HandlingErrors { errors })
}

Ok(())
Self::handle_messages(messages)
}

/// Create consensus clients
Expand Down Expand Up @@ -399,6 +346,69 @@ where
mmr.generate_proof(leaf_indices)
}

/// Provides a way to handle messages.
pub fn handle_messages(messages: Vec<Message>) -> DispatchResult {
// Define a host
let host = Host::<T>::default();
let mut errors: Vec<HandlingError> = vec![];

for message in messages {
match handle_incoming_message(&host, message) {
Ok(MessageResult::ConsensusMessage(res)) => {
// check if this is a trusted state machine
let is_trusted_state_machine = host
.challenge_period(res.consensus_client_id.clone()) ==
Duration::from_secs(0);

if is_trusted_state_machine {
for (_, latest_height) in res.state_updates.into_iter() {
Self::deposit_event(Event::<T>::StateMachineUpdated {
state_machine_id: latest_height.id,
latest_height: latest_height.height,
})
}
} else {
if let Some(pending_updates) =
ConsensusUpdateResults::<T>::get(res.consensus_client_id)
{
for (_, latest_height) in pending_updates.into_iter() {
Self::deposit_event(Event::<T>::StateMachineUpdated {
state_machine_id: latest_height.id,
latest_height: latest_height.height,
})
}
}

Self::deposit_event(Event::<T>::ChallengePeriodStarted {
consensus_client_id: res.consensus_client_id,
state_machines: res.state_updates.clone(),
});

// Store the new update result that have just entered the challenge
// period
ConsensusUpdateResults::<T>::insert(
res.consensus_client_id,
res.state_updates,
);
}
}
Ok(_) => {
// Do nothing, event should have been deposited by the ismp router
}
Err(err) => {
errors.push(err.into());
}
}
}

if !errors.is_empty() {
debug!(target: "ismp-rust", "Handling Errors {:?}", errors);
Self::deposit_event(Event::<T>::HandlingErrors { errors })
}

Ok(())
}

/// Return the on-chain MMR root hash.
pub fn mmr_root() -> <T as frame_system::Config>::Hash {
Self::mmr_root_hash()
Expand Down
111 changes: 0 additions & 111 deletions parachain-consensus/src/lib.rs

This file was deleted.

2 changes: 2 additions & 0 deletions parachain-consensus/Cargo.toml → parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ismp = { git = "ssh://[email protected]/polytope-labs/ismp-rs.git", branch = "main"
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" }
sp-trie = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" }
sp-inherents = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" }
sp-consensus-aura = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" }
Expand Down Expand Up @@ -52,6 +53,7 @@ std = [
"sp-consensus-aura/std",
"sp-runtime/std",
"sp-io/std",
"sp-inherents/std",
"primitive-types/std",
"ismp-primitives/std",
"pallet-ismp/std",
Expand Down
25 changes: 25 additions & 0 deletions parachain/inherent/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "ismp-parachain-inherent"
version = "0.1.0"
edition = "2021"
authors = ["Polytope Labs <[email protected]>"]

[dependencies]
async-trait = { version = "0.1.63" }
codec = { package = "parity-scale-codec", version = "3.0.0", features = [ "derive" ] }
anyhow = "1.0.57"

# Substrate
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }

# Cumulus
cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", branch = "release-v0.9.400" }
cumulus-relay-chain-interface = { git = "https://github.com/paritytech/cumulus", branch = "release-v0.9.400" }

# polytope-labs
ismp = { git = "ssh://[email protected]/polytope-labs/ismp-rs.git", branch = "main" }
ismp-parachain = { path = "../" }
ismp-parachain-runtime-api = { path = "../runtime-api" }
Loading

0 comments on commit 0e73b69

Please sign in to comment.