From ff96919e732d1fe6764b588e34e373f86533d59b Mon Sep 17 00:00:00 2001 From: mr-t Date: Thu, 25 Jan 2024 22:40:52 +0100 Subject: [PATCH] test backtransfer to banned recipient, but also to to regular recpient --- contracts/cw721-tester/src/lib.rs | 30 +++++++++-------- ts-relayer-tests/src/ics721.spec.ts | 51 +++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/contracts/cw721-tester/src/lib.rs b/contracts/cw721-tester/src/lib.rs index ed686b1e..b3345e59 100644 --- a/contracts/cw721-tester/src/lib.rs +++ b/contracts/cw721-tester/src/lib.rs @@ -14,13 +14,13 @@ pub struct InstantiateMsg { pub name: String, pub symbol: String, pub minter: String, - /// An address which will be unable to transfer NFTs away from - /// themselves (they are a black hole). If this address attempts a - /// `TransferNft` message it will fail with an out-of-gas error. - pub target: String, + /// An address which will be unable receive NFT on `TransferNft` message + /// If `TransferNft` message attempts sending to banned recipient + /// it will fail with an out-of-gas error. + pub banned_recipient: String, } -const TARGET: Item = Item::new("target"); +const BANNED_RECIPIENT: Item = Item::new("banned_recipient"); const CONTRACT_NAME: &str = "crates.io:cw721-gas-tester"; const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -43,7 +43,7 @@ pub fn instantiate( withdraw_address: None, }, )?; - TARGET.save(deps.storage, &deps.api.addr_validate(&msg.target)?)?; + BANNED_RECIPIENT.save(deps.storage, &msg.banned_recipient)?; set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; Ok(response) @@ -56,13 +56,17 @@ pub fn execute( info: MessageInfo, msg: ExecuteMsg, ) -> Result { - if matches!(msg, ExecuteMsg::TransferNft { .. }) && info.sender == TARGET.load(deps.storage)? { - // loop here causes the relayer to hang while it tries to - // simulate the TX. - panic!("gotem") - // loop {} - } else { - cw721_base::entry::execute(deps, env, info, msg) + match msg.clone() { + ExecuteMsg::TransferNft { recipient, .. } => { + if recipient == BANNED_RECIPIENT.load(deps.storage)? { + // loop here causes the relayer to hang while it tries to + // simulate the TX. + panic!("gotem") + // loop {} + } + cw721_base::entry::execute(deps, env, info, msg) + } + _ => cw721_base::entry::execute(deps, env, info, msg), } } diff --git a/ts-relayer-tests/src/ics721.spec.ts b/ts-relayer-tests/src/ics721.spec.ts index 530de734..4514dc03 100644 --- a/ts-relayer-tests/src/ics721.spec.ts +++ b/ts-relayer-tests/src/ics721.spec.ts @@ -386,9 +386,9 @@ test.serial( // Verify we got an error assertAckErrors(info.acksFromA); - // assert NFT on chain A is returned to owner + // assert NFT on chain B is returned to owner tokenOwner = await ownerOf(osmoClient, osmoCw721, tokenId); - t.is(osmoClient.senderAddress, tokenOwner.owner); + t.is(osmoAddr, tokenOwner.owner); t.log(`NFT #${tokenId} returned to owner`); } ); @@ -415,7 +415,7 @@ test.serial("malicious NFT", async (t) => { name: "evil", symbol: "evil", minter: wasmClient.senderAddress, - target: wasmIcs721, // panic every time the ICS721 contract tries to return a NFT. + banned_recipient: "banned_recipient", // panic every time the ICS721 contract tries to transfer NFT to this address }, }, }); @@ -452,7 +452,7 @@ test.serial("malicious NFT", async (t) => { assertAckSuccess(info.acksFromB); - t.log("transferring back to wasm chain"); + t.log("transferring back to wasm chain to banned recipient"); const osmoClassId = `${t.context.channel.channel.dest.portId}/${t.context.channel.channel.dest.channelId}/${cw721}`; const osmoCw721 = await osmoClient.sign.queryContractSmart(osmoIcs721, { @@ -460,7 +460,7 @@ test.serial("malicious NFT", async (t) => { }); ibcMsg = { - receiver: wasmAddr, + receiver: "banned_recipient", channel_id: channel.channel.dest.channelId, timeout: { block: { @@ -481,10 +481,49 @@ test.serial("malicious NFT", async (t) => { t.log("relaying packets"); - const pending = await channel.link.getPendingPackets("B"); + let pending = await channel.link.getPendingPackets("B"); t.is(pending.length, 1); // Despite the transfer panicking, a fail ack should be returned. info = await channel.link.relayAll(); assertAckErrors(info.acksFromA); + // assert NFT on chain B is returned to owner + let tokenOwner = await ownerOf(osmoClient, osmoCw721, tokenId); + t.is(osmoAddr, tokenOwner.owner); + t.log(`NFT #${tokenId} returned to owner`); + + t.log("transferring back to wasm chain to recipient", wasmAddr); + + ibcMsg = { + receiver: wasmAddr, + channel_id: channel.channel.dest.channelId, + timeout: { + block: { + revision: 1, + height: 90000, + }, + }, + }; + + transferResponse = await sendNft( + osmoClient, + osmoCw721, + osmoCw721OutgoingProxy, + ibcMsg, + tokenId + ); + t.truthy(transferResponse); + + t.log("relaying packets"); + + pending = await channel.link.getPendingPackets("B"); + t.is(pending.length, 1); + + // Verify we got a success + info = await channel.link.relayAll(); + assertAckSuccess(info.acksFromB); + + // assert NFT on chain A is returned to owner + tokenOwner = await ownerOf(wasmClient, cw721, tokenId); + t.is(wasmAddr, tokenOwner.owner); });