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

Commit

Permalink
contract support
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 committed Jul 10, 2023
1 parent 2ba0e54 commit 3a9c095
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ismp-testsuite/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ismp::{
ConsensusClient, ConsensusClientId, ConsensusStateId, StateCommitment, StateMachineClient,
StateMachineHeight, StateMachineId,
},
contracts::Gas,
error::Error,
host::{IsmpHost, StateMachine},
messaging::{Proof, StateCommitmentHeight},
Expand All @@ -21,7 +22,6 @@ use std::{
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use ismp::module::Gas;

#[derive(Default)]
pub struct MockClient;
Expand Down
27 changes: 27 additions & 0 deletions ismp/src/contracts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Contains ismp primitives for contract support
use alloc::vec::Vec;
use codec::{Decode, Encode};

/// A return type that indicates the gas consumed by the contract executor
#[derive(PartialEq, Eq, Debug)]
pub struct Gas {
/// Gas consumed when executing the contract call
pub gas_used: Option<u64>,
/// Gas limit passed to the contract executor
pub gas_limit: Option<u64>,
}

impl From<()> for Gas {
fn from(_: ()) -> Self {
Self { gas_used: None, gas_limit: None }
}
}

/// The contract data to provide additional metadata for the contract executor
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
pub struct ContractData {
/// Opaque bytes that would be decoded by the contract
pub data: Vec<u8>,
/// The gas limit the executor should use when executing the contract call
pub gas_limit: u64,
}
3 changes: 2 additions & 1 deletion ismp/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ where
let cb = router.module_for_id(request.to.clone())?;
let res = cb
.on_accept(request.clone())
.map(|_| DispatchSuccess {
.map(|gas| DispatchSuccess {
dest_chain: request.dest_chain,
source_chain: request.source_chain,
nonce: request.nonce,
gas,
})
.map_err(|e| DispatchError {
msg: format!("{e:?}"),
Expand Down
6 changes: 4 additions & 2 deletions ismp/src/handlers/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ where
let cb = router.module_for_id(response.destination_module())?;
let res = cb
.on_response(response.clone())
.map(|_| DispatchSuccess {
.map(|gas| DispatchSuccess {
dest_chain: response.dest_chain(),
source_chain: response.source_chain(),
nonce: response.nonce(),
gas,
})
.map_err(|e| DispatchError {
msg: format!("{e:?}"),
Expand Down Expand Up @@ -109,10 +110,11 @@ where
get: request.get_request()?,
values,
}))
.map(|_| DispatchSuccess {
.map(|gas| DispatchSuccess {
dest_chain: request.dest_chain(),
source_chain: request.source_chain(),
nonce: request.nonce(),
gas,
})
.map_err(|e| DispatchError {
msg: format!("{e:?}"),
Expand Down
6 changes: 4 additions & 2 deletions ismp/src/handlers/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ where
let cb = router.module_for_id(request.source_module())?;
let res = cb
.on_timeout(request.clone())
.map(|_| DispatchSuccess {
.map(|gas| DispatchSuccess {
dest_chain: request.dest_chain(),
source_chain: request.source_chain(),
nonce: request.nonce(),
gas,
})
.map_err(|e| DispatchError {
msg: format!("{e:?}"),
Expand Down Expand Up @@ -104,10 +105,11 @@ where
let cb = router.module_for_id(request.source_module())?;
let res = cb
.on_timeout(request.clone())
.map(|_| DispatchSuccess {
.map(|gas| DispatchSuccess {
dest_chain: request.dest_chain(),
source_chain: request.source_chain(),
nonce: request.nonce(),
gas,
})
.map_err(|e| DispatchError {
msg: format!("{e:?}"),
Expand Down
1 change: 1 addition & 0 deletions ismp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern crate alloc;
extern crate core;

pub mod consensus;
pub mod contracts;
pub mod error;
pub mod handlers;
pub mod host;
Expand Down
19 changes: 3 additions & 16 deletions ismp/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! ISMPModule definition

use crate::{
contracts::Gas,
error::Error,
host::StateMachine,
router::{Post as PostRequest, Request, Response},
Expand All @@ -31,6 +32,8 @@ pub struct DispatchSuccess {
pub source_chain: StateMachine,
/// Request nonce
pub nonce: u64,
/// Gas used by contract executor;
pub gas: Gas,
}

/// The result of unsuccessfully dispatching a request or response
Expand Down Expand Up @@ -64,19 +67,3 @@ pub trait IsmpModule {
/// sent but have now timed-out
fn on_timeout(&self, request: Request) -> Result<Gas, Error>;
}

pub struct Gas {
/// Gas consumed when executing the contract call
pub gas_used: Option<u64>,
/// Gas limit passed to the contract executor
pub gas_limit: Option<u64>
}

impl From<()> for Gas {
fn from(_: ()) -> Self {
Self {
gas_used: None,
gas_limit: None
}
}
}

0 comments on commit 3a9c095

Please sign in to comment.