Internalize Client.detectVersion and add Client.create

This commit is contained in:
Simon Warta 2020-11-18 14:06:12 +01:00
parent 0d4531bf4e
commit 453f634662
4 changed files with 41 additions and 14 deletions

View File

@ -13,6 +13,10 @@
- @cosmjs/proto-signing: Add new package for handling transaction signing with
protobuf encoding.
- @cosmjs/stargate: Add new package for Cosmos SDK Stargate support.
- @cosmjs/tendermint-rpc: Make `Client.detectVersion` private and let it return
a version instead of a client.
- @cosmjs/tendermint-rpc: Add `Client.create` for creating a Tendermint client
given an RPC client.
## 0.23.1 (2020-10-27)

View File

@ -42,6 +42,16 @@ function randomString(): string {
}
function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, expected: ExpectedValues): void {
describe("create", () => {
it("can auto-discover Tendermint version and communicate", async () => {
pendingWithoutTendermint();
const client = await Client.create(rpcFactory());
const info = await client.abciInfo();
expect(info).toBeTruthy();
client.disconnect();
});
});
it("can connect to tendermint with known version", async () => {
pendingWithoutTendermint();
const client = new Client(rpcFactory(), adaptor);
@ -49,14 +59,6 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, expecte
client.disconnect();
});
it("can auto-discover tendermint version and connect", async () => {
pendingWithoutTendermint();
const client = await Client.detectVersion(rpcFactory());
const info = await client.abciInfo();
expect(info).toBeTruthy();
client.disconnect();
});
it("can get genesis", async () => {
pendingWithoutTendermint();
const client = new Client(rpcFactory(), adaptor);

View File

@ -14,13 +14,26 @@ import {
} from "./rpcclients";
export class Client {
/**
* Creates a new Tendermint client for the given endpoint.
*
* Uses HTTP when the URL schema is http or https. Uses WebSockets otherwise.
*/
public static async connect(url: string): Promise<Client> {
const useHttp = url.startsWith("http://") || url.startsWith("https://");
const client = useHttp ? new HttpClient(url) : new WebsocketClient(url);
return this.detectVersion(client);
const rpcClient = useHttp ? new HttpClient(url) : new WebsocketClient(url);
return Client.create(rpcClient);
}
public static async detectVersion(client: RpcClient): Promise<Client> {
/**
* Creates a new Tendermint client given an RPC client.
*/
public static async create(rpcClient: RpcClient): Promise<Client> {
const version = await this.detectVersion(rpcClient);
return new Client(rpcClient, adaptorForVersion(version));
}
private static async detectVersion(client: RpcClient): Promise<string> {
const req = createJsonRpcRequest(requests.Method.Status);
const response = await client.execute(req);
const result = response.result;
@ -33,8 +46,7 @@ export class Client {
if (typeof version !== "string") {
throw new Error("Unrecognized version format: must be string");
}
return new Client(client, adaptorForVersion(version));
return version;
}
private readonly client: RpcClient;

View File

@ -4,8 +4,17 @@ import * as requests from "./requests";
import * as responses from "./responses";
import { RpcClient } from "./rpcclients";
export declare class Client {
/**
* Creates a new Tendermint client for the given endpoint.
*
* Uses HTTP when the URL schema is http or https. Uses WebSockets otherwise.
*/
static connect(url: string): Promise<Client>;
static detectVersion(client: RpcClient): Promise<Client>;
/**
* Creates a new Tendermint client given an RPC client.
*/
static create(rpcClient: RpcClient): Promise<Client>;
private static detectVersion;
private readonly client;
private readonly p;
private readonly r;