Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Return canonical ISMP events from RPC (#84)
Browse files Browse the repository at this point in the history
rework events rpc
  • Loading branch information
Wizdave97 authored Aug 21, 2023
1 parent 356a516 commit a6af485
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

75 changes: 71 additions & 4 deletions pallet-ismp/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,7 +136,7 @@ where
fn query_events(
&self,
block_numbers: Vec<BlockNumberOrHash<Hash>>,
) -> Result<HashMap<String, Vec<pallet_ismp::events::Event>>>;
) -> Result<HashMap<String, Vec<Event>>>;

/// Query pending get requests that have a `state_machine_height` <= `height`.
#[method(name = "ismp_pendingGetRequests")]
Expand Down Expand Up @@ -301,7 +302,7 @@ where
fn query_events(
&self,
block_numbers: Vec<BlockNumberOrHash<Block::Hash>>,
) -> Result<HashMap<String, Vec<pallet_ismp::events::Event>>> {
) -> Result<HashMap<String, Vec<Event>>> {
let api = self.client.runtime_api();
let mut events = HashMap::new();
for block_number_or_hash in block_numbers {
Expand All @@ -314,10 +315,76 @@ where
}
};

let temp = api
let mut request_indices = vec![];
let mut response_indices = vec![];
let mut temp: Vec<Event> = 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<LeafIndex> =
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<LeafIndex> =
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)
Expand Down
11 changes: 7 additions & 4 deletions pallet-ismp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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)>,
},
Expand Down Expand Up @@ -72,7 +72,10 @@ pub fn to_core_protocol_event<T: Config>(event: PalletEvent<T>) -> Option<Event>
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,
}
Expand Down

0 comments on commit a6af485

Please sign in to comment.