Skip to content

Commit

Permalink
Batch messaging update (#29)
Browse files Browse the repository at this point in the history
* batch messaging update

* rebase

* implement runtime apis

* use return result in router
  • Loading branch information
Wizdave97 authored Apr 20, 2023
1 parent 9fecfce commit 3d7691b
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 111 deletions.
33 changes: 17 additions & 16 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions pallet-ismp/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ismp = { git = "ssh://[email protected]/polytope-labs/ismp-rs.git", branch = "main"
merkle-mountain-range = { package = "ckb-merkle-mountain-range", version = "0.5.2", default-features = false }
codec = { package = "parity-scale-codec", version = "3.1.3", default-features = false }
primitive-types = { version = "0.12.1", default-features = false }
serde = { version = "1.0.136", features = ["derive"], optional = true }

[features]
default = ["std"]
Expand All @@ -26,4 +27,5 @@ std = [
"codec/std",
"sp-runtime/std",
"primitive-types/std",
"serde"
]
10 changes: 10 additions & 0 deletions pallet-ismp/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@

#![cfg_attr(not(feature = "std"), no_std)]

use ismp::host::StateMachine;

pub mod mmr;

#[derive(codec::Encode, codec::Decode)]
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub struct LeafIndexQuery {
pub source_chain: StateMachine,
pub dest_chain: StateMachine,
pub nonce: u64,
}
59 changes: 33 additions & 26 deletions pallet-ismp/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ use jsonrpsee::{
};

use codec::Encode;
use ismp_primitives::mmr::{Leaf, LeafIndex};
use ismp_primitives::{
mmr::{Leaf, LeafIndex},
LeafIndexQuery,
};
use ismp_rs::{
consensus_client::ConsensusClientId,
consensus_client::{ConsensusClientId, StateMachineId},
router::{Request, Response},
};
use ismp_runtime_api::{IsmpRuntimeApi, LeafIndexQuery};
use ismp_runtime_api::IsmpRuntimeApi;
use sc_client_api::{BlockBackend, ProofProvider};
use serde::{Deserialize, Serialize};
use sp_api::ProvideRuntimeApi;
Expand Down Expand Up @@ -99,6 +102,10 @@ where
#[method(name = "ismp_queryConsensusUpdateTime")]
fn query_consensus_update_time(&self, client_id: ConsensusClientId) -> Result<u64>;

/// Query the latest height for a state machine
#[method(name = "ismp_queryStateMachineLatestHeight")]
fn query_state_machine_latest_height(&self, id: StateMachineId) -> Result<u64>;

/// Query ISMP Events that were deposited in a series of blocks
/// Using String keys because HashMap fails to deserialize when key is not a String
#[method(name = "ibc_queryEvents")]
Expand Down Expand Up @@ -136,29 +143,23 @@ where
fn query_requests(&self, query: Vec<LeafIndexQuery>) -> Result<Vec<Request>> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;
let request_indices: Vec<LeafIndex> =
api.get_request_leaf_indices(at, query).ok().flatten().ok_or_else(|| {
runtime_error_into_rpc_error("Error fetching request leaf indices")
})?;
let request_indices: Vec<LeafIndex> = api
.get_request_leaf_indices(at, query)
.map_err(|_| runtime_error_into_rpc_error("Error fetching request leaf indices"))?;

api.get_requests(at, request_indices)
.ok()
.flatten()
.ok_or_else(|| runtime_error_into_rpc_error("Error fetching requests"))
.map_err(|_| runtime_error_into_rpc_error("Error fetching requests"))
}

fn query_responses(&self, query: Vec<LeafIndexQuery>) -> Result<Vec<Response>> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;
let response_indices: Vec<LeafIndex> =
api.get_response_leaf_indices(at, query).ok().flatten().ok_or_else(|| {
runtime_error_into_rpc_error("Error fetching response leaf indices")
})?;
let response_indices: Vec<LeafIndex> = api
.get_response_leaf_indices(at, query)
.map_err(|_| runtime_error_into_rpc_error("Error fetching response leaf indices"))?;

api.get_responses(at, response_indices)
.ok()
.flatten()
.ok_or_else(|| runtime_error_into_rpc_error("Error fetching responses"))
.map_err(|_| runtime_error_into_rpc_error("Error fetching responses"))
}

