tendermint-rpc: Add validatorsAll query

This commit is contained in:
willclarktech 2021-02-10 18:09:46 +00:00
parent 484fdc2fd0
commit c5adeace6d
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
2 changed files with 48 additions and 0 deletions

View File

@ -148,6 +148,24 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, expecte
client.disconnect();
});
it("can get all validators", async () => {
pendingWithoutTendermint();
const client = await Client.create(rpcFactory(), adaptor);
const response = await client.validatorsAll({});
expect(response).toBeTruthy();
expect(response.blockHeight).toBeGreaterThanOrEqual(1);
expect(response.count).toBeGreaterThanOrEqual(1);
expect(response.total).toBeGreaterThanOrEqual(1);
expect(response.validators.length).toBeGreaterThanOrEqual(1);
expect(response.validators[0].address.length).toEqual(20);
expect(response.validators[0].pubkey).toBeDefined();
expect(response.validators[0].votingPower).toBeGreaterThanOrEqual(0);
expect(response.validators[0].proposerPriority).toBeGreaterThanOrEqual(0);
client.disconnect();
});
it("can call a bunch of methods", async () => {
pendingWithoutTendermint();
const client = await Client.create(rpcFactory(), adaptor);

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Stream } from "xstream";
import { Adaptor, Decoder, Encoder, Params, Responses } from "./adaptor";
@ -256,6 +257,35 @@ export class Client {
return this.doCall(query, this.p.encodeValidators, this.r.decodeValidators);
}
public async validatorsAll(params: requests.ValidatorsParams): Promise<responses.ValidatorsResponse> {
let page = params.page || 1;
const validators: responses.Validator[] = [];
let done = false;
let blockHeight = 0;
while (!done) {
const resp = await this.validators({
per_page: 50,
...params,
page: page,
});
validators.push(...resp.validators);
blockHeight = resp.blockHeight;
if (validators.length < resp.total) {
page++;
} else {
done = true;
}
}
return {
blockHeight: blockHeight,
count: validators.length,
total: validators.length,
validators: validators,
};
}
// doCall is a helper to handle the encode/call/decode logic
private async doCall<T extends requests.Request, U extends responses.Response>(
request: T,