Skip to content

Commit

Permalink
feat(timeout-height): add past timeout height test
Browse files Browse the repository at this point in the history
  • Loading branch information
janfabian committed Oct 25, 2023
1 parent efeb501 commit 9ba0d30
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 1 deletion.
72 changes: 71 additions & 1 deletion packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ describe("SigningCosmWasmClient", () => {
client.disconnect();
});

it("works with a custom timeout height", async () => {
it("works with a custom timeoutHeight", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, {
Expand Down Expand Up @@ -1222,6 +1222,41 @@ describe("SigningCosmWasmClient", () => {

client.disconnect();
});

it("failt with past timeoutHeight", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, {
...defaultSigningClientOptions,
});

const msg = MsgSend.fromPartial({
fromAddress: alice.address0,
toAddress: alice.address0,
amount: [coin(1, "ucosm")],
});
const msgAny: MsgSendEncodeObject = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: msg,
};
const fee = {
amount: coins(2000, "ucosm"),
gas: "222000", // 222k
};
const memo = "Use your power wisely";
const height = await client.getHeight();
const signed = await client.sign(alice.address0, [msgAny], fee, memo, undefined, BigInt(height - 1));

try {
await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish()));

throw new Error("tx should have failed because of past timeoutHeight");
} catch (e: any) {
assert(e.code === 30);
}

client.disconnect();
});
});

describe("legacy Amino mode", () => {
Expand Down Expand Up @@ -1469,6 +1504,41 @@ describe("SigningCosmWasmClient", () => {

client.disconnect();
});

it("fails with past timeoutHeight", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, {
...defaultSigningClientOptions,
});

const msg = MsgSend.fromPartial({
fromAddress: alice.address0,
toAddress: alice.address0,
amount: [coin(1, "ucosm")],
});
const msgAny: MsgSendEncodeObject = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: msg,
};
const fee = {
amount: coins(2000, "ucosm"),
gas: "200000",
};
const memo = "Use your tokens wisely";
const height = await client.getHeight();
const signed = await client.sign(alice.address0, [msgAny], fee, memo, undefined, BigInt(height - 1));

try {
await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish()));

throw new Error("tx should have failed because of past timeoutHeight");
} catch (e: any) {
assert(e.code === 30);
}

client.disconnect();
});
});
});
});
70 changes: 70 additions & 0 deletions packages/stargate/src/signingstargateclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,41 @@ describe("SigningStargateClient", () => {
const result = await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish()));
assertIsDeliverTxSuccess(result);
});

it("fails with past timeoutHeight", async () => {
pendingWithoutSimapp();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const client = await SigningStargateClient.connectWithSigner(
simapp.tendermintUrl,
wallet,
defaultSigningClientOptions,
);

const msg = MsgSend.fromPartial({
fromAddress: faucet.address0,
toAddress: faucet.address0,
amount: [coin(1, "ucosm")],
});
const msgAny: MsgSendEncodeObject = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: msg,
};
const fee = {
amount: coins(2000, "ucosm"),
gas: "222000", // 222k
};
const memo = "Use your power wisely";
const height = await client.getHeight();
const signed = await client.sign(faucet.address0, [msgAny], fee, memo, undefined, BigInt(height - 1));

try {
await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish()));

throw new Error("tx should have failed because of past timeoutHeight");
} catch (e: any) {
assert(e.code === 30);
}
});
});

describe("legacy Amino mode", () => {
Expand Down Expand Up @@ -1183,6 +1218,41 @@ describe("SigningStargateClient", () => {
const result = await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish()));
assertIsDeliverTxSuccess(result);
});

fit("failt with past timeoutHeight", async () => {
pendingWithoutSimapp();
const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const client = await SigningStargateClient.connectWithSigner(
simapp.tendermintUrl,
wallet,
defaultSigningClientOptions,
);

const msg = MsgSend.fromPartial({
fromAddress: faucet.address0,
toAddress: faucet.address0,
amount: [coin(1, "ucosm")],
});
const msgAny: MsgSendEncodeObject = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: msg,
};
const fee = {
amount: coins(2000, "ucosm"),
gas: "200000",
};
const memo = "Use your tokens wisely";
const height = await client.getHeight();
const signed = await client.sign(faucet.address0, [msgAny], fee, memo, undefined, BigInt(height - 1));

try {
await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish()));

throw new Error("tx should have failed because of past timeoutHeight");
} catch (e: any) {
assert(e.code === 30);
}
});
});
});
});

0 comments on commit 9ba0d30

Please sign in to comment.