Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poll node in batch to get validator indices #2730

Merged
merged 5 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions packages/validator/src/services/indices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,22 @@ export class IndicesService {
}

// Query the remote BN to resolve a pubkey to a validator index.
const pubkeysHex = pubkeysToPoll.map((pubkey) => toHexString(pubkey));
const validatorsState = await this.api.beacon.getStateValidators("head", {indices: pubkeysHex});
// support up to 1000 pubkeys per poll
const pubkeysHex = pubkeysToPoll.map((pubkey) => toHexString(pubkey)).slice(0, MAX_PUBKEYS_PER_POLL);
const batches = pubkeysToBatches(pubkeysHex);

const newIndicesArr = [];
for (const batch of batches) {
const validatorIndicesArr = await Promise.all(batch.map(this.getIndicesPerHttpRequest));
newIndicesArr.push(...validatorIndicesArr.flat());
}
const newIndices = newIndicesArr.flat();
this.logger.info("Discovered new validators", {count: newIndices.length});
return newIndices;
}

private getIndicesPerHttpRequest = async (pubkeysHex: string[]): Promise<ValidatorIndex[]> => {
const validatorsState = await this.api.beacon.getStateValidators("head", {indices: pubkeysHex});
const newIndices = [];
for (const validatorState of validatorsState.data) {
const pubkeyHex = toHexString(validatorState.validator.pubkey);
Expand All @@ -71,7 +84,42 @@ export class IndicesService {
newIndices.push(validatorState.index);
}
}

return newIndices;
};
}

type Batch = string[][];
const PUBKEYS_PER_REQUEST = 10;
const REQUESTS_PER_BATCH = 5;
const MAX_PUBKEYS_PER_POLL = 5 * PUBKEYS_PER_REQUEST * REQUESTS_PER_BATCH;

/**
* Divide pubkeys into batches, each batch contains at most 5 http requests,
* each request can work on at most 40 pubkeys.
* @param pubkeysHex
*/
export function pubkeysToBatches(
pubkeysHex: string[],
maxPubkeysPerRequest: number = PUBKEYS_PER_REQUEST,
maxRequesPerBatch: number = REQUESTS_PER_BATCH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo maxRequestsPerBatch

): Batch[] {
if (!pubkeysHex || pubkeysHex.length === 0) {
return [[[]]];
twoeths marked this conversation as resolved.
Show resolved Hide resolved
}
const batches: Batch[] = [];

const pubkeysPerBatch = maxPubkeysPerRequest * maxRequesPerBatch;
let batch: Batch = [];
let pubkeysPerRequest: string[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can find a similar approach without mutating scoped variables? You could have two for loops and scope the arrays inside them only with const

for (let i = 0; i < pubkeysHex.length; i += maxPubkeysPerRequest) {
if (i % pubkeysPerBatch === 0) {
batch = [];
batches.push(batch);
}
if (i % maxPubkeysPerRequest === 0) {
pubkeysPerRequest = pubkeysHex.slice(i, Math.min(i + maxPubkeysPerRequest, pubkeysHex.length));
batch.push(pubkeysPerRequest);
}
}
return batches;
}
50 changes: 50 additions & 0 deletions packages/validator/test/unit/services/indices.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {expect} from "chai";
import {pubkeysToBatches} from "../../../src/services/indices";

describe("pubkeysToBatches", function () {
const testCases: {pubkeys: string[]; expected: string[][][]}[] = [
{pubkeys: [], expected: [[[]]]},
{pubkeys: ["1"], expected: [[["1"]]]},
{pubkeys: ["1", "2"], expected: [[["1", "2"]]]},
{pubkeys: ["1", "2", "3"], expected: [[["1", "2"], ["3"]]]},
{
pubkeys: ["1", "2", "3", "4"],
expected: [
[
["1", "2"],
["3", "4"],
],
],
},
{pubkeys: ["1", "2", "3", "4", "5"], expected: [[["1", "2"], ["3", "4"], ["5"]]]},
{
pubkeys: ["1", "2", "3", "4", "5", "6"],
expected: [
[
["1", "2"],
["3", "4"],
["5", "6"],
],
],
},
// new batch
{
pubkeys: ["1", "2", "3", "4", "5", "6", "7"],
expected: [
[
["1", "2"],
["3", "4"],
["5", "6"],
],
[["7"]],
],
},
];
const maxPubkeysPerRequest = 2;
const maxRequestsPerBatch = 3;
for (const {pubkeys, expected} of testCases) {
it(`should work with ${pubkeys.length} pubkeys`, () => {
expect(pubkeysToBatches(pubkeys, maxPubkeysPerRequest, maxRequestsPerBatch)).to.be.deep.equals(expected);
});
}
});