From 3e41b9ce048ca6c3dcda498e61de18658e25c445 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Thu, 18 Feb 2021 10:43:03 +0000 Subject: [PATCH] tendermint-rpc: Update validatorsAll for review --- packages/tendermint-rpc/src/client.spec.ts | 2 +- packages/tendermint-rpc/src/client.ts | 25 ++++++++++------------ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/tendermint-rpc/src/client.spec.ts b/packages/tendermint-rpc/src/client.spec.ts index 5eef03cf..c9b523ea 100644 --- a/packages/tendermint-rpc/src/client.spec.ts +++ b/packages/tendermint-rpc/src/client.spec.ts @@ -151,7 +151,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, expecte it("can get all validators", async () => { pendingWithoutTendermint(); const client = await Client.create(rpcFactory(), adaptor); - const response = await client.validatorsAll({}); + const response = await client.validatorsAll(); expect(response).toBeTruthy(); expect(response.blockHeight).toBeGreaterThanOrEqual(1); diff --git a/packages/tendermint-rpc/src/client.ts b/packages/tendermint-rpc/src/client.ts index 299d4960..945db730 100644 --- a/packages/tendermint-rpc/src/client.ts +++ b/packages/tendermint-rpc/src/client.ts @@ -257,25 +257,21 @@ export class Client { return this.doCall(query, this.p.encodeValidators, this.r.decodeValidators); } - public async validatorsAll(params: requests.ValidatorsParams): Promise { - let page = params.page || 1; + public async validatorsAll(height?: number): Promise { const validators: responses.Validator[] = []; - const baseParams = { - per_page: 50, - height: params.height ?? (await this.status()).syncInfo.latestBlockHeight, - ...params, - }; + let page = 1; let done = false; - let blockHeight = 0; + let blockHeight = height; while (!done) { - const resp = await this.validators({ - ...baseParams, + const response = await this.validators({ + per_page: 50, + height: blockHeight, page: page, }); - validators.push(...resp.validators); - blockHeight = resp.blockHeight; - if (validators.length < resp.total) { + validators.push(...response.validators); + blockHeight = blockHeight || response.blockHeight; + if (validators.length < response.total) { page++; } else { done = true; @@ -283,7 +279,8 @@ export class Client { } return { - blockHeight: blockHeight, + // NOTE: Default value is for type safety but this should always be set + blockHeight: blockHeight ?? 0, count: validators.length, total: validators.length, validators: validators,