From 51a7a9e526e94b547bb0044b10f5fd1a7dcb2c84 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Fri, 16 Feb 2024 13:10:08 +0100 Subject: [PATCH] Add callback entrypoint function to vm --- packages/vm/src/calls.rs | 49 +++++++++++++++++++++++++++++++++++++++- packages/vm/src/lib.rs | 3 ++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/packages/vm/src/calls.rs b/packages/vm/src/calls.rs index 1b9fd3f1a6..c1402504b4 100644 --- a/packages/vm/src/calls.rs +++ b/packages/vm/src/calls.rs @@ -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, @@ -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. @@ -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( @@ -327,6 +334,27 @@ where Ok(result) } +pub fn call_ibc_source_chain_callback( + instance: &mut Instance, + env: &Env, + msg: &IbcSourceChainCallbackMsg, +) -> VmResult>> +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( @@ -560,6 +588,25 @@ where ) } +pub fn call_ibc_source_chain_callback_raw( + instance: &mut Instance, + env: &[u8], + msg: &[u8], +) -> VmResult> +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( diff --git a/packages/vm/src/lib.rs b/packages/vm/src/lib.rs index 8a3ae48b1e..7467b1d62c 100644 --- a/packages/vm/src/lib.rs +++ b/packages/vm/src/lib.rs @@ -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, };