Skip to content

Commit

Permalink
StateMachine (#28)
Browse files Browse the repository at this point in the history
* use strings

* no_std

* cargo fmt

* clippy
  • Loading branch information
seunlanlege authored Apr 19, 2023
1 parent 668e58f commit df0438f
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 48 deletions.
8 changes: 6 additions & 2 deletions src/consensus_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
//! Consensus client definitions

use crate::{
error::Error, host::ISMPHost, messaging::Proof, prelude::Vec, router::RequestResponse,
error::Error,
host::{ISMPHost, StateMachine},
messaging::Proof,
prelude::Vec,
router::RequestResponse,
};
use codec::{Decode, Encode};
use core::time::Duration;
Expand Down Expand Up @@ -57,7 +61,7 @@ pub struct IntermediateState {
)]
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub struct StateMachineId {
pub state_id: u64,
pub state_id: StateMachine,
pub consensus_client: ConsensusClientId,
}

Expand Down
22 changes: 11 additions & 11 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use crate::{
consensus_client::{ConsensusClientId, StateMachineHeight},
host::ChainID,
host::StateMachine,
};
use alloc::string::String;
use core::time::Duration;
Expand Down Expand Up @@ -46,30 +46,30 @@ pub enum Error {
},
RequestCommitmentNotFound {
nonce: u64,
source: ChainID,
dest: ChainID,
source: StateMachine,
dest: StateMachine,
},
RequestVerificationFailed {
nonce: u64,
source: ChainID,
dest: ChainID,
source: StateMachine,
dest: StateMachine,
},
RequestTimeoutNotElapsed {
nonce: u64,
source: ChainID,
dest: ChainID,
source: StateMachine,
dest: StateMachine,
timeout_timestamp: Duration,
state_machine_time: Duration,
},
RequestTimeoutVerificationFailed {
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 Down
6 changes: 3 additions & 3 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::{
consensus_client::{ConsensusClient, ConsensusClientId, StateMachineHeight},
error::Error,
host::{ChainID, ISMPHost},
host::{ISMPHost, StateMachine},
messaging::{Message, Proof},
};
use alloc::{boxed::Box, collections::BTreeSet};
Expand All @@ -42,9 +42,9 @@ pub struct ConsensusClientCreatedResult {

pub struct RequestResponseResult {
/// Destination chain for request or response
pub dest_chain: ChainID,
pub dest_chain: StateMachine,
/// Source chain for request or response
pub source_chain: ChainID,
pub source_chain: StateMachine,
/// Request nonce
pub nonce: u64,
}
Expand Down
85 changes: 69 additions & 16 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ use crate::{
prelude::Vec,
router::{ISMPRouter, Request},
};
use alloc::boxed::Box;
use alloc::{
boxed::Box,
format,
string::{String, ToString},
};
use codec::{Decode, Encode};
use core::time::Duration;
use derive_more::Display;
use core::{str::FromStr, time::Duration};
use primitive_types::H256;

pub trait ISMPHost {
fn host(&self) -> ChainID;
fn host(&self) -> StateMachineId;

// Storage Read functions

Expand Down Expand Up @@ -101,23 +104,73 @@ pub trait ISMPHost {
fn ismp_router(&self) -> Box<dyn ISMPRouter>;
}

#[derive(Clone, Debug, Copy, Encode, Decode, Display, PartialEq, Eq, scale_info::TypeInfo)]
/// Currently supported state machines.
#[derive(
Clone, Debug, Copy, Encode, Decode, PartialOrd, Ord, PartialEq, Eq, scale_info::TypeInfo,
)]
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub enum ChainID {
pub enum StateMachine {
/// Ethereum Execution layer
#[codec(index = 0)]
ETHEREUM = 0,
Ethereum,
/// Arbitrum Optimistic L2
#[codec(index = 1)]
GNOSIS = 1,
Arbitrum,
/// Optimism Optimistic L2
#[codec(index = 2)]
ARBITRUM = 2,
Optimism,
/// Base Optimistic L2
#[codec(index = 3)]
OPTIMISM = 3,
Base,
/// Polkadot parachains
#[codec(index = 4)]
BASE = 4,
Polkadot(u32),
/// Kusama parachains
#[codec(index = 5)]
MOONBEAM = 5,
#[codec(index = 6)]
ASTAR = 6,
#[codec(index = 7)]
HYPERSPACE = 7,
Kusama(u32),
}

impl ToString for StateMachine {
fn to_string(&self) -> String {
match self {
StateMachine::Ethereum => "ETHEREUM".to_string(),
StateMachine::Arbitrum => "ARBITRUM".to_string(),
StateMachine::Optimism => "OPTIMISM".to_string(),
StateMachine::Base => "BASE".to_string(),
StateMachine::Polkadot(id) => format!("POLKADOT-{id}"),
StateMachine::Kusama(id) => format!("KUSAMA-{id}"),
}
}
}

impl FromStr for StateMachine {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = match s {
"ETHEREUM" => StateMachine::Ethereum,
"ARBITRUM" => StateMachine::Arbitrum,
"OPTIMISM" => StateMachine::Optimism,
"BASE" => StateMachine::Base,
name if name.starts_with("POLKADOT-") => {
let id = name
.split('-')
.last()
.and_then(|id| u32::from_str(id).ok())
.ok_or_else(|| format!("invalid state machine: {name}"))?;
StateMachine::Polkadot(id)
}
name if name.starts_with("KUSAMA-") => {
let id = name
.split('-')
.last()
.and_then(|id| u32::from_str(id).ok())
.ok_or_else(|| format!("invalid state machine: {name}"))?;
StateMachine::Kusama(id)
}
name => Err(format!("Unkown state machine: {name}"))?,
};

Ok(s)
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
extern crate core;

pub mod consensus_client;
pub mod error;
pub mod handlers;
Expand Down
14 changes: 7 additions & 7 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

//! ISMPRouter definition

use crate::{consensus_client::StateMachineHeight, error::Error, host::ChainID, prelude::Vec};
use crate::{consensus_client::StateMachineHeight, error::Error, host::StateMachine, prelude::Vec};
use codec::{Decode, Encode};
use core::time::Duration;

Expand All @@ -24,9 +24,9 @@ use core::time::Duration;
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub struct Post {
/// The source state machine of this request.
pub source_chain: ChainID,
pub source_chain: StateMachine,
/// The destination state machine of this request.
pub dest_chain: ChainID,
pub dest_chain: StateMachine,
/// The nonce of this request on the source chain
pub nonce: u64,
/// Moudle Id of the sending module
Expand All @@ -44,9 +44,9 @@ pub struct Post {
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub struct Get {
/// The source state machine of this request.
pub source_chain: ChainID,
pub source_chain: StateMachine,
/// The destination state machine of this request.
pub dest_chain: ChainID,
pub dest_chain: StateMachine,
/// The nonce of this request on the source chain
pub nonce: u64,
/// Moudle Id of the sending module
Expand All @@ -73,15 +73,15 @@ pub enum Request {

impl Request {
/// Get the source chain
pub fn source_chain(&self) -> ChainID {
pub fn source_chain(&self) -> StateMachine {
match self {
Request::Get(get) => get.source_chain,
Request::Post(post) => post.source_chain,
}
}

/// Get the destination chain
pub fn dest_chain(&self) -> ChainID {
pub fn dest_chain(&self) -> StateMachine {
match self {
Request::Get(get) => get.dest_chain,
Request::Post(post) => post.dest_chain,
Expand Down
18 changes: 9 additions & 9 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
host::ISMPHost,
router::{Request, Response},
};
use alloc::vec::Vec;
use alloc::{string::ToString, vec::Vec};
use primitive_types::H256;

/// Return the keccak256 hash of a request
Expand All @@ -16,12 +16,12 @@ pub fn hash_request<H: ISMPHost>(req: &Request) -> H256 {

let mut buf = Vec::new();

let source_chain = (req.source_chain as u8).to_be_bytes();
let dest_chain = (req.dest_chain as u8).to_be_bytes();
let source_chain = req.source_chain.to_string();
let dest_chain = req.dest_chain.to_string();
let nonce = req.nonce.to_be_bytes();
let timestamp = req.timeout_timestamp.to_be_bytes();
buf.extend_from_slice(&source_chain);
buf.extend_from_slice(&dest_chain);
buf.extend_from_slice(source_chain.as_bytes());
buf.extend_from_slice(dest_chain.as_bytes());
buf.extend_from_slice(&nonce);
buf.extend_from_slice(&timestamp);
buf.extend_from_slice(&req.data);
Expand All @@ -37,12 +37,12 @@ pub fn hash_response<H: ISMPHost>(res: &Response) -> H256 {
_ => unimplemented!(),
};
let mut buf = Vec::new();
let source_chain = (req.source_chain as u8).to_be_bytes();
let dest_chain = (req.dest_chain as u8).to_be_bytes();
let source_chain = req.source_chain.to_string();
let dest_chain = req.dest_chain.to_string();
let nonce = req.nonce.to_be_bytes();
let timestamp = req.timeout_timestamp.to_be_bytes();
buf.extend_from_slice(&source_chain);
buf.extend_from_slice(&dest_chain);
buf.extend_from_slice(source_chain.as_bytes());
buf.extend_from_slice(dest_chain.as_bytes());
buf.extend_from_slice(&nonce);
buf.extend_from_slice(&timestamp);
buf.extend_from_slice(&req.data);
Expand Down

0 comments on commit df0438f

Please sign in to comment.