@aztec/validator-client 0.0.1-commit.9593d84 → 0.0.1-commit.96bb3f7
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 +256 -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 +336 -75
- package/dest/checkpoint_builder.d.ts +70 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +155 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +10 -0
- package/dest/duties/validation_service.d.ts +27 -11
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +53 -23
- package/dest/factory.d.ts +12 -7
- 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/local_key_store.js +1 -1
- package/dest/key_store/web3signer_key_store.js +1 -1
- package/dest/metrics.d.ts +1 -1
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +8 -33
- 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 +18 -0
- package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
- package/dest/tx_validator/tx_validator_factory.js +53 -0
- package/dest/validator.d.ts +42 -14
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +304 -47
- package/package.json +18 -12
- package/src/block_proposal_handler.ts +258 -43
- package/src/checkpoint_builder.ts +267 -0
- package/src/config.ts +10 -0
- package/src/duties/validation_service.ts +81 -27
- package/src/factory.ts +16 -8
- package/src/index.ts +2 -0
- package/src/key_store/local_key_store.ts +1 -1
- package/src/key_store/node_keystore_adapter.ts +1 -1
- package/src/key_store/web3signer_key_store.ts +1 -1
- package/src/metrics.ts +7 -34
- 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 +133 -0
- package/src/validator.ts +416 -71
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
-
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
4
|
import { TimeoutError } from '@aztec/foundation/error';
|
|
4
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
5
5
|
import { createLogger } from '@aztec/foundation/log';
|
|
6
6
|
import { retryUntil } from '@aztec/foundation/retry';
|
|
7
7
|
import { DateProvider, Timer } from '@aztec/foundation/timer';
|
|
8
8
|
import type { P2P, PeerId } from '@aztec/p2p';
|
|
9
9
|
import { TxProvider } from '@aztec/p2p';
|
|
10
10
|
import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
|
|
11
|
-
import type {
|
|
11
|
+
import type { L2BlockNew, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
12
12
|
import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
13
|
-
import type {
|
|
13
|
+
import type { ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
14
14
|
import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
15
|
-
import {
|
|
16
|
-
import { BlockHeader, type
|
|
15
|
+
import type { BlockProposal } from '@aztec/stdlib/p2p';
|
|
16
|
+
import { BlockHeader, type CheckpointGlobalVariables, type FailedTx, type Tx } from '@aztec/stdlib/tx';
|
|
17
17
|
import {
|
|
18
18
|
ReExFailedTxsError,
|
|
19
19
|
ReExStateMismatchError,
|
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
} from '@aztec/stdlib/validators';
|
|
23
23
|
import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
24
24
|
|
|
25
|
+
import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
25
26
|
import type { ValidatorMetrics } from './metrics.js';
|
|
26
27
|
|
|
27
28
|
export type BlockProposalValidationFailureReason =
|
|
@@ -29,6 +30,7 @@ export type BlockProposalValidationFailureReason =
|
|
|
29
30
|
| 'parent_block_not_found'
|
|
30
31
|
| 'parent_block_wrong_slot'
|
|
31
32
|
| 'in_hash_mismatch'
|
|
33
|
+
| 'global_variables_mismatch'
|
|
32
34
|
| 'block_number_already_exists'
|
|
33
35
|
| 'txs_not_available'
|
|
34
36
|
| 'state_mismatch'
|
|
@@ -37,7 +39,7 @@ export type BlockProposalValidationFailureReason =
|
|
|
37
39
|
| 'unknown_error';
|
|
38
40
|
|
|
39
41
|
type ReexecuteTransactionsResult = {
|
|
40
|
-
block:
|
|
42
|
+
block: L2BlockNew;
|
|
41
43
|
failedTxs: FailedTx[];
|
|
42
44
|
reexecutionTimeMs: number;
|
|
43
45
|
totalManaUsed: number;
|
|
@@ -45,25 +47,30 @@ type ReexecuteTransactionsResult = {
|
|
|
45
47
|
|
|
46
48
|
export type BlockProposalValidationSuccessResult = {
|
|
47
49
|
isValid: true;
|
|
48
|
-
blockNumber:
|
|
50
|
+
blockNumber: BlockNumber;
|
|
49
51
|
reexecutionResult?: ReexecuteTransactionsResult;
|
|
50
52
|
};
|
|
51
53
|
|
|
52
54
|
export type BlockProposalValidationFailureResult = {
|
|
53
55
|
isValid: false;
|
|
54
56
|
reason: BlockProposalValidationFailureReason;
|
|
55
|
-
blockNumber?:
|
|
57
|
+
blockNumber?: BlockNumber;
|
|
56
58
|
reexecutionResult?: ReexecuteTransactionsResult;
|
|
57
59
|
};
|
|
58
60
|
|
|
59
61
|
export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
|
|
60
62
|
|
|
63
|
+
type CheckpointComputationResult =
|
|
64
|
+
| { checkpointNumber: CheckpointNumber; reason?: undefined }
|
|
65
|
+
| { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
|
|
66
|
+
|
|
61
67
|
export class BlockProposalHandler {
|
|
62
68
|
public readonly tracer: Tracer;
|
|
63
69
|
|
|
64
70
|
constructor(
|
|
65
|
-
private
|
|
66
|
-
private
|
|
71
|
+
private checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
72
|
+
private worldState: WorldStateSynchronizer,
|
|
73
|
+
private blockSource: L2BlockSource & L2BlockSink,
|
|
67
74
|
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
68
75
|
private txProvider: TxProvider,
|
|
69
76
|
private blockProposalValidator: BlockProposalValidator,
|
|
@@ -80,7 +87,9 @@ export class BlockProposalHandler {
|
|
|
80
87
|
}
|
|
81
88
|
|
|
82
89
|
registerForReexecution(p2pClient: P2P): BlockProposalHandler {
|
|
83
|
-
|
|
90
|
+
// Non-validator handler that re-executes for monitoring but does not attest.
|
|
91
|
+
// Returns boolean indicating whether the proposal was valid.
|
|
92
|
+
const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
|
|
84
93
|
try {
|
|
85
94
|
const result = await this.handleBlockProposal(proposal, proposalSender, true);
|
|
86
95
|
if (result.isValid) {
|
|
@@ -90,16 +99,18 @@ export class BlockProposalHandler {
|
|
|
90
99
|
totalManaUsed: result.reexecutionResult?.totalManaUsed,
|
|
91
100
|
numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
|
|
92
101
|
});
|
|
102
|
+
return true;
|
|
93
103
|
} else {
|
|
94
104
|
this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
|
|
95
105
|
blockNumber: result.blockNumber,
|
|
96
106
|
reason: result.reason,
|
|
97
107
|
});
|
|
108
|
+
return false;
|
|
98
109
|
}
|
|
99
110
|
} catch (error) {
|
|
100
111
|
this.log.error('Error processing block proposal in non-validator handler', error);
|
|
112
|
+
return false;
|
|
101
113
|
}
|
|
102
|
-
return undefined; // Non-validator nodes don't return attestations
|
|
103
114
|
};
|
|
104
115
|
|
|
105
116
|
p2pClient.registerBlockProposalHandler(handler);
|
|
@@ -113,7 +124,7 @@ export class BlockProposalHandler {
|
|
|
113
124
|
): Promise<BlockProposalValidationResult> {
|
|
114
125
|
const slotNumber = proposal.slotNumber;
|
|
115
126
|
const proposer = proposal.getSender();
|
|
116
|
-
const config = this.
|
|
127
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
117
128
|
|
|
118
129
|
// Reject proposals with invalid signatures
|
|
119
130
|
if (!proposer) {
|
|
@@ -153,7 +164,10 @@ export class BlockProposalHandler {
|
|
|
153
164
|
}
|
|
154
165
|
|
|
155
166
|
// Compute the block number based on the parent block
|
|
156
|
-
const blockNumber =
|
|
167
|
+
const blockNumber =
|
|
168
|
+
parentBlockHeader === 'genesis'
|
|
169
|
+
? BlockNumber(INITIAL_L2_BLOCK_NUM)
|
|
170
|
+
: BlockNumber(parentBlockHeader.getBlockNumber() + 1);
|
|
157
171
|
|
|
158
172
|
// Check that this block number does not exist already
|
|
159
173
|
const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
|
|
@@ -169,10 +183,17 @@ export class BlockProposalHandler {
|
|
|
169
183
|
deadline: this.getReexecutionDeadline(slotNumber, config),
|
|
170
184
|
});
|
|
171
185
|
|
|
186
|
+
// Compute the checkpoint number for this block and validate checkpoint consistency
|
|
187
|
+
const checkpointResult = await this.computeCheckpointNumber(proposal, parentBlockHeader, proposalInfo);
|
|
188
|
+
if (checkpointResult.reason) {
|
|
189
|
+
return { isValid: false, blockNumber, reason: checkpointResult.reason };
|
|
190
|
+
}
|
|
191
|
+
const checkpointNumber = checkpointResult.checkpointNumber;
|
|
192
|
+
|
|
172
193
|
// Check that I have the same set of l1ToL2Messages as the proposal
|
|
173
|
-
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(
|
|
194
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
174
195
|
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
175
|
-
const proposalInHash = proposal.
|
|
196
|
+
const proposalInHash = proposal.inHash;
|
|
176
197
|
if (!computedInHash.equals(proposalInHash)) {
|
|
177
198
|
this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
|
|
178
199
|
proposalInHash: proposalInHash.toString(),
|
|
@@ -193,7 +214,13 @@ export class BlockProposalHandler {
|
|
|
193
214
|
if (shouldReexecute) {
|
|
194
215
|
try {
|
|
195
216
|
this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
|
|
196
|
-
reexecutionResult = await this.reexecuteTransactions(
|
|
217
|
+
reexecutionResult = await this.reexecuteTransactions(
|
|
218
|
+
proposal,
|
|
219
|
+
blockNumber,
|
|
220
|
+
checkpointNumber,
|
|
221
|
+
txs,
|
|
222
|
+
l1ToL2Messages,
|
|
223
|
+
);
|
|
197
224
|
} catch (error) {
|
|
198
225
|
this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
|
|
199
226
|
const reason = this.getReexecuteFailureReason(error);
|
|
@@ -201,14 +228,24 @@ export class BlockProposalHandler {
|
|
|
201
228
|
}
|
|
202
229
|
}
|
|
203
230
|
|
|
204
|
-
this
|
|
231
|
+
// If we succeeded, push this block into the archiver (unless disabled)
|
|
232
|
+
// TODO(palla/mbps): Change default to false once block sync is stable.
|
|
233
|
+
if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
|
|
234
|
+
await this.blockSource.addBlock(reexecutionResult?.block);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
this.log.info(
|
|
238
|
+
`Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
|
|
239
|
+
proposalInfo,
|
|
240
|
+
);
|
|
241
|
+
|
|
205
242
|
return { isValid: true, blockNumber, reexecutionResult };
|
|
206
243
|
}
|
|
207
244
|
|
|
208
245
|
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockHeader | undefined> {
|
|
209
|
-
const parentArchive = proposal.
|
|
246
|
+
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
210
247
|
const slot = proposal.slotNumber;
|
|
211
|
-
const config = this.
|
|
248
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
212
249
|
const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
|
|
213
250
|
|
|
214
251
|
if (parentArchive.equals(genesisArchiveRoot)) {
|
|
@@ -242,12 +279,168 @@ export class BlockProposalHandler {
|
|
|
242
279
|
}
|
|
243
280
|
}
|
|
244
281
|
|
|
282
|
+
private async computeCheckpointNumber(
|
|
283
|
+
proposal: BlockProposal,
|
|
284
|
+
parentBlockHeader: 'genesis' | BlockHeader,
|
|
285
|
+
proposalInfo: object,
|
|
286
|
+
): Promise<CheckpointComputationResult> {
|
|
287
|
+
if (parentBlockHeader === 'genesis') {
|
|
288
|
+
// First block is in checkpoint 1
|
|
289
|
+
if (proposal.indexWithinCheckpoint !== 0) {
|
|
290
|
+
this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
|
|
291
|
+
return { reason: 'invalid_proposal' };
|
|
292
|
+
}
|
|
293
|
+
return { checkpointNumber: CheckpointNumber.INITIAL };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Get the parent block to find its checkpoint number
|
|
297
|
+
// TODO(palla/mbps): The block header should include the checkpoint number to avoid this lookup,
|
|
298
|
+
// or at least the L2BlockSource should return a different struct that includes it.
|
|
299
|
+
const parentBlockNumber = parentBlockHeader.getBlockNumber();
|
|
300
|
+
const parentBlock = await this.blockSource.getL2BlockNew(parentBlockNumber);
|
|
301
|
+
if (!parentBlock) {
|
|
302
|
+
this.log.warn(`Parent block ${parentBlockNumber} not found in archiver`, proposalInfo);
|
|
303
|
+
return { reason: 'invalid_proposal' };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (proposal.indexWithinCheckpoint === 0) {
|
|
307
|
+
// If this is the first block in a new checkpoint, increment the checkpoint number
|
|
308
|
+
if (!(proposal.blockHeader.getSlot() > parentBlockHeader.getSlot())) {
|
|
309
|
+
this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
|
|
310
|
+
return { reason: 'invalid_proposal' };
|
|
311
|
+
}
|
|
312
|
+
return { checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1) };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Otherwise it should follow the previous block in the same checkpoint
|
|
316
|
+
if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
|
|
317
|
+
this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
|
|
318
|
+
return { reason: 'invalid_proposal' };
|
|
319
|
+
}
|
|
320
|
+
if (proposal.blockHeader.getSlot() !== parentBlockHeader.getSlot()) {
|
|
321
|
+
this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
|
|
322
|
+
return { reason: 'invalid_proposal' };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
|
|
326
|
+
const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
|
|
327
|
+
if (validationResult) {
|
|
328
|
+
return validationResult;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return { checkpointNumber: parentBlock.checkpointNumber };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Validates that a non-first block in a checkpoint has consistent global variables with its parent.
|
|
336
|
+
* For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
|
|
337
|
+
* @returns A failure result if validation fails, undefined if validation passes
|
|
338
|
+
*/
|
|
339
|
+
private validateNonFirstBlockInCheckpoint(
|
|
340
|
+
proposal: BlockProposal,
|
|
341
|
+
parentBlock: L2BlockNew,
|
|
342
|
+
proposalInfo: object,
|
|
343
|
+
): CheckpointComputationResult | undefined {
|
|
344
|
+
const proposalGlobals = proposal.blockHeader.globalVariables;
|
|
345
|
+
const parentGlobals = parentBlock.header.globalVariables;
|
|
346
|
+
|
|
347
|
+
// All global variables except blockNumber should match the parent
|
|
348
|
+
// blockNumber naturally increments between blocks
|
|
349
|
+
if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
|
|
350
|
+
this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
|
|
351
|
+
...proposalInfo,
|
|
352
|
+
proposalChainId: proposalGlobals.chainId.toString(),
|
|
353
|
+
parentChainId: parentGlobals.chainId.toString(),
|
|
354
|
+
});
|
|
355
|
+
return { reason: 'global_variables_mismatch' };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (!proposalGlobals.version.equals(parentGlobals.version)) {
|
|
359
|
+
this.log.warn(`Non-first block in checkpoint has mismatched version`, {
|
|
360
|
+
...proposalInfo,
|
|
361
|
+
proposalVersion: proposalGlobals.version.toString(),
|
|
362
|
+
parentVersion: parentGlobals.version.toString(),
|
|
363
|
+
});
|
|
364
|
+
return { reason: 'global_variables_mismatch' };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
|
|
368
|
+
this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
|
|
369
|
+
...proposalInfo,
|
|
370
|
+
proposalSlotNumber: proposalGlobals.slotNumber,
|
|
371
|
+
parentSlotNumber: parentGlobals.slotNumber,
|
|
372
|
+
});
|
|
373
|
+
return { reason: 'global_variables_mismatch' };
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
|
|
377
|
+
this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
|
|
378
|
+
...proposalInfo,
|
|
379
|
+
proposalTimestamp: proposalGlobals.timestamp.toString(),
|
|
380
|
+
parentTimestamp: parentGlobals.timestamp.toString(),
|
|
381
|
+
});
|
|
382
|
+
return { reason: 'global_variables_mismatch' };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
|
|
386
|
+
this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
|
|
387
|
+
...proposalInfo,
|
|
388
|
+
proposalCoinbase: proposalGlobals.coinbase.toString(),
|
|
389
|
+
parentCoinbase: parentGlobals.coinbase.toString(),
|
|
390
|
+
});
|
|
391
|
+
return { reason: 'global_variables_mismatch' };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
|
|
395
|
+
this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
|
|
396
|
+
...proposalInfo,
|
|
397
|
+
proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
|
|
398
|
+
parentFeeRecipient: parentGlobals.feeRecipient.toString(),
|
|
399
|
+
});
|
|
400
|
+
return { reason: 'global_variables_mismatch' };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
|
|
404
|
+
this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
|
|
405
|
+
...proposalInfo,
|
|
406
|
+
proposalGasFees: proposalGlobals.gasFees.toInspect(),
|
|
407
|
+
parentGasFees: parentGlobals.gasFees.toInspect(),
|
|
408
|
+
});
|
|
409
|
+
return { reason: 'global_variables_mismatch' };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
return undefined;
|
|
413
|
+
}
|
|
414
|
+
|
|
245
415
|
private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
|
|
246
416
|
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
247
417
|
const msNeededForPropagationAndPublishing = this.config.validatorReexecuteDeadlineMs;
|
|
248
418
|
return new Date(nextSlotTimestampSeconds * 1000 - msNeededForPropagationAndPublishing);
|
|
249
419
|
}
|
|
250
420
|
|
|
421
|
+
/**
|
|
422
|
+
* Gets all prior blocks in the same checkpoint (same slot and checkpoint number) up to but not including upToBlockNumber.
|
|
423
|
+
*/
|
|
424
|
+
private async getBlocksInCheckpoint(
|
|
425
|
+
slot: SlotNumber,
|
|
426
|
+
upToBlockNumber: BlockNumber,
|
|
427
|
+
checkpointNumber: CheckpointNumber,
|
|
428
|
+
): Promise<L2BlockNew[]> {
|
|
429
|
+
const blocks: L2BlockNew[] = [];
|
|
430
|
+
let currentBlockNumber = BlockNumber(upToBlockNumber - 1);
|
|
431
|
+
|
|
432
|
+
while (currentBlockNumber >= INITIAL_L2_BLOCK_NUM) {
|
|
433
|
+
const block = await this.blockSource.getL2BlockNew(currentBlockNumber);
|
|
434
|
+
if (!block || block.header.getSlot() !== slot || block.checkpointNumber !== checkpointNumber) {
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
blocks.unshift(block);
|
|
438
|
+
currentBlockNumber = BlockNumber(currentBlockNumber - 1);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return blocks;
|
|
442
|
+
}
|
|
443
|
+
|
|
251
444
|
private getReexecuteFailureReason(err: any) {
|
|
252
445
|
if (err instanceof ReExStateMismatchError) {
|
|
253
446
|
return 'state_mismatch';
|
|
@@ -262,12 +455,12 @@ export class BlockProposalHandler {
|
|
|
262
455
|
|
|
263
456
|
async reexecuteTransactions(
|
|
264
457
|
proposal: BlockProposal,
|
|
265
|
-
blockNumber:
|
|
458
|
+
blockNumber: BlockNumber,
|
|
459
|
+
checkpointNumber: CheckpointNumber,
|
|
266
460
|
txs: Tx[],
|
|
267
461
|
l1ToL2Messages: Fr[],
|
|
268
462
|
): Promise<ReexecuteTransactionsResult> {
|
|
269
|
-
const {
|
|
270
|
-
const { txHashes } = proposal;
|
|
463
|
+
const { blockHeader, txHashes } = proposal;
|
|
271
464
|
|
|
272
465
|
// If we do not have all of the transactions, then we should fail
|
|
273
466
|
if (txs.length !== txHashes.length) {
|
|
@@ -276,28 +469,46 @@ export class BlockProposalHandler {
|
|
|
276
469
|
throw new TransactionsNotAvailableError(missingTxHashes);
|
|
277
470
|
}
|
|
278
471
|
|
|
279
|
-
// Use the sequencer's block building logic to re-execute the transactions
|
|
280
472
|
const timer = new Timer();
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
473
|
+
const slot = proposal.slotNumber;
|
|
474
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
475
|
+
|
|
476
|
+
// Get prior blocks in this checkpoint (same slot and checkpoint number)
|
|
477
|
+
const priorBlocks = await this.getBlocksInCheckpoint(slot, blockNumber, checkpointNumber);
|
|
478
|
+
|
|
479
|
+
// Fork before the block to be built
|
|
480
|
+
const parentBlockNumber = BlockNumber(blockNumber - 1);
|
|
481
|
+
using fork = await this.worldState.fork(parentBlockNumber);
|
|
482
|
+
|
|
483
|
+
// Build checkpoint constants from proposal (excludes blockNumber and timestamp which are per-block)
|
|
484
|
+
const constants: CheckpointGlobalVariables = {
|
|
291
485
|
chainId: new Fr(config.l1ChainId),
|
|
292
486
|
version: new Fr(config.rollupVersion),
|
|
293
|
-
|
|
487
|
+
slotNumber: slot,
|
|
488
|
+
coinbase: blockHeader.globalVariables.coinbase,
|
|
489
|
+
feeRecipient: blockHeader.globalVariables.feeRecipient,
|
|
490
|
+
gasFees: blockHeader.globalVariables.gasFees,
|
|
491
|
+
};
|
|
294
492
|
|
|
295
|
-
|
|
296
|
-
|
|
493
|
+
// Create checkpoint builder with prior blocks
|
|
494
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
495
|
+
checkpointNumber,
|
|
496
|
+
constants,
|
|
497
|
+
l1ToL2Messages,
|
|
498
|
+
fork,
|
|
499
|
+
priorBlocks,
|
|
500
|
+
);
|
|
501
|
+
|
|
502
|
+
// Build the new block
|
|
503
|
+
const deadline = this.getReexecutionDeadline(slot, config);
|
|
504
|
+
const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
|
|
505
|
+
deadline,
|
|
506
|
+
expectedEndState: blockHeader.state,
|
|
297
507
|
});
|
|
298
508
|
|
|
509
|
+
const { block, failedTxs } = result;
|
|
299
510
|
const numFailedTxs = failedTxs.length;
|
|
300
|
-
|
|
511
|
+
|
|
301
512
|
this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
|
|
302
513
|
numFailedTxs,
|
|
303
514
|
numProposalTxs: txHashes.length,
|
|
@@ -316,11 +527,15 @@ export class BlockProposalHandler {
|
|
|
316
527
|
}
|
|
317
528
|
|
|
318
529
|
// Throw a ReExStateMismatchError error if state updates do not match
|
|
319
|
-
|
|
320
|
-
|
|
530
|
+
// Compare the full block structure (archive and header) from the built block with the proposal
|
|
531
|
+
const archiveMatches = proposal.archive.equals(block.archive.root);
|
|
532
|
+
const headerMatches = proposal.blockHeader.equals(block.header);
|
|
533
|
+
if (!archiveMatches || !headerMatches) {
|
|
321
534
|
this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
|
|
322
|
-
|
|
323
|
-
|
|
535
|
+
expectedArchive: block.archive.root.toString(),
|
|
536
|
+
actualArchive: proposal.archive.toString(),
|
|
537
|
+
expectedHeader: block.header.toInspect(),
|
|
538
|
+
actualHeader: proposal.blockHeader.toInspect(),
|
|
324
539
|
});
|
|
325
540
|
this.metrics?.recordFailedReexecution(proposal);
|
|
326
541
|
throw new ReExStateMismatchError(proposal.archive, block.archive.root);
|