diff --git a/Cargo.lock b/Cargo.lock index 7062809..f2e70b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -839,9 +839,9 @@ dependencies = [ [[package]] name = "either" -version = "1.8.1" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "elliptic-curve" @@ -1627,7 +1627,7 @@ dependencies = [ [[package]] name = "ismp" version = "0.1.0" -source = "git+https://github.com/polytope-labs/ismp-rs?branch=main#317cb2ffc16d4d5b0b50dfee282728bf70fc0641" +source = "git+https://github.com/polytope-labs/ismp-rs?branch=main#3a85db0d968eab5a88427a19a2a612332caec23f" dependencies = [ "derive_more", "parity-scale-codec", @@ -1706,7 +1706,7 @@ dependencies = [ [[package]] name = "ismp-testsuite" version = "0.1.0" -source = "git+https://github.com/polytope-labs/ismp-rs?branch=main#317cb2ffc16d4d5b0b50dfee282728bf70fc0641" +source = "git+https://github.com/polytope-labs/ismp-rs?branch=main#3a85db0d968eab5a88427a19a2a612332caec23f" dependencies = [ "ismp", "parity-scale-codec", @@ -2450,9 +2450,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.64" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78803b62cbf1f46fde80d7c0e803111524b9877184cfe7c3033659490ac7a7da" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] @@ -2482,9 +2482,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.29" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] @@ -3059,9 +3059,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.102" +version = "1.0.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5062a995d481b2308b6064e9af76011f2921c35f97b0468811ed9f6cd91dfed" +checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" dependencies = [ "itoa", "ryu", diff --git a/pallet-ismp/primitives/src/mmr.rs b/pallet-ismp/primitives/src/mmr.rs index 6508872..5f265fe 100644 --- a/pallet-ismp/primitives/src/mmr.rs +++ b/pallet-ismp/primitives/src/mmr.rs @@ -13,6 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +// This file contains code adapted from https://github.com/paritytech/substrate/blob/master/frame/merkle-mountain-range/src/mmr/mod.rs + //! MMR utilities use core::fmt::Formatter; diff --git a/pallet-ismp/rpc/src/lib.rs b/pallet-ismp/rpc/src/lib.rs index e21c781..baa676e 100644 --- a/pallet-ismp/rpc/src/lib.rs +++ b/pallet-ismp/rpc/src/lib.rs @@ -30,6 +30,7 @@ use ismp_primitives::{ }; use ismp_rs::{ consensus::{ConsensusClientId, StateMachineId}, + events::{ChallengePeriodStarted, Event, StateMachineUpdated}, router::{Get, Request, Response}, }; use ismp_runtime_api::IsmpRuntimeApi; @@ -135,7 +136,7 @@ where fn query_events( &self, block_numbers: Vec>, - ) -> Result>>; + ) -> Result>>; /// Query pending get requests that have a `state_machine_height` <= `height`. #[method(name = "ismp_pendingGetRequests")] @@ -301,7 +302,7 @@ where fn query_events( &self, block_numbers: Vec>, - ) -> Result>> { + ) -> Result>> { let api = self.client.runtime_api(); let mut events = HashMap::new(); for block_number_or_hash in block_numbers { @@ -314,10 +315,76 @@ where } }; - let temp = api + let mut request_indices = vec![]; + let mut response_indices = vec![]; + let mut temp: Vec = api .block_events(at) .ok() - .ok_or_else(|| runtime_error_into_rpc_error("failed to read block events"))?; + .ok_or_else(|| runtime_error_into_rpc_error("failed to read block events"))? + .into_iter() + .filter_map(|event| match event { + pallet_ismp::events::Event::Request { + source_chain, + dest_chain, + request_nonce, + } => { + let query = + LeafIndexQuery { source_chain, dest_chain, nonce: request_nonce }; + let indices: Vec = + api.get_request_leaf_indices(at, vec![query]).ok()?; + request_indices.extend_from_slice(&indices); + None + } + pallet_ismp::events::Event::Response { + source_chain, + dest_chain, + request_nonce, + } => { + let query = + LeafIndexQuery { source_chain, dest_chain, nonce: request_nonce }; + let indices: Vec = + api.get_response_leaf_indices(at, vec![query]).ok()?; + response_indices.extend_from_slice(&indices); + None + } + pallet_ismp::events::Event::ChallengePeriodStarted { + consensus_state_id, + state_machines, + } => Some(Event::ChallengePeriodStarted(ChallengePeriodStarted { + consensus_state_id, + state_machines, + })), + pallet_ismp::events::Event::StateMachineUpdated { + state_machine_id, + latest_height, + } => Some(Event::StateMachineUpdated(StateMachineUpdated { + state_machine_id, + latest_height, + })), + }) + .collect(); + + let request_events = api + .get_requests(at, request_indices) + .map_err(|_| runtime_error_into_rpc_error("Error fetching requests"))? + .into_iter() + .map(|req| match req { + Request::Post(post) => Event::PostRequest(post), + Request::Get(get) => Event::GetRequest(get), + }); + + let response_events = api + .get_responses(at, response_indices) + .map_err(|_| runtime_error_into_rpc_error("Error fetching response"))? + .into_iter() + .filter_map(|res| match res { + Response::Post(post) => Some(Event::PostResponse(post)), + _ => None, + }); + + temp.extend(request_events); + temp.extend(response_events); + events.insert(block_number_or_hash.to_string(), temp); } Ok(events) diff --git a/pallet-ismp/src/events.rs b/pallet-ismp/src/events.rs index 6822f46..fff810e 100644 --- a/pallet-ismp/src/events.rs +++ b/pallet-ismp/src/events.rs @@ -17,7 +17,7 @@ use crate::{Config, Event as PalletEvent}; use alloc::collections::BTreeSet; use ismp_rs::{ - consensus::{ConsensusClientId, StateMachineHeight, StateMachineId}, + consensus::{ConsensusStateId, StateMachineHeight, StateMachineId}, host::StateMachine, }; @@ -34,8 +34,8 @@ pub enum Event { }, /// Emitted when a challenge period has begun for a consensus client ChallengePeriodStarted { - /// Consensus client id - consensus_client_id: ConsensusClientId, + /// Consensus state id + consensus_state_id: ConsensusStateId, /// Tuple of previous height and latest height state_machines: BTreeSet<(StateMachineHeight, StateMachineHeight)>, }, @@ -72,7 +72,10 @@ pub fn to_core_protocol_event(event: PalletEvent) -> Option Some(Event::Request { dest_chain, source_chain, request_nonce }) } PalletEvent::ChallengePeriodStarted { consensus_client_id, state_machines } => { - Some(Event::ChallengePeriodStarted { consensus_client_id, state_machines }) + Some(Event::ChallengePeriodStarted { + consensus_state_id: consensus_client_id, + state_machines, + }) } _ => None, } diff --git a/pallet-ismp/src/lib.rs b/pallet-ismp/src/lib.rs index 2f307e0..38f5211 100644 --- a/pallet-ismp/src/lib.rs +++ b/pallet-ismp/src/lib.rs @@ -107,14 +107,6 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Prefix for elements stored in the Off-chain DB via Indexing API. - /// - /// Each node of the MMR is inserted both on-chain and off-chain via Indexing API. - /// The former does not store full leaf content, just its compact version (hash), - /// and some of the inner mmr nodes might be pruned from on-chain storage. - /// The latter will contain all the entries in their full form. - /// - /// Each node is stored in the Off-chain DB under key derived from the - /// [`Self::INDEXING_PREFIX`] and its in-tree index (MMR position). const INDEXING_PREFIX: &'static [u8]; /// Admin origin for privileged actions diff --git a/pallet-ismp/src/mmr.rs b/pallet-ismp/src/mmr.rs index 4bc7fde..64a2410 100644 --- a/pallet-ismp/src/mmr.rs +++ b/pallet-ismp/src/mmr.rs @@ -13,6 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +// This module contains code adapted from https://github.com/paritytech/substrate/blob/master/frame/merkle-mountain-range/src/mmr/mod.rs + pub mod mmr; pub mod storage; pub mod utils;