Merge pull request #762 from cosmos/739-sync-cosmwasm

Use sync mode in CosmWasmClient.broadcastTx
This commit is contained in:
Simon Warta 2021-04-14 13:52:01 +02:00 committed by GitHub
commit 212318d2a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 32 deletions

View File

@ -116,6 +116,9 @@ and this project adheres to
- @cosmjs/stargate: `StargateClient.broadcastTx` now uses sync mode and then - @cosmjs/stargate: `StargateClient.broadcastTx` now uses sync mode and then
polls for the transaction before resolving. The timeout and poll interval can polls for the transaction before resolving. The timeout and poll interval can
be configured. be configured.
- @cosmjs/cosmwasm-stargate: `CosmWasmClient.broadcastTx` now uses sync mode and
then polls for the transaction before resolving. The timeout and poll interval
can be configured.
### Deprecated ### Deprecated

View File

@ -26,14 +26,10 @@ import {
SequenceResponse, SequenceResponse,
setupAuthExtension, setupAuthExtension,
setupBankExtension, setupBankExtension,
TimeoutError,
} from "@cosmjs/stargate"; } from "@cosmjs/stargate";
import { TxMsgData } from "@cosmjs/stargate/build/codec/cosmos/base/abci/v1beta1/abci"; import { Tendermint34Client, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc";
import { import { assert, sleep } from "@cosmjs/utils";
broadcastTxCommitSuccess,
Tendermint34Client,
toRfc3339WithNanoseconds,
} from "@cosmjs/tendermint-rpc";
import { assert } from "@cosmjs/utils";
import { CodeInfoResponse } from "./codec/x/wasm/internal/types/query"; import { CodeInfoResponse } from "./codec/x/wasm/internal/types/query";
import { ContractCodeHistoryOperationType } from "./codec/x/wasm/internal/types/types"; import { ContractCodeHistoryOperationType } from "./codec/x/wasm/internal/types/types";
@ -201,31 +197,44 @@ export class CosmWasmClient {
if (this.tmClient) this.tmClient.disconnect(); if (this.tmClient) this.tmClient.disconnect();
} }
public async broadcastTx(tx: Uint8Array): Promise<BroadcastTxResponse> { // NOTE: This method is tested against slow chains and timeouts in the @cosmjs/stargate package.
const response = await this.forceGetTmClient().broadcastTxCommit({ tx }); // Make sure it is kept in sync!
if (broadcastTxCommitSuccess(response)) { public async broadcastTx(
return { tx: Uint8Array,
height: response.height, timeoutMs = 60_000,
transactionHash: toHex(response.hash).toUpperCase(), pollIntervalMs = 3_000,
rawLog: response.deliverTx?.log, ): Promise<BroadcastTxResponse> {
data: response.deliverTx?.data ? TxMsgData.decode(response.deliverTx?.data).data : undefined, let timedOut = false;
}; const txPollTimeout = setTimeout(() => {
} timedOut = true;
return response.checkTx.code !== 0 }, timeoutMs);
? {
height: response.height, const pollForTx = async (txId: string): Promise<BroadcastTxResponse> => {
code: response.checkTx.code, if (timedOut) {
transactionHash: toHex(response.hash).toUpperCase(), throw new TimeoutError(
rawLog: response.checkTx.log, `Transaction with ID ${txId} was submitted but was not yet found on the chain. You might want to check later.`,
data: response.checkTx.data ? TxMsgData.decode(response.checkTx.data).data : undefined, txId,
} );
: { }
height: response.height, await sleep(pollIntervalMs);
code: response.deliverTx?.code, const result = await this.getTx(txId);
transactionHash: toHex(response.hash).toUpperCase(), return result
rawLog: response.deliverTx?.log, ? {
data: response.deliverTx?.data ? TxMsgData.decode(response.deliverTx?.data).data : undefined, code: result.code,
}; height: result.height,
rawLog: result.rawLog,
transactionHash: txId,
}
: pollForTx(txId);
};
return new Promise((resolve, reject) =>
this.forceGetTmClient()
.broadcastTxSync({ tx })
.then(({ hash }) => pollForTx(toHex(hash).toUpperCase()))
.then(resolve, reject)
.finally(() => clearTimeout(txPollTimeout)),
);
} }
public async getCodes(): Promise<readonly Code[]> { public async getCodes(): Promise<readonly Code[]> {