From 5eb7d8ba336bd4aa926dbb923026a8c1abc655e5 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 25 Oct 2023 14:54:26 +0200 Subject: [PATCH 1/3] Improve CometBFT support in high level clients --- CHANGELOG.md | 10 +++++ .../cosmwasm-stargate/src/cosmwasmclient.ts | 10 ++--- .../src/signingcosmwasmclient.ts | 10 ++--- .../stargate/src/signingstargateclient.ts | 40 ++++++------------- packages/stargate/src/stargateclient.ts | 10 ++--- 5 files changed, 37 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c2f7832..fe6d1d53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,16 @@ and this project adheres to - @cosmjs/tendermint-rpc: Add `CometClient = Tendermint34Client | Tendermint37Client | Comet38Client` and `connectComet` for auto-detecting the right client for a provided endpoint. +- @cosmjs/stargate: Let `SigningStargateClient.createWithSigner` and + `StargateClient.create` take a `CometClient` argument, adding support for + `Comet38Client`. The auto-detection in + `SigningStargateClient.connectWithSigner` and `StargateClient.connect` now + supports CometBFT 0.38. +- @cosmjs/cosmwasm-stargate: Let `SigningCosmWasmClient.createWithSigner` and + `CosmWasmClient.create` take a `CometClient` argument, adding support for + `Comet38Client`. The auto-detection in + `SigningCosmWasmClient.connectWithSigner` and `CosmWasmClient.connect` now + supports CometBFT 0.38. [#1421]: https://github.com/cosmos/cosmjs/issues/1421 [#1484]: https://github.com/cosmos/cosmjs/pull/1484 diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 7a825196..7c35d797 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -90,10 +90,10 @@ export class CosmWasmClient { private chainId: string | undefined; /** - * Creates an instance by connecting to the given Tendermint RPC endpoint. + * Creates an instance by connecting to the given CometBFT RPC endpoint. * - * This uses auto-detection to decide between a Tendermint 0.37 and 0.34 client. - * To set the Tendermint client explicitly, use `create`. + * This uses auto-detection to decide between a CometBFT 0.38, Tendermint 0.37 and 0.34 client. + * To set the Comet client explicitly, use `create`. */ public static async connect(endpoint: string | HttpEndpoint): Promise { const cometClient = await connectComet(endpoint); @@ -101,8 +101,8 @@ export class CosmWasmClient { } /** - * Creates an instance from a manually created Tendermint client. - * Use this to use `Tendermint37Client` instead of `Tendermint34Client`. + * Creates an instance from a manually created Comet client. + * Use this to use `Comet38Client` or `Tendermint37Client` instead of `Tendermint34Client`. */ public static async create(cometClient: CometClient): Promise { return new CosmWasmClient(cometClient); diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 6de59caf..ab80616b 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -180,10 +180,10 @@ export class SigningCosmWasmClient extends CosmWasmClient { private readonly gasPrice: GasPrice | undefined; /** - * Creates an instance by connecting to the given Tendermint RPC endpoint. + * Creates an instance by connecting to the given CometBFT RPC endpoint. * - * This uses auto-detection to decide between a Tendermint 0.37 and 0.34 client. - * To set the Tendermint client explicitly, use `createWithSigner`. + * This uses auto-detection to decide between a CometBFT 0.38, Tendermint 0.37 and 0.34 client. + * To set the Comet client explicitly, use `createWithSigner`. */ public static async connectWithSigner( endpoint: string | HttpEndpoint, @@ -195,8 +195,8 @@ export class SigningCosmWasmClient extends CosmWasmClient { } /** - * Creates an instance from a manually created Tendermint client. - * Use this to use `Tendermint37Client` instead of `Tendermint34Client`. + * Creates an instance from a manually created Comet client. + * Use this to use `Comet38Client` or `Tendermint37Client` instead of `Tendermint34Client`. */ public static async createWithSigner( cometClient: CometClient, diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index c468502f..e649e869 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -12,12 +12,7 @@ import { Registry, TxBodyEncodeObject, } from "@cosmjs/proto-signing"; -import { - HttpEndpoint, - Tendermint34Client, - Tendermint37Client, - TendermintClient, -} from "@cosmjs/tendermint-rpc"; +import { CometClient, connectComet, HttpEndpoint } from "@cosmjs/tendermint-rpc"; import { assert, assertDefined } from "@cosmjs/utils"; import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin"; import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx"; @@ -117,41 +112,30 @@ export class SigningStargateClient extends StargateClient { private readonly gasPrice: GasPrice | undefined; /** - * Creates an instance by connecting to the given Tendermint RPC endpoint. + * Creates an instance by connecting to the given CometBFT RPC endpoint. * - * This uses auto-detection to decide between a Tendermint 0.37 and 0.34 client. - * To set the Tendermint client explicitly, use `createWithSigner`. + * This uses auto-detection to decide between a CometBFT 0.38, Tendermint 0.37 and 0.34 client. + * To set the Comet client explicitly, use `createWithSigner`. */ public static async connectWithSigner( endpoint: string | HttpEndpoint, signer: OfflineSigner, options: SigningStargateClientOptions = {}, ): Promise { - // Tendermint/CometBFT 0.34/0.37 auto-detection. Starting with 0.37 we seem to get reliable versions again 🎉 - // Using 0.34 as the fallback. - let tmClient: TendermintClient; - const tm37Client = await Tendermint37Client.connect(endpoint); - const version = (await tm37Client.status()).nodeInfo.version; - if (version.startsWith("0.37.")) { - tmClient = tm37Client; - } else { - tm37Client.disconnect(); - tmClient = await Tendermint34Client.connect(endpoint); - } - - return SigningStargateClient.createWithSigner(tmClient, signer, options); + const cometClient = await connectComet(endpoint); + return SigningStargateClient.createWithSigner(cometClient, signer, options); } /** - * Creates an instance from a manually created Tendermint client. - * Use this to use `Tendermint37Client` instead of `Tendermint34Client`. + * Creates an instance from a manually created Comet client. + * Use this to use `Comet38Client` or `Tendermint37Client` instead of `Tendermint34Client`. */ public static async createWithSigner( - tmClient: TendermintClient, + cometClient: CometClient, signer: OfflineSigner, options: SigningStargateClientOptions = {}, ): Promise { - return new SigningStargateClient(tmClient, signer, options); + return new SigningStargateClient(cometClient, signer, options); } /** @@ -171,11 +155,11 @@ export class SigningStargateClient extends StargateClient { } protected constructor( - tmClient: TendermintClient | undefined, + cometClient: CometClient | undefined, signer: OfflineSigner, options: SigningStargateClientOptions, ) { - super(tmClient, options); + super(cometClient, options); const { registry = new Registry(defaultRegistryTypes), aminoTypes = new AminoTypes(createDefaultAminoConverters()), diff --git a/packages/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index 69ff4582..8022d2d5 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -198,10 +198,10 @@ export class StargateClient { private readonly accountParser: AccountParser; /** - * Creates an instance by connecting to the given Tendermint RPC endpoint. + * Creates an instance by connecting to the given CometBFT RPC endpoint. * - * This uses auto-detection to decide between a Tendermint 0.37 and 0.34 client. - * To set the Tendermint client explicitly, use `create`. + * This uses auto-detection to decide between a CometBFT 0.38, Tendermint 0.37 and 0.34 client. + * To set the Comet client explicitly, use `create`. */ public static async connect( endpoint: string | HttpEndpoint, @@ -212,8 +212,8 @@ export class StargateClient { } /** - * Creates an instance from a manually created Tendermint client. - * Use this to use `Tendermint37Client` instead of `Tendermint34Client`. + * Creates an instance from a manually created Comet client. + * Use this to use `Comet38Client` or `Tendermint37Client` instead of `Tendermint34Client`. */ public static async create( cometClient: CometClient, From 6d763bd06781547d84f4efac0ae1abb6ac81f88a Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 25 Oct 2023 14:58:33 +0200 Subject: [PATCH 2/3] Rename getTmClient -> getCometClient --- CHANGELOG.md | 8 ++++++-- packages/cosmwasm-stargate/src/cosmwasmclient.ts | 14 +++++++------- packages/stargate/src/stargateclient.ts | 14 +++++++------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe6d1d53..9745c383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,12 +22,16 @@ and this project adheres to `StargateClient.create` take a `CometClient` argument, adding support for `Comet38Client`. The auto-detection in `SigningStargateClient.connectWithSigner` and `StargateClient.connect` now - supports CometBFT 0.38. + supports CometBFT 0.38. Rename + `StargateClient.getTmClient`/`.forceGetTmClient` to + `.getCometClient`/`.forceGetCometClient`. - @cosmjs/cosmwasm-stargate: Let `SigningCosmWasmClient.createWithSigner` and `CosmWasmClient.create` take a `CometClient` argument, adding support for `Comet38Client`. The auto-detection in `SigningCosmWasmClient.connectWithSigner` and `CosmWasmClient.connect` now - supports CometBFT 0.38. + supports CometBFT 0.38. Rename + `CosmWasmClient.getTmClient`/`.forceGetTmClient` to + `.getCometClient`/`.forceGetCometClient`. [#1421]: https://github.com/cosmos/cosmjs/issues/1421 [#1484]: https://github.com/cosmos/cosmjs/pull/1484 diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 7c35d797..c2e27871 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -121,11 +121,11 @@ export class CosmWasmClient { } } - protected getTmClient(): CometClient | undefined { + protected getCometClient(): CometClient | undefined { return this.cometClient; } - protected forceGetTmClient(): CometClient { + protected forceGetCometClient(): CometClient { if (!this.cometClient) { throw new Error("Comet client not available. You cannot use online functionality in offline mode."); } @@ -147,7 +147,7 @@ export class CosmWasmClient { public async getChainId(): Promise { if (!this.chainId) { - const response = await this.forceGetTmClient().status(); + const response = await this.forceGetCometClient().status(); const chainId = response.nodeInfo.network; if (!chainId) throw new Error("Chain ID must not be empty"); this.chainId = chainId; @@ -157,7 +157,7 @@ export class CosmWasmClient { } public async getHeight(): Promise { - const status = await this.forceGetTmClient().status(); + const status = await this.forceGetCometClient().status(); return status.syncInfo.latestBlockHeight; } @@ -187,7 +187,7 @@ export class CosmWasmClient { } public async getBlock(height?: number): Promise { - const response = await this.forceGetTmClient().block(height); + const response = await this.forceGetCometClient().block(height); return { id: toHex(response.blockId.hash).toUpperCase(), header: { @@ -305,7 +305,7 @@ export class CosmWasmClient { * @returns Returns the hash of the transaction */ public async broadcastTxSync(tx: Uint8Array): Promise { - const broadcasted = await this.forceGetTmClient().broadcastTxSync({ tx }); + const broadcasted = await this.forceGetCometClient().broadcastTxSync({ tx }); if (broadcasted.code) { return Promise.reject( @@ -480,7 +480,7 @@ export class CosmWasmClient { } private async txsQuery(query: string): Promise { - const results = await this.forceGetTmClient().txSearchAll({ query: query }); + const results = await this.forceGetCometClient().txSearchAll({ query: query }); return results.txs.map((tx): IndexedTx => { const txMsgData = TxMsgData.decode(tx.result.data ?? new Uint8Array()); return { diff --git a/packages/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index 8022d2d5..f8a556b9 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -237,11 +237,11 @@ export class StargateClient { this.accountParser = accountParser; } - protected getTmClient(): CometClient | undefined { + protected getCometClient(): CometClient | undefined { return this.cometClient; } - protected forceGetTmClient(): CometClient { + protected forceGetCometClient(): CometClient { if (!this.cometClient) { throw new Error("Comet client not available. You cannot use online functionality in offline mode."); } @@ -267,7 +267,7 @@ export class StargateClient { public async getChainId(): Promise { if (!this.chainId) { - const response = await this.forceGetTmClient().status(); + const response = await this.forceGetCometClient().status(); const chainId = response.nodeInfo.network; if (!chainId) throw new Error("Chain ID must not be empty"); this.chainId = chainId; @@ -277,7 +277,7 @@ export class StargateClient { } public async getHeight(): Promise { - const status = await this.forceGetTmClient().status(); + const status = await this.forceGetCometClient().status(); return status.syncInfo.latestBlockHeight; } @@ -307,7 +307,7 @@ export class StargateClient { } public async getBlock(height?: number): Promise { - const response = await this.forceGetTmClient().block(height); + const response = await this.forceGetCometClient().block(height); return { id: toHex(response.blockId.hash).toUpperCase(), header: { @@ -473,7 +473,7 @@ export class StargateClient { * @returns Returns the hash of the transaction */ public async broadcastTxSync(tx: Uint8Array): Promise { - const broadcasted = await this.forceGetTmClient().broadcastTxSync({ tx }); + const broadcasted = await this.forceGetCometClient().broadcastTxSync({ tx }); if (broadcasted.code) { return Promise.reject( @@ -487,7 +487,7 @@ export class StargateClient { } private async txsQuery(query: string): Promise { - const results = await this.forceGetTmClient().txSearchAll({ query: query }); + const results = await this.forceGetCometClient().txSearchAll({ query: query }); return results.txs.map((tx): IndexedTx => { const txMsgData = TxMsgData.decode(tx.result.data ?? new Uint8Array()); return { From 77d7ef3640749f085110b2ec9be29f4236dee114 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 25 Oct 2023 15:37:53 +0200 Subject: [PATCH 3/3] Test against cometbft/cometbft:v0.38.0 --- packages/tendermint-rpc/src/testutil.spec.ts | 2 +- scripts/tendermint/all_start.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/tendermint-rpc/src/testutil.spec.ts b/packages/tendermint-rpc/src/testutil.spec.ts index 97ba2c85..e47bead8 100644 --- a/packages/tendermint-rpc/src/testutil.spec.ts +++ b/packages/tendermint-rpc/src/testutil.spec.ts @@ -68,7 +68,7 @@ export const tendermintInstances = { blockTime: 500, expected: { chainId: /^dockerchain$/, - version: /^0\.38\.0-rc3$/, + version: /^0\.38\.0$/, appCreator: "Cosmoshi Netowoko", p2pVersion: 8, blockVersion: 11, diff --git a/scripts/tendermint/all_start.sh b/scripts/tendermint/all_start.sh index 0b7e1622..736beea9 100755 --- a/scripts/tendermint/all_start.sh +++ b/scripts/tendermint/all_start.sh @@ -8,7 +8,7 @@ command -v shellcheck >/dev/null && shellcheck "$0" declare -a TM_IMAGES TM_IMAGES[34]="tendermint/tendermint:v0.34.19" TM_IMAGES[37]="cometbft/cometbft:v0.37.0-rc3" -TM_IMAGES[38]="cometbft/cometbft:v0.38.0-rc3" +TM_IMAGES[38]="cometbft/cometbft:v0.38.0" declare -a TM_ROOTS TM_ROOTS[34]="/tendermint"