-
-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathvalidatorStore.ts
More file actions
840 lines (724 loc) · 28.9 KB
/
validatorStore.ts
File metadata and controls
840 lines (724 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
import {SecretKey} from "@chainsafe/blst";
import {BitArray} from "@chainsafe/ssz";
import {routes} from "@lodestar/api";
import {BeaconConfig} from "@lodestar/config";
import {
DOMAIN_AGGREGATE_AND_PROOF,
DOMAIN_APPLICATION_BUILDER,
DOMAIN_BEACON_ATTESTER,
DOMAIN_BEACON_PROPOSER,
DOMAIN_CONTRIBUTION_AND_PROOF,
DOMAIN_RANDAO,
DOMAIN_SELECTION_PROOF,
DOMAIN_SYNC_COMMITTEE,
DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF,
ForkSeq,
MAX_COMMITTEES_PER_SLOT,
} from "@lodestar/params";
import {
ZERO_HASH,
blindedOrFullBlockHashTreeRoot,
computeDomain,
computeEpochAtSlot,
computeSigningRoot,
computeStartSlotAtEpoch,
} from "@lodestar/state-transition";
import {
AggregateAndProof,
Attestation,
BLSPubkey,
BLSSignature,
BeaconBlock,
BlindedBeaconBlock,
Epoch,
Root,
SignedAggregateAndProof,
SignedBeaconBlock,
SignedBlindedBeaconBlock,
Slot,
ValidatorIndex,
altair,
bellatrix,
phase0,
ssz,
} from "@lodestar/types";
import {fromHex, toPubkeyHex, toRootHex} from "@lodestar/utils";
import {Metrics} from "../metrics.js";
import {ISlashingProtection} from "../slashingProtection/index.js";
import {PubkeyHex} from "../types.js";
import {SignableMessage, SignableMessageType, externalSignerPostSignature} from "../util/externalSignerClient.js";
import {isValidatePubkeyHex} from "../util/format.js";
import {LoggerVc} from "../util/logger.js";
import {DoppelgangerService} from "./doppelgangerService.js";
import {IndicesService} from "./indices.js";
type BLSPubkeyMaybeHex = BLSPubkey | PubkeyHex;
type Eth1Address = string;
export enum SignerType {
Local,
Remote,
}
export type SignerLocal = {
type: SignerType.Local;
secretKey: SecretKey;
};
export type SignerRemote = {
type: SignerType.Remote;
url: string;
pubkey: PubkeyHex;
};
type DefaultProposerConfig = {
graffiti?: string;
strictFeeRecipientCheck: boolean;
feeRecipient: Eth1Address;
builder: {
gasLimit: number;
selection: routes.validator.BuilderSelection;
boostFactor: bigint;
};
};
export type ProposerConfig = {
graffiti?: string;
strictFeeRecipientCheck?: boolean;
feeRecipient?: Eth1Address;
builder?: {
gasLimit?: number;
selection?: routes.validator.BuilderSelection;
boostFactor?: bigint;
};
};
export type ValidatorProposerConfig = {
proposerConfig: {[index: PubkeyHex]: ProposerConfig};
defaultConfig: ProposerConfig;
};
export type ValidatorStoreModules = {
config: BeaconConfig;
slashingProtection: ISlashingProtection;
indicesService: IndicesService;
doppelgangerService: DoppelgangerService | null;
metrics: Metrics | null;
};
/**
* This cache stores SignedValidatorRegistrationV1 data for a validator so that
* we do not create and send new registration objects to avoid DOSing the builder
*
* See: https://github.com/ChainSafe/lodestar/issues/4208
*/
type BuilderData = {
validatorRegistration: bellatrix.SignedValidatorRegistrationV1;
regFullKey: string;
};
/**
* Validator entity capable of producing signatures. Either:
* - local: With BLS secret key
* - remote: With data to contact a remote signer
*/
export type Signer = SignerLocal | SignerRemote;
type ValidatorData = ProposerConfig & {
signer: Signer;
builderData?: BuilderData;
};
export const defaultOptions = {
suggestedFeeRecipient: "0x0000000000000000000000000000000000000000",
defaultGasLimit: 36_000_000,
builderSelection: routes.validator.BuilderSelection.ExecutionOnly,
builderAliasSelection: routes.validator.BuilderSelection.Default,
builderBoostFactor: BigInt(100),
// spec asks for gossip validation by default
broadcastValidation: routes.beacon.BroadcastValidation.gossip,
// should request fetching the locally produced block in blinded format
blindedLocal: false,
};
export const MAX_BUILDER_BOOST_FACTOR = 2n ** 64n - 1n;
/**
* Service that sets up and handles validator attester duties.
*/
export class ValidatorStore {
private readonly config: BeaconConfig;
private readonly slashingProtection: ISlashingProtection;
private readonly indicesService: IndicesService;
private readonly doppelgangerService: DoppelgangerService | null;
private readonly metrics: Metrics | null;
private readonly validators = new Map<PubkeyHex, ValidatorData>();
/** Initially true because there are no validators */
private pubkeysToDiscover: PubkeyHex[] = [];
private readonly defaultProposerConfig: DefaultProposerConfig;
constructor(modules: ValidatorStoreModules, valProposerConfig: ValidatorProposerConfig) {
const {config, slashingProtection, indicesService, doppelgangerService, metrics} = modules;
this.config = config;
this.slashingProtection = slashingProtection;
this.indicesService = indicesService;
this.doppelgangerService = doppelgangerService;
this.metrics = metrics;
const defaultConfig = valProposerConfig.defaultConfig;
const builderBoostFactor = defaultConfig.builder?.boostFactor ?? defaultOptions.builderBoostFactor;
if (builderBoostFactor > MAX_BUILDER_BOOST_FACTOR) {
throw Error(`Invalid builderBoostFactor=${builderBoostFactor} > MAX_BUILDER_BOOST_FACTOR for defaultConfig`);
}
this.defaultProposerConfig = {
graffiti: defaultConfig.graffiti,
strictFeeRecipientCheck: defaultConfig.strictFeeRecipientCheck ?? false,
feeRecipient: defaultConfig.feeRecipient ?? defaultOptions.suggestedFeeRecipient,
builder: {
gasLimit: defaultConfig.builder?.gasLimit ?? defaultOptions.defaultGasLimit,
selection: defaultConfig.builder?.selection ?? defaultOptions.builderSelection,
boostFactor: builderBoostFactor,
},
};
if (metrics) {
metrics.signers.addCollect(() => metrics.signers.set(this.validators.size));
}
}
/**
* Create a validator store with initial signers
*/
static async init(
modules: ValidatorStoreModules,
initialSigners: Signer[],
valProposerConfig: ValidatorProposerConfig = {defaultConfig: {}, proposerConfig: {}}
): Promise<ValidatorStore> {
const validatorStore = new ValidatorStore(modules, valProposerConfig);
await Promise.all(initialSigners.map((signer) => validatorStore.addSigner(signer, valProposerConfig)));
return validatorStore;
}
/** Return all known indices from the validatorStore pubkeys */
getAllLocalIndices(): ValidatorIndex[] {
return this.indicesService.getAllLocalIndices();
}
getPubkeyOfIndex(index: ValidatorIndex): PubkeyHex | undefined {
return this.indicesService.index2pubkey.get(index);
}
pollValidatorIndices(): Promise<ValidatorIndex[]> {
// Consumers will call this function every epoch forever. If everyone has been discovered, skip
return this.indicesService.indexCount >= this.validators.size
? Promise.resolve([])
: this.indicesService.pollValidatorIndices(Array.from(this.validators.keys()));
}
getFeeRecipient(pubkeyHex: PubkeyHex): Eth1Address {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
return validatorData.feeRecipient ?? this.defaultProposerConfig.feeRecipient;
}
getFeeRecipientByIndex(index: ValidatorIndex): Eth1Address {
const pubkey = this.indicesService.index2pubkey.get(index);
return pubkey ? this.getFeeRecipient(pubkey) : this.defaultProposerConfig.feeRecipient;
}
setFeeRecipient(pubkeyHex: PubkeyHex, feeRecipient: Eth1Address): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
// This should directly modify data in the map
validatorData.feeRecipient = feeRecipient;
}
deleteFeeRecipient(pubkeyHex: PubkeyHex): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
// This should directly modify data in the map
delete validatorData.feeRecipient;
}
getGraffiti(pubkeyHex: PubkeyHex): string | undefined {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
return validatorData.graffiti ?? this.defaultProposerConfig.graffiti;
}
setGraffiti(pubkeyHex: PubkeyHex, graffiti: string): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
validatorData.graffiti = graffiti;
}
deleteGraffiti(pubkeyHex: PubkeyHex): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
delete validatorData.graffiti;
}
getBuilderSelectionParams(pubkeyHex: PubkeyHex): {selection: routes.validator.BuilderSelection; boostFactor: bigint} {
const selection =
this.validators.get(pubkeyHex)?.builder?.selection ?? this.defaultProposerConfig.builder.selection;
let boostFactor: bigint;
switch (selection) {
case routes.validator.BuilderSelection.Default:
// Default value slightly favors local block to improve censorship resistance of Ethereum
// The people have spoken and so it shall be https://x.com/lodestar_eth/status/1772679499928191044
boostFactor = BigInt(90);
break;
case routes.validator.BuilderSelection.MaxProfit:
boostFactor =
this.validators.get(pubkeyHex)?.builder?.boostFactor ?? this.defaultProposerConfig.builder.boostFactor;
break;
case routes.validator.BuilderSelection.BuilderAlways:
case routes.validator.BuilderSelection.BuilderOnly:
boostFactor = MAX_BUILDER_BOOST_FACTOR;
break;
case routes.validator.BuilderSelection.ExecutionAlways:
case routes.validator.BuilderSelection.ExecutionOnly:
boostFactor = BigInt(0);
}
return {selection, boostFactor};
}
strictFeeRecipientCheck(pubkeyHex: PubkeyHex): boolean {
return (
this.validators.get(pubkeyHex)?.strictFeeRecipientCheck ?? this.defaultProposerConfig?.strictFeeRecipientCheck
);
}
getGasLimit(pubkeyHex: PubkeyHex): number {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
return validatorData?.builder?.gasLimit ?? this.defaultProposerConfig.builder.gasLimit;
}
setGasLimit(pubkeyHex: PubkeyHex, gasLimit: number): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
validatorData.builder = {...validatorData.builder, gasLimit};
}
deleteGasLimit(pubkeyHex: PubkeyHex): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
delete validatorData.builder?.gasLimit;
}
getBuilderBoostFactor(pubkeyHex: PubkeyHex): bigint {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
return validatorData?.builder?.boostFactor ?? this.defaultProposerConfig.builder.boostFactor;
}
setBuilderBoostFactor(pubkeyHex: PubkeyHex, boostFactor: bigint): void {
if (boostFactor > MAX_BUILDER_BOOST_FACTOR) {
throw Error(`Invalid builderBoostFactor=${boostFactor} > MAX_BUILDER_BOOST_FACTOR`);
}
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
validatorData.builder = {...validatorData.builder, boostFactor};
}
deleteBuilderBoostFactor(pubkeyHex: PubkeyHex): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
delete validatorData.builder?.boostFactor;
}
/** Return true if `index` is active part of this validator client */
hasValidatorIndex(index: ValidatorIndex): boolean {
return this.indicesService.index2pubkey.has(index);
}
getProposerConfig(pubkeyHex: PubkeyHex): ProposerConfig | null {
let proposerConfig: ProposerConfig | null = null;
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
const {graffiti, strictFeeRecipientCheck, feeRecipient, builder} = validatorData;
// if anything is set , i.e not default then return
if (
graffiti !== undefined ||
strictFeeRecipientCheck !== undefined ||
feeRecipient !== undefined ||
builder?.gasLimit !== undefined ||
builder?.selection !== undefined ||
builder?.boostFactor !== undefined
) {
proposerConfig = {graffiti, strictFeeRecipientCheck, feeRecipient, builder};
}
return proposerConfig;
}
async addSigner(signer: Signer, valProposerConfig?: ValidatorProposerConfig): Promise<void> {
const pubkey = getSignerPubkeyHex(signer);
const proposerConfig = valProposerConfig?.proposerConfig?.[pubkey];
const builderBoostFactor = proposerConfig?.builder?.boostFactor;
if (builderBoostFactor !== undefined && builderBoostFactor > MAX_BUILDER_BOOST_FACTOR) {
throw Error(`Invalid builderBoostFactor=${builderBoostFactor} > MAX_BUILDER_BOOST_FACTOR for pubkey=${pubkey}`);
}
if (!this.validators.has(pubkey)) {
// Doppelganger registration must be done before adding validator to signers
await this.doppelgangerService?.registerValidator(pubkey);
this.pubkeysToDiscover.push(pubkey);
this.validators.set(pubkey, {
signer,
...proposerConfig,
});
}
}
getSigner(pubkeyHex: PubkeyHex): Signer | undefined {
return this.validators.get(pubkeyHex)?.signer;
}
removeSigner(pubkeyHex: PubkeyHex): boolean {
this.doppelgangerService?.unregisterValidator(pubkeyHex);
return this.indicesService.removeForKey(pubkeyHex) || this.validators.delete(pubkeyHex);
}
/** Return true if there is at least 1 pubkey registered */
hasSomeValidators(): boolean {
return this.validators.size > 0;
}
votingPubkeys(): PubkeyHex[] {
return Array.from(this.validators.keys());
}
hasVotingPubkey(pubkeyHex: PubkeyHex): boolean {
return this.validators.has(pubkeyHex);
}
getRemoteSignerPubkeys(signerUrl: string): PubkeyHex[] {
const pubkeysHex = [];
for (const {signer} of this.validators.values()) {
if (signer.type === SignerType.Remote && signer.url === signerUrl) {
pubkeysHex.push(signer.pubkey);
}
}
return pubkeysHex;
}
async signBlock(
pubkey: BLSPubkey,
blindedOrFull: BeaconBlock | BlindedBeaconBlock,
currentSlot: Slot,
logger?: LoggerVc
): Promise<SignedBeaconBlock | SignedBlindedBeaconBlock> {
// Make sure the block slot is not higher than the current slot to avoid potential attacks.
if (blindedOrFull.slot > currentSlot) {
throw Error(`Not signing block with slot ${blindedOrFull.slot} greater than current slot ${currentSlot}`);
}
// Duties are filtered before-hard by doppelganger-safe, this assert should never throw
this.assertDoppelgangerSafe(pubkey);
const signingSlot = blindedOrFull.slot;
const domain = this.config.getDomain(signingSlot, DOMAIN_BEACON_PROPOSER);
const blockRoot = blindedOrFullBlockHashTreeRoot(this.config, blindedOrFull);
// Don't use `computeSigningRoot()` here to compute the objectRoot in typesafe function blindedOrFullBlockHashTreeRoot()
const signingRoot = ssz.phase0.SigningData.hashTreeRoot({objectRoot: blockRoot, domain});
logger?.debug("Signing the block proposal", {
slot: signingSlot,
blockRoot: toRootHex(blockRoot),
signingRoot: toRootHex(signingRoot),
});
try {
await this.slashingProtection.checkAndInsertBlockProposal(pubkey, {slot: signingSlot, signingRoot});
} catch (e) {
this.metrics?.slashingProtectionBlockError.inc();
throw e;
}
const signableMessage: SignableMessage = {
type: SignableMessageType.BLOCK_V2,
data: blindedOrFull,
};
return {
message: blindedOrFull,
signature: await this.getSignature(pubkey, signingRoot, signingSlot, signableMessage),
} as SignedBeaconBlock | SignedBlindedBeaconBlock;
}
async signRandao(pubkey: BLSPubkey, slot: Slot): Promise<BLSSignature> {
const signingSlot = slot;
const domain = this.config.getDomain(slot, DOMAIN_RANDAO);
const epoch = computeEpochAtSlot(slot);
const signingRoot = computeSigningRoot(ssz.Epoch, epoch, domain);
const signableMessage: SignableMessage = {
type: SignableMessageType.RANDAO_REVEAL,
data: {epoch},
};
return this.getSignature(pubkey, signingRoot, signingSlot, signableMessage);
}
async signAttestation(
duty: routes.validator.AttesterDuty,
attestationData: phase0.AttestationData,
currentEpoch: Epoch
): Promise<Attestation> {
// Make sure the target epoch is not higher than the current epoch to avoid potential attacks.
if (attestationData.target.epoch > currentEpoch) {
throw Error(
`Not signing attestation with target epoch ${attestationData.target.epoch} greater than current epoch ${currentEpoch}`
);
}
// Duties are filtered before-hard by doppelganger-safe, this assert should never throw
this.assertDoppelgangerSafe(duty.pubkey);
this.validateAttestationDuty(duty, attestationData);
const signingSlot = computeStartSlotAtEpoch(attestationData.target.epoch);
const domain = this.config.getDomain(signingSlot, DOMAIN_BEACON_ATTESTER);
const signingRoot = computeSigningRoot(ssz.phase0.AttestationData, attestationData, domain);
try {
await this.slashingProtection.checkAndInsertAttestation(duty.pubkey, {
sourceEpoch: attestationData.source.epoch,
targetEpoch: attestationData.target.epoch,
signingRoot,
});
} catch (e) {
this.metrics?.slashingProtectionAttestationError.inc();
throw e;
}
const signableMessage: SignableMessage = {
type: SignableMessageType.ATTESTATION,
data: attestationData,
};
if (this.config.getForkSeq(signingSlot) >= ForkSeq.electra) {
return {
aggregationBits: BitArray.fromSingleBit(duty.committeeLength, duty.validatorCommitteeIndex),
data: attestationData,
signature: await this.getSignature(duty.pubkey, signingRoot, signingSlot, signableMessage),
committeeBits: BitArray.fromSingleBit(MAX_COMMITTEES_PER_SLOT, duty.committeeIndex),
};
}
return {
aggregationBits: BitArray.fromSingleBit(duty.committeeLength, duty.validatorCommitteeIndex),
data: attestationData,
signature: await this.getSignature(duty.pubkey, signingRoot, signingSlot, signableMessage),
} as phase0.Attestation;
}
async signAggregateAndProof(
duty: routes.validator.AttesterDuty,
selectionProof: BLSSignature,
aggregate: Attestation
): Promise<SignedAggregateAndProof> {
this.validateAttestationDuty(duty, aggregate.data);
const aggregateAndProof: AggregateAndProof = {
aggregate,
aggregatorIndex: duty.validatorIndex,
selectionProof,
};
const signingSlot = aggregate.data.slot;
const domain = this.config.getDomain(signingSlot, DOMAIN_AGGREGATE_AND_PROOF);
const isPostElectra = this.config.getForkSeq(signingSlot) >= ForkSeq.electra;
const signingRoot = isPostElectra
? computeSigningRoot(ssz.electra.AggregateAndProof, aggregateAndProof, domain)
: computeSigningRoot(ssz.phase0.AggregateAndProof, aggregateAndProof, domain);
const signableMessage: SignableMessage = {
type: isPostElectra ? SignableMessageType.AGGREGATE_AND_PROOF_V2 : SignableMessageType.AGGREGATE_AND_PROOF,
data: aggregateAndProof,
};
return {
message: aggregateAndProof,
signature: await this.getSignature(duty.pubkey, signingRoot, signingSlot, signableMessage),
};
}
async signSyncCommitteeSignature(
pubkey: BLSPubkeyMaybeHex,
validatorIndex: ValidatorIndex,
slot: Slot,
beaconBlockRoot: Root
): Promise<altair.SyncCommitteeMessage> {
const signingSlot = slot;
const domain = this.config.getDomain(slot, DOMAIN_SYNC_COMMITTEE);
const signingRoot = computeSigningRoot(ssz.Root, beaconBlockRoot, domain);
const signableMessage: SignableMessage = {
type: SignableMessageType.SYNC_COMMITTEE_MESSAGE,
data: {beaconBlockRoot, slot},
};
return {
slot,
validatorIndex,
beaconBlockRoot,
signature: await this.getSignature(pubkey, signingRoot, signingSlot, signableMessage),
};
}
async signContributionAndProof(
duty: {pubkey: BLSPubkeyMaybeHex; validatorIndex: number},
selectionProof: BLSSignature,
contribution: altair.SyncCommitteeContribution
): Promise<altair.SignedContributionAndProof> {
const contributionAndProof: altair.ContributionAndProof = {
contribution,
aggregatorIndex: duty.validatorIndex,
selectionProof,
};
const signingSlot = contribution.slot;
const domain = this.config.getDomain(signingSlot, DOMAIN_CONTRIBUTION_AND_PROOF);
const signingRoot = computeSigningRoot(ssz.altair.ContributionAndProof, contributionAndProof, domain);
const signableMessage: SignableMessage = {
type: SignableMessageType.SYNC_COMMITTEE_CONTRIBUTION_AND_PROOF,
data: contributionAndProof,
};
return {
message: contributionAndProof,
signature: await this.getSignature(duty.pubkey, signingRoot, signingSlot, signableMessage),
};
}
async signAttestationSelectionProof(pubkey: BLSPubkeyMaybeHex, slot: Slot): Promise<BLSSignature> {
const signingSlot = slot;
const domain = this.config.getDomain(slot, DOMAIN_SELECTION_PROOF);
const signingRoot = computeSigningRoot(ssz.Slot, slot, domain);
const signableMessage: SignableMessage = {
type: SignableMessageType.AGGREGATION_SLOT,
data: {slot},
};
return this.getSignature(pubkey, signingRoot, signingSlot, signableMessage);
}
async signSyncCommitteeSelectionProof(
pubkey: BLSPubkeyMaybeHex,
slot: Slot,
subcommitteeIndex: number
): Promise<BLSSignature> {
const signingSlot = slot;
const domain = this.config.getDomain(signingSlot, DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF);
const signingData: altair.SyncAggregatorSelectionData = {
slot,
subcommitteeIndex,
};
const signingRoot = computeSigningRoot(ssz.altair.SyncAggregatorSelectionData, signingData, domain);
const signableMessage: SignableMessage = {
type: SignableMessageType.SYNC_COMMITTEE_SELECTION_PROOF,
data: {slot, subcommitteeIndex},
};
return this.getSignature(pubkey, signingRoot, signingSlot, signableMessage);
}
async signVoluntaryExit(
pubkey: BLSPubkeyMaybeHex,
validatorIndex: number,
exitEpoch: Epoch
): Promise<phase0.SignedVoluntaryExit> {
const signingSlot = computeStartSlotAtEpoch(exitEpoch);
const domain = this.config.getDomainForVoluntaryExit(signingSlot);
const voluntaryExit: phase0.VoluntaryExit = {epoch: exitEpoch, validatorIndex};
const signingRoot = computeSigningRoot(ssz.phase0.VoluntaryExit, voluntaryExit, domain);
const signableMessage: SignableMessage = {
type: SignableMessageType.VOLUNTARY_EXIT,
data: voluntaryExit,
};
return {
message: voluntaryExit,
signature: await this.getSignature(pubkey, signingRoot, signingSlot, signableMessage),
};
}
isDoppelgangerSafe(pubkeyHex: PubkeyHex): boolean {
// If doppelganger is not enabled we assumed all keys to be safe for use
return !this.doppelgangerService || this.doppelgangerService.isDoppelgangerSafe(pubkeyHex);
}
async signValidatorRegistration(
pubkeyMaybeHex: BLSPubkeyMaybeHex,
regAttributes: {feeRecipient: Eth1Address; gasLimit: number},
_slot: Slot
): Promise<bellatrix.SignedValidatorRegistrationV1> {
const pubkey = typeof pubkeyMaybeHex === "string" ? fromHex(pubkeyMaybeHex) : pubkeyMaybeHex;
const feeRecipient = fromHex(regAttributes.feeRecipient);
const {gasLimit} = regAttributes;
const validatorRegistration: bellatrix.ValidatorRegistrationV1 = {
feeRecipient,
gasLimit,
timestamp: Math.floor(Date.now() / 1000),
pubkey,
};
const signingSlot = 0;
const domain = computeDomain(DOMAIN_APPLICATION_BUILDER, this.config.GENESIS_FORK_VERSION, ZERO_HASH);
const signingRoot = computeSigningRoot(ssz.bellatrix.ValidatorRegistrationV1, validatorRegistration, domain);
const signableMessage: SignableMessage = {
type: SignableMessageType.VALIDATOR_REGISTRATION,
data: validatorRegistration,
};
return {
message: validatorRegistration,
signature: await this.getSignature(pubkeyMaybeHex, signingRoot, signingSlot, signableMessage),
};
}
async getValidatorRegistration(
pubkeyMaybeHex: BLSPubkeyMaybeHex,
regAttributes: {feeRecipient: Eth1Address; gasLimit: number},
slot: Slot
): Promise<bellatrix.SignedValidatorRegistrationV1> {
const pubkeyHex = typeof pubkeyMaybeHex === "string" ? pubkeyMaybeHex : toPubkeyHex(pubkeyMaybeHex);
const {feeRecipient, gasLimit} = regAttributes;
const regFullKey = `${feeRecipient}-${gasLimit}`;
const validatorData = this.validators.get(pubkeyHex);
const builderData = validatorData?.builderData;
if (builderData?.regFullKey === regFullKey) {
return builderData.validatorRegistration;
}
const validatorRegistration = await this.signValidatorRegistration(pubkeyMaybeHex, regAttributes, slot);
// If pubkeyHex was actually registered, then update the regData
if (validatorData !== undefined) {
validatorData.builderData = {validatorRegistration, regFullKey};
this.validators.set(pubkeyHex, validatorData);
}
return validatorRegistration;
}
private async getSignature(
pubkey: BLSPubkeyMaybeHex,
signingRoot: Uint8Array,
signingSlot: Slot,
signableMessage: SignableMessage
): Promise<BLSSignature> {
// TODO: Refactor indexing to not have to run toHex() on the pubkey every time
const pubkeyHex = typeof pubkey === "string" ? pubkey : toPubkeyHex(pubkey);
const signer = this.validators.get(pubkeyHex)?.signer;
if (!signer) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
switch (signer.type) {
case SignerType.Local: {
const timer = this.metrics?.localSignTime.startTimer();
const signature = signer.secretKey.sign(signingRoot).toBytes();
timer?.();
return signature;
}
case SignerType.Remote: {
const timer = this.metrics?.remoteSignTime.startTimer();
try {
const signatureHex = await externalSignerPostSignature(
this.config,
signer.url,
pubkeyHex,
signingRoot,
signingSlot,
signableMessage
);
return fromHex(signatureHex);
} catch (e) {
this.metrics?.remoteSignErrors.inc();
throw e;
} finally {
timer?.();
}
}
}
}
private getSignerAndPubkeyHex(pubkey: BLSPubkeyMaybeHex): [Signer, string] {
// TODO: Refactor indexing to not have to run toHex() on the pubkey every time
const pubkeyHex = typeof pubkey === "string" ? pubkey : toPubkeyHex(pubkey);
const signer = this.validators.get(pubkeyHex)?.signer;
if (!signer) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
return [signer, pubkeyHex];
}
/** Prevent signing bad data sent by the Beacon node */
private validateAttestationDuty(duty: routes.validator.AttesterDuty, data: phase0.AttestationData): void {
if (duty.slot !== data.slot) {
throw Error(`Inconsistent duties during signing: duty.slot ${duty.slot} != att.slot ${data.slot}`);
}
const isPostElectra = this.config.getForkSeq(data.slot) >= ForkSeq.electra;
if (!isPostElectra && duty.committeeIndex !== data.index) {
throw Error(
`Inconsistent duties during signing: duty.committeeIndex ${duty.committeeIndex} != att.committeeIndex ${data.index}`
);
}
if (isPostElectra && data.index !== 0) {
throw Error(`Non-zero committee index post-electra during signing: att.committeeIndex ${data.index}`);
}
}
private assertDoppelgangerSafe(pubKey: PubkeyHex | BLSPubkey): void {
const pubkeyHex = typeof pubKey === "string" ? pubKey : toPubkeyHex(pubKey);
if (!this.isDoppelgangerSafe(pubkeyHex)) {
throw new Error(`Doppelganger state for key ${pubkeyHex} is not safe`);
}
}
}
function getSignerPubkeyHex(signer: Signer): PubkeyHex {
switch (signer.type) {
case SignerType.Local:
return toPubkeyHex(signer.secretKey.toPublicKey().toBytes());
case SignerType.Remote:
if (!isValidatePubkeyHex(signer.pubkey)) {
throw Error(`Bad format in RemoteSigner.pubkey ${signer.pubkey}`);
}
return signer.pubkey;
}
}