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

Commit

Permalink
store latest messaging height
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege committed Oct 4, 2023
1 parent 66f4a78 commit cfa53dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
12 changes: 12 additions & 0 deletions pallet-ismp/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ where
#[method(name = "ismp_queryStateMachineLatestHeight")]
fn query_state_machine_latest_height(&self, id: StateMachineId) -> Result<u64>;

/// Query the most recent height at which we've processed requests for a state machine
#[method(name = "ismp_queryLatestMessagingHeight")]
fn query_latest_messaging_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 = "ismp_queryEvents")]
Expand Down Expand Up @@ -390,4 +394,12 @@ where
}
Ok(events)
}

fn query_latest_messaging_height(&self, id: StateMachineId) -> Result<u64> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;
api.latest_messaging_height(at, id).ok().flatten().ok_or_else(|| {
runtime_error_into_rpc_error("Error fetching latest state machine height")
})
}
}
3 changes: 3 additions & 0 deletions pallet-ismp/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ sp_api::decl_runtime_apis! {
/// Return the latest height of the state machine
fn latest_state_machine_height(id: StateMachineId) -> Option<u64>;

/// Return the most recent height we've processed requests for a state machine
fn latest_messaging_height(id: StateMachineId) -> Option<u64>;

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

Expand Down
28 changes: 25 additions & 3 deletions pallet-ismp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use ismp_primitives::{
mmr::{DataOrHash, Leaf, LeafIndex, NodeIndex},
LeafIndexQuery,
};
use ismp_rs::{host::IsmpHost, messaging::Message};
use ismp_rs::{consensus::StateMachineHeight, host::IsmpHost, messaging::Message};
pub use pallet::*;
use sp_runtime::RuntimeDebug;
use sp_std::prelude::*;
Expand Down Expand Up @@ -170,10 +170,16 @@ pub mod pallet {
/// Holds a map of state machines to the height at which they've been frozen due to byzantine
/// behaviour
#[pallet::storage]
#[pallet::getter(fn frozen_heights)]
#[pallet::getter(fn latest_messaging_heights)]
pub type FrozenHeights<T: Config> =
StorageMap<_, Blake2_128Concat, StateMachineId, u64, OptionQuery>;

/// Holds a map of state machines to the latest height we've processed requests for
#[pallet::storage]
#[pallet::getter(fn frozen_heights)]
pub type LatestMessagingHeight<T: Config> =
StorageMap<_, Blake2_128Concat, StateMachineId, u64, ValueQuery>;

/// A mapping of ConsensusStateId to ConsensusClientId
#[pallet::storage]
pub type ConsensusStateClient<T: Config> =
Expand Down Expand Up @@ -468,7 +474,7 @@ impl<T: Config> Pallet<T> {
let mut errors: Vec<HandlingError> = vec![];
let total_weight = get_weight::<T>(&messages);
for message in messages {
match handle_incoming_message(&host, message) {
match handle_incoming_message(&host, message.clone()) {
Ok(MessageResult::ConsensusMessage(res)) => {
// check if this is a trusted state machine
let is_trusted_state_machine = host
Expand Down Expand Up @@ -508,9 +514,25 @@ impl<T: Config> Pallet<T> {
}
}
Ok(MessageResult::Response(res)) => {
let StateMachineHeight { id, height } = match message {
Message::Request(ref request) => request.proof.height.clone(),
_ => unreachable!(),
};
// update the messaging heights
if LatestMessagingHeight::<T>::get(&id) > height {
LatestMessagingHeight::<T>::insert(id, height);
}
debug!(target: "ismp-modules", "Module Callback Results {:?}", ModuleCallbackResult::Response(res));
}
Ok(MessageResult::Request(res)) => {
let StateMachineHeight { id, height } = match message {
Message::Request(ref request) => request.proof.height.clone(),
_ => unreachable!(),
};
// update the messaging heights
if LatestMessagingHeight::<T>::get(&id) > height {
LatestMessagingHeight::<T>::insert(id, height);
}
debug!(target: "ismp-modules", "Module Callback Results {:?}", ModuleCallbackResult::Request(res));
}
Ok(MessageResult::Timeout(res)) => {
Expand Down

0 comments on commit cfa53dd

Please sign in to comment.