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