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