Skip to content

Commit

Permalink
Merge pull request #716 from CosmWasm/add-ibc-stargate-to-vm
Browse files Browse the repository at this point in the history
Add ibc stargate to vm
  • Loading branch information
ethanfrey authored Jan 14, 2021
2 parents 24c357a + 790c046 commit f77ffcf
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 10 deletions.
16 changes: 8 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ jobs:
working_directory: ~/project/packages/vm
command: cargo build --locked
- run:
name: Build with iterator
name: Build with all features
working_directory: ~/project/packages/vm
command: cargo build --locked --features iterator
command: cargo build --locked --features iterator,staking,stargate
- run:
name: Test
working_directory: ~/project/packages/vm
command: cargo test --locked
- run:
name: Test with iterator
name: Test with all features
working_directory: ~/project/packages/vm
command: cargo test --locked --features iterator
command: cargo test --locked --features iterator,staking,stargate
- save_cache:
paths:
- /usr/local/cargo/registry
Expand All @@ -208,17 +208,17 @@ jobs:
working_directory: ~/project/packages/vm
command: cargo build --locked --features cranelift
- run:
name: Build with iterator
name: Build with all features
working_directory: ~/project/packages/vm
command: cargo build --locked --features cranelift,iterator
command: cargo build --locked --features cranelift,iterator,staking,stargate
- run:
name: Test
working_directory: ~/project/packages/vm
command: cargo test --locked --features cranelift
- run:
name: Test with iterator
name: Test with all features
working_directory: ~/project/packages/vm
command: cargo test --locked --features cranelift,iterator
command: cargo test --locked --features cranelift,iterator,staking,stargate
- save_cache:
paths:
- /usr/local/cargo/registry
Expand Down
2 changes: 2 additions & 0 deletions packages/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ backtraces = []
# we keep this optional, to allow possible future integration (or different Cosmos Backends)
iterator = ["cosmwasm-std/iterator"]
staking = ["cosmwasm-std/staking"]
# this enables all stargate-related functionality, including the ibc entry points
stargate = ["cosmwasm-std/stargate"]
# Use cranelift backend instead of singlepass. This is required for development on Windows.
cranelift = ["wasmer/cranelift"]

Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where

/// Calls a function with the given arguments.
/// The exported function must return exactly one result (an offset to the result Region).
fn call_raw<A, S, Q>(
pub(crate) fn call_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
name: &str,
args: &[&[u8]],
Expand Down
233 changes: 233 additions & 0 deletions packages/vm/src/ibc_calls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
#![cfg(feature = "stargate")]

use cosmwasm_std::{
ContractResult, Env, IbcAcknowledgement, IbcBasicResponse, IbcChannel, IbcPacket,
IbcReceiveResponse,
};
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use std::fmt;

use crate::backend::{Api, Querier, Storage};
use crate::calls::call_raw;
use crate::errors::VmResult;
use crate::instance::Instance;
use crate::serde::{from_slice, to_vec};

const MAX_LENGTH_IBC: usize = 100_000;

pub fn call_ibc_channel_open<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &Env,
channel: &IbcChannel,
) -> VmResult<ContractResult<()>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
let env = to_vec(env)?;
let channel = to_vec(channel)?;
let data = call_ibc_channel_open_raw(instance, &env, &channel)?;
let result: ContractResult<()> = from_slice(&data)?;
Ok(result)
}

pub fn call_ibc_channel_connect<A, S, Q, U>(
instance: &mut Instance<A, S, Q>,
env: &Env,
channel: &IbcChannel,
) -> VmResult<ContractResult<IbcBasicResponse<U>>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
U: DeserializeOwned + Clone + fmt::Debug + JsonSchema + PartialEq,
{
let env = to_vec(env)?;
let channel = to_vec(channel)?;
let data = call_ibc_channel_connect_raw(instance, &env, &channel)?;
let result = from_slice(&data)?;
Ok(result)
}

pub fn call_ibc_channel_close<A, S, Q, U>(
instance: &mut Instance<A, S, Q>,
env: &Env,
channel: &IbcChannel,
) -> VmResult<ContractResult<IbcBasicResponse<U>>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
U: DeserializeOwned + Clone + fmt::Debug + JsonSchema + PartialEq,
{
let env = to_vec(env)?;
let channel = to_vec(channel)?;
let data = call_ibc_channel_close_raw(instance, &env, &channel)?;
let result = from_slice(&data)?;
Ok(result)
}

