-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update ismp implementation * chore * fix faulty host implementation * fix no-std * updated failing tests * some changes --------- Co-authored-by: Seun Lanlege <[email protected]>
- Loading branch information
1 parent
ad946c0
commit 3d68655
Showing
13 changed files
with
446 additions
and
478 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (C) 2023 Polytope Labs. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Implementation for the ISMP Router | ||
use crate::{host::Host, Config, Event, OutgoingRequestAcks, OutgoingResponseAcks, Pallet}; | ||
use alloc::string::ToString; | ||
use codec::{Decode, Encode}; | ||
use core::marker::PhantomData; | ||
use ismp_primitives::mmr::Leaf; | ||
use ismp_rs::{ | ||
host::IsmpHost, | ||
router::{ | ||
DispatchError, DispatchRequest, DispatchResult, DispatchSuccess, Get, IsmpDispatcher, Post, | ||
PostResponse, Request, Response, | ||
}, | ||
util::{hash_request, hash_response}, | ||
}; | ||
use sp_core::H256; | ||
|
||
/// A receipt or an outgoing or incoming request or response | ||
#[derive(Encode, Decode, scale_info::TypeInfo)] | ||
pub enum Receipt { | ||
/// Ok | ||
Ok, | ||
} | ||
|
||
/// The dispatcher commits outgoing requests and responses to the mmr | ||
pub struct Dispatcher<T>(PhantomData<T>); | ||
|
||
impl<T> Default for Dispatcher<T> { | ||
fn default() -> Self { | ||
Self(PhantomData) | ||
} | ||
} | ||
|
||
impl<T> IsmpDispatcher for Dispatcher<T> | ||
where | ||
T: Config, | ||
<T as frame_system::Config>::Hash: From<H256>, | ||
{ | ||
fn dispatch_request(&self, request: DispatchRequest) -> DispatchResult { | ||
let host = Host::<T>::default(); | ||
let request = match request { | ||
DispatchRequest::Get(dispatch_get) => { | ||
let get = Get { | ||
source_chain: host.host_state_machine(), | ||
dest_chain: dispatch_get.dest_chain, | ||
nonce: host.next_nonce(), | ||
from: dispatch_get.from, | ||
keys: dispatch_get.keys, | ||
height: dispatch_get.height, | ||
timeout_timestamp: dispatch_get.timeout_timestamp, | ||
}; | ||
Request::Get(get) | ||
} | ||
DispatchRequest::Post(dispatch_post) => { | ||
let post = Post { | ||
source_chain: host.host_state_machine(), | ||
dest_chain: dispatch_post.dest_chain, | ||
nonce: host.next_nonce(), | ||
from: dispatch_post.from, | ||
to: dispatch_post.to, | ||
timeout_timestamp: dispatch_post.timeout_timestamp, | ||
data: dispatch_post.data, | ||
}; | ||
Request::Post(post) | ||
} | ||
}; | ||
|
||
let commitment = hash_request::<Host<T>>(&request).0.to_vec(); | ||
|
||
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, | ||
}); | ||
// We need this step since it's not trivial to check the mmr for commitments on chain | ||
OutgoingRequestAcks::<T>::insert(commitment, Receipt::Ok); | ||
Ok(DispatchSuccess { dest_chain, source_chain, nonce }) | ||
} | ||
|
||
fn dispatch_response(&self, response: PostResponse) -> DispatchResult { | ||
let response = Response::Post(response); | ||
|
||
let commitment = hash_response::<Host<T>>(&response).0.to_vec(); | ||
|
||
if OutgoingResponseAcks::<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, | ||
}); | ||
OutgoingResponseAcks::<T>::insert(commitment, Receipt::Ok); | ||
Ok(DispatchSuccess { dest_chain, source_chain, nonce }) | ||
} | ||
} |
Oops, something went wrong.