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

Commit

Permalink
rpc methods (#78)
Browse files Browse the repository at this point in the history
Co-authored-by: David Salami <[email protected]>
  • Loading branch information
dharjeezy and Wizdave97 authored Aug 1, 2023
1 parent 3c4a9f3 commit 8f561ba
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
26 changes: 26 additions & 0 deletions pallet-ismp/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ where
#[method(name = "ismp_queryConsensusUpdateTime")]
fn query_consensus_update_time(&self, client_id: ConsensusClientId) -> Result<u64>;

/// Query the challenge period for client
#[method(name = "ismp_queryChallengePeriod")]
fn query_challenge_period(&self, client_id: ConsensusClientId) -> Result<u64>;

/// Query the latest timestamp for chain
#[method(name = "ismp_queryTimestamp")]
fn query_timestamp(&self) -> 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>;
Expand Down Expand Up @@ -255,6 +263,24 @@ where
.ok_or_else(|| runtime_error_into_rpc_error("Error fetching Consensus update time"))
}

fn query_challenge_period(&self, client_id: ConsensusClientId) -> Result<u64> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;
api.challenge_period(at, client_id)
.ok()
.flatten()
.ok_or_else(|| runtime_error_into_rpc_error("Error fetching Challenge period"))
}

fn query_timestamp(&self) -> Result<u64> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;
api.timestamp(at)
.ok()
.flatten()
.ok_or_else(|| runtime_error_into_rpc_error("Error fetching latest timestamp"))
}

fn query_state_machine_latest_height(&self, id: StateMachineId) -> Result<u64> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;
Expand Down
6 changes: 6 additions & 0 deletions pallet-ismp/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ 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 timestamp for the chain
fn timestamp() -> Option<u64>;

/// Return the challenge period timestamp
fn challenge_period(id: ConsensusClientId) -> Option<u64>;

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

Expand Down
12 changes: 11 additions & 1 deletion pallet-ismp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use core::time::Duration;
use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo, Pays, PostDispatchInfo},
log::debug,
traits::Get,
traits::{Get, UnixTime},
RuntimeDebug,
};
use ismp_rs::{
Expand Down Expand Up @@ -667,6 +667,16 @@ where
ConsensusClientUpdateTime::<T>::get(id)
}

/// Return the challenge period
pub fn get_challenge_period(id: ConsensusClientId) -> Option<u64> {
ChallengePeriod::<T>::get(id)
}

/// Return latest timestamp on chain
pub fn get_timestamp() -> Option<u64> {
Some(<T::TimeProvider as UnixTime>::now().as_secs())
}

/// Return the latest height of the state machine
pub fn get_latest_state_machine_height(id: StateMachineId) -> Option<u64> {
Some(LatestStateMachineHeight::<T>::get(id))
Expand Down

0 comments on commit 8f561ba

Please sign in to comment.