-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache and reuse old validator registation data if no params change
- Loading branch information
Showing
5 changed files
with
150 additions
and
6 deletions.
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
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
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
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,49 @@ | ||
import {bellatrix} from "@lodestar/types"; | ||
import {PubkeyHex} from "../types.js"; | ||
import {pruneSetToMax} from "./map.js"; | ||
|
||
/** Maximum number of validators that can connect to a single validator process */ | ||
const MAX_REGISTRATION_IDS = 1_00_000; | ||
type RegistrationKeyAttributes = { | ||
pubKey: PubkeyHex; | ||
feeRecipient: string; | ||
gasLimit: number; | ||
}; | ||
|
||
export class ValidatorRegistrationCache { | ||
private readonly validatorRegistrationMap = new Map< | ||
string, | ||
{validatorRegistration: bellatrix.SignedValidatorRegistrationV1; fullKey: string} | ||
>(); | ||
|
||
getKey({pubKey}: Pick<RegistrationKeyAttributes, "pubKey">): string { | ||
return pubKey; | ||
} | ||
|
||
getFullKey({pubKey, feeRecipient, gasLimit}: RegistrationKeyAttributes): string { | ||
return `${pubKey}-${feeRecipient}-${gasLimit}`; | ||
} | ||
|
||
add(regAttributes: RegistrationKeyAttributes, validatorRegistration: bellatrix.SignedValidatorRegistrationV1): void { | ||
const key = this.getKey(regAttributes); | ||
const fullKey = this.getFullKey(regAttributes); | ||
this.validatorRegistrationMap.set(key, {validatorRegistration, fullKey}); | ||
} | ||
|
||
prune(): void { | ||
// This is not so optimized function, but could maintain a 2d array may be? | ||
pruneSetToMax(this.validatorRegistrationMap, MAX_REGISTRATION_IDS); | ||
} | ||
|
||
get(regAttributes: RegistrationKeyAttributes): bellatrix.SignedValidatorRegistrationV1 | undefined { | ||
const key = this.getKey(regAttributes); | ||
const fullKey = this.getFullKey(regAttributes); | ||
const regData = this.validatorRegistrationMap.get(key); | ||
return regData?.fullKey === fullKey ? regData.validatorRegistration : undefined; | ||
} | ||
|
||
has(pubKey: PubkeyHex): boolean { | ||
const key = this.getKey({pubKey}); | ||
return this.validatorRegistrationMap.get(key) !== undefined; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/validator/test/unit/services/getValidatorRegistration.test.ts
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,56 @@ | ||
import {expect} from "chai"; | ||
import sinon from "sinon"; | ||
import bls from "@chainsafe/bls"; | ||
import {toHexString, fromHexString} from "@chainsafe/ssz"; | ||
import {chainConfig} from "@lodestar/config/default"; | ||
import {ValidatorStore} from "../../../src/services/validatorStore.js"; | ||
import {getApiClientStub} from "../../utils/apiStub.js"; | ||
import {initValidatorStore} from "../../utils/validatorStore.js"; | ||
|
||
describe("getValidatorRegistration", function () { | ||
const sandbox = sinon.createSandbox(); | ||
const api = getApiClientStub(sandbox); | ||
|
||
let pubkeys: string[]; // Initialize pubkeys in before() so bls is already initialized | ||
let validatorStore: ValidatorStore; | ||
let signValidatorStub: sinon.SinonStub<any>; | ||
|
||
before(() => { | ||
const secretKeys = Array.from({length: 1}, (_, i) => bls.SecretKey.fromBytes(Buffer.alloc(32, i + 1))); | ||
pubkeys = secretKeys.map((sk) => toHexString(sk.toPublicKey().toBytes())); | ||
|
||
validatorStore = initValidatorStore(secretKeys, api, chainConfig); | ||
|
||
signValidatorStub = sinon.stub(validatorStore, "signValidatorRegistration").resolves({ | ||
message: { | ||
feeRecipient: fromHexString("0x00"), | ||
gasLimit: 10000, | ||
timestamp: Date.now(), | ||
pubkey: fromHexString(pubkeys[0]), | ||
}, | ||
signature: Buffer.alloc(96, 0), | ||
}); | ||
}); | ||
|
||
after(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it("Should update cache and return from cache next time", async () => { | ||
const slot = 0; | ||
const val1 = validatorStore.getValidatorRegistration(pubkeys[0], "0x00", slot); | ||
await new Promise((r) => setTimeout(r, 10)); | ||
expect(validatorStore["validatorRegistrationCache"].has(pubkeys[0])).to.be.true; | ||
expect(signValidatorStub.callCount).to.equal(1, "signValidatorRegistration() must be called once after 1st call"); | ||
|
||
const val2 = validatorStore.getValidatorRegistration(pubkeys[0], "0x00", slot); | ||
expect(JSON.stringify(val1) === JSON.stringify(val2)); | ||
expect(signValidatorStub.callCount).to.equal( | ||
1, | ||
"signValidatorRegistration() must be called once even after 2nd call" | ||
); | ||
|
||
await validatorStore.getValidatorRegistration(pubkeys[0], "0x10", slot); | ||
expect(signValidatorStub.callCount).to.equal(2, "signValidatorRegistration() must be called twice"); | ||
}); | ||
}); |