@aztec/validator-client 0.0.1-commit.3e3d0c9cd → 0.0.1-commit.3f5453c7b
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 +9 -13
- package/dest/checkpoint_builder.d.ts +10 -8
- package/dest/checkpoint_builder.d.ts.map +1 -1
- package/dest/checkpoint_builder.js +29 -28
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +0 -5
- package/dest/duties/validation_service.d.ts +3 -4
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +7 -12
- package/dest/factory.d.ts +7 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +5 -5
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -1
- package/dest/metrics.d.ts +6 -2
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +12 -0
- package/dest/proposal_handler.d.ts +107 -0
- package/dest/proposal_handler.d.ts.map +1 -0
- package/dest/{block_proposal_handler.js → proposal_handler.js} +392 -25
- package/dest/validator.d.ts +10 -15
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +44 -216
- package/package.json +19 -19
- package/src/checkpoint_builder.ts +38 -33
- package/src/config.ts +0 -5
- package/src/duties/validation_service.ts +7 -14
- package/src/factory.ts +10 -4
- package/src/index.ts +1 -1
- package/src/metrics.ts +19 -1
- package/src/{block_proposal_handler.ts → proposal_handler.ts} +439 -23
- package/src/validator.ts +60 -235
- package/dest/block_proposal_handler.d.ts +0 -64
- package/dest/block_proposal_handler.d.ts.map +0 -1
|
@@ -1,20 +1,30 @@
|
|
|
1
|
+
import type { Archiver } from '@aztec/archiver';
|
|
2
|
+
import type { BlobClientInterface } from '@aztec/blob-client/client';
|
|
3
|
+
import { type Blob, encodeCheckpointBlobDataFromBlocks, getBlobsPerL1Block } from '@aztec/blob-lib';
|
|
1
4
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
5
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
6
|
+
import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
|
|
3
7
|
import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
8
|
import { pick } from '@aztec/foundation/collection';
|
|
5
9
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
6
10
|
import { TimeoutError } from '@aztec/foundation/error';
|
|
11
|
+
import type { LogData } from '@aztec/foundation/log';
|
|
7
12
|
import { createLogger } from '@aztec/foundation/log';
|
|
8
13
|
import { retryUntil } from '@aztec/foundation/retry';
|
|
9
14
|
import { DateProvider, Timer } from '@aztec/foundation/timer';
|
|
10
15
|
import type { P2P, PeerId } from '@aztec/p2p';
|
|
11
16
|
import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
|
|
12
17
|
import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
18
|
+
import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
13
19
|
import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
14
20
|
import { Gas } from '@aztec/stdlib/gas';
|
|
15
21
|
import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
16
|
-
import {
|
|
17
|
-
|
|
22
|
+
import {
|
|
23
|
+
type L1ToL2MessageSource,
|
|
24
|
+
accumulateCheckpointOutHashes,
|
|
25
|
+
computeInHashFromL1ToL2Messages,
|
|
26
|
+
} from '@aztec/stdlib/messaging';
|
|
27
|
+
import type { BlockProposal, CheckpointAttestation, CheckpointProposalCore } from '@aztec/stdlib/p2p';
|
|
18
28
|
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
19
29
|
import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
|
|
20
30
|
import {
|
|
@@ -66,13 +76,29 @@ export type BlockProposalValidationFailureResult = {
|
|
|
66
76
|
|
|
67
77
|
export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
|
|
68
78
|
|
|
79
|
+
export type CheckpointProposalValidationResult = { isValid: true } | { isValid: false; reason: string };
|
|
80
|
+
|
|
69
81
|
type CheckpointComputationResult =
|
|
70
82
|
| { checkpointNumber: CheckpointNumber; reason?: undefined }
|
|
71
83
|
| { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
|
|
72
84
|
|
|
73
|
-
|
|
85
|
+
/** Handles block and checkpoint proposals for both validator and non-validator nodes. */
|
|
86
|
+
export class ProposalHandler {
|
|
74
87
|
public readonly tracer: Tracer;
|
|
75
88
|
|
|
89
|
+
/** Cached last checkpoint validation result to avoid double-validation on validator nodes. */
|
|
90
|
+
private lastCheckpointValidationResult?: {
|
|
91
|
+
archive: Fr;
|
|
92
|
+
slotNumber: SlotNumber;
|
|
93
|
+
result: CheckpointProposalValidationResult;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** Archiver reference for setting proposed checkpoints (pipelining). Set via register(). */
|
|
97
|
+
private archiver?: Pick<Archiver, 'setProposedCheckpoint' | 'getL1Constants'>;
|
|
98
|
+
|
|
99
|
+
/** Returns current validator addresses for own-proposal detection. Set via register(). */
|
|
100
|
+
private getOwnValidatorAddresses?: () => string[];
|
|
101
|
+
|
|
76
102
|
constructor(
|
|
77
103
|
private checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
78
104
|
private worldState: WorldStateSynchronizer,
|
|
@@ -82,21 +108,37 @@ export class BlockProposalHandler {
|
|
|
82
108
|
private blockProposalValidator: BlockProposalValidator,
|
|
83
109
|
private epochCache: EpochCache,
|
|
84
110
|
private config: ValidatorClientFullConfig,
|
|
111
|
+
private blobClient: BlobClientInterface,
|
|
85
112
|
private metrics?: ValidatorMetrics,
|
|
86
113
|
private dateProvider: DateProvider = new DateProvider(),
|
|
87
114
|
telemetry: TelemetryClient = getTelemetryClient(),
|
|
88
|
-
private log = createLogger('validator:
|
|
115
|
+
private log = createLogger('validator:proposal-handler'),
|
|
89
116
|
) {
|
|
90
117
|
if (config.fishermanMode) {
|
|
91
118
|
this.log = this.log.createChild('[FISHERMAN]');
|
|
92
119
|
}
|
|
93
|
-
this.tracer = telemetry.getTracer('
|
|
120
|
+
this.tracer = telemetry.getTracer('ProposalHandler');
|
|
94
121
|
}
|
|
95
122
|
|
|
96
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Registers handlers for block and checkpoint proposals on the p2p client.
|
|
125
|
+
* Block proposals are registered for non-validator nodes (validators register their own enhanced handler).
|
|
126
|
+
* The all-nodes checkpoint proposal handler is always registered for validation, caching, and pipelining.
|
|
127
|
+
* @param archiver - Archiver reference for setting proposed checkpoints (pipelining)
|
|
128
|
+
* @param getOwnValidatorAddresses - Returns current validator addresses for own-proposal detection
|
|
129
|
+
*/
|
|
130
|
+
register(
|
|
131
|
+
p2pClient: P2P,
|
|
132
|
+
shouldReexecute: boolean,
|
|
133
|
+
archiver?: Pick<Archiver, 'setProposedCheckpoint' | 'getL1Constants'>,
|
|
134
|
+
getOwnValidatorAddresses?: () => string[],
|
|
135
|
+
): ProposalHandler {
|
|
136
|
+
this.archiver = archiver;
|
|
137
|
+
this.getOwnValidatorAddresses = getOwnValidatorAddresses;
|
|
138
|
+
|
|
97
139
|
// Non-validator handler that processes or re-executes for monitoring but does not attest.
|
|
98
140
|
// Returns boolean indicating whether the proposal was valid.
|
|
99
|
-
const
|
|
141
|
+
const blockHandler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
|
|
100
142
|
try {
|
|
101
143
|
const { slotNumber, blockNumber } = proposal;
|
|
102
144
|
const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
|
|
@@ -123,7 +165,51 @@ export class BlockProposalHandler {
|
|
|
123
165
|
}
|
|
124
166
|
};
|
|
125
167
|
|
|
126
|
-
p2pClient.registerBlockProposalHandler(
|
|
168
|
+
p2pClient.registerBlockProposalHandler(blockHandler);
|
|
169
|
+
|
|
170
|
+
// All-nodes checkpoint proposal handler: validates, caches, and sets proposed checkpoint for pipelining.
|
|
171
|
+
// Runs for all nodes (validators and non-validators). Validators get the cached result in the
|
|
172
|
+
// validator-specific callback (attestToCheckpointProposal) which runs after this one.
|
|
173
|
+
const checkpointHandler = async (
|
|
174
|
+
proposal: CheckpointProposalCore,
|
|
175
|
+
_sender: PeerId,
|
|
176
|
+
): Promise<CheckpointAttestation[] | undefined> => {
|
|
177
|
+
try {
|
|
178
|
+
const pipeliningTimer = new Timer();
|
|
179
|
+
const proposalInfo: LogData = {
|
|
180
|
+
slot: proposal.slotNumber,
|
|
181
|
+
archive: proposal.archive.toString(),
|
|
182
|
+
proposer: proposal.getSender()?.toString(),
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// For own proposals, skip validation — the proposer already built and validated the checkpoint
|
|
186
|
+
const proposer = proposal.getSender();
|
|
187
|
+
const ownAddresses = this.getOwnValidatorAddresses?.();
|
|
188
|
+
const isOwnProposal = proposer && ownAddresses?.some(addr => addr === proposer.toString());
|
|
189
|
+
|
|
190
|
+
if (isOwnProposal) {
|
|
191
|
+
this.log.debug(`Skipping validation for own checkpoint proposal at slot ${proposal.slotNumber}`);
|
|
192
|
+
if (this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
|
|
193
|
+
await this.setProposedCheckpointFromBlocks(proposal);
|
|
194
|
+
}
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const result = await this.handleCheckpointProposal(proposal, proposalInfo);
|
|
199
|
+
if (result.isValid && this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
|
|
200
|
+
const set = await this.setProposedCheckpointFromValidation(proposal);
|
|
201
|
+
if (set) {
|
|
202
|
+
this.metrics?.recordCheckpointProposalToPipelinedStateDuration(pipeliningTimer.ms());
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
} catch (err) {
|
|
206
|
+
this.log.warn(`Error handling checkpoint proposal for slot ${proposal.slotNumber}`, { err });
|
|
207
|
+
}
|
|
208
|
+
return undefined;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
p2pClient.registerAllNodesCheckpointProposalHandler(checkpointHandler);
|
|
212
|
+
|
|
127
213
|
return this;
|
|
128
214
|
}
|
|
129
215
|
|
|
@@ -163,14 +249,18 @@ export class BlockProposalHandler {
|
|
|
163
249
|
}
|
|
164
250
|
|
|
165
251
|
// Ensure the block source is synced before checking for existing blocks,
|
|
166
|
-
// since a
|
|
252
|
+
// since a proposed checkpoint prune may remove blocks we'd otherwise find.
|
|
167
253
|
// This affects mostly the block_number_already_exists check, since a pending
|
|
168
254
|
// checkpoint prune could remove a block that would conflict with this proposal.
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
255
|
+
// When pipelining is enabled, the proposer builds ahead of L1 submission, so the
|
|
256
|
+
// block source won't have synced to the proposed slot yet. Skip the sync wait to
|
|
257
|
+
// avoid eating into the attestation window.
|
|
258
|
+
if (!this.epochCache.isProposerPipeliningEnabled()) {
|
|
259
|
+
const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
|
|
260
|
+
if (!blockSourceSync) {
|
|
261
|
+
this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
|
|
262
|
+
return { isValid: false, reason: 'block_source_not_synced' };
|
|
263
|
+
}
|
|
174
264
|
}
|
|
175
265
|
|
|
176
266
|
// Check that the parent proposal is a block we know, otherwise reexecution would fail.
|
|
@@ -275,7 +365,7 @@ export class BlockProposalHandler {
|
|
|
275
365
|
|
|
276
366
|
// If we succeeded, push this block into the archiver (unless disabled)
|
|
277
367
|
if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
|
|
278
|
-
await this.blockSource.addBlock(reexecutionResult
|
|
368
|
+
await this.blockSource.addBlock(reexecutionResult.block);
|
|
279
369
|
}
|
|
280
370
|
|
|
281
371
|
this.log.info(
|
|
@@ -288,7 +378,6 @@ export class BlockProposalHandler {
|
|
|
288
378
|
|
|
289
379
|
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
|
|
290
380
|
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
291
|
-
const slot = proposal.slotNumber;
|
|
292
381
|
const config = this.checkpointsBuilder.getConfig();
|
|
293
382
|
const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
|
|
294
383
|
|
|
@@ -296,7 +385,7 @@ export class BlockProposalHandler {
|
|
|
296
385
|
return 'genesis';
|
|
297
386
|
}
|
|
298
387
|
|
|
299
|
-
const deadline = this.getReexecutionDeadline(
|
|
388
|
+
const deadline = this.getReexecutionDeadline(proposal.slotNumber, config);
|
|
300
389
|
const currentTime = this.dateProvider.now();
|
|
301
390
|
const timeoutDurationMs = deadline.getTime() - currentTime;
|
|
302
391
|
|
|
@@ -445,8 +534,14 @@ export class BlockProposalHandler {
|
|
|
445
534
|
return undefined;
|
|
446
535
|
}
|
|
447
536
|
|
|
448
|
-
private getReexecutionDeadline(
|
|
449
|
-
|
|
537
|
+
private getReexecutionDeadline(
|
|
538
|
+
slotNumber: SlotNumber,
|
|
539
|
+
config: { l1GenesisTime: bigint; slotDuration: number },
|
|
540
|
+
): Date {
|
|
541
|
+
// Under proposer pipelining, the proposal slot may be ahead of wall clock time.
|
|
542
|
+
// Reexecution budgets should still be bounded by the current slot we are in now.
|
|
543
|
+
const wallclockSlot = slotNumber - this.epochCache.pipeliningOffset();
|
|
544
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(wallclockSlot + 1), config));
|
|
450
545
|
return new Date(nextSlotTimestampSeconds * 1000);
|
|
451
546
|
}
|
|
452
547
|
|
|
@@ -459,8 +554,9 @@ export class BlockProposalHandler {
|
|
|
459
554
|
}
|
|
460
555
|
|
|
461
556
|
// Make a quick check before triggering an archiver sync
|
|
557
|
+
// If we are pipelining and have a pending checkpoint number stored, we will allow the block proposal to be for a slot further
|
|
462
558
|
const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
|
|
463
|
-
if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
|
|
559
|
+
if (syncedSlot !== undefined && syncedSlot + 1 + this.epochCache.pipeliningOffset() >= slot) {
|
|
464
560
|
return true;
|
|
465
561
|
}
|
|
466
562
|
|
|
@@ -469,8 +565,8 @@ export class BlockProposalHandler {
|
|
|
469
565
|
return await retryUntil(
|
|
470
566
|
async () => {
|
|
471
567
|
await this.blockSource.syncImmediate();
|
|
472
|
-
const
|
|
473
|
-
return
|
|
568
|
+
const updatedSyncedSlot = await this.blockSource.getSyncedL2SlotNumber();
|
|
569
|
+
return updatedSyncedSlot !== undefined && updatedSyncedSlot + 1 >= slot;
|
|
474
570
|
},
|
|
475
571
|
'wait for block source sync',
|
|
476
572
|
timeoutMs / 1000,
|
|
@@ -487,7 +583,9 @@ export class BlockProposalHandler {
|
|
|
487
583
|
}
|
|
488
584
|
|
|
489
585
|
private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
|
|
490
|
-
if (err instanceof
|
|
586
|
+
if (err instanceof TransactionsNotAvailableError) {
|
|
587
|
+
return 'txs_not_available';
|
|
588
|
+
} else if (err instanceof ReExInitialStateMismatchError) {
|
|
491
589
|
return 'initial_state_mismatch';
|
|
492
590
|
} else if (err instanceof ReExStateMismatchError) {
|
|
493
591
|
return 'state_mismatch';
|
|
@@ -567,6 +665,8 @@ export class BlockProposalHandler {
|
|
|
567
665
|
? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
|
|
568
666
|
: undefined;
|
|
569
667
|
const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
|
|
668
|
+
isBuildingProposal: false,
|
|
669
|
+
minValidTxs: 0,
|
|
570
670
|
deadline,
|
|
571
671
|
expectedEndState: blockHeader.state,
|
|
572
672
|
maxTransactions: this.config.validateMaxTxsPerBlock,
|
|
@@ -621,4 +721,320 @@ export class BlockProposalHandler {
|
|
|
621
721
|
totalManaUsed,
|
|
622
722
|
};
|
|
623
723
|
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Validates a checkpoint proposal, caches the result, and uploads blobs if configured.
|
|
727
|
+
* Returns a cached result if the same proposal (archive + slot) was already validated.
|
|
728
|
+
* Used by both the all-nodes callback (via register) and the validator client (via delegation).
|
|
729
|
+
*/
|
|
730
|
+
async handleCheckpointProposal(
|
|
731
|
+
proposal: CheckpointProposalCore,
|
|
732
|
+
proposalInfo: LogData,
|
|
733
|
+
): Promise<CheckpointProposalValidationResult> {
|
|
734
|
+
const slot = proposal.slotNumber;
|
|
735
|
+
|
|
736
|
+
// Check cache: same archive+slot means we already validated this proposal
|
|
737
|
+
if (
|
|
738
|
+
this.lastCheckpointValidationResult &&
|
|
739
|
+
this.lastCheckpointValidationResult.archive.equals(proposal.archive) &&
|
|
740
|
+
this.lastCheckpointValidationResult.slotNumber === slot
|
|
741
|
+
) {
|
|
742
|
+
this.log.debug(`Returning cached validation result for checkpoint proposal at slot ${slot}`, proposalInfo);
|
|
743
|
+
return this.lastCheckpointValidationResult.result;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
const proposer = proposal.getSender();
|
|
747
|
+
if (!proposer) {
|
|
748
|
+
this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
|
|
749
|
+
const result: CheckpointProposalValidationResult = { isValid: false, reason: 'invalid_signature' };
|
|
750
|
+
this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
|
|
751
|
+
return result;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
|
|
755
|
+
this.log.warn(
|
|
756
|
+
`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`,
|
|
757
|
+
);
|
|
758
|
+
const result: CheckpointProposalValidationResult = { isValid: false, reason: 'invalid_fee_asset_price_modifier' };
|
|
759
|
+
this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
|
|
760
|
+
return result;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
const result = await this.validateCheckpointProposal(proposal, proposalInfo);
|
|
764
|
+
this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
|
|
765
|
+
|
|
766
|
+
// Upload blobs to filestore if validation passed (fire and forget)
|
|
767
|
+
if (result.isValid) {
|
|
768
|
+
this.tryUploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
return result;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
|
|
776
|
+
* @returns Validation result with isValid flag and reason if invalid.
|
|
777
|
+
*/
|
|
778
|
+
async validateCheckpointProposal(
|
|
779
|
+
proposal: CheckpointProposalCore,
|
|
780
|
+
proposalInfo: LogData,
|
|
781
|
+
): Promise<CheckpointProposalValidationResult> {
|
|
782
|
+
const slot = proposal.slotNumber;
|
|
783
|
+
|
|
784
|
+
// Timeout block syncing at the start of the next slot
|
|
785
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
786
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
787
|
+
const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
|
|
788
|
+
|
|
789
|
+
// Wait for last block to sync by archive
|
|
790
|
+
let lastBlockHeader;
|
|
791
|
+
try {
|
|
792
|
+
lastBlockHeader = await retryUntil(
|
|
793
|
+
async () => {
|
|
794
|
+
await this.blockSource.syncImmediate();
|
|
795
|
+
return this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
796
|
+
},
|
|
797
|
+
`waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
|
|
798
|
+
timeoutSeconds,
|
|
799
|
+
0.5,
|
|
800
|
+
);
|
|
801
|
+
} catch (err) {
|
|
802
|
+
if (err instanceof TimeoutError) {
|
|
803
|
+
this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
|
|
804
|
+
return { isValid: false, reason: 'last_block_not_found' };
|
|
805
|
+
}
|
|
806
|
+
this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
|
|
807
|
+
return { isValid: false, reason: 'block_fetch_error' };
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
if (!lastBlockHeader) {
|
|
811
|
+
this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
|
|
812
|
+
return { isValid: false, reason: 'last_block_not_found' };
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// Get all full blocks for the slot and checkpoint
|
|
816
|
+
const blocks = await this.blockSource.getBlocksForSlot(slot);
|
|
817
|
+
if (blocks.length === 0) {
|
|
818
|
+
this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
|
|
819
|
+
return { isValid: false, reason: 'no_blocks_for_slot' };
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
// Ensure the last block for this slot matches the archive in the checkpoint proposal
|
|
823
|
+
if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
|
|
824
|
+
this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
|
|
825
|
+
return { isValid: false, reason: 'last_block_archive_mismatch' };
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
|
|
829
|
+
...proposalInfo,
|
|
830
|
+
blockNumbers: blocks.map(b => b.number),
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
// Get checkpoint constants from first block
|
|
834
|
+
const firstBlock = blocks[0];
|
|
835
|
+
const constants = this.extractCheckpointConstants(firstBlock);
|
|
836
|
+
const checkpointNumber = firstBlock.checkpointNumber;
|
|
837
|
+
|
|
838
|
+
// Get L1-to-L2 messages for this checkpoint
|
|
839
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
840
|
+
|
|
841
|
+
// Collect the out hashes of all the checkpoints before this one in the same epoch
|
|
842
|
+
const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
|
|
843
|
+
const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
|
|
844
|
+
.filter(c => c.checkpointNumber < checkpointNumber)
|
|
845
|
+
.map(c => c.checkpointOutHash);
|
|
846
|
+
|
|
847
|
+
// Fork world state at the block before the first block
|
|
848
|
+
const parentBlockNumber = BlockNumber(firstBlock.number - 1);
|
|
849
|
+
await using fork = await this.checkpointsBuilder.getFork(parentBlockNumber);
|
|
850
|
+
|
|
851
|
+
// Create checkpoint builder with all existing blocks
|
|
852
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
853
|
+
checkpointNumber,
|
|
854
|
+
constants,
|
|
855
|
+
proposal.feeAssetPriceModifier,
|
|
856
|
+
l1ToL2Messages,
|
|
857
|
+
previousCheckpointOutHashes,
|
|
858
|
+
fork,
|
|
859
|
+
blocks,
|
|
860
|
+
this.log.getBindings(),
|
|
861
|
+
);
|
|
862
|
+
|
|
863
|
+
// Complete the checkpoint to get computed values
|
|
864
|
+
const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
|
|
865
|
+
|
|
866
|
+
// Compare checkpoint header with proposal
|
|
867
|
+
if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
|
|
868
|
+
this.log.warn(`Checkpoint header mismatch`, {
|
|
869
|
+
...proposalInfo,
|
|
870
|
+
computed: computedCheckpoint.header.toInspect(),
|
|
871
|
+
proposal: proposal.checkpointHeader.toInspect(),
|
|
872
|
+
});
|
|
873
|
+
return { isValid: false, reason: 'checkpoint_header_mismatch' };
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// Compare archive root with proposal
|
|
877
|
+
if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
|
|
878
|
+
this.log.warn(`Archive root mismatch`, {
|
|
879
|
+
...proposalInfo,
|
|
880
|
+
computed: computedCheckpoint.archive.root.toString(),
|
|
881
|
+
proposal: proposal.archive.toString(),
|
|
882
|
+
});
|
|
883
|
+
return { isValid: false, reason: 'archive_mismatch' };
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
// Check that the accumulated epoch out hash matches the value in the proposal.
|
|
887
|
+
// The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
|
|
888
|
+
const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
|
|
889
|
+
const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
|
|
890
|
+
const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
|
|
891
|
+
if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
|
|
892
|
+
this.log.warn(`Epoch out hash mismatch`, {
|
|
893
|
+
proposalEpochOutHash: proposalEpochOutHash.toString(),
|
|
894
|
+
computedEpochOutHash: computedEpochOutHash.toString(),
|
|
895
|
+
checkpointOutHash: checkpointOutHash.toString(),
|
|
896
|
+
previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
|
|
897
|
+
...proposalInfo,
|
|
898
|
+
});
|
|
899
|
+
return { isValid: false, reason: 'out_hash_mismatch' };
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// Final round of validations on the checkpoint, just in case.
|
|
903
|
+
try {
|
|
904
|
+
validateCheckpoint(computedCheckpoint, {
|
|
905
|
+
rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
|
|
906
|
+
maxDABlockGas: this.config.validateMaxDABlockGas,
|
|
907
|
+
maxL2BlockGas: this.config.validateMaxL2BlockGas,
|
|
908
|
+
maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
|
|
909
|
+
maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
|
|
910
|
+
});
|
|
911
|
+
} catch (err) {
|
|
912
|
+
this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
|
|
913
|
+
return { isValid: false, reason: 'checkpoint_validation_failed' };
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
|
|
917
|
+
return { isValid: true };
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
/** Extracts checkpoint global variables from a block. */
|
|
921
|
+
private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
|
|
922
|
+
const gv = block.header.globalVariables;
|
|
923
|
+
return {
|
|
924
|
+
chainId: gv.chainId,
|
|
925
|
+
version: gv.version,
|
|
926
|
+
slotNumber: gv.slotNumber,
|
|
927
|
+
timestamp: gv.timestamp,
|
|
928
|
+
coinbase: gv.coinbase,
|
|
929
|
+
feeRecipient: gv.feeRecipient,
|
|
930
|
+
gasFees: gv.gasFees,
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/** Triggers blob upload for a checkpoint if the blob client can upload (fire and forget). */
|
|
935
|
+
protected tryUploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): void {
|
|
936
|
+
if (this.blobClient.canUpload()) {
|
|
937
|
+
void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
/** Uploads blobs for a checkpoint to the filestore. */
|
|
942
|
+
protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
|
|
943
|
+
try {
|
|
944
|
+
const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
945
|
+
if (!lastBlockHeader) {
|
|
946
|
+
this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
|
|
951
|
+
if (blocks.length === 0) {
|
|
952
|
+
this.log.warn(`No blocks found for blob upload`, proposalInfo);
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
const blockBlobData = blocks.map(b => b.toBlockBlobData());
|
|
957
|
+
const blobFields = encodeCheckpointBlobDataFromBlocks(blockBlobData);
|
|
958
|
+
const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
|
|
959
|
+
await this.blobClient.sendBlobsToFilestore(blobs);
|
|
960
|
+
this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
|
|
961
|
+
...proposalInfo,
|
|
962
|
+
numBlobs: blobs.length,
|
|
963
|
+
});
|
|
964
|
+
} catch (err) {
|
|
965
|
+
this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Derives proposed checkpoint data from validated blocks and sets it on the archiver.
|
|
971
|
+
* Used after successful validation of a foreign proposal.
|
|
972
|
+
* Does not retry since we already waited for the block during validation.
|
|
973
|
+
*/
|
|
974
|
+
private async setProposedCheckpointFromValidation(proposal: CheckpointProposalCore): Promise<boolean> {
|
|
975
|
+
if (!this.archiver) {
|
|
976
|
+
return false;
|
|
977
|
+
}
|
|
978
|
+
const blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
|
|
979
|
+
if (!blockData) {
|
|
980
|
+
this.log.debug(`Block data not found for checkpoint proposal archive, cannot set proposed checkpoint`, {
|
|
981
|
+
archive: proposal.archive.toString(),
|
|
982
|
+
});
|
|
983
|
+
return false;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
await this.archiver.setProposedCheckpoint({
|
|
987
|
+
header: proposal.checkpointHeader,
|
|
988
|
+
checkpointNumber: blockData.checkpointNumber,
|
|
989
|
+
startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
|
|
990
|
+
blockCount: blockData.indexWithinCheckpoint + 1,
|
|
991
|
+
totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
|
|
992
|
+
feeAssetPriceModifier: proposal.feeAssetPriceModifier,
|
|
993
|
+
});
|
|
994
|
+
return true;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
/**
|
|
998
|
+
* Sets proposed checkpoint from blocks for own proposals (skips full validation).
|
|
999
|
+
* Retries fetching block data since the checkpoint proposal often arrives before the last block
|
|
1000
|
+
* finishes re-execution.
|
|
1001
|
+
*/
|
|
1002
|
+
private async setProposedCheckpointFromBlocks(proposal: CheckpointProposalCore): Promise<boolean> {
|
|
1003
|
+
if (!this.archiver) {
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
let blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
|
|
1007
|
+
|
|
1008
|
+
if (!blockData) {
|
|
1009
|
+
// The checkpoint proposal often arrives before the last block finishes re-execution.
|
|
1010
|
+
// Retry until we find the data or give up at the end of the slot.
|
|
1011
|
+
const nextSlot = this.epochCache.getSlotNow() + 1;
|
|
1012
|
+
const timeOfNextSlot = getTimestampForSlot(SlotNumber(nextSlot), await this.archiver.getL1Constants());
|
|
1013
|
+
const timeoutSeconds = Math.max(1, Number(timeOfNextSlot) - Math.floor(this.dateProvider.now() / 1000));
|
|
1014
|
+
|
|
1015
|
+
blockData = await retryUntil(
|
|
1016
|
+
() => this.blockSource.getBlockDataByArchive(proposal.archive),
|
|
1017
|
+
'block data for own checkpoint proposal',
|
|
1018
|
+
timeoutSeconds,
|
|
1019
|
+
0.25,
|
|
1020
|
+
).catch(() => undefined);
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
if (blockData) {
|
|
1024
|
+
await this.archiver.setProposedCheckpoint({
|
|
1025
|
+
header: proposal.checkpointHeader,
|
|
1026
|
+
checkpointNumber: blockData.checkpointNumber,
|
|
1027
|
+
startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
|
|
1028
|
+
blockCount: blockData.indexWithinCheckpoint + 1,
|
|
1029
|
+
totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
|
|
1030
|
+
feeAssetPriceModifier: proposal.feeAssetPriceModifier,
|
|
1031
|
+
});
|
|
1032
|
+
return true;
|
|
1033
|
+
} else {
|
|
1034
|
+
this.log.debug(`Block data not found for own checkpoint proposal archive, cannot set proposed checkpoint`, {
|
|
1035
|
+
archive: proposal.archive.toString(),
|
|
1036
|
+
});
|
|
1037
|
+
return false;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
624
1040
|
}
|