Skip to content

Commit

Permalink
Add callback entrypoint function to vm
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Feb 19, 2024
1 parent 297b28b commit 51a7a9e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
49 changes: 48 additions & 1 deletion packages/vm/src/calls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use serde::de::DeserializeOwned;
use wasmer::Value;

use cosmwasm_std::{ContractResult, CustomMsg, Env, MessageInfo, QueryResponse, Reply, Response};
use cosmwasm_std::{
ContractResult, CustomMsg, Env, IbcSourceChainCallbackMsg, MessageInfo, QueryResponse, Reply,
Response,
};
#[cfg(feature = "stargate")]
use cosmwasm_std::{
Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg,
Expand Down Expand Up @@ -54,6 +57,8 @@ mod read_limits {
/// Max length (in bytes) of the result data from a ibc_packet_timeout call.
#[cfg(feature = "stargate")]
pub const RESULT_IBC_PACKET_TIMEOUT: usize = 64 * MI;
/// Max length (in bytes) of the result data from a ibc_source_chain_callback call.
pub const RESULT_IBC_SOURCE_CHAIN_CALLBACK: usize = 64 * MI;
}

/// The limits for the JSON deserialization.
Expand Down Expand Up @@ -93,6 +98,8 @@ mod deserialization_limits {
/// Max length (in bytes) of the result data from a ibc_packet_timeout call.
#[cfg(feature = "stargate")]
pub const RESULT_IBC_PACKET_TIMEOUT: usize = 256 * KI;
/// Max length (in bytes) of the result data from a ibc_source_chain_callback call.
pub const RESULT_IBC_SOURCE_CHAIN_CALLBACK: usize = 256 * KI;
}

pub fn call_instantiate<A, S, Q, U>(
Expand Down Expand Up @@ -327,6 +334,27 @@ where
Ok(result)
}

pub fn call_ibc_source_chain_callback<A, S, Q, U>(
instance: &mut Instance<A, S, Q>,
env: &Env,
msg: &IbcSourceChainCallbackMsg,
) -> VmResult<ContractResult<IbcBasicResponse<U>>>
where
A: BackendApi + 'static,
S: Storage + 'static,
Q: Querier + 'static,
U: DeserializeOwned + CustomMsg,
{
let env = to_vec(env)?;
let msg = to_vec(msg)?;
let data = call_ibc_source_chain_callback_raw(instance, &env, &msg)?;
let result = from_slice(
&data,
deserialization_limits::RESULT_IBC_SOURCE_CHAIN_CALLBACK,
)?;
Ok(result)
}

/// Calls Wasm export "instantiate" and returns raw data from the contract.
/// The result is length limited to prevent abuse but otherwise unchecked.
pub fn call_instantiate_raw<A, S, Q>(
Expand Down Expand Up @@ -560,6 +588,25 @@ where
)
}

pub fn call_ibc_source_chain_callback_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &[u8],
msg: &[u8],
) -> VmResult<Vec<u8>>
where
A: BackendApi + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
instance.set_storage_readonly(false);
call_raw(
instance,
"ibc_source_chain_callback",
&[env, msg],
read_limits::RESULT_IBC_SOURCE_CHAIN_CALLBACK,
)
}

/// Calls a function with the given arguments.
/// The exported function must return exactly one result (an offset to the result Region).
pub(crate) fn call_raw<A, S, Q>(
Expand Down
3 changes: 2 additions & 1 deletion packages/vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub use crate::backend::{
};
pub use crate::cache::{AnalysisReport, Cache, CacheOptions, Metrics, Stats};
pub use crate::calls::{
call_execute, call_execute_raw, call_instantiate, call_instantiate_raw, call_migrate,
call_execute, call_execute_raw, call_ibc_source_chain_callback,
call_ibc_source_chain_callback_raw, call_instantiate, call_instantiate_raw, call_migrate,
call_migrate_raw, call_query, call_query_raw, call_reply, call_reply_raw, call_sudo,
call_sudo_raw,
};
Expand Down

0 comments on commit 51a7a9e

Please sign in to comment.