Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cosmwasm: cw multi test wormchain receiver ibc tests #97

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,659 changes: 935 additions & 724 deletions cosmwasm/Cargo.lock

Large diffs are not rendered by default.

23 changes: 20 additions & 3 deletions cosmwasm/contracts/wormchain-ibc-receiver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,31 @@ crate-type = ["cdylib", "rlib"]
[features]
backtraces = ["cosmwasm-std/backtraces"]
library = []
test-interface = []

[dependencies]
cosmwasm-std = { version = "1.0.0", features = ["ibc3"] }
cosmwasm-schema = "1"
cw-storage-plus = "0.13.2"
cosmwasm-std = { version = "1.5.8", features = ["ibc3"] }
cosmwasm-schema = "1.5.8"
cw-storage-plus = "1.2.0"
anyhow = "1"
semver = "1.0.16"
thiserror = "1.0.31"
wormhole-bindings = "0.1.0"
wormhole-sdk = { workspace = true, features = ["schemars"] }
serde_wormhole.workspace = true

[dev-dependencies]
abstract-cw-multi-test = "1.0.1"
serde-json-wasm = "0.4"
wormhole-bindings = { version = "0.1.0", features = ["fake"] }
serde = { version = "1.0.137", default-features = false, features = ["derive"] }

hex = "0.4.3"
wormhole-ibc = { path = "../wormhole-ibc", features = ["test-interface"] }
wormhole-cosmwasm = { version = "0.1.0", default-features = false, features = [
"library",
"full",
] }
wormchain-ibc-receiver = { path = "../wormchain-ibc-receiver", features = [
"test-interface",
] }
6 changes: 3 additions & 3 deletions cosmwasm/contracts/wormchain-ibc-receiver/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::ContractError;
use crate::msg::{AllChannelChainsResponse, ChannelChainResponse, ExecuteMsg, QueryMsg};
use crate::state::{CHANNEL_CHAIN, VAA_ARCHIVE};
use anyhow::{bail, ensure, Context};
use cosmwasm_std::{entry_point, to_binary, Binary, Deps, Empty, Event, StdResult};
use cosmwasm_std::{entry_point, to_json_binary, Binary, Deps, Empty, Event, StdResult};
use cosmwasm_std::{DepsMut, Env, MessageInfo, Order, Response};
use serde_wormhole::RawMessage;
use std::str;
Expand Down Expand Up @@ -132,10 +132,10 @@ fn handle_vaa(deps: DepsMut<WormholeQuery>, vaa: Binary) -> anyhow::Result<Event
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::ChannelChain { channel_id } => {
query_channel_chain(deps, channel_id).and_then(|resp| to_binary(&resp))
query_channel_chain(deps, channel_id).and_then(|resp| to_json_binary(&resp))
}
QueryMsg::AllChannelChains {} => {
query_all_channel_chains(deps).and_then(|resp| to_binary(&resp))
query_all_channel_chains(deps).and_then(|resp| to_json_binary(&resp))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions cosmwasm/contracts/wormchain-ibc-receiver/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{bail, ensure};
use cosmwasm_std::{
entry_point, from_slice, to_binary, Attribute, Binary, ContractResult, DepsMut, Env,
entry_point, from_json, to_json_binary, Attribute, Binary, ContractResult, DepsMut, Env,
Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg,
IbcChannelOpenMsg, IbcChannelOpenResponse, IbcPacketAckMsg, IbcPacketReceiveMsg,
IbcPacketTimeoutMsg, IbcReceiveResponse, StdError, StdResult,
Expand Down Expand Up @@ -91,7 +91,7 @@ fn handle_packet_receive(msg: IbcPacketReceiveMsg) -> Result<IbcReceiveResponse,
let packet = msg.packet;
// which local channel did this packet come on
let channel_id = packet.dest.channel_id;
let wormhole_msg: WormholeIbcPacketMsg = from_slice(&packet.data)?;
let wormhole_msg: WormholeIbcPacketMsg = from_json(&packet.data)?;
match wormhole_msg {
WormholeIbcPacketMsg::Publish { msg: publish_attrs } => {
receive_publish(channel_id, publish_attrs)
Expand Down Expand Up @@ -135,7 +135,7 @@ fn receive_publish(
}

// send the ack and emit the message with the attributes from the wormhole message
let acknowledgement = to_binary(&ContractResult::<()>::Ok(()))?;
let acknowledgement = to_json_binary(&ContractResult::<()>::Ok(()))?;
Ok(IbcReceiveResponse::new()
.set_ack(acknowledgement)
.add_attribute("action", "receive_publish")
Expand All @@ -146,7 +146,7 @@ fn receive_publish(
// this encode an error or error message into a proper acknowledgement to the recevier
fn encode_ibc_error(msg: impl Into<String>) -> Binary {
// this cannot error, unwrap to keep the interface simple
to_binary(&ContractResult::<()>::Err(msg.into())).unwrap()
to_json_binary(&ContractResult::<()>::Err(msg.into())).unwrap()
}

/// 5. Acknowledging a packet. Called when the other chain successfully receives a packet from us.
Expand Down
96 changes: 96 additions & 0 deletions cosmwasm/contracts/wormchain-ibc-receiver/src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use crate::{
contract::{execute, instantiate, query},
ibc::{
ibc_channel_close, ibc_channel_connect, ibc_channel_open, ibc_packet_ack,
ibc_packet_receive, ibc_packet_timeout,
},
};
use abstract_cw_multi_test::{
ibc::IbcSimpleModule, no_init, App, AppBuilder, BankKeeper, ContractWrapper,
DistributionKeeper, Executor, MockAddressGenerator, MockApiBech32, StakeKeeper, WasmKeeper,
};
use cosmwasm_std::{Addr, Empty, MemoryStorage, StdError};
use wormhole_bindings::{fake::WormholeKeeper, WormholeQuery};

pub type MockIbcApp = (
App<
BankKeeper,
MockApiBech32,
MemoryStorage,
WormholeKeeper,
WasmKeeper<Empty, WormholeQuery>,
StakeKeeper,
DistributionKeeper,
IbcSimpleModule,
>,
Addr,
Addr,
);

pub fn create_receiver_app(hrp: &'static str) -> MockIbcApp {
let mut app = AppBuilder::new_custom()
.with_ibc(IbcSimpleModule::default())
.with_wasm(WasmKeeper::new().with_address_generator(MockAddressGenerator))
.with_api(MockApiBech32::new(hrp))
.with_custom(wormhole_bindings::fake::WormholeKeeper::default())
.build(no_init);

let user = app.api().addr_make("user");

let contract_wrap = ContractWrapper::new(
|deps, env, info, msg| {
execute(deps, env, info, msg)
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
|deps, env, info, msg| {
instantiate(deps.into_empty(), env, info, msg)
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
|deps, env, msg| {
query(deps.into_empty(), env, msg)
// .map(|res| Binary::from(res.0))
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
)
.with_ibc(
|deps, env, msg| {
ibc_channel_open(deps.into_empty(), env, msg)
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
|deps, env, msg| {
ibc_channel_connect(deps.into_empty(), env, msg)
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
|deps, env, msg| {
ibc_channel_close(deps.into_empty(), env, msg)
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
|deps, env, msg| {
ibc_packet_receive(deps.into_empty(), env, msg)
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
|deps, env, msg| {
ibc_packet_ack(deps.into_empty(), env, msg)
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
|deps, env, msg| {
ibc_packet_timeout(deps.into_empty(), env, msg)
.map_err(|anyhow_err| StdError::generic_err(anyhow_err.to_string()))
},
);

let receiver_code_id = app.store_code(Box::new(contract_wrap));

let contract_addr = app
.instantiate_contract(
receiver_code_id,
user.clone(),
&Empty {},
&[],
"receiver_contract",
None,
)
.unwrap();

(app, user, contract_addr)
}
6 changes: 6 additions & 0 deletions cosmwasm/contracts/wormchain-ibc-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ pub mod error;
pub mod ibc;
pub mod msg;
pub mod state;

#[cfg(test)]
pub mod tests;

#[cfg(all(test, feature = "test-interface"))]
pub mod interface;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{ibc as receiver_contract_ibc, interface::create_receiver_app};
use abstract_cw_multi_test::ibc::relayer::{create_channel, create_connection};
use anyhow::Error;
use cosmwasm_std::IbcOrder;
use wormhole_ibc::interface::create_sender_app;

#[test]
pub fn ibc_channel_happy_path() -> anyhow::Result<(), Error> {
let (mut wormchain, wormchain_user, receiver_contract) = create_receiver_app("worm");
let (mut sei, sei_user, sender_contract) = create_sender_app("sei");

let receiver_port = wormchain
.wrap()
.query_wasm_contract_info(receiver_contract.clone())?
.ibc_port
.unwrap_or(format!("wasm.{}", receiver_contract.clone()));

let sender_port = sei
.wrap()
.query_wasm_contract_info(sender_contract.clone())?
.ibc_port
.unwrap_or(format!("wasm.{}", sender_contract.clone()));

println!(
"receiver_ibc_port: {:?}, sender_port: {}",
receiver_port, sender_port
);

let (sei_connection_id, _) = create_connection(&mut sei, &mut wormchain)?;

println!("sei_connection: {:?}", sei_connection_id);

let new_channel = create_channel(
&mut sei,
&mut wormchain,
sei_connection_id,
sender_port,
receiver_port,
receiver_contract_ibc::IBC_APP_VERSION.to_string(),
IbcOrder::Unordered,
)?;

println!("channel_creation: {:?}", new_channel);

Ok(())
}
Loading
Loading