Skip to content

Commit

Permalink
Merge pull request #1466 from cosmos/tendermint-0.38
Browse files Browse the repository at this point in the history
Create CometBFT 0.38 client and auto-detection
  • Loading branch information
webmaster128 committed Sep 7, 2023
2 parents a242608 + efdf198 commit 92a0c10
Show file tree
Hide file tree
Showing 36 changed files with 3,937 additions and 341 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ and this project adheres to

## [Unreleased]

### Changed

- @cosmjs/tendermint-rpc: Remove `Adaptor` abstractions which are not needed
anymore by haing a dedicated client for each backend.
- @cosmjs/tendermint-rpc: Add
`CometClient = Tendermint34Client | Tendermint37Client | Comet38Client` and
`connectComet` for auto-detecting the right client for a provided endpoint.

### Deprecated

- @cosmjs/tendermint-rpc: `CometClient` should be used instead of
`TendermintClient`.

## [0.31.1] - 2023-08-21

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("CosmWasmClient", () => {
pendingWithoutWasmd();
const client = await CosmWasmClient.connect(wasmd.endpoint);
const openedClient = client as unknown as PrivateCosmWasmClient;
const getCodeSpy = spyOn(openedClient.tmClient!, "status").and.callThrough();
const getCodeSpy = spyOn(openedClient.cometClient!, "status").and.callThrough();

expect(await client.getChainId()).toEqual(wasmd.chainId); // from network
expect(await client.getChainId()).toEqual(wasmd.chainId); // from cache
Expand Down
55 changes: 18 additions & 37 deletions packages/cosmwasm-stargate/src/cosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ import {
TimeoutError,
TxExtension,
} from "@cosmjs/stargate";
import {
HttpEndpoint,
Tendermint34Client,
Tendermint37Client,
TendermintClient,
toRfc3339WithNanoseconds,
} from "@cosmjs/tendermint-rpc";
import { CometClient, connectComet, HttpEndpoint, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc";
import { assert, sleep } from "@cosmjs/utils";
import { TxMsgData } from "cosmjs-types/cosmos/base/abci/v1beta1/abci";
import {
Expand Down Expand Up @@ -81,14 +75,14 @@ export interface ContractCodeHistoryEntry {

/** Use for testing only */
export interface PrivateCosmWasmClient {
readonly tmClient: TendermintClient | undefined;
readonly cometClient: CometClient | undefined;
readonly queryClient:
| (QueryClient & AuthExtension & BankExtension & TxExtension & WasmExtension)
| undefined;
}

export class CosmWasmClient {
private readonly tmClient: TendermintClient | undefined;
private readonly cometClient: CometClient | undefined;
private readonly queryClient:
| (QueryClient & AuthExtension & BankExtension & TxExtension & WasmExtension)
| undefined;
Expand All @@ -102,34 +96,23 @@ export class CosmWasmClient {
* To set the Tendermint client explicitly, use `create`.
*/
public static async connect(endpoint: string | HttpEndpoint): Promise<CosmWasmClient> {
// 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 CosmWasmClient.create(tmClient);
const cometClient = await connectComet(endpoint);
return CosmWasmClient.create(cometClient);
}

/**
* Creates an instance from a manually created Tendermint client.
* Use this to use `Tendermint37Client` instead of `Tendermint34Client`.
*/
public static async create(tmClient: TendermintClient): Promise<CosmWasmClient> {
return new CosmWasmClient(tmClient);
public static async create(cometClient: CometClient): Promise<CosmWasmClient> {
return new CosmWasmClient(cometClient);
}

protected constructor(tmClient: TendermintClient | undefined) {
if (tmClient) {
this.tmClient = tmClient;
protected constructor(cometClient: CometClient | undefined) {
if (cometClient) {
this.cometClient = cometClient;
this.queryClient = QueryClient.withExtensions(
tmClient,
cometClient,
setupAuthExtension,
setupBankExtension,
setupWasmExtension,
Expand All @@ -138,17 +121,15 @@ export class CosmWasmClient {
}
}

protected getTmClient(): TendermintClient | undefined {
return this.tmClient;
protected getTmClient(): CometClient | undefined {
return this.cometClient;
}

protected forceGetTmClient(): TendermintClient {
if (!this.tmClient) {
throw new Error(
"Tendermint client not available. You cannot use online functionality in offline mode.",
);
protected forceGetTmClient(): CometClient {
if (!this.cometClient) {
throw new Error("Comet client not available. You cannot use online functionality in offline mode.");
}
return this.tmClient;
return this.cometClient;
}

protected getQueryClient():
Expand Down Expand Up @@ -244,7 +225,7 @@ export class CosmWasmClient {
}

public disconnect(): void {
if (this.tmClient) this.tmClient.disconnect();
if (this.cometClient) this.cometClient.disconnect();
}

/**
Expand Down
30 changes: 7 additions & 23 deletions packages/cosmwasm-stargate/src/signingcosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ import {
SignerData,
StdFee,
} from "@cosmjs/stargate";
import {
HttpEndpoint,
Tendermint34Client,
Tendermint37Client,
TendermintClient,
} from "@cosmjs/tendermint-rpc";
import { CometClient, connectComet, HttpEndpoint } from "@cosmjs/tendermint-rpc";
import { assert, assertDefined } from "@cosmjs/utils";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import { MsgDelegate, MsgUndelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx";
Expand Down Expand Up @@ -196,31 +191,20 @@ export class SigningCosmWasmClient extends CosmWasmClient {
signer: OfflineSigner,
options: SigningCosmWasmClientOptions = {},
): Promise<SigningCosmWasmClient> {
// 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 SigningCosmWasmClient.createWithSigner(tmClient, signer, options);
const cometClient = await connectComet(endpoint);
return SigningCosmWasmClient.createWithSigner(cometClient, signer, options);
}

/**
* Creates an instance from a manually created Tendermint client.
* Use this to use `Tendermint37Client` instead of `Tendermint34Client`.
*/
public static async createWithSigner(
tmClient: TendermintClient,
cometClient: CometClient,
signer: OfflineSigner,
options: SigningCosmWasmClientOptions = {},
): Promise<SigningCosmWasmClient> {
return new SigningCosmWasmClient(tmClient, signer, options);
return new SigningCosmWasmClient(cometClient, signer, options);
}

/**
Expand All @@ -240,11 +224,11 @@ export class SigningCosmWasmClient extends CosmWasmClient {
}

protected constructor(
tmClient: TendermintClient | undefined,
cometClient: CometClient | undefined,
signer: OfflineSigner,
options: SigningCosmWasmClientOptions,
) {
super(tmClient);
super(cometClient);
const {
registry = new Registry([...defaultStargateTypes, ...wasmTypes]),
aminoTypes = new AminoTypes({
Expand Down
Loading

0 comments on commit 92a0c10

Please sign in to comment.