Merge pull request #1495 from cosmos/more-cometclient2

More renamings and refactorings from Tendermint -> Comet (2)
This commit is contained in:
Simon Warta 2023-10-26 16:06:49 +02:00 committed by GitHub
commit 8db0ef9a88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 59 deletions

View File

@ -18,6 +18,20 @@ 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. 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. Rename
`CosmWasmClient.getTmClient`/`.forceGetTmClient` to
`.getCometClient`/`.forceGetCometClient`.
[#1421]: https://github.com/cosmos/cosmjs/issues/1421
[#1484]: https://github.com/cosmos/cosmjs/pull/1484

View File

@ -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<CosmWasmClient> {
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<CosmWasmClient> {
return new CosmWasmClient(cometClient);
@ -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<string> {
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<number> {
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<Block> {
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<string> {
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<IndexedTx[]> {
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 {

View File

@ -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,

View File

@ -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<SigningStargateClient> {
// 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<SigningStargateClient> {
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()),

View File

@ -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,
@ -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<string> {
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<number> {
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<Block> {
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<string> {
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<IndexedTx[]> {
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 {

View File

@ -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,

View File

@ -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"