Skip to content

Commit 04df139

Browse files
committed
chore(zkgm): timeout happy paths
Signed-off-by: aeryz <[email protected]>
1 parent b979c59 commit 04df139

File tree

2 files changed

+213
-62
lines changed

2 files changed

+213
-62
lines changed

cosmwasm/ibc-union/app/ucs03-zkgm/src/contract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ pub fn verify_token_order_v2(
520520

521521
/// Handles IBC packet timeouts by either processing forwarded packet timeouts or
522522
/// executing timeout logic for normal packets.
523-
fn timeout_packet(
523+
pub(crate) fn timeout_packet(
524524
deps: DepsMut,
525525
env: Env,
526526
info: MessageInfo,
@@ -573,7 +573,7 @@ fn timeout_packet(
573573
#[allow(clippy::too_many_arguments)]
574574
/// Handles the internal timeout logic for a packet.
575575
/// Processes timeouts based on instruction type and executes appropriate refund/cleanup actions.
576-
fn timeout_internal(
576+
pub(crate) fn timeout_internal(
577577
mut deps: DepsMut,
578578
env: Env,
579579
info: MessageInfo,

cosmwasm/ibc-union/app/ucs03-zkgm/src/tests.rs

Lines changed: 211 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ use crate::{
4343
};
4444

4545
const DEFAULT_IBC_HOST: &str = "blabla";
46+
const ADMIN: &str = "union12qdvmw22n72mem0ysff3nlyj2c76cuy4x60lua";
47+
const SOURCE_CHANNEL_ID: ChannelId = ChannelId!(1);
48+
const DESTINATION_CHANNEL_ID: ChannelId = ChannelId!(2);
49+
const AMOUNT: u128 = 10;
50+
const TOKEN: &str = "au";
51+
const PREDICT_TOKEN: &str = "union1xlzyzcerp2r3dd8w877j0uqnhjllkhv22ahevl";
4652

4753
#[test]
4854
fn test_dequeue_channel_from_path_ok_1() {
@@ -3751,6 +3757,58 @@ fn test_on_channel_close_confirm_only_ibc() {
37513757
);
37523758
}
37533759

3760+
fn init_with_custom_querier<T: Querier>(
3761+
querier: T,
3762+
) -> (
3763+
OwnedDeps<MockStorage, MockApi, T, Empty>,
3764+
Env,
3765+
MessageInfo,
3766+
Config,
3767+
) {
3768+
let mut deps = OwnedDeps {
3769+
storage: MockStorage::default(),
3770+
api: MockApi::default(),
3771+
querier,
3772+
custom_query_type: std::marker::PhantomData,
3773+
};
3774+
deps.api = MockApi::default().with_prefix("union");
3775+
let mut env = mock_env();
3776+
let ibc_host = Addr::unchecked(DEFAULT_IBC_HOST);
3777+
let info = message_info(&ibc_host, &[]);
3778+
let config = Config {
3779+
admin: Addr::unchecked(""),
3780+
ibc_host,
3781+
token_minter_code_id: 0,
3782+
rate_limit_disabled: false,
3783+
dummy_code_id: 0,
3784+
cw_account_code_id: 0,
3785+
};
3786+
env.contract.address = Addr::unchecked(ADMIN);
3787+
crate::contract::init(
3788+
deps.as_mut(),
3789+
env.clone(),
3790+
InitMsg {
3791+
config: Config {
3792+
admin: Addr::unchecked(ADMIN),
3793+
ibc_host: Addr::unchecked(DEFAULT_IBC_HOST),
3794+
token_minter_code_id: 0,
3795+
rate_limit_disabled: false,
3796+
dummy_code_id: 0,
3797+
cw_account_code_id: 0,
3798+
},
3799+
minter_init_params: TokenMinterInitParams::Cw20 {
3800+
cw20_impl_code_id: 0,
3801+
dummy_code_id: 0,
3802+
},
3803+
access_managed_init_msg: access_managed::InitMsg {
3804+
initial_authority: Addr::unchecked(ADMIN),
3805+
},
3806+
},
3807+
)
3808+
.unwrap();
3809+
(deps, env, info, config)
3810+
}
3811+
37543812
#[cfg(test)]
37553813
mod verify_token_order_v2_tests {
37563814
use cosmwasm_std::QueryRequest;
@@ -3759,14 +3817,7 @@ mod verify_token_order_v2_tests {
37593817
use super::*;
37603818
use crate::{contract::make_wasm_msg, state::TOKEN_MINTER};
37613819

3762-
const SOURCE_CHANNEL_ID: ChannelId = ChannelId!(1);
3763-
const DESTINATION_CHANNEL_ID: ChannelId = ChannelId!(2);
3764-
const AMOUNT: u128 = 10;
3765-
const TOKEN: &str = "au";
3766-
const ADMIN: &str = "union12qdvmw22n72mem0ysff3nlyj2c76cuy4x60lua";
3767-
const PREDICT_TOKEN: &str = "union1xlzyzcerp2r3dd8w877j0uqnhjllkhv22ahevl";
3768-
3769-
struct MockCodeHashQuerier;
3820+
pub struct MockCodeHashQuerier;
37703821

37713822
impl Querier for MockCodeHashQuerier {
37723823
fn raw_query(&self, q: &[u8]) -> cosmwasm_std::QuerierResult {
@@ -3801,58 +3852,6 @@ mod verify_token_order_v2_tests {
38013852
}
38023853
}
38033854

3804-
fn init_with_custom_querier<T: Querier>(
3805-
querier: T,
3806-
) -> (
3807-
OwnedDeps<MockStorage, MockApi, T, Empty>,
3808-
Env,
3809-
MessageInfo,
3810-
Config,
3811-
) {
3812-
let mut deps = OwnedDeps {
3813-
storage: MockStorage::default(),
3814-
api: MockApi::default(),
3815-
querier,
3816-
custom_query_type: std::marker::PhantomData,
3817-
};
3818-
deps.api = MockApi::default().with_prefix("union");
3819-
let mut env = mock_env();
3820-
let ibc_host = Addr::unchecked(DEFAULT_IBC_HOST);
3821-
let info = message_info(&ibc_host, &[]);
3822-
let config = Config {
3823-
admin: Addr::unchecked(""),
3824-
ibc_host,
3825-
token_minter_code_id: 0,
3826-
rate_limit_disabled: false,
3827-
dummy_code_id: 0,
3828-
cw_account_code_id: 0,
3829-
};
3830-
env.contract.address = Addr::unchecked(ADMIN);
3831-
crate::contract::init(
3832-
deps.as_mut(),
3833-
env.clone(),
3834-
InitMsg {
3835-
config: Config {
3836-
admin: Addr::unchecked(ADMIN),
3837-
ibc_host: Addr::unchecked(DEFAULT_IBC_HOST),
3838-
token_minter_code_id: 0,
3839-
rate_limit_disabled: false,
3840-
dummy_code_id: 0,
3841-
cw_account_code_id: 0,
3842-
},
3843-
minter_init_params: TokenMinterInitParams::Cw20 {
3844-
cw20_impl_code_id: 0,
3845-
dummy_code_id: 0,
3846-
},
3847-
access_managed_init_msg: access_managed::InitMsg {
3848-
initial_authority: Addr::unchecked(ADMIN),
3849-
},
3850-
},
3851-
)
3852-
.unwrap();
3853-
(deps, env, info, config)
3854-
}
3855-
38563855
#[test]
38573856
fn test_verify_token_order_v2_not_unescrow() {
38583857
let (mut deps, _, info, _) = init_with_custom_querier(MockCodeHashQuerier);
@@ -3986,3 +3985,155 @@ mod verify_token_order_v2_tests {
39863985
);
39873986
}
39883987
}
3988+
3989+
#[cfg(test)]
3990+
mod timeout_tests {
3991+
use ucs03_zkgm_token_minter_api::{LocalTokenMsg, WrappedTokenMsg};
3992+
3993+
use super::*;
3994+
use crate::{
3995+
contract::{decrease_channel_balance_v2, make_wasm_msg, timeout_internal},
3996+
state::TOKEN_MINTER,
3997+
};
3998+
3999+
#[test]
4000+
fn test_timeout_packet_token_order_v2_unescrow() {
4001+
let (mut deps, env, info, _) =
4002+
init_with_custom_querier(verify_token_order_v2_tests::MockCodeHashQuerier);
4003+
4004+
let token_order = TokenOrderV2 {
4005+
sender: ADMIN.as_bytes().into(),
4006+
receiver: b"asdasdasd".into(),
4007+
base_token: PREDICT_TOKEN.as_bytes().into(),
4008+
base_amount: AMOUNT.try_into().unwrap(),
4009+
quote_token: TOKEN.as_bytes().into(),
4010+
quote_amount: AMOUNT.try_into().unwrap(),
4011+
kind: TOKEN_ORDER_KIND_UNESCROW,
4012+
metadata: b"".into(),
4013+
};
4014+
4015+
let response = timeout_internal(
4016+
deps.as_mut(),
4017+
env,
4018+
info,
4019+
Addr::unchecked(ADMIN),
4020+
Packet {
4021+
source_channel_id: SOURCE_CHANNEL_ID,
4022+
destination_channel_id: DESTINATION_CHANNEL_ID,
4023+
// we dont use this
4024+
data: b"".into(),
4025+
timeout_height: MustBeZero,
4026+
timeout_timestamp: Default::default(),
4027+
},
4028+
Addr::unchecked(ADMIN),
4029+
[0; 32].into(),
4030+
U256::ZERO,
4031+
Instruction {
4032+
version: INSTR_VERSION_2,
4033+
opcode: OP_TOKEN_ORDER,
4034+
operand: token_order.abi_encode_params().into(),
4035+
},
4036+
)
4037+
.unwrap();
4038+
4039+
let minter = TOKEN_MINTER.load(&deps.storage).unwrap();
4040+
4041+
assert_eq!(
4042+
response.messages[0],
4043+
cosmwasm_std::SubMsg::new(
4044+
make_wasm_msg(
4045+
WrappedTokenMsg::MintTokens {
4046+
denom: PREDICT_TOKEN.into(),
4047+
amount: AMOUNT.into(),
4048+
mint_to_address: Addr::unchecked(ADMIN),
4049+
},
4050+
minter,
4051+
vec![]
4052+
)
4053+
.unwrap()
4054+
)
4055+
);
4056+
}
4057+
4058+
#[test]
4059+
fn test_timeout_packet_token_order_v2_not_unescrow() {
4060+
let (mut deps, env, info, _) =
4061+
init_with_custom_querier(verify_token_order_v2_tests::MockCodeHashQuerier);
4062+
4063+
let token_order = TokenOrderV2 {
4064+
sender: ADMIN.as_bytes().into(),
4065+
receiver: b"asdasdasd".into(),
4066+
base_token: PREDICT_TOKEN.as_bytes().into(),
4067+
base_amount: AMOUNT.try_into().unwrap(),
4068+
quote_token: TOKEN.as_bytes().into(),
4069+
quote_amount: AMOUNT.try_into().unwrap(),
4070+
kind: TOKEN_ORDER_KIND_ESCROW,
4071+
metadata: b"".into(),
4072+
};
4073+
4074+
increase_channel_balance_v2(
4075+
&mut deps.storage,
4076+
SOURCE_CHANNEL_ID,
4077+
U256::ZERO,
4078+
PREDICT_TOKEN.to_string(),
4079+
TOKEN.as_bytes().into(),
4080+
AMOUNT.into(),
4081+
)
4082+
.unwrap();
4083+
4084+
let response = timeout_internal(
4085+
deps.as_mut(),
4086+
env,
4087+
info,
4088+
Addr::unchecked(ADMIN),
4089+
Packet {
4090+
source_channel_id: SOURCE_CHANNEL_ID,
4091+
destination_channel_id: DESTINATION_CHANNEL_ID,
4092+
// we dont use this
4093+
data: b"".into(),
4094+
timeout_height: MustBeZero,
4095+
timeout_timestamp: Default::default(),
4096+
},
4097+
Addr::unchecked(ADMIN),
4098+
[0; 32].into(),
4099+
U256::ZERO,
4100+
Instruction {
4101+
version: INSTR_VERSION_2,
4102+
opcode: OP_TOKEN_ORDER,
4103+
operand: token_order.abi_encode_params().into(),
4104+
},
4105+
)
4106+
.unwrap();
4107+
4108+
let minter = TOKEN_MINTER.load(&deps.storage).unwrap();
4109+
4110+
// no more balance to decrease, hence failure
4111+
assert!(
4112+
decrease_channel_balance_v2(
4113+
deps.as_mut(),
4114+
SOURCE_CHANNEL_ID,
4115+
U256::ZERO,
4116+
PREDICT_TOKEN.into(),
4117+
TOKEN.as_bytes().into(),
4118+
AMOUNT.into()
4119+
)
4120+
.is_err()
4121+
);
4122+
4123+
assert_eq!(
4124+
response.messages[0],
4125+
cosmwasm_std::SubMsg::new(
4126+
make_wasm_msg(
4127+
LocalTokenMsg::Unescrow {
4128+
denom: PREDICT_TOKEN.into(),
4129+
recipient: ADMIN.into(),
4130+
amount: AMOUNT.into(),
4131+
},
4132+
minter,
4133+
vec![]
4134+
)
4135+
.unwrap()
4136+
)
4137+
);
4138+
}
4139+
}

0 commit comments

Comments
 (0)