fn query_requests_mmr_proof(&self, height: u32, query: Vec<LeafIndexQuery>) -> Result<Proof> {
Expand All @@ -169,10 +170,9 @@ where
.ok()
.flatten()
.ok_or_else(|| runtime_error_into_rpc_error("invalid block height provided"))?;
let request_indices: Vec<LeafIndex> =
api.get_request_leaf_indices(at, query).ok().flatten().ok_or_else(|| {
runtime_error_into_rpc_error("Error fetching response leaf indices")
})?;
let request_indices: Vec<LeafIndex> = api
.get_request_leaf_indices(at, query)
.map_err(|_| runtime_error_into_rpc_error("Error fetching response leaf indices"))?;

let (leaves, proof): (Vec<Leaf>, pallet_ismp::primitives::Proof<Block::Hash>) = api
.generate_proof(at, request_indices)
Expand All @@ -189,10 +189,9 @@ where
.ok()
.flatten()
.ok_or_else(|| runtime_error_into_rpc_error("invalid block height provided"))?;
let response_indices: Vec<LeafIndex> =
api.get_response_leaf_indices(at, query).ok().flatten().ok_or_else(|| {
runtime_error_into_rpc_error("Error fetching response leaf indices")
})?;
let response_indices: Vec<LeafIndex> = api
.get_response_leaf_indices(at, query)
.map_err(|_| runtime_error_into_rpc_error("Error fetching response leaf indices"))?;

let (leaves, proof): (Vec<Leaf>, pallet_ismp::primitives::Proof<Block::Hash>) = api
.generate_proof(at, response_indices)
Expand Down Expand Up @@ -226,7 +225,15 @@ where
api.consensus_update_time(at, client_id)
.ok()
.flatten()
.ok_or_else(|| runtime_error_into_rpc_error("Error fetching Consensus state"))
.ok_or_else(|| runtime_error_into_rpc_error("Error fetching Consensus update time"))
}

fn query_state_machine_latest_height(&self, id: StateMachineId) -> Result<u64> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;
api.latest_state_machine_height(at, id).ok().flatten().ok_or_else(|| {
runtime_error_into_rpc_error("Error fetching latest state machine height")
})
}

fn query_events(
Expand Down
27 changes: 12 additions & 15 deletions pallet-ismp/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@
#![allow(clippy::too_many_arguments)]

use ismp_rs::{
consensus_client::ConsensusClientId,
host::StateMachine,
consensus_client::{ConsensusClientId, StateMachineId},
router::{Request, Response},
};
use pallet_ismp::primitives::{Error, Proof};

use ismp_primitives::mmr::{Leaf, LeafIndex};
use ismp_primitives::{
mmr::{Leaf, LeafIndex},
LeafIndexQuery,
};
#[cfg(not(feature = "std"))]
use sp_std::vec::Vec;

#[derive(codec::Encode, codec::Decode)]
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub struct LeafIndexQuery {
pub source_chain: StateMachine,
pub dest_chain: StateMachine,
pub nonce: u64,
}

sp_api::decl_runtime_apis! {
/// ISMP Runtime Apis
pub trait IsmpRuntimeApi<Hash: codec::Codec> {
Expand All @@ -43,16 +37,19 @@ sp_api::decl_runtime_apis! {
/// Return the timestamp this client was last updated in seconds
fn consensus_update_time(id: ConsensusClientId) -> Option<u64>;

/// Return the latest height of the state machine
fn latest_state_machine_height(id: StateMachineId) -> Option<u64>;

/// Get Request Leaf Indices
fn get_request_leaf_indices(leaf_queries: Vec<LeafIndexQuery>) -> Option<Vec<LeafIndex>>;
fn get_request_leaf_indices(leaf_queries: Vec<LeafIndexQuery>) -> Vec<LeafIndex>;

/// Get Response Leaf Indices
fn get_response_leaf_indices(leaf_queries: Vec<LeafIndexQuery>) -> Option<Vec<LeafIndex>>;
fn get_response_leaf_indices(leaf_queries: Vec<LeafIndexQuery>) -> Vec<LeafIndex>;

/// Get actual requests
fn get_requests(leaf_indices: Vec<LeafIndex>) -> Option<Vec<Request>>;
fn get_requests(leaf_indices: Vec<LeafIndex>) -> Vec<Request>;

/// Get actual requests
fn get_responses(leaf_indices: Vec<LeafIndex>) -> Option<Vec<Response>>;
fn get_responses(leaf_indices: Vec<LeafIndex>) -> Vec<Response>;
}
}
3 changes: 3 additions & 0 deletions pallet-ismp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub fn to_core_protocol_events<T: Config>(event: PalletEvent<T>) -> Option<Event
PalletEvent::Request { dest_chain, source_chain, request_nonce } => {
Some(Event::Request { dest_chain, source_chain, request_nonce })
}
PalletEvent::ChallengePeriodStarted { consensus_client_id, state_machines } => {
Some(Event::ChallengePeriodStarted { consensus_client_id, state_machines })
}
_ => None,
}
}
Loading

0 comments on commit 3d7691b

Please sign in to comment.