diff --git a/Cargo.lock b/Cargo.lock index 91db2f8..78a03f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3386,7 +3386,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", @@ -3639,7 +3639,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", 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, }