Skip to content

Commit

Permalink
Some utility functions (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 authored May 30, 2023
1 parent 17fe7df commit 836f90f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
82 changes: 82 additions & 0 deletions pallet-ismp/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! Some extra utilities for pallet-ismp
use crate::{
dispatcher::Receipt, host::Host, Config, Event, IncomingRequestAcks, IncomingResponseAcks,
Pallet,
};
use alloc::string::ToString;
use ismp_primitives::mmr::Leaf;
use ismp_rs::{
router::{DispatchError, DispatchResult, DispatchSuccess, Request, Response},
util::{hash_request, hash_response},
};
use sp_core::H256;

impl<T: Config> Pallet<T>
where
<T as frame_system::Config>::Hash: From<H256>,
{
/// Handle an incoming request
pub fn handle_request(&self, request: Request) -> DispatchResult {
let commitment = hash_request::<Host<T>>(&request).0.to_vec();

if IncomingRequestAcks::<T>::contains_key(commitment.clone()) {
Err(DispatchError {
msg: "Duplicate request".to_string(),
nonce: request.nonce(),
source: request.source_chain(),
dest: request.dest_chain(),
})?
}

let (dest_chain, source_chain, nonce) =
(request.dest_chain(), request.source_chain(), request.nonce());
Pallet::<T>::mmr_push(Leaf::Request(request)).ok_or_else(|| DispatchError {
msg: "Failed to push request into mmr".to_string(),
nonce,
source: source_chain,
dest: dest_chain,
})?;
// Deposit Event
Pallet::<T>::deposit_event(Event::Request {
request_nonce: nonce,
source_chain,
dest_chain,
});

IncomingRequestAcks::<T>::insert(commitment, Receipt::Ok);
Ok(DispatchSuccess { dest_chain, source_chain, nonce })
}

/// Handle an incoming response
pub fn handle_response(&self, response: Response) -> DispatchResult {
let commitment = hash_response::<Host<T>>(&response).0.to_vec();

if IncomingResponseAcks::<T>::contains_key(commitment.clone()) {
Err(DispatchError {
msg: "Duplicate response".to_string(),
nonce: response.nonce(),
source: response.source_chain(),
dest: response.dest_chain(),
})?
}

let (dest_chain, source_chain, nonce) =
(response.dest_chain(), response.source_chain(), response.nonce());

Pallet::<T>::mmr_push(Leaf::Response(response)).ok_or_else(|| DispatchError {
msg: "Failed to push response into mmr".to_string(),
nonce,
source: source_chain,
dest: dest_chain,
})?;

Pallet::<T>::deposit_event(Event::Response {
request_nonce: nonce,
dest_chain,
source_chain,
});
IncomingResponseAcks::<T>::insert(commitment, Receipt::Ok);
Ok(DispatchSuccess { dest_chain, source_chain, nonce })
}
}
3 changes: 2 additions & 1 deletion pallet-ismp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod benchmarking;
pub mod dispatcher;
mod errors;
pub mod events;
pub mod handlers;
pub mod host;
mod mmr;
#[cfg(test)]
Expand Down Expand Up @@ -586,7 +587,7 @@ where
}

/// Insert a leaf into the mmr
pub fn mmr_push(leaf: Leaf) -> Option<NodeIndex> {
pub(crate) fn mmr_push(leaf: Leaf) -> Option<NodeIndex> {
let offchain_key = match &leaf {
Leaf::Request(req) => Pallet::<T>::request_leaf_index_offchain_key(
req.source_chain(),
Expand Down

0 comments on commit 836f90f

Please sign in to comment.