Skip to content

Commit

Permalink
TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Jan 31, 2025
1 parent 80473e1 commit a3482b4
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ staking = []
# stargate enables stargate-dependent messages and queries, like raw protobuf messages
# as well as ibc-related functionality
stargate = []
# eureka enables eureka-dependent messages and queries
# eureka enables eureka-dependent messages, entrypoints and queries
eureka = []
# This feature makes `BankQuery::Supply` available for the contract to call, but requires
# the host blockchain to run CosmWasm `1.1.0` or higher.
Expand Down
16 changes: 15 additions & 1 deletion packages/std/src/eureka.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{Binary, Timestamp};
use crate::{Addr, Binary, Timestamp};

/// Payload value should be encoded in a format defined by the channel version,
/// and the module on the other side should know how to parse this.
Expand Down Expand Up @@ -33,6 +33,20 @@ pub enum EurekaMsg {
},
}

/// The message that is passed into `eureka_packet_receive`
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct EurekaPacketReceiveMsg {
pub packet: EurekaPayload,
pub relayer: Addr,
}

impl EurekaPacketReceiveMsg {
pub fn new(packet: EurekaPayload, relayer: Addr) -> Self {
Self { packet, relayer }
}
}

#[cfg(test)]
mod tests {
use serde_json::to_string;
Expand Down
28 changes: 28 additions & 0 deletions packages/std/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,34 @@ where
Region::from_vec(v).to_heap_ptr() as u32
}

/// do_ibc_channel_close is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is a callback when a IBC channel belonging to this contract is closed
///
/// - `Q`: custom query type (see QueryRequest)
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
#[cfg(feature = "stargate")]
pub fn do_eureka_packet_receive<Q, C, E>(
contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcChannelCloseMsg) -> Result<IbcBasicResponse<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
Q: CustomQuery,
C: CustomMsg,
E: ToString,
{
install_panic_handler();
let res = _do_eureka_packet_receive(
contract_fn,
env_ptr as *mut Region<Owned>,
msg_ptr as *mut Region<Owned>,
);
let v = to_json_vec(&res).unwrap();
Region::from_vec(v).to_heap_ptr() as u32
}

fn _do_instantiate<Q, M, C, E>(
instantiate_fn: &dyn Fn(DepsMut<Q>, Env, MessageInfo, M) -> Result<Response<C>, E>,
env_ptr: *mut Region<Owned>,
Expand Down
2 changes: 2 additions & 0 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ pub use crate::exports::{
do_ibc_channel_close, do_ibc_channel_connect, do_ibc_channel_open, do_ibc_packet_ack,
do_ibc_packet_receive, do_ibc_packet_timeout,
};
#[cfg(all(feature = "eureka", target_arch = "wasm32"))]
pub use crate::exports::eureka_packet_receive;
#[cfg(target_arch = "wasm32")]
pub use crate::imports::{ExternalApi, ExternalQuerier, ExternalStorage};

Expand Down
12 changes: 12 additions & 0 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use crate::ibc::{
IbcEndpoint, IbcOrder, IbcPacket, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg,
IbcTimeoutBlock,
};
#[cfg(feature = "eureka")]
use crate::eureka::EurekaPacketReceiveMsg;
#[cfg(feature = "cosmwasm_1_1")]
use crate::query::SupplyResponse;
use crate::query::{
Expand Down Expand Up @@ -579,6 +581,16 @@ pub fn mock_ibc_packet_timeout(
Ok(IbcPacketTimeoutMsg::new(packet, Addr::unchecked("relayer")))
}

/// Creates a IbcChannelOpenMsg::OpenInit for testing ibc_channel_open.
#[cfg(feature = "eureka")]
pub fn mock_eureka_packet_receive(
my_channel_id: &str,
order: IbcOrder,
version: &str,
) -> EurekaPacketReceiveMsg {
EurekaPacketReceiveMsg::new()
}

/// The same type as cosmwasm-std's QuerierResult, but easier to reuse in
/// cosmwasm-vm. It might diverge from QuerierResult at some point.
pub type MockQuerierCustomHandlerResult = SystemResult<ContractResult<Binary>>;
Expand Down
2 changes: 2 additions & 0 deletions packages/std/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ pub use mock::{
mock_ibc_channel_connect_ack, mock_ibc_channel_connect_confirm, mock_ibc_channel_open_init,
mock_ibc_channel_open_try, mock_ibc_packet_ack, mock_ibc_packet_recv, mock_ibc_packet_timeout,
};
#[cfg(feature = "eureka")]
pub use mock::mock_eureka_packet_receive;
19 changes: 19 additions & 0 deletions packages/vm/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,25 @@ where
Ok(data)
}

#[cfg(feature = "eureka")]
pub fn call_eureka_packet_receive<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &Env,
msg: &IbcChannelOpenMsg,
) -> VmResult<ContractResult<Option<Ibc3ChannelOpenResponse>>>
where
A: BackendApi + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
let env = to_vec(env)?;
let msg = to_vec(msg)?;
let data = call_eureka_packet_receive_raw(instance, &env, &msg)?;
let result: ContractResult<Option<Ibc3ChannelOpenResponse>> =
from_slice(&data, deserialization_limits::RESULT_IBC_CHANNEL_OPEN)?;
Ok(result)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions packages/vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub use crate::calls::{
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,
};
#[cfg(feature = "eureka")]
pub use crate::calls::call_eureka_packet_receive;
pub use crate::capabilities::capabilities_from_csv;
pub use crate::config::{CacheOptions, Config, WasmLimits};
pub use crate::errors::{
Expand Down
2 changes: 2 additions & 0 deletions packages/vm/src/static_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Entrypoint {
IbcPacketAck,
#[strum(serialize = "ibc_packet_timeout")]
IbcPacketTimeout,
#[strum(serialize = "eureka_packet_receive")]
EurekaPacketReceive,
}

// sort entrypoints by their &str representation
Expand Down
2 changes: 2 additions & 0 deletions packages/vm/src/testing/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use crate::calls::{
call_ibc_channel_close, call_ibc_channel_connect, call_ibc_channel_open, call_ibc_packet_ack,
call_ibc_packet_receive, call_ibc_packet_timeout,
};
#[cfg(feature = "eureka")]
use crate::calls::call_eureka_packet_receive;
use crate::instance::Instance;
use crate::serde::to_vec;
use crate::{BackendApi, Querier, Storage};
Expand Down

0 comments on commit a3482b4

Please sign in to comment.