Skip to content

Commit

Permalink
rework apis (#27)
Browse files Browse the repository at this point in the history
* rework apis

* cargo fmt

* Get<StateMachine>

* don't clone proxy router

* ismp-parachain

* use pallet_ismp::host::Host

* import boxed

* cargo fmt
  • Loading branch information
seunlanlege authored Apr 19, 2023
1 parent 0569c24 commit fbb0eaa
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 75 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build-test-and-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
pull_request:
branches: [ main ]

concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

jobs:
build_and_test:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -43,7 +47,7 @@ jobs:
- name: Build `no-std`
run: |
cargo +nightly check -p pallet-ismp --no-default-features --target=wasm32-unknown-unknown --verbose
cargo +nightly check -p ismp-parachain-consensus --no-default-features --target=wasm32-unknown-unknown --verbose
cargo +nightly check -p ismp-parachain --no-default-features --target=wasm32-unknown-unknown --verbose
- name: Run tests
run: |
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion pallet-ismp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = ["Polytope Labs <[email protected]>"]
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false, features = ["disable_panic_handler"] }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions pallet-ismp/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use ismp_rs::{
consensus_client::ConsensusClientId,
host::ChainID,
host::StateMachine,
router::{Request, Response},
};
use pallet_ismp::primitives::{Error, Proof};
Expand All @@ -15,8 +15,8 @@ use sp_std::vec::Vec;
#[derive(codec::Encode, codec::Decode)]
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub struct LeafIndexQuery {
pub source_chain: ChainID,
pub dest_chain: ChainID,
pub source_chain: StateMachine,
pub dest_chain: StateMachine,
pub nonce: u64,
}

Expand Down
22 changes: 11 additions & 11 deletions pallet-ismp/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use codec::{Decode, Encode};
use ismp_rs::{
consensus_client::{ConsensusClientId, StateMachineHeight},
error::Error as IsmpError,
host::ChainID,
host::StateMachine,
};
use sp_std::prelude::*;

Expand All @@ -28,18 +28,18 @@ pub enum HandlingError {
},
RequestCommitmentNotFound {
nonce: u64,
source: ChainID,
dest: ChainID,
source: StateMachine,
dest: StateMachine,
},
RequestVerificationFailed {
nonce: u64,
source: ChainID,
dest: ChainID,
source: StateMachine,
dest: StateMachine,
},
ResponseVerificationFailed {
nonce: u64,
source: ChainID,
dest: ChainID,
source: StateMachine,
dest: StateMachine,
},
ConsensusProofVerificationFailed {
id: ConsensusClientId,
Expand All @@ -65,15 +65,15 @@ pub enum HandlingError {
},
RequestTimeoutNotElapsed {
nonce: u64,
source: ChainID,
dest: ChainID,
source: StateMachine,
dest: StateMachine,
timeout_timestamp: u64,
state_machine_time: u64,
},
RequestTimeoutVerificationFailed {
nonce: u64,
source: ChainID,
dest: ChainID,
source: StateMachine,
dest: StateMachine,
},
}

Expand Down
10 changes: 5 additions & 5 deletions pallet-ismp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Config, Event as PalletEvent};
use alloc::collections::BTreeSet;
use ismp_rs::{
consensus_client::{ConsensusClientId, StateMachineHeight, StateMachineId},
host::ChainID,
host::StateMachine,
};

