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

feat(timeout-height): allow define optional timeout height parameter #1489

Merged
merged 6 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/amino/src/signdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface StdSignDoc {
readonly fee: StdFee;
readonly msgs: readonly AminoMsg[];
readonly memo: string;
readonly timeout_height?: string;
}

function sortedObject(obj: any): any {
Expand Down Expand Up @@ -61,6 +62,7 @@ export function makeSignDoc(
memo: string | undefined,
accountNumber: number | string,
sequence: number | string,
timeout_height?: bigint,
): StdSignDoc {
return {
chain_id: chainId,
Expand All @@ -69,6 +71,7 @@ export function makeSignDoc(
fee: fee,
msgs: msgs,
memo: memo || "",
...(timeout_height && { timeout_height: timeout_height.toString() }),
};
}

Expand Down
132 changes: 132 additions & 0 deletions packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,72 @@ describe("SigningCosmWasmClient", () => {

client.disconnect();
});

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, {
...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 + 3));

// ensure signature is valid
const result = await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish()));
assertIsDeliverTxSuccess(result);

client.disconnect();
});

it("fails 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));

await expectAsync(
client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish())),
).toBeRejectedWith(
jasmine.objectContaining({
code: 30,
}),
);

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

describe("legacy Amino mode", () => {
Expand Down Expand Up @@ -1407,6 +1473,72 @@ describe("SigningCosmWasmClient", () => {

client.disconnect();
});

it("works with custom 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 + 3));

// ensure signature is valid
const result = await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish()));
assertIsDeliverTxSuccess(result);

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));

await expectAsync(
client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish())),
).toBeRejectedWith(
jasmine.objectContaining({
code: 30,
}),
);

client.disconnect();
});
});
});
});
23 changes: 16 additions & 7 deletions packages/cosmwasm-stargate/src/signingcosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,18 +574,20 @@ export class SigningCosmWasmClient extends CosmWasmClient {
}

/**
* Creates a transaction with the given messages, fee and memo. Then signs and broadcasts the transaction.
* Creates a transaction with the given messages, fee, memo and timeout height. Then signs and broadcasts the transaction.
*
* @param signerAddress The address that will sign transactions using this instance. The signer must be able to sign with this address.
* @param messages
* @param fee
* @param memo
* @param timeoutHeight (optional) timeout height to prevent the tx from being committed past a certain height
*/
public async signAndBroadcast(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee | "auto" | number,
memo = "",
timeoutHeight?: bigint,
): Promise<DeliverTxResponse> {
let usedFee: StdFee;
if (fee == "auto" || typeof fee === "number") {
Expand All @@ -598,13 +600,13 @@ export class SigningCosmWasmClient extends CosmWasmClient {
} else {
usedFee = fee;
}
const txRaw = await this.sign(signerAddress, messages, usedFee, memo);
const txRaw = await this.sign(signerAddress, messages, usedFee, memo, undefined, timeoutHeight);
const txBytes = TxRaw.encode(txRaw).finish();
return this.broadcastTx(txBytes, this.broadcastTimeoutMs, this.broadcastPollIntervalMs);
}

/**
* Creates a transaction with the given messages, fee and memo. Then signs and broadcasts the transaction.
* Creates a transaction with the given messages, fee, memo and timeout height. Then signs and broadcasts the transaction.
*
* This method is useful if you want to send a transaction in broadcast,
* without waiting for it to be placed inside a block, because for example
Expand All @@ -614,6 +616,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
* @param messages
* @param fee
* @param memo
* @param timeoutHeight (optional) timeout height to prevent the tx from being committed past a certain height
*
* @returns Returns the hash of the transaction
*/
Expand All @@ -622,6 +625,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
messages: readonly EncodeObject[],
fee: StdFee | "auto" | number,
memo = "",
timeoutHeight?: bigint,
): Promise<string> {
let usedFee: StdFee;
if (fee == "auto" || typeof fee === "number") {
Expand All @@ -632,7 +636,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
} else {
usedFee = fee;
}
const txRaw = await this.sign(signerAddress, messages, usedFee, memo);
const txRaw = await this.sign(signerAddress, messages, usedFee, memo, undefined, timeoutHeight);
const txBytes = TxRaw.encode(txRaw).finish();
return this.broadcastTxSync(txBytes);
}
Expand All @@ -643,6 +647,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
fee: StdFee,
memo: string,
explicitSignerData?: SignerData,
timeoutHeight?: bigint,
): Promise<TxRaw> {
let signerData: SignerData;
if (explicitSignerData) {
Expand All @@ -658,8 +663,8 @@ export class SigningCosmWasmClient extends CosmWasmClient {
}

return isOfflineDirectSigner(this.signer)
? this.signDirect(signerAddress, messages, fee, memo, signerData)
: this.signAmino(signerAddress, messages, fee, memo, signerData);
? this.signDirect(signerAddress, messages, fee, memo, signerData, timeoutHeight)
: this.signAmino(signerAddress, messages, fee, memo, signerData, timeoutHeight);
}

private async signAmino(
Expand All @@ -668,6 +673,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
fee: StdFee,
memo: string,
{ accountNumber, sequence, chainId }: SignerData,
timeoutHeight?: bigint,
): Promise<TxRaw> {
assert(!isOfflineDirectSigner(this.signer));
const accountFromSigner = (await this.signer.getAccounts()).find(
Expand All @@ -679,13 +685,14 @@ export class SigningCosmWasmClient extends CosmWasmClient {
const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
const msgs = messages.map((msg) => this.aminoTypes.toAmino(msg));
const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence);
const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence, timeoutHeight);
const { signature, signed } = await this.signer.signAmino(signerAddress, signDoc);
const signedTxBody: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
memo: signed.memo,
timeoutHeight: timeoutHeight,
},
};
const signedTxBodyBytes = this.registry.encode(signedTxBody);
Expand All @@ -712,6 +719,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
fee: StdFee,
memo: string,
{ accountNumber, sequence, chainId }: SignerData,
timeoutHeight?: bigint,
): Promise<TxRaw> {
assert(isOfflineDirectSigner(this.signer));
const accountFromSigner = (await this.signer.getAccounts()).find(
Expand All @@ -726,6 +734,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
value: {
messages: messages,
memo: memo,
timeoutHeight: timeoutHeight,
},
};
const txBodyBytes = this.registry.encode(txBody);
Expand Down
Loading