pub fn call_ibc_packet_receive<A, S, Q, U>(
instance: &mut Instance<A, S, Q>,
env: &Env,
packet: &IbcPacket,
) -> VmResult<ContractResult<IbcReceiveResponse<U>>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
U: DeserializeOwned + Clone + fmt::Debug + JsonSchema + PartialEq,
{
let env = to_vec(env)?;
let packet = to_vec(packet)?;
let data = call_ibc_packet_receive_raw(instance, &env, &packet)?;
let result = from_slice(&data)?;
Ok(result)
}

pub fn call_ibc_packet_ack<A, S, Q, U>(
instance: &mut Instance<A, S, Q>,
env: &Env,
ack: &IbcAcknowledgement,
) -> VmResult<ContractResult<IbcBasicResponse<U>>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
U: DeserializeOwned + Clone + fmt::Debug + JsonSchema + PartialEq,
{
let env = to_vec(env)?;
let ack = to_vec(ack)?;
let data = call_ibc_packet_ack_raw(instance, &env, &ack)?;
let result = from_slice(&data)?;
Ok(result)
}

pub fn call_ibc_packet_timeout<A, S, Q, U>(
instance: &mut Instance<A, S, Q>,
env: &Env,
packet: &IbcPacket,
) -> VmResult<ContractResult<IbcBasicResponse<U>>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
U: DeserializeOwned + Clone + fmt::Debug + JsonSchema + PartialEq,
{
let env = to_vec(env)?;
let packet = to_vec(packet)?;
let data = call_ibc_packet_timeout_raw(instance, &env, &packet)?;
let result = from_slice(&data)?;
Ok(result)
}

pub fn call_ibc_channel_open_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &[u8],
channel: &[u8],
) -> VmResult<Vec<u8>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
instance.set_storage_readonly(false);
call_raw(
instance,
"ibc_channel_open",
&[env, channel],
MAX_LENGTH_IBC,
)
}

pub fn call_ibc_channel_connect_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &[u8],
channel: &[u8],
) -> VmResult<Vec<u8>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
instance.set_storage_readonly(false);
call_raw(
instance,
"ibc_channel_connect",
&[env, channel],
MAX_LENGTH_IBC,
)
}

pub fn call_ibc_channel_close_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &[u8],
channel: &[u8],
) -> VmResult<Vec<u8>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
instance.set_storage_readonly(false);
call_raw(
instance,
"ibc_channel_close",
&[env, channel],
MAX_LENGTH_IBC,
)
}

pub fn call_ibc_packet_receive_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &[u8],
packet: &[u8],
) -> VmResult<Vec<u8>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
instance.set_storage_readonly(false);
call_raw(
instance,
"ibc_packet_receive",
&[env, packet],
MAX_LENGTH_IBC,
)
}

pub fn call_ibc_packet_ack_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &[u8],
ack: &[u8],
) -> VmResult<Vec<u8>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
instance.set_storage_readonly(false);
call_raw(instance, "ibc_packet_ack", &[env, ack], MAX_LENGTH_IBC)
}

pub fn call_ibc_packet_timeout_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &[u8],
packet: &[u8],
) -> VmResult<Vec<u8>>
where
A: Api + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
instance.set_storage_readonly(false);
call_raw(
instance,
"ibc_packet_timeout",
&[env, packet],
MAX_LENGTH_IBC,
)
}
9 changes: 9 additions & 0 deletions packages/vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod conversion;
mod environment;
mod errors;
mod features;
#[cfg(feature = "stargate")]
mod ibc_calls;
mod imports;
mod instance;
mod limited;
Expand All @@ -32,6 +34,13 @@ pub use crate::errors::{
VmError, VmResult,
};
pub use crate::features::features_from_csv;
#[cfg(feature = "stargate")]
pub use crate::ibc_calls::{
call_ibc_channel_close, call_ibc_channel_close_raw, call_ibc_channel_connect,
call_ibc_channel_connect_raw, call_ibc_channel_open, call_ibc_channel_open_raw,
call_ibc_packet_ack, call_ibc_packet_ack_raw, call_ibc_packet_receive,
call_ibc_packet_receive_raw, call_ibc_packet_timeout, call_ibc_packet_timeout_raw,
};
pub use crate::instance::{GasReport, Instance, InstanceOptions};
pub use crate::serde::{from_slice, to_vec};
pub use crate::size::Size;
Loading

0 comments on commit f77ffcf

Please sign in to comment.