#[derive(codec::Encode, codec::Decode)]
Expand All @@ -22,17 +22,17 @@ pub enum Event {

Response {
/// Chain that this response will be routed to
dest_chain: ChainID,
dest_chain: StateMachine,
/// Source Chain for this response
source_chain: ChainID,
source_chain: StateMachine,
/// Nonce for the request which this response is for
request_nonce: u64,
},
Request {
/// Chain that this request will be routed to
dest_chain: ChainID,
dest_chain: StateMachine,
/// Source Chain for request
source_chain: ChainID,
source_chain: StateMachine,
/// Request nonce
request_nonce: u64,
},
Expand Down
32 changes: 16 additions & 16 deletions pallet-ismp/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use crate::{
};
use alloc::{format, string::ToString};
use core::time::Duration;
use frame_support::traits::UnixTime;
use frame_support::traits::{Get, UnixTime};
use ismp_rs::{
consensus_client::{
ConsensusClient, ConsensusClientId, StateCommitment, StateMachineHeight, StateMachineId,
},
error::Error,
host::{ChainID, ISMPHost},
host::{ISMPHost, StateMachine},
router::{ISMPRouter, Request},
util::hash_request,
};
Expand All @@ -31,8 +31,8 @@ impl<T: Config> ISMPHost for Host<T>
where
<T as frame_system::Config>::Hash: From<H256>,
{
fn host(&self) -> ChainID {
<T as Config>::CHAIN_ID
fn host_state_machine(&self) -> StateMachine {
T::StateMachine::get()
}

fn latest_commitment_height(&self, id: StateMachineId) -> Result<StateMachineHeight, Error> {
Expand Down Expand Up @@ -116,27 +116,27 @@ where
Ok(())
}

fn consensus_client(&self, id: ConsensusClientId) -> Result<Box<dyn ConsensusClient>, Error> {
<T as Config>::ConsensusClientProvider::consensus_client(id)
}

fn challenge_period(&self, id: ConsensusClientId) -> Duration {
<T as Config>::ConsensusClientProvider::challenge_period(id)
}

fn ismp_router(&self) -> Box<dyn ISMPRouter> {
Box::new(T::IsmpRouter::default())
}

fn store_latest_commitment_height(&self, height: StateMachineHeight) -> Result<(), Error> {
LatestStateMachineHeight::<T>::insert(height.id, height.height);
Ok(())
}

fn consensus_client(&self, id: ConsensusClientId) -> Result<Box<dyn ConsensusClient>, Error> {
<T as Config>::ConsensusClientProvider::consensus_client(id)
}

fn keccak256(bytes: &[u8]) -> H256
where
Self: Sized,
{
sp_io::hashing::keccak_256(bytes).into()
}

fn challenge_period(&self, id: ConsensusClientId) -> Duration {
<T as Config>::ConsensusClientProvider::challenge_period(id)
}

fn ismp_router(&self) -> Box<dyn ISMPRouter> {
Box::new(T::IsmpRouter::default())
}
}
31 changes: 18 additions & 13 deletions pallet-ismp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::host::Host;
use codec::{Decode, Encode};
use frame_support::{log::debug, RuntimeDebug};
use ismp_rs::{
host::ChainID,
host::StateMachine,
router::{Request, Response},
};
use sp_core::{offchain::StorageKind, H256};
Expand Down Expand Up @@ -60,7 +60,7 @@ pub mod pallet {
ConsensusClientId, StateCommitment, StateMachineHeight, StateMachineId,
},
handlers::{handle_incoming_message, MessageResult},
host::ChainID,
host::StateMachine,
messaging::Message,
router::ISMPRouter,
};
Expand All @@ -85,9 +85,14 @@ pub mod pallet {
/// Each node is stored in the Off-chain DB under key derived from the
/// [`Self::INDEXING_PREFIX`] and its in-tree index (MMR position).
const INDEXING_PREFIX: &'static [u8];

/// Admin origin for privileged actions
type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;

const CHAIN_ID: ChainID;
/// Host state machine identifier
type StateMachine: Get<StateMachine>;

/// Timestamp provider
type TimeProvider: UnixTime;

/// Configurable router that dispatches calls to modules
Expand Down Expand Up @@ -329,18 +334,18 @@ pub mod pallet {
/// Response was process successfully
Response {
/// Chain that this response will be routed to
dest_chain: ChainID,
dest_chain: StateMachine,
/// Source Chain for this response
source_chain: ChainID,
source_chain: StateMachine,
/// Nonce for the request which this response is for
request_nonce: u64,
},
/// Request processed successfully
Request {
/// Chain that this request will be routed to
dest_chain: ChainID,
dest_chain: StateMachine,
/// Source Chain for request
source_chain: ChainID,
source_chain: StateMachine,
/// Request nonce
request_nonce: u64,
},
Expand Down Expand Up @@ -412,16 +417,16 @@ pub struct RequestResponseLog<T: Config> {

impl<T: Config> Pallet<T> {
pub fn request_leaf_index_offchain_key(
source_chain: ChainID,
dest_chain: ChainID,
source_chain: StateMachine,
dest_chain: StateMachine,
nonce: u64,
) -> Vec<u8> {
(T::INDEXING_PREFIX, "Requests/leaf_indices", source_chain, dest_chain, nonce).encode()
}

pub fn response_leaf_index_offchain_key(
source_chain: ChainID,
dest_chain: ChainID,
source_chain: StateMachine,
dest_chain: StateMachine,
nonce: u64,
) -> Vec<u8> {
(T::INDEXING_PREFIX, "Responses/leaf_indices", source_chain, dest_chain, nonce).encode()
Expand Down Expand Up @@ -462,8 +467,8 @@ impl<T: Config> Pallet<T> {
}

pub fn get_leaf_index(
source_chain: ChainID,
dest_chain: ChainID,
source_chain: StateMachine,
dest_chain: StateMachine,
nonce: u64,
is_req: bool,
) -> Option<LeafIndex> {
Expand Down
25 changes: 13 additions & 12 deletions pallet-ismp/src/router.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{host::Host, mmr, mmr::mmr::Mmr, Config, Event, Pallet, RequestAcks, ResponseAcks};
use alloc::{format, string::ToString};
use alloc::{boxed::Box, format, string::ToString};
use codec::{Decode, Encode};
use core::marker::PhantomData;
use ismp_primitives::mmr::Leaf;
Expand All @@ -18,35 +18,36 @@ pub enum Receipt {

/// The proxy router, This router allows for routing requests & responses from a source chain
/// to a destination chain.
#[derive(Clone)]
pub struct ProxyRouter<T, R> {
inner: Option<R>,
pub struct ProxyRouter<T> {
inner: Option<Box<dyn ISMPRouter>>,
_phantom: PhantomData<T>,
}

impl<T, R> ProxyRouter<T, R> {
impl<T> ProxyRouter<T> {
/// Initialize the proxy router with an inner router.
pub fn new(router: R) -> Self {
Self { inner: Some(router), _phantom: PhantomData }
pub fn new<R>(router: R) -> Self
where
R: ISMPRouter + 'static,
{
Self { inner: Some(Box::new(router)), _phantom: PhantomData }
}
}

impl<T, R> Default for ProxyRouter<T, R> {
impl<T> Default for ProxyRouter<T> {
fn default() -> Self {
Self { inner: None, _phantom: PhantomData }
}
}

impl<T, R> ISMPRouter for ProxyRouter<T, R>
impl<T> ISMPRouter for ProxyRouter<T>
where
T: Config,
R: ISMPRouter,
<T as frame_system::Config>::Hash: From<H256>,
{
fn dispatch(&self, request: Request) -> Result<(), Error> {
let host = Host::<T>::default();

if host.host() != request.dest_chain() {
if host.host_state_machine() != request.dest_chain() {
let commitment = hash_request::<Host<T>>(&request).0.to_vec();

if RequestAcks::<T>::contains_key(commitment.clone()) {
Expand Down Expand Up @@ -90,7 +91,7 @@ where
fn write_response(&self, response: Response) -> Result<(), Error> {
let host = Host::<T>::default();

if host.host() != response.request.source_chain() {
if host.host_state_machine() != response.request.source_chain() {
let commitment = hash_response::<Host<T>>(&response).0.to_vec();

if ResponseAcks::<T>::contains_key(commitment.clone()) {
Expand Down
4 changes: 3 additions & 1 deletion parachain-consensus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "ismp-parachain-consensus"
name = "ismp-parachain"
version = "0.1.0"
edition = "2021"
authors = ["Polytope Labs <[email protected]>"]
Expand Down Expand Up @@ -32,6 +32,7 @@ cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", branc

# local
ismp-primitives = { path = "../pallet-ismp/primitives", default-features = false }
pallet-ismp = { path = "../pallet-ismp", default-features = false }

[dev-dependencies]

Expand All @@ -52,4 +53,5 @@ std = [
"sp-io/std",
"primitive-types/std",
"ismp-primitives/std",
"pallet-ismp/std",
]
Loading

0 comments on commit fbb0eaa

Please sign in to comment.