@aztec/validator-client 0.0.1-commit.b655e406 → 0.0.1-commit.b6e433891
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 +27 -15
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +441 -113
- 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 +41 -7
- 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 +107 -31
- 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 -11
- 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 +77 -22
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +529 -66
- package/package.json +24 -14
- package/src/block_proposal_handler.ts +380 -91
- package/src/checkpoint_builder.ts +417 -0
- package/src/config.ts +41 -6
- package/src/duties/validation_service.ts +158 -38
- 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 +705 -98
|
@@ -1,38 +1,46 @@
|
|
|
1
1
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
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';
|
|
2
6
|
import { TimeoutError } from '@aztec/foundation/error';
|
|
3
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
4
7
|
import { createLogger } from '@aztec/foundation/log';
|
|
5
8
|
import { retryUntil } from '@aztec/foundation/retry';
|
|
6
9
|
import { DateProvider, Timer } from '@aztec/foundation/timer';
|
|
7
10
|
import type { P2P, PeerId } from '@aztec/p2p';
|
|
8
|
-
import { TxProvider } from '@aztec/p2p';
|
|
9
11
|
import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
import type {
|
|
14
|
-
import type
|
|
15
|
-
import {
|
|
16
|
-
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';
|
|
16
|
+
import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
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,58 +53,74 @@ 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(),
|
|
73
87
|
telemetry: TelemetryClient = getTelemetryClient(),
|
|
74
88
|
private log = createLogger('validator:block-proposal-handler'),
|
|
75
89
|
) {
|
|
90
|
+
if (config.fishermanMode) {
|
|
91
|
+
this.log = this.log.createChild('[FISHERMAN]');
|
|
92
|
+
}
|
|
76
93
|
this.tracer = telemetry.getTracer('BlockProposalHandler');
|
|
77
94
|
}
|
|
78
95
|
|
|
79
|
-
|
|
80
|
-
|
|
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> => {
|
|
81
100
|
try {
|
|
82
|
-
const
|
|
101
|
+
const { slotNumber, blockNumber } = proposal;
|
|
102
|
+
const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
|
|
83
103
|
if (result.isValid) {
|
|
84
|
-
this.log.info(`Non-validator
|
|
104
|
+
this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
|
|
85
105
|
blockNumber: result.blockNumber,
|
|
106
|
+
slotNumber,
|
|
86
107
|
reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
|
|
87
108
|
totalManaUsed: result.reexecutionResult?.totalManaUsed,
|
|
88
109
|
numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
|
|
110
|
+
reexecuted: shouldReexecute,
|
|
89
111
|
});
|
|
112
|
+
return true;
|
|
90
113
|
} else {
|
|
91
|
-
this.log.warn(
|
|
92
|
-
blockNumber
|
|
93
|
-
reason: result.reason,
|
|
94
|
-
|
|
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;
|
|
95
119
|
}
|
|
96
120
|
} catch (error) {
|
|
97
121
|
this.log.error('Error processing block proposal in non-validator handler', error);
|
|
122
|
+
return false;
|
|
98
123
|
}
|
|
99
|
-
return undefined; // Non-validator nodes don't return attestations
|
|
100
124
|
};
|
|
101
125
|
|
|
102
126
|
p2pClient.registerBlockProposalHandler(handler);
|
|
@@ -108,9 +132,9 @@ export class BlockProposalHandler {
|
|
|
108
132
|
proposalSender: PeerId,
|
|
109
133
|
shouldReexecute: boolean,
|
|
110
134
|
): Promise<BlockProposalValidationResult> {
|
|
111
|
-
const slotNumber = proposal.slotNumber
|
|
135
|
+
const slotNumber = proposal.slotNumber;
|
|
112
136
|
const proposer = proposal.getSender();
|
|
113
|
-
const config = this.
|
|
137
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
114
138
|
|
|
115
139
|
// Reject proposals with invalid signatures
|
|
116
140
|
if (!proposer) {
|
|
@@ -118,7 +142,13 @@ export class BlockProposalHandler {
|
|
|
118
142
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
119
143
|
}
|
|
120
144
|
|
|
121
|
-
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
|
+
|
|
122
152
|
this.log.info(`Processing proposal for slot ${slotNumber}`, {
|
|
123
153
|
...proposalInfo,
|
|
124
154
|
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
@@ -126,23 +156,40 @@ export class BlockProposalHandler {
|
|
|
126
156
|
|
|
127
157
|
// Check that the proposal is from the current proposer, or the next proposer
|
|
128
158
|
// This should have been handled by the p2p layer, but we double check here out of caution
|
|
129
|
-
const
|
|
130
|
-
if (
|
|
159
|
+
const validationResult = await this.blockProposalValidator.validate(proposal);
|
|
160
|
+
if (validationResult.result !== 'accept') {
|
|
131
161
|
this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
|
|
132
162
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
133
163
|
}
|
|
134
164
|
|
|
135
|
-
//
|
|
136
|
-
|
|
137
|
-
|
|
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) {
|
|
138
185
|
this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
|
|
139
186
|
return { isValid: false, reason: 'parent_block_not_found' };
|
|
140
187
|
}
|
|
141
188
|
|
|
142
|
-
// Check that the parent block's slot is
|
|
143
|
-
if (
|
|
144
|
-
this.log.warn(`Parent block slot is greater than
|
|
145
|
-
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(),
|
|
146
193
|
proposalSlot: slotNumber.toString(),
|
|
147
194
|
...proposalInfo,
|
|
148
195
|
});
|
|
@@ -150,7 +197,11 @@ export class BlockProposalHandler {
|
|
|
150
197
|
}
|
|
151
198
|
|
|
152
199
|
// Compute the block number based on the parent block
|
|
153
|
-
const blockNumber =
|
|
200
|
+
const blockNumber =
|
|
201
|
+
parentBlock === 'genesis'
|
|
202
|
+
? BlockNumber(INITIAL_L2_BLOCK_NUM)
|
|
203
|
+
: BlockNumber(parentBlock.header.getBlockNumber() + 1);
|
|
204
|
+
proposalInfo.blockNumber = blockNumber;
|
|
154
205
|
|
|
155
206
|
// Check that this block number does not exist already
|
|
156
207
|
const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
|
|
@@ -166,10 +217,27 @@ export class BlockProposalHandler {
|
|
|
166
217
|
deadline: this.getReexecutionDeadline(slotNumber, config),
|
|
167
218
|
});
|
|
168
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
|
+
|
|
169
237
|
// Check that I have the same set of l1ToL2Messages as the proposal
|
|
170
|
-
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(
|
|
171
|
-
const computedInHash =
|
|
172
|
-
const proposalInHash = proposal.
|
|
238
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
239
|
+
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
240
|
+
const proposalInHash = proposal.inHash;
|
|
173
241
|
if (!computedInHash.equals(proposalInHash)) {
|
|
174
242
|
this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
|
|
175
243
|
proposalInHash: proposalInHash.toString(),
|
|
@@ -185,27 +253,47 @@ export class BlockProposalHandler {
|
|
|
185
253
|
return { isValid: false, blockNumber, reason: 'txs_not_available' };
|
|
186
254
|
}
|
|
187
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
|
+
|
|
188
262
|
// Try re-executing the transactions in the proposal if needed
|
|
189
263
|
let reexecutionResult;
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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 };
|
|
278
|
+
}
|
|
279
|
+
|
|
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);
|
|
199
283
|
}
|
|
200
284
|
|
|
201
|
-
this.log.info(
|
|
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
|
+
|
|
202
290
|
return { isValid: true, blockNumber, reexecutionResult };
|
|
203
291
|
}
|
|
204
292
|
|
|
205
|
-
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' |
|
|
206
|
-
const parentArchive = proposal.
|
|
207
|
-
const slot = proposal.slotNumber
|
|
208
|
-
const config = this.
|
|
293
|
+
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
|
|
294
|
+
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
295
|
+
const slot = proposal.slotNumber;
|
|
296
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
209
297
|
const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
|
|
210
298
|
|
|
211
299
|
if (parentArchive.equals(genesisArchiveRoot)) {
|
|
@@ -218,12 +306,11 @@ export class BlockProposalHandler {
|
|
|
218
306
|
|
|
219
307
|
try {
|
|
220
308
|
return (
|
|
221
|
-
(await this.blockSource.
|
|
309
|
+
(await this.blockSource.getBlockDataByArchive(parentArchive)) ??
|
|
222
310
|
(timeoutDurationMs <= 0
|
|
223
311
|
? undefined
|
|
224
312
|
: await retryUntil(
|
|
225
|
-
() =>
|
|
226
|
-
this.blockSource.syncImmediate().then(() => this.blockSource.getBlockHeaderByArchive(parentArchive)),
|
|
313
|
+
() => this.blockSource.syncImmediate().then(() => this.blockSource.getBlockDataByArchive(parentArchive)),
|
|
227
314
|
'force archiver sync',
|
|
228
315
|
timeoutDurationMs / 1000,
|
|
229
316
|
0.5,
|
|
@@ -239,14 +326,176 @@ export class BlockProposalHandler {
|
|
|
239
326
|
}
|
|
240
327
|
}
|
|
241
328
|
|
|
242
|
-
private
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
+
|
|
452
|
+
private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
|
|
453
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
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
|
+
}
|
|
246
491
|
}
|
|
247
492
|
|
|
248
|
-
private getReexecuteFailureReason(err: any) {
|
|
249
|
-
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) {
|
|
250
499
|
return 'state_mismatch';
|
|
251
500
|
} else if (err instanceof ReExFailedTxsError) {
|
|
252
501
|
return 'failed_txs';
|
|
@@ -259,12 +508,13 @@ export class BlockProposalHandler {
|
|
|
259
508
|
|
|
260
509
|
async reexecuteTransactions(
|
|
261
510
|
proposal: BlockProposal,
|
|
262
|
-
blockNumber:
|
|
511
|
+
blockNumber: BlockNumber,
|
|
512
|
+
checkpointNumber: CheckpointNumber,
|
|
263
513
|
txs: Tx[],
|
|
264
514
|
l1ToL2Messages: Fr[],
|
|
515
|
+
previousCheckpointOutHashes: Fr[],
|
|
265
516
|
): Promise<ReexecuteTransactionsResult> {
|
|
266
|
-
const {
|
|
267
|
-
const { txHashes } = proposal;
|
|
517
|
+
const { blockHeader, txHashes } = proposal;
|
|
268
518
|
|
|
269
519
|
// If we do not have all of the transactions, then we should fail
|
|
270
520
|
if (txs.length !== txHashes.length) {
|
|
@@ -273,32 +523,72 @@ export class BlockProposalHandler {
|
|
|
273
523
|
throw new TransactionsNotAvailableError(missingTxHashes);
|
|
274
524
|
}
|
|
275
525
|
|
|
276
|
-
// Use the sequencer's block building logic to re-execute the transactions
|
|
277
526
|
const timer = new Timer();
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
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 = {
|
|
288
548
|
chainId: new Fr(config.l1ChainId),
|
|
289
549
|
version: new Fr(config.rollupVersion),
|
|
290
|
-
|
|
550
|
+
slotNumber: slot,
|
|
551
|
+
timestamp: blockHeader.globalVariables.timestamp,
|
|
552
|
+
coinbase: blockHeader.globalVariables.coinbase,
|
|
553
|
+
feeRecipient: blockHeader.globalVariables.feeRecipient,
|
|
554
|
+
gasFees: blockHeader.globalVariables.gasFees,
|
|
555
|
+
};
|
|
291
556
|
|
|
292
|
-
|
|
293
|
-
|
|
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,
|
|
294
582
|
});
|
|
295
583
|
|
|
584
|
+
const { block, failedTxs } = result;
|
|
296
585
|
const numFailedTxs = failedTxs.length;
|
|
297
|
-
|
|
298
|
-
this.log.verbose(`
|
|
586
|
+
|
|
587
|
+
this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
|
|
299
588
|
numFailedTxs,
|
|
300
589
|
numProposalTxs: txHashes.length,
|
|
301
590
|
numProcessedTxs: block.body.txEffects.length,
|
|
591
|
+
blockNumber,
|
|
302
592
|
slot,
|
|
303
593
|
});
|
|
304
594
|
|
|
@@ -313,19 +603,18 @@ export class BlockProposalHandler {
|
|
|
313
603
|
}
|
|
314
604
|
|
|
315
605
|
// Throw a ReExStateMismatchError error if state updates do not match
|
|
316
|
-
|
|
317
|
-
|
|
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) {
|
|
318
610
|
this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
|
|
319
|
-
|
|
320
|
-
|
|
611
|
+
expectedArchive: block.archive.root.toString(),
|
|
612
|
+
actualArchive: proposal.archive.toString(),
|
|
613
|
+
expectedHeader: block.header.toInspect(),
|
|
614
|
+
actualHeader: proposal.blockHeader.toInspect(),
|
|
321
615
|
});
|
|
322
616
|
this.metrics?.recordFailedReexecution(proposal);
|
|
323
|
-
throw new ReExStateMismatchError(
|
|
324
|
-
proposal.archive,
|
|
325
|
-
block.archive.root,
|
|
326
|
-
proposal.payload.stateReference,
|
|
327
|
-
block.header.state,
|
|
328
|
-
);
|
|
617
|
+
throw new ReExStateMismatchError(proposal.archive, block.archive.root);
|
|
329
618
|
}
|
|
330
619
|
|
|
331
620
|
const reexecutionTimeMs = timer.ms();
|