@aztec/validator-client 0.0.1-commit.b655e406 → 0.0.1-commit.b6e433891
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 +27 -15
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +441 -113
- 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 +41 -7
- 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 +107 -31
- 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 -11
- 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 +77 -22
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +529 -66
- package/package.json +24 -14
- package/src/block_proposal_handler.ts +380 -91
- package/src/checkpoint_builder.ts +417 -0
- package/src/config.ts +41 -6
- package/src/duties/validation_service.ts +158 -38
- 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 +705 -98
package/src/validator.ts
CHANGED
|
@@ -1,30 +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';
|
|
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';
|
|
2
14
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
15
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
16
|
+
import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
|
|
17
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
6
18
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
7
19
|
import { sleep } from '@aztec/foundation/sleep';
|
|
8
20
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
9
21
|
import type { KeystoreManager } from '@aztec/node-keystore';
|
|
10
|
-
import type { P2P, PeerId
|
|
22
|
+
import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } from '@aztec/p2p';
|
|
11
23
|
import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
|
|
12
24
|
import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
|
|
13
25
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
14
|
-
import type { CommitteeAttestationsAndSigners, L2BlockSource } from '@aztec/stdlib/block';
|
|
15
|
-
import
|
|
16
|
-
import
|
|
17
|
-
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';
|
|
18
45
|
import type { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
19
|
-
import type {
|
|
46
|
+
import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
20
47
|
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
21
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';
|
|
22
56
|
|
|
23
57
|
import { EventEmitter } from 'events';
|
|
24
58
|
import type { TypedDataDefinition } from 'viem';
|
|
25
59
|
|
|
26
60
|
import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
|
|
61
|
+
import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
27
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';
|
|
28
65
|
import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
|
|
29
66
|
import { ValidatorMetrics } from './metrics.js';
|
|
30
67
|
|
|
@@ -45,36 +82,54 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
45
82
|
public readonly tracer: Tracer;
|
|
46
83
|
private validationService: ValidationService;
|
|
47
84
|
private metrics: ValidatorMetrics;
|
|
48
|
-
|
|
85
|
+
private log: Logger;
|
|
49
86
|
// Whether it has already registered handlers on the p2p client
|
|
50
87
|
private hasRegisteredHandlers = false;
|
|
51
88
|
|
|
52
|
-
|
|
53
|
-
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;
|
|
54
94
|
|
|
55
|
-
private lastEpochForCommitteeUpdateLoop:
|
|
95
|
+
private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
|
|
56
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();
|
|
57
99
|
|
|
58
100
|
private proposersOfInvalidBlocks: Set<string> = new Set();
|
|
59
101
|
|
|
102
|
+
/** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */
|
|
103
|
+
private lastAttestedProposal?: CheckpointProposalCore;
|
|
104
|
+
|
|
60
105
|
protected constructor(
|
|
61
|
-
private keyStore:
|
|
106
|
+
private keyStore: ExtendedValidatorKeyStore,
|
|
62
107
|
private epochCache: EpochCache,
|
|
63
108
|
private p2pClient: P2P,
|
|
64
109
|
private blockProposalHandler: BlockProposalHandler,
|
|
110
|
+
private blockSource: L2BlockSource,
|
|
111
|
+
private checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
112
|
+
private worldState: WorldStateSynchronizer,
|
|
113
|
+
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
65
114
|
private config: ValidatorClientFullConfig,
|
|
115
|
+
private blobClient: BlobClientInterface,
|
|
116
|
+
private slashingProtectionSigner: ValidatorHASigner,
|
|
66
117
|
private dateProvider: DateProvider = new DateProvider(),
|
|
67
118
|
telemetry: TelemetryClient = getTelemetryClient(),
|
|
68
|
-
|
|
119
|
+
log = createLogger('validator'),
|
|
69
120
|
) {
|
|
70
121
|
super();
|
|
122
|
+
|
|
123
|
+
// Create child logger with fisherman prefix if in fisherman mode
|
|
124
|
+
this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
|
|
125
|
+
|
|
71
126
|
this.tracer = telemetry.getTracer('Validator');
|
|
72
127
|
this.metrics = new ValidatorMetrics(telemetry);
|
|
73
128
|
|
|
74
|
-
this.validationService = new ValidationService(keyStore, log.createChild('validation-service'));
|
|
129
|
+
this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
|
|
75
130
|
|
|
76
131
|
// Refresh epoch cache every second to trigger alert if participation in committee changes
|
|
77
|
-
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), log, 1000);
|
|
132
|
+
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
|
|
78
133
|
|
|
79
134
|
const myAddresses = this.getValidatorAddresses();
|
|
80
135
|
this.log.verbose(`Initialized validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
|
|
@@ -112,6 +167,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
112
167
|
this.log.trace(`No committee found for slot`);
|
|
113
168
|
return;
|
|
114
169
|
}
|
|
170
|
+
this.metrics.setCurrentEpoch(epoch);
|
|
115
171
|
if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
|
|
116
172
|
const me = this.getValidatorAddresses();
|
|
117
173
|
const committeeSet = new Set(committee.map(v => v.toString()));
|
|
@@ -132,40 +188,81 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
132
188
|
}
|
|
133
189
|
}
|
|
134
190
|
|
|
135
|
-
static new(
|
|
191
|
+
static async new(
|
|
136
192
|
config: ValidatorClientFullConfig,
|
|
137
|
-
|
|
193
|
+
checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
194
|
+
worldState: WorldStateSynchronizer,
|
|
138
195
|
epochCache: EpochCache,
|
|
139
196
|
p2pClient: P2P,
|
|
140
|
-
blockSource: L2BlockSource,
|
|
197
|
+
blockSource: L2BlockSource & L2BlockSink,
|
|
141
198
|
l1ToL2MessageSource: L1ToL2MessageSource,
|
|
142
|
-
txProvider:
|
|
199
|
+
txProvider: ITxProvider,
|
|
143
200
|
keyStoreManager: KeystoreManager,
|
|
201
|
+
blobClient: BlobClientInterface,
|
|
144
202
|
dateProvider: DateProvider = new DateProvider(),
|
|
145
203
|
telemetry: TelemetryClient = getTelemetryClient(),
|
|
204
|
+
slashingProtectionDb?: SlashingProtectionDatabase,
|
|
146
205
|
) {
|
|
147
206
|
const metrics = new ValidatorMetrics(telemetry);
|
|
148
207
|
const blockProposalValidator = new BlockProposalValidator(epochCache, {
|
|
149
208
|
txsPermitted: !config.disableTransactions,
|
|
209
|
+
maxTxsPerBlock: config.validateMaxTxsPerBlock,
|
|
150
210
|
});
|
|
151
211
|
const blockProposalHandler = new BlockProposalHandler(
|
|
152
|
-
|
|
212
|
+
checkpointsBuilder,
|
|
213
|
+
worldState,
|
|
153
214
|
blockSource,
|
|
154
215
|
l1ToL2MessageSource,
|
|
155
216
|
txProvider,
|
|
156
217
|
blockProposalValidator,
|
|
218
|
+
epochCache,
|
|
157
219
|
config,
|
|
158
220
|
metrics,
|
|
159
221
|
dateProvider,
|
|
160
222
|
telemetry,
|
|
161
223
|
);
|
|
162
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
|
+
|
|
163
254
|
const validator = new ValidatorClient(
|
|
164
|
-
|
|
255
|
+
validatorKeyStore,
|
|
165
256
|
epochCache,
|
|
166
257
|
p2pClient,
|
|
167
258
|
blockProposalHandler,
|
|
259
|
+
blockSource,
|
|
260
|
+
checkpointsBuilder,
|
|
261
|
+
worldState,
|
|
262
|
+
l1ToL2MessageSource,
|
|
168
263
|
config,
|
|
264
|
+
blobClient,
|
|
265
|
+
slashingProtectionSigner,
|
|
169
266
|
dateProvider,
|
|
170
267
|
telemetry,
|
|
171
268
|
);
|
|
@@ -183,18 +280,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
183
280
|
return this.blockProposalHandler;
|
|
184
281
|
}
|
|
185
282
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
proposal: BlockProposal,
|
|
189
|
-
blockNumber: number,
|
|
190
|
-
txs: any[],
|
|
191
|
-
l1ToL2Messages: Fr[],
|
|
192
|
-
): Promise<any> {
|
|
193
|
-
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
public signWithAddress(addr: EthAddress, msg: TypedDataDefinition) {
|
|
197
|
-
return this.keyStore.signTypedDataWithAddress(addr, msg);
|
|
283
|
+
public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
|
|
284
|
+
return this.keyStore.signTypedDataWithAddress(addr, msg, context);
|
|
198
285
|
}
|
|
199
286
|
|
|
200
287
|
public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
|
|
@@ -213,24 +300,27 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
213
300
|
this.config = { ...this.config, ...config };
|
|
214
301
|
}
|
|
215
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
|
+
|
|
216
309
|
public async start() {
|
|
217
310
|
if (this.epochCacheUpdateLoop.isRunning()) {
|
|
218
311
|
this.log.warn(`Validator client already started`);
|
|
219
312
|
return;
|
|
220
313
|
}
|
|
221
314
|
|
|
315
|
+
await this.keyStore.start();
|
|
316
|
+
|
|
222
317
|
await this.registerHandlers();
|
|
223
318
|
|
|
224
319
|
const myAddresses = this.getValidatorAddresses();
|
|
225
320
|
const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
|
|
321
|
+
this.log.info(`Started validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
|
|
226
322
|
if (inCommittee.length > 0) {
|
|
227
|
-
this.log.info(
|
|
228
|
-
`Started validator with addresses in current validator committee: ${inCommittee
|
|
229
|
-
.map(a => a.toString())
|
|
230
|
-
.join(', ')}`,
|
|
231
|
-
);
|
|
232
|
-
} else {
|
|
233
|
-
this.log.info(`Started validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
|
|
323
|
+
this.log.info(`Addresses in current validator committee: ${inCommittee.map(a => a.toString()).join(', ')}`);
|
|
234
324
|
}
|
|
235
325
|
this.epochCacheUpdateLoop.start();
|
|
236
326
|
|
|
@@ -239,6 +329,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
239
329
|
|
|
240
330
|
public async stop() {
|
|
241
331
|
await this.epochCacheUpdateLoop.stop();
|
|
332
|
+
await this.keyStore.stop();
|
|
242
333
|
}
|
|
243
334
|
|
|
244
335
|
/** Register handlers on the p2p client */
|
|
@@ -247,9 +338,29 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
247
338
|
this.hasRegisteredHandlers = true;
|
|
248
339
|
this.log.debug(`Registering validator handlers for p2p client`);
|
|
249
340
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
+
});
|
|
253
364
|
|
|
254
365
|
const myAddresses = this.getValidatorAddresses();
|
|
255
366
|
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
@@ -258,44 +369,67 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
258
369
|
}
|
|
259
370
|
}
|
|
260
371
|
|
|
261
|
-
|
|
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> {
|
|
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
|
+
|
|
263
384
|
const proposer = proposal.getSender();
|
|
264
385
|
|
|
265
386
|
// Reject proposals with invalid signatures
|
|
266
387
|
if (!proposer) {
|
|
267
|
-
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
268
|
-
return
|
|
388
|
+
this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
|
|
389
|
+
return false;
|
|
269
390
|
}
|
|
270
391
|
|
|
271
|
-
//
|
|
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)
|
|
272
401
|
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
273
402
|
const partOfCommittee = inCommittee.length > 0;
|
|
274
403
|
|
|
275
404
|
const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
|
|
276
|
-
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
405
|
+
this.log.info(`Received block proposal for slot ${slotNumber}`, {
|
|
277
406
|
...proposalInfo,
|
|
278
407
|
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
408
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
279
409
|
});
|
|
280
410
|
|
|
281
|
-
// Reexecute txs if we are part of the committee
|
|
282
|
-
//
|
|
283
|
-
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals } =
|
|
411
|
+
// Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
|
|
412
|
+
// In fisherman mode, we always reexecute to validate proposals.
|
|
413
|
+
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } =
|
|
414
|
+
this.config;
|
|
284
415
|
const shouldReexecute =
|
|
416
|
+
fishermanMode ||
|
|
285
417
|
(slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute) ||
|
|
286
418
|
(partOfCommittee && validatorReexecute) ||
|
|
287
|
-
alwaysReexecuteBlockProposals
|
|
419
|
+
alwaysReexecuteBlockProposals ||
|
|
420
|
+
this.blobClient.canUpload();
|
|
288
421
|
|
|
289
422
|
const validationResult = await this.blockProposalHandler.handleBlockProposal(
|
|
290
423
|
proposal,
|
|
291
424
|
proposalSender,
|
|
292
|
-
!!shouldReexecute,
|
|
425
|
+
!!shouldReexecute && !escapeHatchOpen,
|
|
293
426
|
);
|
|
294
427
|
|
|
295
428
|
if (!validationResult.isValid) {
|
|
296
|
-
this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
297
|
-
|
|
298
429
|
const reason = validationResult.reason || 'unknown';
|
|
430
|
+
|
|
431
|
+
this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
|
|
432
|
+
|
|
299
433
|
// Classify failure reason: bad proposal vs node issue
|
|
300
434
|
const badProposalReasons: BlockProposalValidationFailureReason[] = [
|
|
301
435
|
'invalid_proposal',
|
|
@@ -308,12 +442,13 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
308
442
|
if (badProposalReasons.includes(reason as BlockProposalValidationFailureReason)) {
|
|
309
443
|
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
310
444
|
} else {
|
|
311
|
-
// Node issues so we can't
|
|
445
|
+
// Node issues so we can't validate
|
|
312
446
|
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
313
447
|
}
|
|
314
448
|
|
|
315
449
|
// Slash invalid block proposals (can happen even when not in committee)
|
|
316
450
|
if (
|
|
451
|
+
!escapeHatchOpen &&
|
|
317
452
|
validationResult.reason &&
|
|
318
453
|
SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) &&
|
|
319
454
|
slashBroadcastedInvalidBlockPenalty > 0n
|
|
@@ -321,21 +456,386 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
321
456
|
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
322
457
|
this.slashInvalidBlock(proposal);
|
|
323
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`);
|
|
493
|
+
return undefined;
|
|
494
|
+
}
|
|
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
|
+
});
|
|
324
508
|
return undefined;
|
|
325
509
|
}
|
|
326
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
|
+
|
|
327
549
|
// Check that I have any address in current committee before attesting
|
|
328
|
-
if
|
|
550
|
+
// In fisherman mode, we still create attestations for validation even if not in committee
|
|
551
|
+
if (!partOfCommittee && !this.config.fishermanMode) {
|
|
329
552
|
this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
|
|
330
553
|
return undefined;
|
|
331
554
|
}
|
|
332
555
|
|
|
333
556
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
334
|
-
this.log.info(
|
|
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
|
+
);
|
|
565
|
+
|
|
335
566
|
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
336
567
|
|
|
337
|
-
//
|
|
338
|
-
|
|
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
|
+
|
|
579
|
+
// Determine which validators should attest
|
|
580
|
+
let attestors: EthAddress[];
|
|
581
|
+
if (partOfCommittee) {
|
|
582
|
+
attestors = inCommittee;
|
|
583
|
+
} else if (this.config.fishermanMode) {
|
|
584
|
+
// In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
|
|
585
|
+
attestors = this.getValidatorAddresses();
|
|
586
|
+
} else {
|
|
587
|
+
attestors = [];
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// Only create attestations if we have attestors
|
|
591
|
+
if (attestors.length === 0) {
|
|
592
|
+
return undefined;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
if (this.config.fishermanMode) {
|
|
596
|
+
// bail out early and don't save attestations to the pool in fisherman mode
|
|
597
|
+
this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
|
|
598
|
+
...proposalInfo,
|
|
599
|
+
attestors: attestors.map(a => a.toString()),
|
|
600
|
+
});
|
|
601
|
+
return undefined;
|
|
602
|
+
}
|
|
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
|
+
}
|
|
339
839
|
}
|
|
340
840
|
|
|
341
841
|
private slashInvalidBlock(proposal: BlockProposal) {
|
|
@@ -360,34 +860,130 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
360
860
|
validator: proposer,
|
|
361
861
|
amount: this.config.slashBroadcastedInvalidBlockPenalty,
|
|
362
862
|
offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
|
|
363
|
-
epochOrSlot: proposal.slotNumber
|
|
863
|
+
epochOrSlot: BigInt(proposal.slotNumber),
|
|
864
|
+
},
|
|
865
|
+
]);
|
|
866
|
+
}
|
|
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),
|
|
364
910
|
},
|
|
365
911
|
]);
|
|
366
912
|
}
|
|
367
913
|
|
|
368
914
|
async createBlockProposal(
|
|
369
|
-
|
|
370
|
-
|
|
915
|
+
blockHeader: BlockHeader,
|
|
916
|
+
indexWithinCheckpoint: IndexWithinCheckpoint,
|
|
917
|
+
inHash: Fr,
|
|
371
918
|
archive: Fr,
|
|
372
|
-
stateReference: StateReference,
|
|
373
919
|
txs: Tx[],
|
|
374
920
|
proposerAddress: EthAddress | undefined,
|
|
375
|
-
options: BlockProposalOptions,
|
|
376
|
-
): Promise<BlockProposal
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
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
|
+
}
|
|
380
935
|
}
|
|
381
936
|
|
|
937
|
+
this.log.info(
|
|
938
|
+
`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
|
|
939
|
+
);
|
|
382
940
|
const newProposal = await this.validationService.createBlockProposal(
|
|
383
|
-
|
|
941
|
+
blockHeader,
|
|
942
|
+
indexWithinCheckpoint,
|
|
943
|
+
inHash,
|
|
384
944
|
archive,
|
|
385
|
-
stateReference,
|
|
386
945
|
txs,
|
|
387
946
|
proposerAddress,
|
|
388
|
-
{
|
|
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,
|
|
389
985
|
);
|
|
390
|
-
this.
|
|
986
|
+
this.lastProposedCheckpoint = newProposal;
|
|
391
987
|
return newProposal;
|
|
392
988
|
}
|
|
393
989
|
|
|
@@ -398,20 +994,38 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
398
994
|
async signAttestationsAndSigners(
|
|
399
995
|
attestationsAndSigners: CommitteeAttestationsAndSigners,
|
|
400
996
|
proposer: EthAddress,
|
|
997
|
+
slot: SlotNumber,
|
|
998
|
+
blockNumber: BlockNumber | CheckpointNumber,
|
|
401
999
|
): Promise<Signature> {
|
|
402
|
-
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
1000
|
+
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
|
|
403
1001
|
}
|
|
404
1002
|
|
|
405
|
-
async collectOwnAttestations(proposal:
|
|
406
|
-
const slot = proposal.
|
|
1003
|
+
async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
|
|
1004
|
+
const slot = proposal.slotNumber;
|
|
407
1005
|
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
408
1006
|
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
|
|
409
|
-
|
|
1007
|
+
const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
|
|
1008
|
+
|
|
1009
|
+
if (!attestations) {
|
|
1010
|
+
return [];
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
// We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
|
|
1014
|
+
// other nodes can see that our validators did attest to this block proposal, and do not slash us
|
|
1015
|
+
// due to inactivity for missed attestations.
|
|
1016
|
+
void this.p2pClient.broadcastCheckpointAttestations(attestations).catch(err => {
|
|
1017
|
+
this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
|
|
1018
|
+
});
|
|
1019
|
+
return attestations;
|
|
410
1020
|
}
|
|
411
1021
|
|
|
412
|
-
async collectAttestations(
|
|
413
|
-
|
|
414
|
-
|
|
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;
|
|
415
1029
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
416
1030
|
|
|
417
1031
|
if (+deadline < this.dateProvider.now()) {
|
|
@@ -426,16 +1040,16 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
426
1040
|
const proposalId = proposal.archive.toString();
|
|
427
1041
|
const myAddresses = this.getValidatorAddresses();
|
|
428
1042
|
|
|
429
|
-
let attestations:
|
|
1043
|
+
let attestations: CheckpointAttestation[] = [];
|
|
430
1044
|
while (true) {
|
|
431
|
-
// Filter out attestations with a mismatching
|
|
1045
|
+
// Filter out attestations with a mismatching archive. This should NOT happen since we have verified
|
|
432
1046
|
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
433
|
-
const collectedAttestations = (await this.p2pClient.
|
|
1047
|
+
const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter(
|
|
434
1048
|
attestation => {
|
|
435
|
-
if (!attestation.
|
|
1049
|
+
if (!attestation.archive.equals(proposal.archive)) {
|
|
436
1050
|
this.log.warn(
|
|
437
|
-
`Received attestation for slot ${slot} with mismatched
|
|
438
|
-
{
|
|
1051
|
+
`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`,
|
|
1052
|
+
{ attestationArchive: attestation.archive.toString(), proposalArchive: proposal.archive.toString() },
|
|
439
1053
|
);
|
|
440
1054
|
return false;
|
|
441
1055
|
}
|
|
@@ -476,15 +1090,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
476
1090
|
}
|
|
477
1091
|
}
|
|
478
1092
|
|
|
479
|
-
private async createBlockAttestationsFromProposal(
|
|
480
|
-
proposal: BlockProposal,
|
|
481
|
-
attestors: EthAddress[] = [],
|
|
482
|
-
): Promise<BlockAttestation[]> {
|
|
483
|
-
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
484
|
-
await this.p2pClient.addAttestations(attestations);
|
|
485
|
-
return attestations;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
1093
|
private async handleAuthRequest(peer: PeerId, msg: Buffer): Promise<Buffer> {
|
|
489
1094
|
const authRequest = AuthRequest.fromBuffer(msg);
|
|
490
1095
|
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch(_ => undefined);
|
|
@@ -503,7 +1108,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
503
1108
|
}
|
|
504
1109
|
|
|
505
1110
|
const payloadToSign = authRequest.getPayloadToSign();
|
|
506
|
-
|
|
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);
|
|
507
1114
|
const authResponse = new AuthResponse(statusMessage, signature);
|
|
508
1115
|
return authResponse.toBuffer();
|
|
509
1116
|
}
|