@aztec/validator-client 0.0.1-commit.3469e52 → 0.0.1-commit.35158ae7e
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 +63 -19
- package/dest/block_proposal_handler.d.ts +9 -10
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +135 -82
- package/dest/checkpoint_builder.d.ts +27 -15
- package/dest/checkpoint_builder.d.ts.map +1 -1
- package/dest/checkpoint_builder.js +136 -45
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +30 -7
- package/dest/duties/validation_service.d.ts +2 -2
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +6 -12
- package/dest/factory.d.ts +3 -1
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +3 -2
- package/dest/index.d.ts +1 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +0 -1
- package/dest/key_store/ha_key_store.d.ts +1 -1
- package/dest/key_store/ha_key_store.d.ts.map +1 -1
- package/dest/key_store/ha_key_store.js +3 -3
- package/dest/metrics.d.ts +12 -3
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +46 -5
- package/dest/validator.d.ts +41 -15
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +230 -69
- package/package.json +19 -17
- package/src/block_proposal_handler.ts +165 -109
- package/src/checkpoint_builder.ts +185 -52
- package/src/config.ts +30 -7
- package/src/duties/validation_service.ts +12 -11
- package/src/factory.ts +4 -0
- package/src/index.ts +0 -1
- package/src/key_store/ha_key_store.ts +3 -3
- package/src/metrics.ts +63 -6
- package/src/validator.ts +294 -86
- package/dest/tx_validator/index.d.ts +0 -3
- package/dest/tx_validator/index.d.ts.map +0 -1
- package/dest/tx_validator/index.js +0 -2
- package/dest/tx_validator/nullifier_cache.d.ts +0 -14
- package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
- package/dest/tx_validator/nullifier_cache.js +0 -24
- package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
- package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
- package/dest/tx_validator/tx_validator_factory.js +0 -54
- package/src/tx_validator/index.ts +0 -2
- package/src/tx_validator/nullifier_cache.ts +0 -30
- package/src/tx_validator/tx_validator_factory.ts +0 -135
|
@@ -1,26 +1,25 @@
|
|
|
1
1
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
2
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
3
3
|
import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
|
+
import { pick } from '@aztec/foundation/collection';
|
|
4
5
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
5
6
|
import { TimeoutError } from '@aztec/foundation/error';
|
|
6
7
|
import { createLogger } from '@aztec/foundation/log';
|
|
7
8
|
import { retryUntil } from '@aztec/foundation/retry';
|
|
8
9
|
import { DateProvider, Timer } from '@aztec/foundation/timer';
|
|
9
10
|
import type { P2P, PeerId } from '@aztec/p2p';
|
|
10
|
-
import { TxProvider } from '@aztec/p2p';
|
|
11
11
|
import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
|
|
12
|
-
import type {
|
|
12
|
+
import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
13
13
|
import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
14
|
-
import
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
computeCheckpointOutHash,
|
|
18
|
-
computeInHashFromL1ToL2Messages,
|
|
19
|
-
} from '@aztec/stdlib/messaging';
|
|
14
|
+
import { Gas } from '@aztec/stdlib/gas';
|
|
15
|
+
import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
16
|
+
import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
20
17
|
import type { BlockProposal } from '@aztec/stdlib/p2p';
|
|
21
|
-
import {
|
|
18
|
+
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
19
|
+
import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
|
|
22
20
|
import {
|
|
23
21
|
ReExFailedTxsError,
|
|
22
|
+
ReExInitialStateMismatchError,
|
|
24
23
|
ReExStateMismatchError,
|
|
25
24
|
ReExTimeoutError,
|
|
26
25
|
TransactionsNotAvailableError,
|
|
@@ -33,6 +32,7 @@ import type { ValidatorMetrics } from './metrics.js';
|
|
|
33
32
|
export type BlockProposalValidationFailureReason =
|
|
34
33
|
| 'invalid_proposal'
|
|
35
34
|
| 'parent_block_not_found'
|
|
35
|
+
| 'block_source_not_synced'
|
|
36
36
|
| 'parent_block_wrong_slot'
|
|
37
37
|
| 'in_hash_mismatch'
|
|
38
38
|
| 'global_variables_mismatch'
|
|
@@ -40,11 +40,12 @@ export type BlockProposalValidationFailureReason =
|
|
|
40
40
|
| 'txs_not_available'
|
|
41
41
|
| 'state_mismatch'
|
|
42
42
|
| 'failed_txs'
|
|
43
|
+
| 'initial_state_mismatch'
|
|
43
44
|
| 'timeout'
|
|
44
45
|
| 'unknown_error';
|
|
45
46
|
|
|
46
47
|
type ReexecuteTransactionsResult = {
|
|
47
|
-
block:
|
|
48
|
+
block: L2Block;
|
|
48
49
|
failedTxs: FailedTx[];
|
|
49
50
|
reexecutionTimeMs: number;
|
|
50
51
|
totalManaUsed: number;
|
|
@@ -77,7 +78,7 @@ export class BlockProposalHandler {
|
|
|
77
78
|
private worldState: WorldStateSynchronizer,
|
|
78
79
|
private blockSource: L2BlockSource & L2BlockSink,
|
|
79
80
|
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
80
|
-
private txProvider:
|
|
81
|
+
private txProvider: ITxProvider,
|
|
81
82
|
private blockProposalValidator: BlockProposalValidator,
|
|
82
83
|
private epochCache: EpochCache,
|
|
83
84
|
private config: ValidatorClientFullConfig,
|
|
@@ -92,25 +93,28 @@ export class BlockProposalHandler {
|
|
|
92
93
|
this.tracer = telemetry.getTracer('BlockProposalHandler');
|
|
93
94
|
}
|
|
94
95
|
|
|
95
|
-
|
|
96
|
-
// Non-validator handler that re-executes for monitoring but does not attest.
|
|
96
|
+
register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
|
|
97
|
+
// Non-validator handler that processes or re-executes for monitoring but does not attest.
|
|
97
98
|
// Returns boolean indicating whether the proposal was valid.
|
|
98
99
|
const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
|
|
99
100
|
try {
|
|
100
|
-
const
|
|
101
|
+
const { slotNumber, blockNumber } = proposal;
|
|
102
|
+
const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
|
|
101
103
|
if (result.isValid) {
|
|
102
|
-
this.log.info(`Non-validator
|
|
104
|
+
this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
|
|
103
105
|
blockNumber: result.blockNumber,
|
|
106
|
+
slotNumber,
|
|
104
107
|
reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
|
|
105
108
|
totalManaUsed: result.reexecutionResult?.totalManaUsed,
|
|
106
109
|
numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
|
|
110
|
+
reexecuted: shouldReexecute,
|
|
107
111
|
});
|
|
108
112
|
return true;
|
|
109
113
|
} else {
|
|
110
|
-
this.log.warn(
|
|
111
|
-
blockNumber
|
|
112
|
-
reason: result.reason,
|
|
113
|
-
|
|
114
|
+
this.log.warn(
|
|
115
|
+
`Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`,
|
|
116
|
+
{ blockNumber: result.blockNumber, slotNumber, reason: result.reason },
|
|
117
|
+
);
|
|
114
118
|
return false;
|
|
115
119
|
}
|
|
116
120
|
} catch (error) {
|
|
@@ -138,7 +142,13 @@ export class BlockProposalHandler {
|
|
|
138
142
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
139
143
|
}
|
|
140
144
|
|
|
141
|
-
const proposalInfo = {
|
|
145
|
+
const proposalInfo = {
|
|
146
|
+
...proposal.toBlockInfo(),
|
|
147
|
+
proposer: proposer.toString(),
|
|
148
|
+
blockNumber: undefined as BlockNumber | undefined,
|
|
149
|
+
checkpointNumber: undefined as CheckpointNumber | undefined,
|
|
150
|
+
};
|
|
151
|
+
|
|
142
152
|
this.log.info(`Processing proposal for slot ${slotNumber}`, {
|
|
143
153
|
...proposalInfo,
|
|
144
154
|
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
@@ -146,23 +156,40 @@ export class BlockProposalHandler {
|
|
|
146
156
|
|
|
147
157
|
// Check that the proposal is from the current proposer, or the next proposer
|
|
148
158
|
// This should have been handled by the p2p layer, but we double check here out of caution
|
|
149
|
-
const
|
|
150
|
-
if (
|
|
159
|
+
const validationResult = await this.blockProposalValidator.validate(proposal);
|
|
160
|
+
if (validationResult.result !== 'accept') {
|
|
151
161
|
this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
|
|
152
162
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
153
163
|
}
|
|
154
164
|
|
|
155
|
-
//
|
|
156
|
-
|
|
157
|
-
|
|
165
|
+
// Ensure the block source is synced before checking for existing blocks,
|
|
166
|
+
// since a pending checkpoint prune may remove blocks we'd otherwise find.
|
|
167
|
+
// This affects mostly the block_number_already_exists check, since a pending
|
|
168
|
+
// checkpoint prune could remove a block that would conflict with this proposal.
|
|
169
|
+
// When pipelining is enabled, the proposer builds ahead of L1 submission, so the
|
|
170
|
+
// block source won't have synced to the proposed slot yet. Skip the sync wait to
|
|
171
|
+
// avoid eating into the attestation window.
|
|
172
|
+
if (!this.epochCache.isProposerPipeliningEnabled()) {
|
|
173
|
+
const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
|
|
174
|
+
if (!blockSourceSync) {
|
|
175
|
+
this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
|
|
176
|
+
return { isValid: false, reason: 'block_source_not_synced' };
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Check that the parent proposal is a block we know, otherwise reexecution would fail.
|
|
181
|
+
// If we don't find it immediately, we keep retrying for a while; it may be we still
|
|
182
|
+
// need to process other block proposals to get to it.
|
|
183
|
+
const parentBlock = await this.getParentBlock(proposal);
|
|
184
|
+
if (parentBlock === undefined) {
|
|
158
185
|
this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
|
|
159
186
|
return { isValid: false, reason: 'parent_block_not_found' };
|
|
160
187
|
}
|
|
161
188
|
|
|
162
|
-
// Check that the parent block's slot is
|
|
163
|
-
if (
|
|
164
|
-
this.log.warn(`Parent block slot is greater than
|
|
165
|
-
parentBlockSlot:
|
|
189
|
+
// Check that the parent block's slot is not greater than the proposal's slot.
|
|
190
|
+
if (parentBlock !== 'genesis' && parentBlock.header.getSlot() > slotNumber) {
|
|
191
|
+
this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
|
|
192
|
+
parentBlockSlot: parentBlock.header.getSlot().toString(),
|
|
166
193
|
proposalSlot: slotNumber.toString(),
|
|
167
194
|
...proposalInfo,
|
|
168
195
|
});
|
|
@@ -171,9 +198,10 @@ export class BlockProposalHandler {
|
|
|
171
198
|
|
|
172
199
|
// Compute the block number based on the parent block
|
|
173
200
|
const blockNumber =
|
|
174
|
-
|
|
201
|
+
parentBlock === 'genesis'
|
|
175
202
|
? BlockNumber(INITIAL_L2_BLOCK_NUM)
|
|
176
|
-
: BlockNumber(
|
|
203
|
+
: BlockNumber(parentBlock.header.getBlockNumber() + 1);
|
|
204
|
+
proposalInfo.blockNumber = blockNumber;
|
|
177
205
|
|
|
178
206
|
// Check that this block number does not exist already
|
|
179
207
|
const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
|
|
@@ -189,12 +217,22 @@ export class BlockProposalHandler {
|
|
|
189
217
|
deadline: this.getReexecutionDeadline(slotNumber, config),
|
|
190
218
|
});
|
|
191
219
|
|
|
220
|
+
// If reexecution is disabled, bail. We were just interested in triggering tx collection.
|
|
221
|
+
if (!shouldReexecute) {
|
|
222
|
+
this.log.info(
|
|
223
|
+
`Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
|
|
224
|
+
proposalInfo,
|
|
225
|
+
);
|
|
226
|
+
return { isValid: true, blockNumber };
|
|
227
|
+
}
|
|
228
|
+
|
|
192
229
|
// Compute the checkpoint number for this block and validate checkpoint consistency
|
|
193
|
-
const checkpointResult =
|
|
230
|
+
const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
|
|
194
231
|
if (checkpointResult.reason) {
|
|
195
232
|
return { isValid: false, blockNumber, reason: checkpointResult.reason };
|
|
196
233
|
}
|
|
197
234
|
const checkpointNumber = checkpointResult.checkpointNumber;
|
|
235
|
+
proposalInfo.checkpointNumber = checkpointNumber;
|
|
198
236
|
|
|
199
237
|
// Check that I have the same set of l1ToL2Messages as the proposal
|
|
200
238
|
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
@@ -215,53 +253,44 @@ export class BlockProposalHandler {
|
|
|
215
253
|
return { isValid: false, blockNumber, reason: 'txs_not_available' };
|
|
216
254
|
}
|
|
217
255
|
|
|
256
|
+
// Collect the out hashes of all the checkpoints before this one in the same epoch
|
|
257
|
+
const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
|
|
258
|
+
const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
|
|
259
|
+
.filter(c => c.checkpointNumber < checkpointNumber)
|
|
260
|
+
.map(c => c.checkpointOutHash);
|
|
261
|
+
|
|
218
262
|
// Try re-executing the transactions in the proposal if needed
|
|
219
263
|
let reexecutionResult;
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const previousCheckpointOutHashes = previousBlocks.map(b =>
|
|
230
|
-
computeCheckpointOutHash([b.body.txEffects.map(tx => tx.l2ToL1Msgs)]),
|
|
264
|
+
try {
|
|
265
|
+
this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
|
|
266
|
+
reexecutionResult = await this.reexecuteTransactions(
|
|
267
|
+
proposal,
|
|
268
|
+
blockNumber,
|
|
269
|
+
checkpointNumber,
|
|
270
|
+
txs,
|
|
271
|
+
l1ToL2Messages,
|
|
272
|
+
previousCheckpointOutHashes,
|
|
231
273
|
);
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
proposal,
|
|
237
|
-
blockNumber,
|
|
238
|
-
checkpointNumber,
|
|
239
|
-
txs,
|
|
240
|
-
l1ToL2Messages,
|
|
241
|
-
previousCheckpointOutHashes,
|
|
242
|
-
);
|
|
243
|
-
} catch (error) {
|
|
244
|
-
this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
|
|
245
|
-
const reason = this.getReexecuteFailureReason(error);
|
|
246
|
-
return { isValid: false, blockNumber, reason, reexecutionResult };
|
|
247
|
-
}
|
|
274
|
+
} catch (error) {
|
|
275
|
+
this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
|
|
276
|
+
const reason = this.getReexecuteFailureReason(error);
|
|
277
|
+
return { isValid: false, blockNumber, reason, reexecutionResult };
|
|
248
278
|
}
|
|
249
279
|
|
|
250
280
|
// If we succeeded, push this block into the archiver (unless disabled)
|
|
251
|
-
// TODO(palla/mbps): Change default to false once block sync is stable.
|
|
252
281
|
if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
|
|
253
282
|
await this.blockSource.addBlock(reexecutionResult?.block);
|
|
254
283
|
}
|
|
255
284
|
|
|
256
285
|
this.log.info(
|
|
257
|
-
`Successfully
|
|
258
|
-
proposalInfo,
|
|
286
|
+
`Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
|
|
287
|
+
{ ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
|
|
259
288
|
);
|
|
260
289
|
|
|
261
290
|
return { isValid: true, blockNumber, reexecutionResult };
|
|
262
291
|
}
|
|
263
292
|
|
|
264
|
-
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' |
|
|
293
|
+
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
|
|
265
294
|
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
266
295
|
const slot = proposal.slotNumber;
|
|
267
296
|
const config = this.checkpointsBuilder.getConfig();
|
|
@@ -277,12 +306,11 @@ export class BlockProposalHandler {
|
|
|
277
306
|
|
|
278
307
|
try {
|
|
279
308
|
return (
|
|
280
|
-
(await this.blockSource.
|
|
309
|
+
(await this.blockSource.getBlockDataByArchive(parentArchive)) ??
|
|
281
310
|
(timeoutDurationMs <= 0
|
|
282
311
|
? undefined
|
|
283
312
|
: await retryUntil(
|
|
284
|
-
() =>
|
|
285
|
-
this.blockSource.syncImmediate().then(() => this.blockSource.getBlockHeaderByArchive(parentArchive)),
|
|
313
|
+
() => this.blockSource.syncImmediate().then(() => this.blockSource.getBlockDataByArchive(parentArchive)),
|
|
286
314
|
'force archiver sync',
|
|
287
315
|
timeoutDurationMs / 1000,
|
|
288
316
|
0.5,
|
|
@@ -298,12 +326,12 @@ export class BlockProposalHandler {
|
|
|
298
326
|
}
|
|
299
327
|
}
|
|
300
328
|
|
|
301
|
-
private
|
|
329
|
+
private computeCheckpointNumber(
|
|
302
330
|
proposal: BlockProposal,
|
|
303
|
-
|
|
331
|
+
parentBlock: 'genesis' | BlockData,
|
|
304
332
|
proposalInfo: object,
|
|
305
|
-
):
|
|
306
|
-
if (
|
|
333
|
+
): CheckpointComputationResult {
|
|
334
|
+
if (parentBlock === 'genesis') {
|
|
307
335
|
// First block is in checkpoint 1
|
|
308
336
|
if (proposal.indexWithinCheckpoint !== 0) {
|
|
309
337
|
this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
|
|
@@ -312,19 +340,9 @@ export class BlockProposalHandler {
|
|
|
312
340
|
return { checkpointNumber: CheckpointNumber.INITIAL };
|
|
313
341
|
}
|
|
314
342
|
|
|
315
|
-
// Get the parent block to find its checkpoint number
|
|
316
|
-
// TODO(palla/mbps): The block header should include the checkpoint number to avoid this lookup,
|
|
317
|
-
// or at least the L2BlockSource should return a different struct that includes it.
|
|
318
|
-
const parentBlockNumber = parentBlockHeader.getBlockNumber();
|
|
319
|
-
const parentBlock = await this.blockSource.getL2BlockNew(parentBlockNumber);
|
|
320
|
-
if (!parentBlock) {
|
|
321
|
-
this.log.warn(`Parent block ${parentBlockNumber} not found in archiver`, proposalInfo);
|
|
322
|
-
return { reason: 'invalid_proposal' };
|
|
323
|
-
}
|
|
324
|
-
|
|
325
343
|
if (proposal.indexWithinCheckpoint === 0) {
|
|
326
344
|
// If this is the first block in a new checkpoint, increment the checkpoint number
|
|
327
|
-
if (!(proposal.blockHeader.getSlot() >
|
|
345
|
+
if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
|
|
328
346
|
this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
|
|
329
347
|
return { reason: 'invalid_proposal' };
|
|
330
348
|
}
|
|
@@ -336,7 +354,7 @@ export class BlockProposalHandler {
|
|
|
336
354
|
this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
|
|
337
355
|
return { reason: 'invalid_proposal' };
|
|
338
356
|
}
|
|
339
|
-
if (proposal.blockHeader.getSlot() !==
|
|
357
|
+
if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
|
|
340
358
|
this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
|
|
341
359
|
return { reason: 'invalid_proposal' };
|
|
342
360
|
}
|
|
@@ -357,7 +375,7 @@ export class BlockProposalHandler {
|
|
|
357
375
|
*/
|
|
358
376
|
private validateNonFirstBlockInCheckpoint(
|
|
359
377
|
proposal: BlockProposal,
|
|
360
|
-
parentBlock:
|
|
378
|
+
parentBlock: BlockData,
|
|
361
379
|
proposalInfo: object,
|
|
362
380
|
): CheckpointComputationResult | undefined {
|
|
363
381
|
const proposalGlobals = proposal.blockHeader.globalVariables;
|
|
@@ -436,31 +454,48 @@ export class BlockProposalHandler {
|
|
|
436
454
|
return new Date(nextSlotTimestampSeconds * 1000);
|
|
437
455
|
}
|
|
438
456
|
|
|
439
|
-
/**
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
slot
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
const block = await this.blockSource.getL2BlockNew(currentBlockNumber);
|
|
452
|
-
if (!block || block.header.getSlot() !== slot || block.checkpointNumber !== checkpointNumber) {
|
|
453
|
-
break;
|
|
454
|
-
}
|
|
455
|
-
blocks.unshift(block);
|
|
456
|
-
currentBlockNumber = BlockNumber(currentBlockNumber - 1);
|
|
457
|
+
/** Waits for the block source to sync L1 data up to at least the slot before the given one. */
|
|
458
|
+
private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
|
|
459
|
+
const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
|
|
460
|
+
const timeoutMs = deadline.getTime() - this.dateProvider.now();
|
|
461
|
+
if (slot === 0) {
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Make a quick check before triggering an archiver sync
|
|
466
|
+
const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
|
|
467
|
+
if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
|
|
468
|
+
return true;
|
|
457
469
|
}
|
|
458
470
|
|
|
459
|
-
|
|
471
|
+
try {
|
|
472
|
+
// Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
|
|
473
|
+
return await retryUntil(
|
|
474
|
+
async () => {
|
|
475
|
+
await this.blockSource.syncImmediate();
|
|
476
|
+
const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
|
|
477
|
+
return syncedSlot !== undefined && syncedSlot + 1 >= slot;
|
|
478
|
+
},
|
|
479
|
+
'wait for block source sync',
|
|
480
|
+
timeoutMs / 1000,
|
|
481
|
+
0.5,
|
|
482
|
+
);
|
|
483
|
+
} catch (err) {
|
|
484
|
+
if (err instanceof TimeoutError) {
|
|
485
|
+
this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
|
|
486
|
+
return false;
|
|
487
|
+
} else {
|
|
488
|
+
throw err;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
460
491
|
}
|
|
461
492
|
|
|
462
|
-
private getReexecuteFailureReason(err: any) {
|
|
463
|
-
if (err instanceof
|
|
493
|
+
private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
|
|
494
|
+
if (err instanceof TransactionsNotAvailableError) {
|
|
495
|
+
return 'txs_not_available';
|
|
496
|
+
} else if (err instanceof ReExInitialStateMismatchError) {
|
|
497
|
+
return 'initial_state_mismatch';
|
|
498
|
+
} else if (err instanceof ReExStateMismatchError) {
|
|
464
499
|
return 'state_mismatch';
|
|
465
500
|
} else if (err instanceof ReExFailedTxsError) {
|
|
466
501
|
return 'failed_txs';
|
|
@@ -492,18 +527,28 @@ export class BlockProposalHandler {
|
|
|
492
527
|
const slot = proposal.slotNumber;
|
|
493
528
|
const config = this.checkpointsBuilder.getConfig();
|
|
494
529
|
|
|
495
|
-
// Get prior blocks in this checkpoint (same slot
|
|
496
|
-
const
|
|
530
|
+
// Get prior blocks in this checkpoint (same slot before current block)
|
|
531
|
+
const allBlocksInSlot = await this.blockSource.getBlocksForSlot(slot);
|
|
532
|
+
const priorBlocks = allBlocksInSlot.filter(b => b.number < blockNumber && b.header.getSlot() === slot);
|
|
497
533
|
|
|
498
534
|
// Fork before the block to be built
|
|
499
535
|
const parentBlockNumber = BlockNumber(blockNumber - 1);
|
|
500
|
-
|
|
536
|
+
await this.worldState.syncImmediate(parentBlockNumber);
|
|
537
|
+
await using fork = await this.worldState.fork(parentBlockNumber);
|
|
538
|
+
|
|
539
|
+
// Verify the fork's archive root matches the proposal's expected last archive.
|
|
540
|
+
// If they don't match, our world state synced to a different chain and reexecution would fail.
|
|
541
|
+
const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
|
|
542
|
+
if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
|
|
543
|
+
throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
|
|
544
|
+
}
|
|
501
545
|
|
|
502
|
-
// Build checkpoint constants from proposal (excludes blockNumber
|
|
546
|
+
// Build checkpoint constants from proposal (excludes blockNumber which is per-block)
|
|
503
547
|
const constants: CheckpointGlobalVariables = {
|
|
504
548
|
chainId: new Fr(config.l1ChainId),
|
|
505
549
|
version: new Fr(config.rollupVersion),
|
|
506
550
|
slotNumber: slot,
|
|
551
|
+
timestamp: blockHeader.globalVariables.timestamp,
|
|
507
552
|
coinbase: blockHeader.globalVariables.coinbase,
|
|
508
553
|
feeRecipient: blockHeader.globalVariables.feeRecipient,
|
|
509
554
|
gasFees: blockHeader.globalVariables.gasFees,
|
|
@@ -513,26 +558,37 @@ export class BlockProposalHandler {
|
|
|
513
558
|
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
514
559
|
checkpointNumber,
|
|
515
560
|
constants,
|
|
561
|
+
0n, // only takes effect in the following checkpoint.
|
|
516
562
|
l1ToL2Messages,
|
|
517
563
|
previousCheckpointOutHashes,
|
|
518
564
|
fork,
|
|
519
565
|
priorBlocks,
|
|
566
|
+
this.log.getBindings(),
|
|
520
567
|
);
|
|
521
568
|
|
|
522
569
|
// Build the new block
|
|
523
570
|
const deadline = this.getReexecutionDeadline(slot, config);
|
|
571
|
+
const maxBlockGas =
|
|
572
|
+
this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
|
|
573
|
+
? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
|
|
574
|
+
: undefined;
|
|
524
575
|
const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
|
|
576
|
+
isBuildingProposal: false,
|
|
577
|
+
minValidTxs: 0,
|
|
525
578
|
deadline,
|
|
526
579
|
expectedEndState: blockHeader.state,
|
|
580
|
+
maxTransactions: this.config.validateMaxTxsPerBlock,
|
|
581
|
+
maxBlockGas,
|
|
527
582
|
});
|
|
528
583
|
|
|
529
584
|
const { block, failedTxs } = result;
|
|
530
585
|
const numFailedTxs = failedTxs.length;
|
|
531
586
|
|
|
532
|
-
this.log.verbose(`
|
|
587
|
+
this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
|
|
533
588
|
numFailedTxs,
|
|
534
589
|
numProposalTxs: txHashes.length,
|
|
535
590
|
numProcessedTxs: block.body.txEffects.length,
|
|
591
|
+
blockNumber,
|
|
536
592
|
slot,
|
|
537
593
|
});
|
|
538
594
|
|