@aztec/validator-client 0.0.1-commit.e0f15ab9b → 0.0.1-commit.e304674f1
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 +0 -2
- 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 +5 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +3 -3
- 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 +2 -2
- package/dest/metrics.d.ts.map +1 -1
- 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} +368 -16
- package/dest/validator.d.ts +8 -13
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +20 -197
- package/package.json +19 -19
- package/src/config.ts +0 -5
- package/src/duties/validation_service.ts +7 -14
- package/src/factory.ts +5 -3
- package/src/index.ts +1 -1
- package/src/metrics.ts +1 -1
- package/src/{block_proposal_handler.ts → proposal_handler.ts} +418 -17
- package/src/validator.ts +23 -212
- 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,47 @@ 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 proposalInfo: LogData = {
|
|
179
|
+
slot: proposal.slotNumber,
|
|
180
|
+
archive: proposal.archive.toString(),
|
|
181
|
+
proposer: proposal.getSender()?.toString(),
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// For own proposals, skip validation — the proposer already built and validated the checkpoint
|
|
185
|
+
const proposer = proposal.getSender();
|
|
186
|
+
const ownAddresses = this.getOwnValidatorAddresses?.();
|
|
187
|
+
const isOwnProposal = proposer && ownAddresses?.some(addr => addr === proposer.toString());
|
|
188
|
+
|
|
189
|
+
if (isOwnProposal) {
|
|
190
|
+
this.log.debug(`Skipping validation for own checkpoint proposal at slot ${proposal.slotNumber}`);
|
|
191
|
+
if (this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
|
|
192
|
+
await this.setProposedCheckpointFromBlocks(proposal);
|
|
193
|
+
}
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const result = await this.handleCheckpointProposal(proposal, proposalInfo);
|
|
198
|
+
if (result.isValid && this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
|
|
199
|
+
await this.setProposedCheckpointFromValidation(proposal);
|
|
200
|
+
}
|
|
201
|
+
} catch (err) {
|
|
202
|
+
this.log.warn(`Error handling checkpoint proposal for slot ${proposal.slotNumber}`, { err });
|
|
203
|
+
}
|
|
204
|
+
return undefined;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
p2pClient.registerAllNodesCheckpointProposalHandler(checkpointHandler);
|
|
208
|
+
|
|
127
209
|
return this;
|
|
128
210
|
}
|
|
129
211
|
|
|
@@ -163,7 +245,7 @@ export class BlockProposalHandler {
|
|
|
163
245
|
}
|
|
164
246
|
|
|
165
247
|
// Ensure the block source is synced before checking for existing blocks,
|
|
166
|
-
// since a
|
|
248
|
+
// since a proposed checkpoint prune may remove blocks we'd otherwise find.
|
|
167
249
|
// This affects mostly the block_number_already_exists check, since a pending
|
|
168
250
|
// checkpoint prune could remove a block that would conflict with this proposal.
|
|
169
251
|
// When pipelining is enabled, the proposer builds ahead of L1 submission, so the
|
|
@@ -279,7 +361,7 @@ export class BlockProposalHandler {
|
|
|
279
361
|
|
|
280
362
|
// If we succeeded, push this block into the archiver (unless disabled)
|
|
281
363
|
if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
|
|
282
|
-
await this.blockSource.addBlock(reexecutionResult
|
|
364
|
+
await this.blockSource.addBlock(reexecutionResult.block);
|
|
283
365
|
}
|
|
284
366
|
|
|
285
367
|
this.log.info(
|
|
@@ -292,7 +374,6 @@ export class BlockProposalHandler {
|
|
|
292
374
|
|
|
293
375
|
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
|
|
294
376
|
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
295
|
-
const slot = proposal.slotNumber;
|
|
296
377
|
const config = this.checkpointsBuilder.getConfig();
|
|
297
378
|
const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
|
|
298
379
|
|
|
@@ -300,7 +381,7 @@ export class BlockProposalHandler {
|
|
|
300
381
|
return 'genesis';
|
|
301
382
|
}
|
|
302
383
|
|
|
303
|
-
const deadline = this.getReexecutionDeadline(
|
|
384
|
+
const deadline = this.getReexecutionDeadline(proposal.slotNumber, config);
|
|
304
385
|
const currentTime = this.dateProvider.now();
|
|
305
386
|
const timeoutDurationMs = deadline.getTime() - currentTime;
|
|
306
387
|
|
|
@@ -449,8 +530,14 @@ export class BlockProposalHandler {
|
|
|
449
530
|
return undefined;
|
|
450
531
|
}
|
|
451
532
|
|
|
452
|
-
private getReexecutionDeadline(
|
|
453
|
-
|
|
533
|
+
private getReexecutionDeadline(
|
|
534
|
+
slotNumber: SlotNumber,
|
|
535
|
+
config: { l1GenesisTime: bigint; slotDuration: number },
|
|
536
|
+
): Date {
|
|
537
|
+
// Under proposer pipelining, the proposal slot may be ahead of wall clock time.
|
|
538
|
+
// Reexecution budgets should still be bounded by the current slot we are in now.
|
|
539
|
+
const wallclockSlot = slotNumber - this.epochCache.pipeliningOffset();
|
|
540
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(wallclockSlot + 1), config));
|
|
454
541
|
return new Date(nextSlotTimestampSeconds * 1000);
|
|
455
542
|
}
|
|
456
543
|
|
|
@@ -463,8 +550,9 @@ export class BlockProposalHandler {
|
|
|
463
550
|
}
|
|
464
551
|
|
|
465
552
|
// Make a quick check before triggering an archiver sync
|
|
553
|
+
// If we are pipelining and have a pending checkpoint number stored, we will allow the block proposal to be for a slot further
|
|
466
554
|
const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
|
|
467
|
-
if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
|
|
555
|
+
if (syncedSlot !== undefined && syncedSlot + 1 + this.epochCache.pipeliningOffset() >= slot) {
|
|
468
556
|
return true;
|
|
469
557
|
}
|
|
470
558
|
|
|
@@ -473,8 +561,8 @@ export class BlockProposalHandler {
|
|
|
473
561
|
return await retryUntil(
|
|
474
562
|
async () => {
|
|
475
563
|
await this.blockSource.syncImmediate();
|
|
476
|
-
const
|
|
477
|
-
return
|
|
564
|
+
const updatedSyncedSlot = await this.blockSource.getSyncedL2SlotNumber();
|
|
565
|
+
return updatedSyncedSlot !== undefined && updatedSyncedSlot + 1 >= slot;
|
|
478
566
|
},
|
|
479
567
|
'wait for block source sync',
|
|
480
568
|
timeoutMs / 1000,
|
|
@@ -629,4 +717,317 @@ export class BlockProposalHandler {
|
|
|
629
717
|
totalManaUsed,
|
|
630
718
|
};
|
|
631
719
|
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* Validates a checkpoint proposal, caches the result, and uploads blobs if configured.
|
|
723
|
+
* Returns a cached result if the same proposal (archive + slot) was already validated.
|
|
724
|
+
* Used by both the all-nodes callback (via register) and the validator client (via delegation).
|
|
725
|
+
*/
|
|
726
|
+
async handleCheckpointProposal(
|
|
727
|
+
proposal: CheckpointProposalCore,
|
|
728
|
+
proposalInfo: LogData,
|
|
729
|
+
): Promise<CheckpointProposalValidationResult> {
|
|
730
|
+
const slot = proposal.slotNumber;
|
|
731
|
+
|
|
732
|
+
// Check cache: same archive+slot means we already validated this proposal
|
|
733
|
+
if (
|
|
734
|
+
this.lastCheckpointValidationResult &&
|
|
735
|
+
this.lastCheckpointValidationResult.archive.equals(proposal.archive) &&
|
|
736
|
+
this.lastCheckpointValidationResult.slotNumber === slot
|
|
737
|
+
) {
|
|
738
|
+
this.log.debug(`Returning cached validation result for checkpoint proposal at slot ${slot}`, proposalInfo);
|
|
739
|
+
return this.lastCheckpointValidationResult.result;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
const proposer = proposal.getSender();
|
|
743
|
+
if (!proposer) {
|
|
744
|
+
this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
|
|
745
|
+
const result: CheckpointProposalValidationResult = { isValid: false, reason: 'invalid_signature' };
|
|
746
|
+
this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
|
|
747
|
+
return result;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
|
|
751
|
+
this.log.warn(
|
|
752
|
+
`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`,
|
|
753
|
+
);
|
|
754
|
+
const result: CheckpointProposalValidationResult = { isValid: false, reason: 'invalid_fee_asset_price_modifier' };
|
|
755
|
+
this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
|
|
756
|
+
return result;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const result = await this.validateCheckpointProposal(proposal, proposalInfo);
|
|
760
|
+
this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
|
|
761
|
+
|
|
762
|
+
// Upload blobs to filestore if validation passed (fire and forget)
|
|
763
|
+
if (result.isValid) {
|
|
764
|
+
this.tryUploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
return result;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
|
|
772
|
+
* @returns Validation result with isValid flag and reason if invalid.
|
|
773
|
+
*/
|
|
774
|
+
async validateCheckpointProposal(
|
|
775
|
+
proposal: CheckpointProposalCore,
|
|
776
|
+
proposalInfo: LogData,
|
|
777
|
+
): Promise<CheckpointProposalValidationResult> {
|
|
778
|
+
const slot = proposal.slotNumber;
|
|
779
|
+
|
|
780
|
+
// Timeout block syncing at the start of the next slot
|
|
781
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
782
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
783
|
+
const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
|
|
784
|
+
|
|
785
|
+
// Wait for last block to sync by archive
|
|
786
|
+
let lastBlockHeader;
|
|
787
|
+
try {
|
|
788
|
+
lastBlockHeader = await retryUntil(
|
|
789
|
+
async () => {
|
|
790
|
+
await this.blockSource.syncImmediate();
|
|
791
|
+
return this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
792
|
+
},
|
|
793
|
+
`waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
|
|
794
|
+
timeoutSeconds,
|
|
795
|
+
0.5,
|
|
796
|
+
);
|
|
797
|
+
} catch (err) {
|
|
798
|
+
if (err instanceof TimeoutError) {
|
|
799
|
+
this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
|
|
800
|
+
return { isValid: false, reason: 'last_block_not_found' };
|
|
801
|
+
}
|
|
802
|
+
this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
|
|
803
|
+
return { isValid: false, reason: 'block_fetch_error' };
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
if (!lastBlockHeader) {
|
|
807
|
+
this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
|
|
808
|
+
return { isValid: false, reason: 'last_block_not_found' };
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// Get all full blocks for the slot and checkpoint
|
|
812
|
+
const blocks = await this.blockSource.getBlocksForSlot(slot);
|
|
813
|
+
if (blocks.length === 0) {
|
|
814
|
+
this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
|
|
815
|
+
return { isValid: false, reason: 'no_blocks_for_slot' };
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// Ensure the last block for this slot matches the archive in the checkpoint proposal
|
|
819
|
+
if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
|
|
820
|
+
this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
|
|
821
|
+
return { isValid: false, reason: 'last_block_archive_mismatch' };
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
|
|
825
|
+
...proposalInfo,
|
|
826
|
+
blockNumbers: blocks.map(b => b.number),
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
// Get checkpoint constants from first block
|
|
830
|
+
const firstBlock = blocks[0];
|
|
831
|
+
const constants = this.extractCheckpointConstants(firstBlock);
|
|
832
|
+
const checkpointNumber = firstBlock.checkpointNumber;
|
|
833
|
+
|
|
834
|
+
// Get L1-to-L2 messages for this checkpoint
|
|
835
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
836
|
+
|
|
837
|
+
// Collect the out hashes of all the checkpoints before this one in the same epoch
|
|
838
|
+
const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
|
|
839
|
+
const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
|
|
840
|
+
.filter(c => c.checkpointNumber < checkpointNumber)
|
|
841
|
+
.map(c => c.checkpointOutHash);
|
|
842
|
+
|
|
843
|
+
// Fork world state at the block before the first block
|
|
844
|
+
const parentBlockNumber = BlockNumber(firstBlock.number - 1);
|
|
845
|
+
await using fork = await this.checkpointsBuilder.getFork(parentBlockNumber);
|
|
846
|
+
|
|
847
|
+
// Create checkpoint builder with all existing blocks
|
|
848
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
849
|
+
checkpointNumber,
|
|
850
|
+
constants,
|
|
851
|
+
proposal.feeAssetPriceModifier,
|
|
852
|
+
l1ToL2Messages,
|
|
853
|
+
previousCheckpointOutHashes,
|
|
854
|
+
fork,
|
|
855
|
+
blocks,
|
|
856
|
+
this.log.getBindings(),
|
|
857
|
+
);
|
|
858
|
+
|
|
859
|
+
// Complete the checkpoint to get computed values
|
|
860
|
+
const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
|
|
861
|
+
|
|
862
|
+
// Compare checkpoint header with proposal
|
|
863
|
+
if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
|
|
864
|
+
this.log.warn(`Checkpoint header mismatch`, {
|
|
865
|
+
...proposalInfo,
|
|
866
|
+
computed: computedCheckpoint.header.toInspect(),
|
|
867
|
+
proposal: proposal.checkpointHeader.toInspect(),
|
|
868
|
+
});
|
|
869
|
+
return { isValid: false, reason: 'checkpoint_header_mismatch' };
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
// Compare archive root with proposal
|
|
873
|
+
if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
|
|
874
|
+
this.log.warn(`Archive root mismatch`, {
|
|
875
|
+
...proposalInfo,
|
|
876
|
+
computed: computedCheckpoint.archive.root.toString(),
|
|
877
|
+
proposal: proposal.archive.toString(),
|
|
878
|
+
});
|
|
879
|
+
return { isValid: false, reason: 'archive_mismatch' };
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// Check that the accumulated epoch out hash matches the value in the proposal.
|
|
883
|
+
// The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
|
|
884
|
+
const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
|
|
885
|
+
const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
|
|
886
|
+
const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
|
|
887
|
+
if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
|
|
888
|
+
this.log.warn(`Epoch out hash mismatch`, {
|
|
889
|
+
proposalEpochOutHash: proposalEpochOutHash.toString(),
|
|
890
|
+
computedEpochOutHash: computedEpochOutHash.toString(),
|
|
891
|
+
checkpointOutHash: checkpointOutHash.toString(),
|
|
892
|
+
previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
|
|
893
|
+
...proposalInfo,
|
|
894
|
+
});
|
|
895
|
+
return { isValid: false, reason: 'out_hash_mismatch' };
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// Final round of validations on the checkpoint, just in case.
|
|
899
|
+
try {
|
|
900
|
+
validateCheckpoint(computedCheckpoint, {
|
|
901
|
+
rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
|
|
902
|
+
maxDABlockGas: this.config.validateMaxDABlockGas,
|
|
903
|
+
maxL2BlockGas: this.config.validateMaxL2BlockGas,
|
|
904
|
+
maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
|
|
905
|
+
maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
|
|
906
|
+
});
|
|
907
|
+
} catch (err) {
|
|
908
|
+
this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
|
|
909
|
+
return { isValid: false, reason: 'checkpoint_validation_failed' };
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
|
|
913
|
+
return { isValid: true };
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/** Extracts checkpoint global variables from a block. */
|
|
917
|
+
private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
|
|
918
|
+
const gv = block.header.globalVariables;
|
|
919
|
+
return {
|
|
920
|
+
chainId: gv.chainId,
|
|
921
|
+
version: gv.version,
|
|
922
|
+
slotNumber: gv.slotNumber,
|
|
923
|
+
timestamp: gv.timestamp,
|
|
924
|
+
coinbase: gv.coinbase,
|
|
925
|
+
feeRecipient: gv.feeRecipient,
|
|
926
|
+
gasFees: gv.gasFees,
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/** Triggers blob upload for a checkpoint if the blob client can upload (fire and forget). */
|
|
931
|
+
protected tryUploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): void {
|
|
932
|
+
if (this.blobClient.canUpload()) {
|
|
933
|
+
void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/** Uploads blobs for a checkpoint to the filestore. */
|
|
938
|
+
protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
|
|
939
|
+
try {
|
|
940
|
+
const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
941
|
+
if (!lastBlockHeader) {
|
|
942
|
+
this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
|
|
943
|
+
return;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
|
|
947
|
+
if (blocks.length === 0) {
|
|
948
|
+
this.log.warn(`No blocks found for blob upload`, proposalInfo);
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
const blockBlobData = blocks.map(b => b.toBlockBlobData());
|
|
953
|
+
const blobFields = encodeCheckpointBlobDataFromBlocks(blockBlobData);
|
|
954
|
+
const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
|
|
955
|
+
await this.blobClient.sendBlobsToFilestore(blobs);
|
|
956
|
+
this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
|
|
957
|
+
...proposalInfo,
|
|
958
|
+
numBlobs: blobs.length,
|
|
959
|
+
});
|
|
960
|
+
} catch (err) {
|
|
961
|
+
this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Derives proposed checkpoint data from validated blocks and sets it on the archiver.
|
|
967
|
+
* Used after successful validation of a foreign proposal.
|
|
968
|
+
* Does not retry since we already waited for the block during validation.
|
|
969
|
+
*/
|
|
970
|
+
private async setProposedCheckpointFromValidation(proposal: CheckpointProposalCore): Promise<void> {
|
|
971
|
+
if (!this.archiver) {
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
const blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
|
|
975
|
+
if (!blockData) {
|
|
976
|
+
this.log.debug(`Block data not found for checkpoint proposal archive, cannot set proposed checkpoint`, {
|
|
977
|
+
archive: proposal.archive.toString(),
|
|
978
|
+
});
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
await this.archiver.setProposedCheckpoint({
|
|
983
|
+
header: proposal.checkpointHeader,
|
|
984
|
+
checkpointNumber: blockData.checkpointNumber,
|
|
985
|
+
startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
|
|
986
|
+
blockCount: blockData.indexWithinCheckpoint + 1,
|
|
987
|
+
totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
|
|
988
|
+
feeAssetPriceModifier: proposal.feeAssetPriceModifier,
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* Sets proposed checkpoint from blocks for own proposals (skips full validation).
|
|
994
|
+
* Retries fetching block data since the checkpoint proposal often arrives before the last block
|
|
995
|
+
* finishes re-execution.
|
|
996
|
+
*/
|
|
997
|
+
private async setProposedCheckpointFromBlocks(proposal: CheckpointProposalCore): Promise<void> {
|
|
998
|
+
if (!this.archiver) {
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
let blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
|
|
1002
|
+
|
|
1003
|
+
if (!blockData) {
|
|
1004
|
+
// The checkpoint proposal often arrives before the last block finishes re-execution.
|
|
1005
|
+
// Retry until we find the data or give up at the end of the slot.
|
|
1006
|
+
const nextSlot = this.epochCache.getSlotNow() + 1;
|
|
1007
|
+
const timeOfNextSlot = getTimestampForSlot(SlotNumber(nextSlot), await this.archiver.getL1Constants());
|
|
1008
|
+
const timeoutSeconds = Math.max(1, Number(timeOfNextSlot) - Math.floor(this.dateProvider.now() / 1000));
|
|
1009
|
+
|
|
1010
|
+
blockData = await retryUntil(
|
|
1011
|
+
() => this.blockSource.getBlockDataByArchive(proposal.archive),
|
|
1012
|
+
'block data for own checkpoint proposal',
|
|
1013
|
+
timeoutSeconds,
|
|
1014
|
+
0.25,
|
|
1015
|
+
).catch(() => undefined);
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
if (blockData) {
|
|
1019
|
+
await this.archiver.setProposedCheckpoint({
|
|
1020
|
+
header: proposal.checkpointHeader,
|
|
1021
|
+
checkpointNumber: blockData.checkpointNumber,
|
|
1022
|
+
startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
|
|
1023
|
+
blockCount: blockData.indexWithinCheckpoint + 1,
|
|
1024
|
+
totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
|
|
1025
|
+
feeAssetPriceModifier: proposal.feeAssetPriceModifier,
|
|
1026
|
+
});
|
|
1027
|
+
} else {
|
|
1028
|
+
this.log.debug(`Block data not found for own checkpoint proposal archive, cannot set proposed checkpoint`, {
|
|
1029
|
+
archive: proposal.archive.toString(),
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
632
1033
|
}
|