@aztec/validator-client 0.0.1-commit.9b94fc1 → 0.0.1-commit.9ee6fcc6
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 +326 -0
- package/dest/block_proposal_handler.d.ts +26 -14
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +433 -109
- package/dest/checkpoint_builder.d.ts +79 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +251 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +37 -8
- package/dest/duties/validation_service.d.ts +42 -13
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +105 -28
- package/dest/factory.d.ts +15 -8
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +4 -3
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/key_store/ha_key_store.d.ts +99 -0
- package/dest/key_store/ha_key_store.d.ts.map +1 -0
- package/dest/key_store/ha_key_store.js +208 -0
- package/dest/key_store/index.d.ts +2 -1
- package/dest/key_store/index.d.ts.map +1 -1
- package/dest/key_store/index.js +1 -0
- package/dest/key_store/interface.d.ts +36 -6
- package/dest/key_store/interface.d.ts.map +1 -1
- package/dest/key_store/local_key_store.d.ts +10 -5
- package/dest/key_store/local_key_store.d.ts.map +1 -1
- package/dest/key_store/local_key_store.js +9 -5
- package/dest/key_store/node_keystore_adapter.d.ts +18 -5
- package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
- package/dest/key_store/node_keystore_adapter.js +18 -4
- package/dest/key_store/web3signer_key_store.d.ts +10 -5
- package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
- package/dest/key_store/web3signer_key_store.js +9 -5
- package/dest/metrics.d.ts +12 -3
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +46 -30
- package/dest/validator.d.ts +76 -21
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +483 -57
- package/package.json +23 -13
- package/src/block_proposal_handler.ts +370 -79
- package/src/checkpoint_builder.ts +417 -0
- package/src/config.ts +36 -7
- package/src/duties/validation_service.ts +156 -33
- package/src/factory.ts +21 -8
- package/src/index.ts +1 -0
- package/src/key_store/ha_key_store.ts +269 -0
- package/src/key_store/index.ts +1 -0
- package/src/key_store/interface.ts +44 -5
- package/src/key_store/local_key_store.ts +14 -5
- package/src/key_store/node_keystore_adapter.ts +28 -5
- package/src/key_store/web3signer_key_store.ts +18 -5
- package/src/metrics.ts +63 -33
- package/src/validator.ts +659 -90
|
@@ -1,38 +1,46 @@
|
|
|
1
1
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
-
import {
|
|
2
|
+
import type { EpochCache } from '@aztec/epoch-cache';
|
|
3
|
+
import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
|
+
import { pick } from '@aztec/foundation/collection';
|
|
5
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
6
|
import { TimeoutError } from '@aztec/foundation/error';
|
|
4
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
5
7
|
import { createLogger } from '@aztec/foundation/log';
|
|
6
8
|
import { retryUntil } from '@aztec/foundation/retry';
|
|
7
9
|
import { DateProvider, Timer } from '@aztec/foundation/timer';
|
|
8
10
|
import type { P2P, PeerId } from '@aztec/p2p';
|
|
9
|
-
import { TxProvider } from '@aztec/p2p';
|
|
10
11
|
import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
|
|
11
|
-
import type { L2Block, L2BlockSource } from '@aztec/stdlib/block';
|
|
12
|
-
import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
13
|
-
import
|
|
12
|
+
import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
13
|
+
import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
14
|
+
import { Gas } from '@aztec/stdlib/gas';
|
|
15
|
+
import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
14
16
|
import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
+
import type { BlockProposal } from '@aztec/stdlib/p2p';
|
|
18
|
+
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
19
|
+
import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
|
|
17
20
|
import {
|
|
18
21
|
ReExFailedTxsError,
|
|
22
|
+
ReExInitialStateMismatchError,
|
|
19
23
|
ReExStateMismatchError,
|
|
20
24
|
ReExTimeoutError,
|
|
21
25
|
TransactionsNotAvailableError,
|
|
22
26
|
} from '@aztec/stdlib/validators';
|
|
23
27
|
import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
24
28
|
|
|
29
|
+
import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
25
30
|
import type { ValidatorMetrics } from './metrics.js';
|
|
26
31
|
|
|
27
32
|
export type BlockProposalValidationFailureReason =
|
|
28
33
|
| 'invalid_proposal'
|
|
29
34
|
| 'parent_block_not_found'
|
|
35
|
+
| 'block_source_not_synced'
|
|
30
36
|
| 'parent_block_wrong_slot'
|
|
31
37
|
| 'in_hash_mismatch'
|
|
38
|
+
| 'global_variables_mismatch'
|
|
32
39
|
| 'block_number_already_exists'
|
|
33
40
|
| 'txs_not_available'
|
|
34
41
|
| 'state_mismatch'
|
|
35
42
|
| 'failed_txs'
|
|
43
|
+
| 'initial_state_mismatch'
|
|
36
44
|
| 'timeout'
|
|
37
45
|
| 'unknown_error';
|
|
38
46
|
|
|
@@ -45,28 +53,34 @@ type ReexecuteTransactionsResult = {
|
|
|
45
53
|
|
|
46
54
|
export type BlockProposalValidationSuccessResult = {
|
|
47
55
|
isValid: true;
|
|
48
|
-
blockNumber:
|
|
56
|
+
blockNumber: BlockNumber;
|
|
49
57
|
reexecutionResult?: ReexecuteTransactionsResult;
|
|
50
58
|
};
|
|
51
59
|
|
|
52
60
|
export type BlockProposalValidationFailureResult = {
|
|
53
61
|
isValid: false;
|
|
54
62
|
reason: BlockProposalValidationFailureReason;
|
|
55
|
-
blockNumber?:
|
|
63
|
+
blockNumber?: BlockNumber;
|
|
56
64
|
reexecutionResult?: ReexecuteTransactionsResult;
|
|
57
65
|
};
|
|
58
66
|
|
|
59
67
|
export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
|
|
60
68
|
|
|
69
|
+
type CheckpointComputationResult =
|
|
70
|
+
| { checkpointNumber: CheckpointNumber; reason?: undefined }
|
|
71
|
+
| { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
|
|
72
|
+
|
|
61
73
|
export class BlockProposalHandler {
|
|
62
74
|
public readonly tracer: Tracer;
|
|
63
75
|
|
|
64
76
|
constructor(
|
|
65
|
-
private
|
|
66
|
-
private
|
|
77
|
+
private checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
78
|
+
private worldState: WorldStateSynchronizer,
|
|
79
|
+
private blockSource: L2BlockSource & L2BlockSink,
|
|
67
80
|
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
68
|
-
private txProvider:
|
|
81
|
+
private txProvider: ITxProvider,
|
|
69
82
|
private blockProposalValidator: BlockProposalValidator,
|
|
83
|
+
private epochCache: EpochCache,
|
|
70
84
|
private config: ValidatorClientFullConfig,
|
|
71
85
|
private metrics?: ValidatorMetrics,
|
|
72
86
|
private dateProvider: DateProvider = new DateProvider(),
|
|
@@ -79,27 +93,34 @@ export class BlockProposalHandler {
|
|
|
79
93
|
this.tracer = telemetry.getTracer('BlockProposalHandler');
|
|
80
94
|
}
|
|
81
95
|
|
|
82
|
-
|
|
83
|
-
|
|
96
|
+
register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
|
|
97
|
+
// Non-validator handler that processes or re-executes for monitoring but does not attest.
|
|
98
|
+
// Returns boolean indicating whether the proposal was valid.
|
|
99
|
+
const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
|
|
84
100
|
try {
|
|
85
|
-
const
|
|
101
|
+
const { slotNumber, blockNumber } = proposal;
|
|
102
|
+
const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
|
|
86
103
|
if (result.isValid) {
|
|
87
|
-
this.log.info(`Non-validator
|
|
104
|
+
this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
|
|
88
105
|
blockNumber: result.blockNumber,
|
|
106
|
+
slotNumber,
|
|
89
107
|
reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
|
|
90
108
|
totalManaUsed: result.reexecutionResult?.totalManaUsed,
|
|
91
109
|
numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
|
|
110
|
+
reexecuted: shouldReexecute,
|
|
92
111
|
});
|
|
112
|
+
return true;
|
|
93
113
|
} else {
|
|
94
|
-
this.log.warn(
|
|
95
|
-
blockNumber
|
|
96
|
-
reason: result.reason,
|
|
97
|
-
|
|
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
|
+
);
|
|
118
|
+
return false;
|
|
98
119
|
}
|
|
99
120
|
} catch (error) {
|
|
100
121
|
this.log.error('Error processing block proposal in non-validator handler', error);
|
|
122
|
+
return false;
|
|
101
123
|
}
|
|
102
|
-
return undefined; // Non-validator nodes don't return attestations
|
|
103
124
|
};
|
|
104
125
|
|
|
105
126
|
p2pClient.registerBlockProposalHandler(handler);
|
|
@@ -113,7 +134,7 @@ export class BlockProposalHandler {
|
|
|
113
134
|
): Promise<BlockProposalValidationResult> {
|
|
114
135
|
const slotNumber = proposal.slotNumber;
|
|
115
136
|
const proposer = proposal.getSender();
|
|
116
|
-
const config = this.
|
|
137
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
117
138
|
|
|
118
139
|
// Reject proposals with invalid signatures
|
|
119
140
|
if (!proposer) {
|
|
@@ -121,7 +142,13 @@ export class BlockProposalHandler {
|
|
|
121
142
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
122
143
|
}
|
|
123
144
|
|
|
124
|
-
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
|
+
|
|
125
152
|
this.log.info(`Processing proposal for slot ${slotNumber}`, {
|
|
126
153
|
...proposalInfo,
|
|
127
154
|
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
@@ -129,23 +156,40 @@ export class BlockProposalHandler {
|
|
|
129
156
|
|
|
130
157
|
// Check that the proposal is from the current proposer, or the next proposer
|
|
131
158
|
// This should have been handled by the p2p layer, but we double check here out of caution
|
|
132
|
-
const
|
|
133
|
-
if (
|
|
159
|
+
const validationResult = await this.blockProposalValidator.validate(proposal);
|
|
160
|
+
if (validationResult.result !== 'accept') {
|
|
134
161
|
this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
|
|
135
162
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
136
163
|
}
|
|
137
164
|
|
|
138
|
-
//
|
|
139
|
-
|
|
140
|
-
|
|
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) {
|
|
141
185
|
this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
|
|
142
186
|
return { isValid: false, reason: 'parent_block_not_found' };
|
|
143
187
|
}
|
|
144
188
|
|
|
145
|
-
// Check that the parent block's slot is
|
|
146
|
-
if (
|
|
147
|
-
this.log.warn(`Parent block slot is greater than
|
|
148
|
-
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(),
|
|
149
193
|
proposalSlot: slotNumber.toString(),
|
|
150
194
|
...proposalInfo,
|
|
151
195
|
});
|
|
@@ -153,7 +197,11 @@ export class BlockProposalHandler {
|
|
|
153
197
|
}
|
|
154
198
|
|
|
155
199
|
// Compute the block number based on the parent block
|
|
156
|
-
const blockNumber =
|
|
200
|
+
const blockNumber =
|
|
201
|
+
parentBlock === 'genesis'
|
|
202
|
+
? BlockNumber(INITIAL_L2_BLOCK_NUM)
|
|
203
|
+
: BlockNumber(parentBlock.header.getBlockNumber() + 1);
|
|
204
|
+
proposalInfo.blockNumber = blockNumber;
|
|
157
205
|
|
|
158
206
|
// Check that this block number does not exist already
|
|
159
207
|
const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
|
|
@@ -169,10 +217,27 @@ export class BlockProposalHandler {
|
|
|
169
217
|
deadline: this.getReexecutionDeadline(slotNumber, config),
|
|
170
218
|
});
|
|
171
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
|
+
|
|
229
|
+
// Compute the checkpoint number for this block and validate checkpoint consistency
|
|
230
|
+
const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
|
|
231
|
+
if (checkpointResult.reason) {
|
|
232
|
+
return { isValid: false, blockNumber, reason: checkpointResult.reason };
|
|
233
|
+
}
|
|
234
|
+
const checkpointNumber = checkpointResult.checkpointNumber;
|
|
235
|
+
proposalInfo.checkpointNumber = checkpointNumber;
|
|
236
|
+
|
|
172
237
|
// Check that I have the same set of l1ToL2Messages as the proposal
|
|
173
|
-
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(
|
|
238
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
174
239
|
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
175
|
-
const proposalInHash = proposal.
|
|
240
|
+
const proposalInHash = proposal.inHash;
|
|
176
241
|
if (!computedInHash.equals(proposalInHash)) {
|
|
177
242
|
this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
|
|
178
243
|
proposalInHash: proposalInHash.toString(),
|
|
@@ -188,27 +253,47 @@ export class BlockProposalHandler {
|
|
|
188
253
|
return { isValid: false, blockNumber, reason: 'txs_not_available' };
|
|
189
254
|
}
|
|
190
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
|
+
|
|
191
262
|
// Try re-executing the transactions in the proposal if needed
|
|
192
263
|
let reexecutionResult;
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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,
|
|
273
|
+
);
|
|
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 };
|
|
202
278
|
}
|
|
203
279
|
|
|
204
|
-
this
|
|
280
|
+
// If we succeeded, push this block into the archiver (unless disabled)
|
|
281
|
+
if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
|
|
282
|
+
await this.blockSource.addBlock(reexecutionResult?.block);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
this.log.info(
|
|
286
|
+
`Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
|
|
287
|
+
{ ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
|
|
288
|
+
);
|
|
289
|
+
|
|
205
290
|
return { isValid: true, blockNumber, reexecutionResult };
|
|
206
291
|
}
|
|
207
292
|
|
|
208
|
-
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' |
|
|
209
|
-
const parentArchive = proposal.
|
|
293
|
+
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
|
|
294
|
+
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
210
295
|
const slot = proposal.slotNumber;
|
|
211
|
-
const config = this.
|
|
296
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
212
297
|
const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
|
|
213
298
|
|
|
214
299
|
if (parentArchive.equals(genesisArchiveRoot)) {
|
|
@@ -221,12 +306,11 @@ export class BlockProposalHandler {
|
|
|
221
306
|
|
|
222
307
|
try {
|
|
223
308
|
return (
|
|
224
|
-
(await this.blockSource.
|
|
309
|
+
(await this.blockSource.getBlockDataByArchive(parentArchive)) ??
|
|
225
310
|
(timeoutDurationMs <= 0
|
|
226
311
|
? undefined
|
|
227
312
|
: await retryUntil(
|
|
228
|
-
() =>
|
|
229
|
-
this.blockSource.syncImmediate().then(() => this.blockSource.getBlockHeaderByArchive(parentArchive)),
|
|
313
|
+
() => this.blockSource.syncImmediate().then(() => this.blockSource.getBlockDataByArchive(parentArchive)),
|
|
230
314
|
'force archiver sync',
|
|
231
315
|
timeoutDurationMs / 1000,
|
|
232
316
|
0.5,
|
|
@@ -242,14 +326,176 @@ export class BlockProposalHandler {
|
|
|
242
326
|
}
|
|
243
327
|
}
|
|
244
328
|
|
|
329
|
+
private computeCheckpointNumber(
|
|
330
|
+
proposal: BlockProposal,
|
|
331
|
+
parentBlock: 'genesis' | BlockData,
|
|
332
|
+
proposalInfo: object,
|
|
333
|
+
): CheckpointComputationResult {
|
|
334
|
+
if (parentBlock === 'genesis') {
|
|
335
|
+
// First block is in checkpoint 1
|
|
336
|
+
if (proposal.indexWithinCheckpoint !== 0) {
|
|
337
|
+
this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
|
|
338
|
+
return { reason: 'invalid_proposal' };
|
|
339
|
+
}
|
|
340
|
+
return { checkpointNumber: CheckpointNumber.INITIAL };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (proposal.indexWithinCheckpoint === 0) {
|
|
344
|
+
// If this is the first block in a new checkpoint, increment the checkpoint number
|
|
345
|
+
if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
|
|
346
|
+
this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
|
|
347
|
+
return { reason: 'invalid_proposal' };
|
|
348
|
+
}
|
|
349
|
+
return { checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1) };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Otherwise it should follow the previous block in the same checkpoint
|
|
353
|
+
if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
|
|
354
|
+
this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
|
|
355
|
+
return { reason: 'invalid_proposal' };
|
|
356
|
+
}
|
|
357
|
+
if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
|
|
358
|
+
this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
|
|
359
|
+
return { reason: 'invalid_proposal' };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
|
|
363
|
+
const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
|
|
364
|
+
if (validationResult) {
|
|
365
|
+
return validationResult;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return { checkpointNumber: parentBlock.checkpointNumber };
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Validates that a non-first block in a checkpoint has consistent global variables with its parent.
|
|
373
|
+
* For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
|
|
374
|
+
* @returns A failure result if validation fails, undefined if validation passes
|
|
375
|
+
*/
|
|
376
|
+
private validateNonFirstBlockInCheckpoint(
|
|
377
|
+
proposal: BlockProposal,
|
|
378
|
+
parentBlock: BlockData,
|
|
379
|
+
proposalInfo: object,
|
|
380
|
+
): CheckpointComputationResult | undefined {
|
|
381
|
+
const proposalGlobals = proposal.blockHeader.globalVariables;
|
|
382
|
+
const parentGlobals = parentBlock.header.globalVariables;
|
|
383
|
+
|
|
384
|
+
// All global variables except blockNumber should match the parent
|
|
385
|
+
// blockNumber naturally increments between blocks
|
|
386
|
+
if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
|
|
387
|
+
this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
|
|
388
|
+
...proposalInfo,
|
|
389
|
+
proposalChainId: proposalGlobals.chainId.toString(),
|
|
390
|
+
parentChainId: parentGlobals.chainId.toString(),
|
|
391
|
+
});
|
|
392
|
+
return { reason: 'global_variables_mismatch' };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (!proposalGlobals.version.equals(parentGlobals.version)) {
|
|
396
|
+
this.log.warn(`Non-first block in checkpoint has mismatched version`, {
|
|
397
|
+
...proposalInfo,
|
|
398
|
+
proposalVersion: proposalGlobals.version.toString(),
|
|
399
|
+
parentVersion: parentGlobals.version.toString(),
|
|
400
|
+
});
|
|
401
|
+
return { reason: 'global_variables_mismatch' };
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
|
|
405
|
+
this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
|
|
406
|
+
...proposalInfo,
|
|
407
|
+
proposalSlotNumber: proposalGlobals.slotNumber,
|
|
408
|
+
parentSlotNumber: parentGlobals.slotNumber,
|
|
409
|
+
});
|
|
410
|
+
return { reason: 'global_variables_mismatch' };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
|
|
414
|
+
this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
|
|
415
|
+
...proposalInfo,
|
|
416
|
+
proposalTimestamp: proposalGlobals.timestamp.toString(),
|
|
417
|
+
parentTimestamp: parentGlobals.timestamp.toString(),
|
|
418
|
+
});
|
|
419
|
+
return { reason: 'global_variables_mismatch' };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
|
|
423
|
+
this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
|
|
424
|
+
...proposalInfo,
|
|
425
|
+
proposalCoinbase: proposalGlobals.coinbase.toString(),
|
|
426
|
+
parentCoinbase: parentGlobals.coinbase.toString(),
|
|
427
|
+
});
|
|
428
|
+
return { reason: 'global_variables_mismatch' };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
|
|
432
|
+
this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
|
|
433
|
+
...proposalInfo,
|
|
434
|
+
proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
|
|
435
|
+
parentFeeRecipient: parentGlobals.feeRecipient.toString(),
|
|
436
|
+
});
|
|
437
|
+
return { reason: 'global_variables_mismatch' };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
|
|
441
|
+
this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
|
|
442
|
+
...proposalInfo,
|
|
443
|
+
proposalGasFees: proposalGlobals.gasFees.toInspect(),
|
|
444
|
+
parentGasFees: parentGlobals.gasFees.toInspect(),
|
|
445
|
+
});
|
|
446
|
+
return { reason: 'global_variables_mismatch' };
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
|
|
245
452
|
private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
|
|
246
453
|
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
247
|
-
|
|
248
|
-
|
|
454
|
+
return new Date(nextSlotTimestampSeconds * 1000);
|
|
455
|
+
}
|
|
456
|
+
|
|
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;
|
|
469
|
+
}
|
|
470
|
+
|
|
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
|
+
}
|
|
249
491
|
}
|
|
250
492
|
|
|
251
|
-
private getReexecuteFailureReason(err: any) {
|
|
252
|
-
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) {
|
|
253
499
|
return 'state_mismatch';
|
|
254
500
|
} else if (err instanceof ReExFailedTxsError) {
|
|
255
501
|
return 'failed_txs';
|
|
@@ -262,12 +508,13 @@ export class BlockProposalHandler {
|
|
|
262
508
|
|
|
263
509
|
async reexecuteTransactions(
|
|
264
510
|
proposal: BlockProposal,
|
|
265
|
-
blockNumber:
|
|
511
|
+
blockNumber: BlockNumber,
|
|
512
|
+
checkpointNumber: CheckpointNumber,
|
|
266
513
|
txs: Tx[],
|
|
267
514
|
l1ToL2Messages: Fr[],
|
|
515
|
+
previousCheckpointOutHashes: Fr[],
|
|
268
516
|
): Promise<ReexecuteTransactionsResult> {
|
|
269
|
-
const {
|
|
270
|
-
const { txHashes } = proposal;
|
|
517
|
+
const { blockHeader, txHashes } = proposal;
|
|
271
518
|
|
|
272
519
|
// If we do not have all of the transactions, then we should fail
|
|
273
520
|
if (txs.length !== txHashes.length) {
|
|
@@ -276,32 +523,72 @@ export class BlockProposalHandler {
|
|
|
276
523
|
throw new TransactionsNotAvailableError(missingTxHashes);
|
|
277
524
|
}
|
|
278
525
|
|
|
279
|
-
// Use the sequencer's block building logic to re-execute the transactions
|
|
280
526
|
const timer = new Timer();
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
527
|
+
const slot = proposal.slotNumber;
|
|
528
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
529
|
+
|
|
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);
|
|
533
|
+
|
|
534
|
+
// Fork before the block to be built
|
|
535
|
+
const parentBlockNumber = BlockNumber(blockNumber - 1);
|
|
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
|
+
}
|
|
545
|
+
|
|
546
|
+
// Build checkpoint constants from proposal (excludes blockNumber which is per-block)
|
|
547
|
+
const constants: CheckpointGlobalVariables = {
|
|
291
548
|
chainId: new Fr(config.l1ChainId),
|
|
292
549
|
version: new Fr(config.rollupVersion),
|
|
293
|
-
|
|
550
|
+
slotNumber: slot,
|
|
551
|
+
timestamp: blockHeader.globalVariables.timestamp,
|
|
552
|
+
coinbase: blockHeader.globalVariables.coinbase,
|
|
553
|
+
feeRecipient: blockHeader.globalVariables.feeRecipient,
|
|
554
|
+
gasFees: blockHeader.globalVariables.gasFees,
|
|
555
|
+
};
|
|
294
556
|
|
|
295
|
-
|
|
296
|
-
|
|
557
|
+
// Create checkpoint builder with prior blocks
|
|
558
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
559
|
+
checkpointNumber,
|
|
560
|
+
constants,
|
|
561
|
+
0n, // only takes effect in the following checkpoint.
|
|
562
|
+
l1ToL2Messages,
|
|
563
|
+
previousCheckpointOutHashes,
|
|
564
|
+
fork,
|
|
565
|
+
priorBlocks,
|
|
566
|
+
this.log.getBindings(),
|
|
567
|
+
);
|
|
568
|
+
|
|
569
|
+
// Build the new block
|
|
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;
|
|
575
|
+
const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
|
|
576
|
+
isBuildingProposal: false,
|
|
577
|
+
minValidTxs: 0,
|
|
578
|
+
deadline,
|
|
579
|
+
expectedEndState: blockHeader.state,
|
|
580
|
+
maxTransactions: this.config.validateMaxTxsPerBlock,
|
|
581
|
+
maxBlockGas,
|
|
297
582
|
});
|
|
298
583
|
|
|
584
|
+
const { block, failedTxs } = result;
|
|
299
585
|
const numFailedTxs = failedTxs.length;
|
|
300
|
-
|
|
301
|
-
this.log.verbose(`
|
|
586
|
+
|
|
587
|
+
this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
|
|
302
588
|
numFailedTxs,
|
|
303
589
|
numProposalTxs: txHashes.length,
|
|
304
590
|
numProcessedTxs: block.body.txEffects.length,
|
|
591
|
+
blockNumber,
|
|
305
592
|
slot,
|
|
306
593
|
});
|
|
307
594
|
|
|
@@ -316,11 +603,15 @@ export class BlockProposalHandler {
|
|
|
316
603
|
}
|
|
317
604
|
|
|
318
605
|
// Throw a ReExStateMismatchError error if state updates do not match
|
|
319
|
-
|
|
320
|
-
|
|
606
|
+
// Compare the full block structure (archive and header) from the built block with the proposal
|
|
607
|
+
const archiveMatches = proposal.archive.equals(block.archive.root);
|
|
608
|
+
const headerMatches = proposal.blockHeader.equals(block.header);
|
|
609
|
+
if (!archiveMatches || !headerMatches) {
|
|
321
610
|
this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
|
|
322
|
-
|
|
323
|
-
|
|
611
|
+
expectedArchive: block.archive.root.toString(),
|
|
612
|
+
actualArchive: proposal.archive.toString(),
|
|
613
|
+
expectedHeader: block.header.toInspect(),
|
|
614
|
+
actualHeader: proposal.blockHeader.toInspect(),
|
|
324
615
|
});
|
|
325
616
|
this.metrics?.recordFailedReexecution(proposal);
|
|
326
617
|
throw new ReExStateMismatchError(proposal.archive, block.archive.root);
|