-
-
Notifications
You must be signed in to change notification settings - Fork 311
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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 | ||
): 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[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
maxRequestsPerBatch