@aztec/validator-client 0.0.1-commit.d3ec352c → 0.0.1-commit.d6f2b3f94
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +285 -0
- package/dest/block_proposal_handler.d.ts +21 -11
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +339 -83
- package/dest/checkpoint_builder.d.ts +66 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +173 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +16 -8
- package/dest/duties/validation_service.d.ts +42 -13
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +111 -28
- package/dest/factory.d.ts +13 -8
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +2 -2
- package/dest/index.d.ts +3 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -0
- package/dest/key_store/ha_key_store.d.ts +99 -0
- package/dest/key_store/ha_key_store.d.ts.map +1 -0
- package/dest/key_store/ha_key_store.js +208 -0
- package/dest/key_store/index.d.ts +2 -1
- package/dest/key_store/index.d.ts.map +1 -1
- package/dest/key_store/index.js +1 -0
- package/dest/key_store/interface.d.ts +36 -6
- package/dest/key_store/interface.d.ts.map +1 -1
- package/dest/key_store/local_key_store.d.ts +10 -5
- package/dest/key_store/local_key_store.d.ts.map +1 -1
- package/dest/key_store/local_key_store.js +9 -5
- package/dest/key_store/node_keystore_adapter.d.ts +18 -5
- package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
- package/dest/key_store/node_keystore_adapter.js +18 -4
- package/dest/key_store/web3signer_key_store.d.ts +10 -5
- package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
- package/dest/key_store/web3signer_key_store.js +9 -5
- package/dest/metrics.d.ts +4 -3
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +34 -30
- package/dest/tx_validator/index.d.ts +3 -0
- package/dest/tx_validator/index.d.ts.map +1 -0
- package/dest/tx_validator/index.js +2 -0
- package/dest/tx_validator/nullifier_cache.d.ts +14 -0
- package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
- package/dest/tx_validator/nullifier_cache.js +24 -0
- package/dest/tx_validator/tx_validator_factory.d.ts +19 -0
- package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
- package/dest/tx_validator/tx_validator_factory.js +54 -0
- package/dest/validator.d.ts +67 -21
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +416 -56
- package/package.json +21 -11
- package/src/block_proposal_handler.ts +260 -49
- package/src/checkpoint_builder.ts +314 -0
- package/src/config.ts +15 -7
- package/src/duties/validation_service.ts +155 -33
- package/src/factory.ts +17 -8
- package/src/index.ts +2 -0
- package/src/key_store/ha_key_store.ts +269 -0
- package/src/key_store/index.ts +1 -0
- package/src/key_store/interface.ts +44 -5
- package/src/key_store/local_key_store.ts +14 -5
- package/src/key_store/node_keystore_adapter.ts +28 -5
- package/src/key_store/web3signer_key_store.ts +18 -5
- package/src/metrics.ts +45 -33
- package/src/tx_validator/index.ts +2 -0
- package/src/tx_validator/nullifier_cache.ts +30 -0
- package/src/tx_validator/tx_validator_factory.ts +154 -0
- package/src/validator.ts +571 -84
package/src/validator.ts
CHANGED
|
@@ -1,31 +1,60 @@
|
|
|
1
|
+
import type { BlobClientInterface } from '@aztec/blob-client/client';
|
|
2
|
+
import { type Blob, getBlobsPerL1Block } from '@aztec/blob-lib';
|
|
1
3
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
2
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
BlockNumber,
|
|
6
|
+
CheckpointNumber,
|
|
7
|
+
EpochNumber,
|
|
8
|
+
IndexWithinCheckpoint,
|
|
9
|
+
SlotNumber,
|
|
10
|
+
} from '@aztec/foundation/branded-types';
|
|
11
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
12
|
+
import { TimeoutError } from '@aztec/foundation/error';
|
|
3
13
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
14
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
15
|
+
import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
|
|
16
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
7
17
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
8
18
|
import { sleep } from '@aztec/foundation/sleep';
|
|
9
19
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
10
20
|
import type { KeystoreManager } from '@aztec/node-keystore';
|
|
11
|
-
import type { P2P, PeerId
|
|
21
|
+
import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } from '@aztec/p2p';
|
|
12
22
|
import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
|
|
13
23
|
import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
|
|
14
24
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
15
|
-
import type { CommitteeAttestationsAndSigners, L2BlockSource } from '@aztec/stdlib/block';
|
|
16
|
-
import
|
|
17
|
-
import type {
|
|
18
|
-
|
|
25
|
+
import type { CommitteeAttestationsAndSigners, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
26
|
+
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
27
|
+
import type {
|
|
28
|
+
CreateCheckpointProposalLastBlockData,
|
|
29
|
+
ITxProvider,
|
|
30
|
+
Validator,
|
|
31
|
+
ValidatorClientFullConfig,
|
|
32
|
+
WorldStateSynchronizer,
|
|
33
|
+
} from '@aztec/stdlib/interfaces/server';
|
|
34
|
+
import { type L1ToL2MessageSource, accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
|
|
35
|
+
import {
|
|
36
|
+
type BlockProposal,
|
|
37
|
+
type BlockProposalOptions,
|
|
38
|
+
type CheckpointAttestation,
|
|
39
|
+
CheckpointProposal,
|
|
40
|
+
type CheckpointProposalCore,
|
|
41
|
+
type CheckpointProposalOptions,
|
|
42
|
+
} from '@aztec/stdlib/p2p';
|
|
19
43
|
import type { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
20
|
-
import type { Tx } from '@aztec/stdlib/tx';
|
|
44
|
+
import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
21
45
|
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
22
46
|
import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
47
|
+
import { createHASigner } from '@aztec/validator-ha-signer/factory';
|
|
48
|
+
import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
|
|
23
49
|
|
|
24
50
|
import { EventEmitter } from 'events';
|
|
25
51
|
import type { TypedDataDefinition } from 'viem';
|
|
26
52
|
|
|
27
53
|
import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
|
|
54
|
+
import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
28
55
|
import { ValidationService } from './duties/validation_service.js';
|
|
56
|
+
import { HAKeyStore } from './key_store/ha_key_store.js';
|
|
57
|
+
import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
|
|
29
58
|
import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
|
|
30
59
|
import { ValidatorMetrics } from './metrics.js';
|
|
31
60
|
|
|
@@ -51,20 +80,31 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
51
80
|
// Whether it has already registered handlers on the p2p client
|
|
52
81
|
private hasRegisteredHandlers = false;
|
|
53
82
|
|
|
54
|
-
|
|
55
|
-
private
|
|
83
|
+
/** Tracks the last block proposal we created, to detect duplicate proposal attempts. */
|
|
84
|
+
private lastProposedBlock?: BlockProposal;
|
|
85
|
+
|
|
86
|
+
/** Tracks the last checkpoint proposal we created. */
|
|
87
|
+
private lastProposedCheckpoint?: CheckpointProposal;
|
|
56
88
|
|
|
57
89
|
private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
|
|
58
90
|
private epochCacheUpdateLoop: RunningPromise;
|
|
59
91
|
|
|
60
92
|
private proposersOfInvalidBlocks: Set<string> = new Set();
|
|
61
93
|
|
|
94
|
+
/** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */
|
|
95
|
+
private lastAttestedProposal?: CheckpointProposalCore;
|
|
96
|
+
|
|
62
97
|
protected constructor(
|
|
63
|
-
private keyStore:
|
|
98
|
+
private keyStore: ExtendedValidatorKeyStore,
|
|
64
99
|
private epochCache: EpochCache,
|
|
65
100
|
private p2pClient: P2P,
|
|
66
101
|
private blockProposalHandler: BlockProposalHandler,
|
|
102
|
+
private blockSource: L2BlockSource,
|
|
103
|
+
private checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
104
|
+
private worldState: WorldStateSynchronizer,
|
|
105
|
+
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
67
106
|
private config: ValidatorClientFullConfig,
|
|
107
|
+
private blobClient: BlobClientInterface,
|
|
68
108
|
private dateProvider: DateProvider = new DateProvider(),
|
|
69
109
|
telemetry: TelemetryClient = getTelemetryClient(),
|
|
70
110
|
log = createLogger('validator'),
|
|
@@ -138,15 +178,17 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
138
178
|
}
|
|
139
179
|
}
|
|
140
180
|
|
|
141
|
-
static new(
|
|
181
|
+
static async new(
|
|
142
182
|
config: ValidatorClientFullConfig,
|
|
143
|
-
|
|
183
|
+
checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
184
|
+
worldState: WorldStateSynchronizer,
|
|
144
185
|
epochCache: EpochCache,
|
|
145
186
|
p2pClient: P2P,
|
|
146
|
-
blockSource: L2BlockSource,
|
|
187
|
+
blockSource: L2BlockSource & L2BlockSink,
|
|
147
188
|
l1ToL2MessageSource: L1ToL2MessageSource,
|
|
148
|
-
txProvider:
|
|
189
|
+
txProvider: ITxProvider,
|
|
149
190
|
keyStoreManager: KeystoreManager,
|
|
191
|
+
blobClient: BlobClientInterface,
|
|
150
192
|
dateProvider: DateProvider = new DateProvider(),
|
|
151
193
|
telemetry: TelemetryClient = getTelemetryClient(),
|
|
152
194
|
) {
|
|
@@ -155,23 +197,41 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
155
197
|
txsPermitted: !config.disableTransactions,
|
|
156
198
|
});
|
|
157
199
|
const blockProposalHandler = new BlockProposalHandler(
|
|
158
|
-
|
|
200
|
+
checkpointsBuilder,
|
|
201
|
+
worldState,
|
|
159
202
|
blockSource,
|
|
160
203
|
l1ToL2MessageSource,
|
|
161
204
|
txProvider,
|
|
162
205
|
blockProposalValidator,
|
|
206
|
+
epochCache,
|
|
163
207
|
config,
|
|
164
208
|
metrics,
|
|
165
209
|
dateProvider,
|
|
166
210
|
telemetry,
|
|
167
211
|
);
|
|
168
212
|
|
|
213
|
+
let validatorKeyStore: ExtendedValidatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
214
|
+
if (config.haSigningEnabled) {
|
|
215
|
+
// If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
|
|
216
|
+
const haConfig = {
|
|
217
|
+
...config,
|
|
218
|
+
maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
|
|
219
|
+
};
|
|
220
|
+
const { signer } = await createHASigner(haConfig);
|
|
221
|
+
validatorKeyStore = new HAKeyStore(validatorKeyStore, signer);
|
|
222
|
+
}
|
|
223
|
+
|
|
169
224
|
const validator = new ValidatorClient(
|
|
170
|
-
|
|
225
|
+
validatorKeyStore,
|
|
171
226
|
epochCache,
|
|
172
227
|
p2pClient,
|
|
173
228
|
blockProposalHandler,
|
|
229
|
+
blockSource,
|
|
230
|
+
checkpointsBuilder,
|
|
231
|
+
worldState,
|
|
232
|
+
l1ToL2MessageSource,
|
|
174
233
|
config,
|
|
234
|
+
blobClient,
|
|
175
235
|
dateProvider,
|
|
176
236
|
telemetry,
|
|
177
237
|
);
|
|
@@ -189,18 +249,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
189
249
|
return this.blockProposalHandler;
|
|
190
250
|
}
|
|
191
251
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
proposal: BlockProposal,
|
|
195
|
-
blockNumber: BlockNumber,
|
|
196
|
-
txs: any[],
|
|
197
|
-
l1ToL2Messages: Fr[],
|
|
198
|
-
): Promise<any> {
|
|
199
|
-
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
public signWithAddress(addr: EthAddress, msg: TypedDataDefinition) {
|
|
203
|
-
return this.keyStore.signTypedDataWithAddress(addr, msg);
|
|
252
|
+
public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
|
|
253
|
+
return this.keyStore.signTypedDataWithAddress(addr, msg, context);
|
|
204
254
|
}
|
|
205
255
|
|
|
206
256
|
public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
|
|
@@ -225,6 +275,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
225
275
|
return;
|
|
226
276
|
}
|
|
227
277
|
|
|
278
|
+
await this.keyStore.start();
|
|
279
|
+
|
|
228
280
|
await this.registerHandlers();
|
|
229
281
|
|
|
230
282
|
const myAddresses = this.getValidatorAddresses();
|
|
@@ -240,6 +292,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
240
292
|
|
|
241
293
|
public async stop() {
|
|
242
294
|
await this.epochCacheUpdateLoop.stop();
|
|
295
|
+
await this.keyStore.stop();
|
|
243
296
|
}
|
|
244
297
|
|
|
245
298
|
/** Register handlers on the p2p client */
|
|
@@ -248,9 +301,29 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
248
301
|
this.hasRegisteredHandlers = true;
|
|
249
302
|
this.log.debug(`Registering validator handlers for p2p client`);
|
|
250
303
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
304
|
+
// Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
|
|
305
|
+
const blockHandler = (block: BlockProposal, proposalSender: PeerId): Promise<boolean> =>
|
|
306
|
+
this.validateBlockProposal(block, proposalSender);
|
|
307
|
+
this.p2pClient.registerBlockProposalHandler(blockHandler);
|
|
308
|
+
|
|
309
|
+
// Checkpoint proposal handler - validates and creates attestations
|
|
310
|
+
// The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
|
|
311
|
+
// and processed separately via the block handler above.
|
|
312
|
+
const checkpointHandler = (
|
|
313
|
+
checkpoint: CheckpointProposalCore,
|
|
314
|
+
proposalSender: PeerId,
|
|
315
|
+
): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
|
|
316
|
+
this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
|
|
317
|
+
|
|
318
|
+
// Duplicate proposal handler - triggers slashing for equivocation
|
|
319
|
+
this.p2pClient.registerDuplicateProposalCallback((info: DuplicateProposalInfo) => {
|
|
320
|
+
this.handleDuplicateProposal(info);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// Duplicate attestation handler - triggers slashing for attestation equivocation
|
|
324
|
+
this.p2pClient.registerDuplicateAttestationCallback((info: DuplicateAttestationInfo) => {
|
|
325
|
+
this.handleDuplicateAttestation(info);
|
|
326
|
+
});
|
|
254
327
|
|
|
255
328
|
const myAddresses = this.getValidatorAddresses();
|
|
256
329
|
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
@@ -259,29 +332,47 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
259
332
|
}
|
|
260
333
|
}
|
|
261
334
|
|
|
262
|
-
|
|
335
|
+
/**
|
|
336
|
+
* Validate a block proposal from a peer.
|
|
337
|
+
* Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
|
|
338
|
+
* @returns true if the proposal is valid, false otherwise
|
|
339
|
+
*/
|
|
340
|
+
async validateBlockProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> {
|
|
263
341
|
const slotNumber = proposal.slotNumber;
|
|
342
|
+
|
|
343
|
+
// Note: During escape hatch, we still want to "validate" proposals for observability,
|
|
344
|
+
// but we intentionally reject them and disable slashing invalid block and attestation flow.
|
|
345
|
+
const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
|
|
346
|
+
|
|
264
347
|
const proposer = proposal.getSender();
|
|
265
348
|
|
|
266
349
|
// Reject proposals with invalid signatures
|
|
267
350
|
if (!proposer) {
|
|
268
|
-
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
269
|
-
return
|
|
351
|
+
this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
|
|
352
|
+
return false;
|
|
270
353
|
}
|
|
271
354
|
|
|
272
|
-
//
|
|
355
|
+
// Ignore proposals from ourselves (may happen in HA setups)
|
|
356
|
+
if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
|
|
357
|
+
this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
|
|
358
|
+
proposer: proposer.toString(),
|
|
359
|
+
slotNumber,
|
|
360
|
+
});
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Check if we're in the committee (for metrics purposes)
|
|
273
365
|
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
274
366
|
const partOfCommittee = inCommittee.length > 0;
|
|
275
367
|
|
|
276
368
|
const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
|
|
277
|
-
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
369
|
+
this.log.info(`Received block proposal for slot ${slotNumber}`, {
|
|
278
370
|
...proposalInfo,
|
|
279
371
|
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
280
372
|
fishermanMode: this.config.fishermanMode || false,
|
|
281
373
|
});
|
|
282
374
|
|
|
283
|
-
// Reexecute txs if we are part of the committee
|
|
284
|
-
// invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
|
|
375
|
+
// Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
|
|
285
376
|
// In fisherman mode, we always reexecute to validate proposals.
|
|
286
377
|
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } =
|
|
287
378
|
this.config;
|
|
@@ -289,16 +380,17 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
289
380
|
fishermanMode ||
|
|
290
381
|
(slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute) ||
|
|
291
382
|
(partOfCommittee && validatorReexecute) ||
|
|
292
|
-
alwaysReexecuteBlockProposals
|
|
383
|
+
alwaysReexecuteBlockProposals ||
|
|
384
|
+
this.blobClient.canUpload();
|
|
293
385
|
|
|
294
386
|
const validationResult = await this.blockProposalHandler.handleBlockProposal(
|
|
295
387
|
proposal,
|
|
296
388
|
proposalSender,
|
|
297
|
-
!!shouldReexecute,
|
|
389
|
+
!!shouldReexecute && !escapeHatchOpen,
|
|
298
390
|
);
|
|
299
391
|
|
|
300
392
|
if (!validationResult.isValid) {
|
|
301
|
-
this.log.warn(`
|
|
393
|
+
this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
302
394
|
|
|
303
395
|
const reason = validationResult.reason || 'unknown';
|
|
304
396
|
// Classify failure reason: bad proposal vs node issue
|
|
@@ -313,12 +405,13 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
313
405
|
if (badProposalReasons.includes(reason as BlockProposalValidationFailureReason)) {
|
|
314
406
|
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
315
407
|
} else {
|
|
316
|
-
// Node issues so we can't
|
|
408
|
+
// Node issues so we can't validate
|
|
317
409
|
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
318
410
|
}
|
|
319
411
|
|
|
320
412
|
// Slash invalid block proposals (can happen even when not in committee)
|
|
321
413
|
if (
|
|
414
|
+
!escapeHatchOpen &&
|
|
322
415
|
validationResult.reason &&
|
|
323
416
|
SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) &&
|
|
324
417
|
slashBroadcastedInvalidBlockPenalty > 0n
|
|
@@ -326,9 +419,90 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
326
419
|
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
327
420
|
this.slashInvalidBlock(proposal);
|
|
328
421
|
}
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
this.log.info(`Validated block proposal for slot ${slotNumber}`, {
|
|
426
|
+
...proposalInfo,
|
|
427
|
+
inCommittee: partOfCommittee,
|
|
428
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
429
|
+
escapeHatchOpen,
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
if (escapeHatchOpen) {
|
|
433
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Validate and attest to a checkpoint proposal from a peer.
|
|
442
|
+
* The proposal is received as CheckpointProposalCore (without lastBlock) since
|
|
443
|
+
* the lastBlock is extracted and processed separately via the block handler.
|
|
444
|
+
* @returns Checkpoint attestations if valid, undefined otherwise
|
|
445
|
+
*/
|
|
446
|
+
async attestToCheckpointProposal(
|
|
447
|
+
proposal: CheckpointProposalCore,
|
|
448
|
+
_proposalSender: PeerId,
|
|
449
|
+
): Promise<CheckpointAttestation[] | undefined> {
|
|
450
|
+
const slotNumber = proposal.slotNumber;
|
|
451
|
+
const proposer = proposal.getSender();
|
|
452
|
+
|
|
453
|
+
// If escape hatch is open for this slot's epoch, do not attest.
|
|
454
|
+
if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
|
|
455
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
|
|
456
|
+
return undefined;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// Reject proposals with invalid signatures
|
|
460
|
+
if (!proposer) {
|
|
461
|
+
this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Ignore proposals from ourselves (may happen in HA setups)
|
|
466
|
+
if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
|
|
467
|
+
this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
|
|
468
|
+
proposer: proposer.toString(),
|
|
469
|
+
slotNumber,
|
|
470
|
+
});
|
|
329
471
|
return undefined;
|
|
330
472
|
}
|
|
331
473
|
|
|
474
|
+
// Check that I have any address in current committee before attesting
|
|
475
|
+
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
476
|
+
const partOfCommittee = inCommittee.length > 0;
|
|
477
|
+
|
|
478
|
+
const proposalInfo = {
|
|
479
|
+
slotNumber,
|
|
480
|
+
archive: proposal.archive.toString(),
|
|
481
|
+
proposer: proposer.toString(),
|
|
482
|
+
txCount: proposal.txHashes.length,
|
|
483
|
+
};
|
|
484
|
+
this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
|
|
485
|
+
...proposalInfo,
|
|
486
|
+
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
487
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
// Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
|
|
491
|
+
if (this.config.skipCheckpointProposalValidation) {
|
|
492
|
+
this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
|
|
493
|
+
} else {
|
|
494
|
+
const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
|
|
495
|
+
if (!validationResult.isValid) {
|
|
496
|
+
this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
497
|
+
return undefined;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Upload blobs to filestore if we can (fire and forget)
|
|
502
|
+
if (this.blobClient.canUpload()) {
|
|
503
|
+
void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
504
|
+
}
|
|
505
|
+
|
|
332
506
|
// Check that I have any address in current committee before attesting
|
|
333
507
|
// In fisherman mode, we still create attestations for validation even if not in committee
|
|
334
508
|
if (!partOfCommittee && !this.config.fishermanMode) {
|
|
@@ -337,7 +511,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
337
511
|
}
|
|
338
512
|
|
|
339
513
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
340
|
-
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} proposal for slot ${slotNumber}`, {
|
|
514
|
+
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
|
|
341
515
|
...proposalInfo,
|
|
342
516
|
inCommittee: partOfCommittee,
|
|
343
517
|
fishermanMode: this.config.fishermanMode || false,
|
|
@@ -345,7 +519,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
345
519
|
|
|
346
520
|
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
347
521
|
|
|
348
|
-
// If the above function does not throw an error, then we can attest to the proposal
|
|
349
522
|
// Determine which validators should attest
|
|
350
523
|
let attestors: EthAddress[];
|
|
351
524
|
if (partOfCommittee) {
|
|
@@ -364,13 +537,225 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
364
537
|
|
|
365
538
|
if (this.config.fishermanMode) {
|
|
366
539
|
// bail out early and don't save attestations to the pool in fisherman mode
|
|
367
|
-
this.log.info(`Creating attestations for
|
|
540
|
+
this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
|
|
368
541
|
...proposalInfo,
|
|
369
542
|
attestors: attestors.map(a => a.toString()),
|
|
370
543
|
});
|
|
371
544
|
return undefined;
|
|
372
545
|
}
|
|
373
|
-
|
|
546
|
+
|
|
547
|
+
return await this.createCheckpointAttestationsFromProposal(proposal, attestors);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Checks if we should attest to a slot based on equivocation prevention rules.
|
|
552
|
+
* @returns true if we should attest, false if we should skip
|
|
553
|
+
*/
|
|
554
|
+
private shouldAttestToSlot(slotNumber: SlotNumber): boolean {
|
|
555
|
+
// If attestToEquivocatedProposals is true, always allow
|
|
556
|
+
if (this.config.attestToEquivocatedProposals) {
|
|
557
|
+
return true;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// Check if incoming slot is strictly greater than last attested
|
|
561
|
+
if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
|
|
562
|
+
this.log.warn(
|
|
563
|
+
`Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`,
|
|
564
|
+
);
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
private async createCheckpointAttestationsFromProposal(
|
|
572
|
+
proposal: CheckpointProposalCore,
|
|
573
|
+
attestors: EthAddress[] = [],
|
|
574
|
+
): Promise<CheckpointAttestation[] | undefined> {
|
|
575
|
+
// Equivocation check: must happen right before signing to minimize the race window
|
|
576
|
+
if (!this.shouldAttestToSlot(proposal.slotNumber)) {
|
|
577
|
+
return undefined;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
|
|
581
|
+
|
|
582
|
+
// Track the proposal we attested to (to prevent equivocation)
|
|
583
|
+
this.lastAttestedProposal = proposal;
|
|
584
|
+
|
|
585
|
+
await this.p2pClient.addOwnCheckpointAttestations(attestations);
|
|
586
|
+
return attestations;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
|
|
591
|
+
* @returns Validation result with isValid flag and reason if invalid.
|
|
592
|
+
*/
|
|
593
|
+
private async validateCheckpointProposal(
|
|
594
|
+
proposal: CheckpointProposalCore,
|
|
595
|
+
proposalInfo: LogData,
|
|
596
|
+
): Promise<{ isValid: true } | { isValid: false; reason: string }> {
|
|
597
|
+
const slot = proposal.slotNumber;
|
|
598
|
+
const timeoutSeconds = 10; // TODO(palla/mbps): This should map to the timetable settings
|
|
599
|
+
|
|
600
|
+
// Wait for last block to sync by archive
|
|
601
|
+
let lastBlockHeader: BlockHeader | undefined;
|
|
602
|
+
try {
|
|
603
|
+
lastBlockHeader = await retryUntil(
|
|
604
|
+
async () => {
|
|
605
|
+
await this.blockSource.syncImmediate();
|
|
606
|
+
return this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
607
|
+
},
|
|
608
|
+
`waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
|
|
609
|
+
timeoutSeconds,
|
|
610
|
+
0.5,
|
|
611
|
+
);
|
|
612
|
+
} catch (err) {
|
|
613
|
+
if (err instanceof TimeoutError) {
|
|
614
|
+
this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
|
|
615
|
+
return { isValid: false, reason: 'last_block_not_found' };
|
|
616
|
+
}
|
|
617
|
+
this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
|
|
618
|
+
return { isValid: false, reason: 'block_fetch_error' };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (!lastBlockHeader) {
|
|
622
|
+
this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
|
|
623
|
+
return { isValid: false, reason: 'last_block_not_found' };
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// Get all full blocks for the slot and checkpoint
|
|
627
|
+
const blocks = await this.blockSource.getBlocksForSlot(slot);
|
|
628
|
+
if (blocks.length === 0) {
|
|
629
|
+
this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
|
|
630
|
+
return { isValid: false, reason: 'no_blocks_for_slot' };
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
|
|
634
|
+
...proposalInfo,
|
|
635
|
+
blockNumbers: blocks.map(b => b.number),
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
// Get checkpoint constants from first block
|
|
639
|
+
const firstBlock = blocks[0];
|
|
640
|
+
const constants = this.extractCheckpointConstants(firstBlock);
|
|
641
|
+
const checkpointNumber = firstBlock.checkpointNumber;
|
|
642
|
+
|
|
643
|
+
// Get L1-to-L2 messages for this checkpoint
|
|
644
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
645
|
+
|
|
646
|
+
// Compute the previous checkpoint out hashes for the epoch.
|
|
647
|
+
// TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
|
|
648
|
+
// actual checkpoints and the blocks/txs in them.
|
|
649
|
+
const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
|
|
650
|
+
const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch))
|
|
651
|
+
.filter(b => b.number < checkpointNumber)
|
|
652
|
+
.sort((a, b) => a.number - b.number);
|
|
653
|
+
const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
|
|
654
|
+
|
|
655
|
+
// Fork world state at the block before the first block
|
|
656
|
+
const parentBlockNumber = BlockNumber(firstBlock.number - 1);
|
|
657
|
+
const fork = await this.worldState.fork(parentBlockNumber);
|
|
658
|
+
|
|
659
|
+
try {
|
|
660
|
+
// Create checkpoint builder with all existing blocks
|
|
661
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
662
|
+
checkpointNumber,
|
|
663
|
+
constants,
|
|
664
|
+
l1ToL2Messages,
|
|
665
|
+
previousCheckpointOutHashes,
|
|
666
|
+
fork,
|
|
667
|
+
blocks,
|
|
668
|
+
this.log.getBindings(),
|
|
669
|
+
);
|
|
670
|
+
|
|
671
|
+
// Complete the checkpoint to get computed values
|
|
672
|
+
const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
|
|
673
|
+
|
|
674
|
+
// Compare checkpoint header with proposal
|
|
675
|
+
if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
|
|
676
|
+
this.log.warn(`Checkpoint header mismatch`, {
|
|
677
|
+
...proposalInfo,
|
|
678
|
+
computed: computedCheckpoint.header.toInspect(),
|
|
679
|
+
proposal: proposal.checkpointHeader.toInspect(),
|
|
680
|
+
});
|
|
681
|
+
return { isValid: false, reason: 'checkpoint_header_mismatch' };
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// Compare archive root with proposal
|
|
685
|
+
if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
|
|
686
|
+
this.log.warn(`Archive root mismatch`, {
|
|
687
|
+
...proposalInfo,
|
|
688
|
+
computed: computedCheckpoint.archive.root.toString(),
|
|
689
|
+
proposal: proposal.archive.toString(),
|
|
690
|
+
});
|
|
691
|
+
return { isValid: false, reason: 'archive_mismatch' };
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// Check that the accumulated epoch out hash matches the value in the proposal.
|
|
695
|
+
// The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
|
|
696
|
+
const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
|
|
697
|
+
const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
|
|
698
|
+
const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
|
|
699
|
+
if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
|
|
700
|
+
this.log.warn(`Epoch out hash mismatch`, {
|
|
701
|
+
proposalEpochOutHash: proposalEpochOutHash.toString(),
|
|
702
|
+
computedEpochOutHash: computedEpochOutHash.toString(),
|
|
703
|
+
checkpointOutHash: checkpointOutHash.toString(),
|
|
704
|
+
previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
|
|
705
|
+
...proposalInfo,
|
|
706
|
+
});
|
|
707
|
+
return { isValid: false, reason: 'out_hash_mismatch' };
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
|
|
711
|
+
return { isValid: true };
|
|
712
|
+
} finally {
|
|
713
|
+
await fork.close();
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Extract checkpoint global variables from a block.
|
|
719
|
+
*/
|
|
720
|
+
private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
|
|
721
|
+
const gv = block.header.globalVariables;
|
|
722
|
+
return {
|
|
723
|
+
chainId: gv.chainId,
|
|
724
|
+
version: gv.version,
|
|
725
|
+
slotNumber: gv.slotNumber,
|
|
726
|
+
coinbase: gv.coinbase,
|
|
727
|
+
feeRecipient: gv.feeRecipient,
|
|
728
|
+
gasFees: gv.gasFees,
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* Uploads blobs for a checkpoint to the filestore (fire and forget).
|
|
734
|
+
*/
|
|
735
|
+
private async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
|
|
736
|
+
try {
|
|
737
|
+
const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
738
|
+
if (!lastBlockHeader) {
|
|
739
|
+
this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
|
|
744
|
+
if (blocks.length === 0) {
|
|
745
|
+
this.log.warn(`No blocks found for blob upload`, proposalInfo);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const blobFields = blocks.flatMap(b => b.toBlobFields());
|
|
750
|
+
const blobs: Blob[] = getBlobsPerL1Block(blobFields);
|
|
751
|
+
await this.blobClient.sendBlobsToFilestore(blobs);
|
|
752
|
+
this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
|
|
753
|
+
...proposalInfo,
|
|
754
|
+
numBlobs: blobs.length,
|
|
755
|
+
});
|
|
756
|
+
} catch (err) {
|
|
757
|
+
this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
|
|
758
|
+
}
|
|
374
759
|
}
|
|
375
760
|
|
|
376
761
|
private slashInvalidBlock(proposal: BlockProposal) {
|
|
@@ -400,24 +785,123 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
400
785
|
]);
|
|
401
786
|
}
|
|
402
787
|
|
|
788
|
+
/**
|
|
789
|
+
* Handle detection of a duplicate proposal (equivocation).
|
|
790
|
+
* Emits a slash event when a proposer sends multiple proposals for the same position.
|
|
791
|
+
*/
|
|
792
|
+
private handleDuplicateProposal(info: DuplicateProposalInfo): void {
|
|
793
|
+
const { slot, proposer, type } = info;
|
|
794
|
+
|
|
795
|
+
this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
|
|
796
|
+
proposer: proposer.toString(),
|
|
797
|
+
slot,
|
|
798
|
+
type,
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
// Emit slash event
|
|
802
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
803
|
+
{
|
|
804
|
+
validator: proposer,
|
|
805
|
+
amount: this.config.slashDuplicateProposalPenalty,
|
|
806
|
+
offenseType: OffenseType.DUPLICATE_PROPOSAL,
|
|
807
|
+
epochOrSlot: BigInt(slot),
|
|
808
|
+
},
|
|
809
|
+
]);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/**
|
|
813
|
+
* Handle detection of a duplicate attestation (equivocation).
|
|
814
|
+
* Emits a slash event when an attester signs attestations for different proposals at the same slot.
|
|
815
|
+
*/
|
|
816
|
+
private handleDuplicateAttestation(info: DuplicateAttestationInfo): void {
|
|
817
|
+
const { slot, attester } = info;
|
|
818
|
+
|
|
819
|
+
this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
|
|
820
|
+
attester: attester.toString(),
|
|
821
|
+
slot,
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
825
|
+
{
|
|
826
|
+
validator: attester,
|
|
827
|
+
amount: this.config.slashDuplicateAttestationPenalty,
|
|
828
|
+
offenseType: OffenseType.DUPLICATE_ATTESTATION,
|
|
829
|
+
epochOrSlot: BigInt(slot),
|
|
830
|
+
},
|
|
831
|
+
]);
|
|
832
|
+
}
|
|
833
|
+
|
|
403
834
|
async createBlockProposal(
|
|
404
|
-
|
|
405
|
-
|
|
835
|
+
blockHeader: BlockHeader,
|
|
836
|
+
indexWithinCheckpoint: IndexWithinCheckpoint,
|
|
837
|
+
inHash: Fr,
|
|
406
838
|
archive: Fr,
|
|
407
839
|
txs: Tx[],
|
|
408
840
|
proposerAddress: EthAddress | undefined,
|
|
409
|
-
options: BlockProposalOptions,
|
|
410
|
-
): Promise<BlockProposal
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
841
|
+
options: BlockProposalOptions = {},
|
|
842
|
+
): Promise<BlockProposal> {
|
|
843
|
+
// Validate that we're not creating a proposal for an older or equal position
|
|
844
|
+
if (this.lastProposedBlock) {
|
|
845
|
+
const lastSlot = this.lastProposedBlock.slotNumber;
|
|
846
|
+
const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
|
|
847
|
+
const newSlot = blockHeader.globalVariables.slotNumber;
|
|
848
|
+
|
|
849
|
+
if (newSlot < lastSlot || (newSlot === lastSlot && indexWithinCheckpoint <= lastIndex)) {
|
|
850
|
+
throw new Error(
|
|
851
|
+
`Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` +
|
|
852
|
+
`already proposed block for slot ${lastSlot} index ${lastIndex}`,
|
|
853
|
+
);
|
|
854
|
+
}
|
|
414
855
|
}
|
|
415
856
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
857
|
+
this.log.info(
|
|
858
|
+
`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
|
|
859
|
+
);
|
|
860
|
+
const newProposal = await this.validationService.createBlockProposal(
|
|
861
|
+
blockHeader,
|
|
862
|
+
indexWithinCheckpoint,
|
|
863
|
+
inHash,
|
|
864
|
+
archive,
|
|
865
|
+
txs,
|
|
866
|
+
proposerAddress,
|
|
867
|
+
{
|
|
868
|
+
...options,
|
|
869
|
+
broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
|
|
870
|
+
},
|
|
871
|
+
);
|
|
872
|
+
this.lastProposedBlock = newProposal;
|
|
873
|
+
return newProposal;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
async createCheckpointProposal(
|
|
877
|
+
checkpointHeader: CheckpointHeader,
|
|
878
|
+
archive: Fr,
|
|
879
|
+
lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
|
|
880
|
+
proposerAddress: EthAddress | undefined,
|
|
881
|
+
options: CheckpointProposalOptions = {},
|
|
882
|
+
): Promise<CheckpointProposal> {
|
|
883
|
+
// Validate that we're not creating a proposal for an older or equal slot
|
|
884
|
+
if (this.lastProposedCheckpoint) {
|
|
885
|
+
const lastSlot = this.lastProposedCheckpoint.slotNumber;
|
|
886
|
+
const newSlot = checkpointHeader.slotNumber;
|
|
887
|
+
|
|
888
|
+
if (newSlot <= lastSlot) {
|
|
889
|
+
throw new Error(
|
|
890
|
+
`Cannot create checkpoint proposal for slot ${newSlot}: ` +
|
|
891
|
+
`already proposed checkpoint for slot ${lastSlot}`,
|
|
892
|
+
);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
|
|
897
|
+
const newProposal = await this.validationService.createCheckpointProposal(
|
|
898
|
+
checkpointHeader,
|
|
899
|
+
archive,
|
|
900
|
+
lastBlockInfo,
|
|
901
|
+
proposerAddress,
|
|
902
|
+
options,
|
|
903
|
+
);
|
|
904
|
+
this.lastProposedCheckpoint = newProposal;
|
|
421
905
|
return newProposal;
|
|
422
906
|
}
|
|
423
907
|
|
|
@@ -428,28 +912,38 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
428
912
|
async signAttestationsAndSigners(
|
|
429
913
|
attestationsAndSigners: CommitteeAttestationsAndSigners,
|
|
430
914
|
proposer: EthAddress,
|
|
915
|
+
slot: SlotNumber,
|
|
916
|
+
blockNumber: BlockNumber | CheckpointNumber,
|
|
431
917
|
): Promise<Signature> {
|
|
432
|
-
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
918
|
+
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
|
|
433
919
|
}
|
|
434
920
|
|
|
435
|
-
async collectOwnAttestations(proposal:
|
|
436
|
-
const slot = proposal.
|
|
921
|
+
async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
|
|
922
|
+
const slot = proposal.slotNumber;
|
|
437
923
|
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
438
924
|
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
|
|
439
|
-
const attestations = await this.
|
|
925
|
+
const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
|
|
926
|
+
|
|
927
|
+
if (!attestations) {
|
|
928
|
+
return [];
|
|
929
|
+
}
|
|
440
930
|
|
|
441
931
|
// We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
|
|
442
932
|
// other nodes can see that our validators did attest to this block proposal, and do not slash us
|
|
443
933
|
// due to inactivity for missed attestations.
|
|
444
|
-
void this.p2pClient.
|
|
934
|
+
void this.p2pClient.broadcastCheckpointAttestations(attestations).catch(err => {
|
|
445
935
|
this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
|
|
446
936
|
});
|
|
447
937
|
return attestations;
|
|
448
938
|
}
|
|
449
939
|
|
|
450
|
-
async collectAttestations(
|
|
451
|
-
|
|
452
|
-
|
|
940
|
+
async collectAttestations(
|
|
941
|
+
proposal: CheckpointProposal,
|
|
942
|
+
required: number,
|
|
943
|
+
deadline: Date,
|
|
944
|
+
): Promise<CheckpointAttestation[]> {
|
|
945
|
+
// Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
|
|
946
|
+
const slot = proposal.slotNumber;
|
|
453
947
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
454
948
|
|
|
455
949
|
if (+deadline < this.dateProvider.now()) {
|
|
@@ -464,16 +958,16 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
464
958
|
const proposalId = proposal.archive.toString();
|
|
465
959
|
const myAddresses = this.getValidatorAddresses();
|
|
466
960
|
|
|
467
|
-
let attestations:
|
|
961
|
+
let attestations: CheckpointAttestation[] = [];
|
|
468
962
|
while (true) {
|
|
469
|
-
// Filter out attestations with a mismatching
|
|
963
|
+
// Filter out attestations with a mismatching archive. This should NOT happen since we have verified
|
|
470
964
|
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
471
|
-
const collectedAttestations = (await this.p2pClient.
|
|
965
|
+
const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter(
|
|
472
966
|
attestation => {
|
|
473
|
-
if (!attestation.
|
|
967
|
+
if (!attestation.archive.equals(proposal.archive)) {
|
|
474
968
|
this.log.warn(
|
|
475
|
-
`Received attestation for slot ${slot} with mismatched
|
|
476
|
-
{
|
|
969
|
+
`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`,
|
|
970
|
+
{ attestationArchive: attestation.archive.toString(), proposalArchive: proposal.archive.toString() },
|
|
477
971
|
);
|
|
478
972
|
return false;
|
|
479
973
|
}
|
|
@@ -514,15 +1008,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
514
1008
|
}
|
|
515
1009
|
}
|
|
516
1010
|
|
|
517
|
-
private async createBlockAttestationsFromProposal(
|
|
518
|
-
proposal: BlockProposal,
|
|
519
|
-
attestors: EthAddress[] = [],
|
|
520
|
-
): Promise<BlockAttestation[]> {
|
|
521
|
-
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
522
|
-
await this.p2pClient.addAttestations(attestations);
|
|
523
|
-
return attestations;
|
|
524
|
-
}
|
|
525
|
-
|
|
526
1011
|
private async handleAuthRequest(peer: PeerId, msg: Buffer): Promise<Buffer> {
|
|
527
1012
|
const authRequest = AuthRequest.fromBuffer(msg);
|
|
528
1013
|
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch(_ => undefined);
|
|
@@ -541,7 +1026,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
541
1026
|
}
|
|
542
1027
|
|
|
543
1028
|
const payloadToSign = authRequest.getPayloadToSign();
|
|
544
|
-
|
|
1029
|
+
// AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
|
|
1030
|
+
const context: SigningContext = { dutyType: DutyType.AUTH_REQUEST };
|
|
1031
|
+
const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
|
|
545
1032
|
const authResponse = new AuthResponse(statusMessage, signature);
|
|
546
1033
|
return authResponse.toBuffer();
|
|
547
1034
|
}
|