Skip to content

Commit

Permalink
test backtransfer to banned recipient, but also to to regular recpient
Browse files Browse the repository at this point in the history
  • Loading branch information
taitruong committed Jan 25, 2024
1 parent 09da354 commit ff96919
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
30 changes: 17 additions & 13 deletions contracts/cw721-tester/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Addr> = Item::new("target");
const BANNED_RECIPIENT: Item<String> = Item::new("banned_recipient");

const CONTRACT_NAME: &str = "crates.io:cw721-gas-tester";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -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)
Expand All @@ -56,13 +56,17 @@ pub fn execute(
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
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),
}
}

Expand Down
51 changes: 45 additions & 6 deletions ts-relayer-tests/src/ics721.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
);
Expand All @@ -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
},
},
});
Expand Down Expand Up @@ -452,15 +452,15 @@ 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, {
nft_contract: { class_id: osmoClassId },
});

ibcMsg = {
receiver: wasmAddr,
receiver: "banned_recipient",
channel_id: channel.channel.dest.channelId,
timeout: {
block: {
Expand All @@ -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);
});

0 comments on commit ff96919

Please sign in to comment.