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

chore: port solidity ibc-union tests to cosmwasm #3439

Merged
merged 11 commits into from
Jan 16, 2025
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cosmwasm/ibc-union/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ library = []

[dependencies]
alloy = { workspace = true, features = ["sol-types"] }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true, features = ["abort"] }
cw-storage-plus = { workspace = true }
ethabi = { workspace = true }
hex = { workspace = true }
ibc-solidity = { workspace = true, features = ["serde"] }
ibc-union-msg = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions cosmwasm/ibc-union/core/msg/src/lightclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ pub struct MisbehaviourResponse {
pub client_state: Bytes,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub struct VerifyCreationResponse {
pub latest_height: u64,
pub counterparty_chain_id: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum QueryMsg {
GetTimestamp {
Expand Down
19 changes: 3 additions & 16 deletions cosmwasm/ibc-union/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
pub mod contract;
pub mod state;

#[cfg(test)]
mod tests;

use cosmwasm_std::{Addr, StdError};
use ibc_solidity::{ChannelState, ConnectionState};
use thiserror::Error;
Expand Down Expand Up @@ -124,19 +127,3 @@ impl ContractErrorKind {
err.strip_prefix("IBC_UNION_ERR_")?.parse().ok()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn display() {
assert_eq!(
ContractErrorKind::ArithmeticOverflow,
ContractErrorKind::parse_from_error_message(
&ContractError::ArithmeticOverflow.to_string()
)
.unwrap()
)
}
}
143 changes: 143 additions & 0 deletions cosmwasm/ibc-union/core/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use contract::execute;
use cosmwasm_std::{
from_json,
testing::{message_info, mock_env, MockApi},
Addr, Binary, DepsMut, QuerierResult, Response, StdResult, WasmQuery,
};
use ibc_union_msg::{
lightclient::QueryMsg as LightClientQueryMsg,
msg::{
ExecuteMsg, MsgChannelOpenInit, MsgConnectionOpenConfirm, MsgConnectionOpenInit,
MsgConnectionOpenTry, MsgCreateClient, MsgRegisterClient,
},
};

use super::*;

mod channel;
mod client;
mod connection;

const CLIENT_TYPE: &str = "union";
const CLIENT_ADDRESS: &str = "unionclient";
const SENDER: &str = "unionsender";
const RELAYER: &str = "unionrelayer";
const VERSION: &str = "version";

/// Creates a mock address from a given string.
/// Addresses are prefixed with the default [`MockApi`] prefix.
fn mock_addr(address_seed: impl Into<String>) -> Addr {
let mock_api = MockApi::default();
mock_api.addr_make(&Into::<String>::into(address_seed))
}

fn wasm_query_handler<F: Fn(LightClientQueryMsg) -> StdResult<Binary> + 'static>(
querier: F,
) -> impl Fn(&WasmQuery) -> QuerierResult + 'static {
move |msg| match msg {
WasmQuery::Smart { msg, .. } => {
let msg: LightClientQueryMsg = from_json(msg).unwrap();
let res = querier(msg).unwrap();
QuerierResult::Ok(cosmwasm_std::ContractResult::Ok(res))
}
_ => panic!("Only smart queries should be possible now. Adjust this based on your needs."),
}
}

/// Creates a mock client.
/// Uses [`mock_addr`] to convert address seeds to addresses
/// Addresses are prefixed with the default [`MockApi`] prefix.
fn register_client(deps: DepsMut) -> Result<Response, ContractError> {
let register_msg = ExecuteMsg::RegisterClient(MsgRegisterClient {
client_type: CLIENT_TYPE.to_owned(),
client_address: mock_addr(CLIENT_ADDRESS).into_string(),
});

let sender = mock_addr(SENDER);
execute(deps, mock_env(), message_info(&sender, &[]), register_msg)
}

fn create_client(deps: DepsMut) -> Result<Response, ContractError> {
let execute_msg = ExecuteMsg::CreateClient(MsgCreateClient {
client_type: CLIENT_TYPE.to_owned(),
client_state_bytes: vec![1, 2, 3].into(),
consensus_state_bytes: vec![1, 2, 3].into(),
relayer: mock_addr(RELAYER).into_string(),
});

let sender = mock_addr(SENDER);
execute(deps, mock_env(), message_info(&sender, &[]), execute_msg)
}

fn connection_open_init(deps: DepsMut) -> Result<Response, ContractError> {
let msg = MsgConnectionOpenInit {
client_id: 1,
counterparty_client_id: 2,
relayer: mock_addr(RELAYER).into_string(),
};
execute(
deps,
mock_env(),
message_info(&mock_addr(SENDER), &[]),
ExecuteMsg::ConnectionOpenInit(msg),
)
}

fn connection_open_try(deps: DepsMut) -> Result<Response, ContractError> {
let msg = MsgConnectionOpenTry {
counterparty_client_id: 2,
counterparty_connection_id: 1,
client_id: 1,
proof_init: vec![1, 2, 3].into(),
proof_height: 1,
relayer: mock_addr(RELAYER).into_string(),
};

execute(
deps,
mock_env(),
message_info(&mock_addr(SENDER), &[]),
ExecuteMsg::ConnectionOpenTry(msg),
)
}

fn connection_open_confirm(deps: DepsMut) -> Result<Response, ContractError> {
let msg = MsgConnectionOpenConfirm {
connection_id: 1,
proof_ack: vec![1, 2, 3].into(),
proof_height: 1,
relayer: mock_addr(RELAYER).into_string(),
};

execute(
deps,
mock_env(),
message_info(&mock_addr(SENDER), &[]),
ExecuteMsg::ConnectionOpenConfirm(msg),
)
}

fn channel_open_init(deps: DepsMut) -> Result<Response, ContractError> {
let msg = MsgChannelOpenInit {
port_id: mock_addr(SENDER).to_string(),
counterparty_port_id: vec![1].into(),
connection_id: 1,
version: VERSION.to_owned(),
relayer: mock_addr(RELAYER).to_string(),
};
execute(
deps,
mock_env(),
message_info(&mock_addr(SENDER), &[]),
ExecuteMsg::ChannelOpenInit(msg),
)
}

#[test]
fn display() {
assert_eq!(
ContractErrorKind::ArithmeticOverflow,
ContractErrorKind::parse_from_error_message(&ContractError::ArithmeticOverflow.to_string())
.unwrap()
)
}
4 changes: 4 additions & 0 deletions cosmwasm/ibc-union/core/src/tests/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use super::*;

mod ibc_channel;
mod ibc_packet;
Loading
Loading