@aztec/validator-client 0.0.1-commit.b655e406 → 0.0.1-commit.d1f2d6c
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 +282 -0
- package/dest/block_proposal_handler.d.ts +24 -12
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +345 -86
- package/dest/checkpoint_builder.d.ts +67 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +160 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +17 -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 +113 -31
- package/dest/factory.d.ts +13 -8
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +2 -2
- package/dest/index.d.ts +3 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -0
- package/dest/key_store/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 +1 -1
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +8 -33
- package/dest/tx_validator/index.d.ts +3 -0
- package/dest/tx_validator/index.d.ts.map +1 -0
- package/dest/tx_validator/index.js +2 -0
- package/dest/tx_validator/nullifier_cache.d.ts +14 -0
- package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
- package/dest/tx_validator/nullifier_cache.js +24 -0
- package/dest/tx_validator/tx_validator_factory.d.ts +18 -0
- package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
- package/dest/tx_validator/tx_validator_factory.js +54 -0
- package/dest/validator.d.ts +47 -18
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +365 -63
- package/package.json +22 -14
- package/src/block_proposal_handler.ts +272 -61
- package/src/checkpoint_builder.ts +284 -0
- package/src/config.ts +17 -6
- package/src/duties/validation_service.ts +157 -38
- package/src/factory.ts +17 -8
- package/src/index.ts +2 -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 +7 -34
- package/src/tx_validator/index.ts +2 -0
- package/src/tx_validator/nullifier_cache.ts +30 -0
- package/src/tx_validator/tx_validator_factory.ts +135 -0
- package/src/validator.ts +490 -91
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/validator-client",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.d1f2d6c",
|
|
4
4
|
"main": "dest/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"start": "node --no-warnings ./dest/bin",
|
|
21
|
-
"build": "yarn clean && tsc
|
|
22
|
-
"build:dev": "tsc
|
|
21
|
+
"build": "yarn clean && ../scripts/tsc.sh",
|
|
22
|
+
"build:dev": "../scripts/tsc.sh --watch",
|
|
23
23
|
"clean": "rm -rf ./dest .tsbuildinfo",
|
|
24
24
|
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
|
|
25
25
|
},
|
|
@@ -64,25 +64,33 @@
|
|
|
64
64
|
]
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@aztec/
|
|
68
|
-
"@aztec/
|
|
69
|
-
"@aztec/
|
|
70
|
-
"@aztec/
|
|
71
|
-
"@aztec/
|
|
72
|
-
"@aztec/
|
|
73
|
-
"@aztec/
|
|
74
|
-
"@aztec/
|
|
75
|
-
"@aztec/
|
|
76
|
-
"@aztec/
|
|
67
|
+
"@aztec/blob-client": "0.0.1-commit.d1f2d6c",
|
|
68
|
+
"@aztec/blob-lib": "0.0.1-commit.d1f2d6c",
|
|
69
|
+
"@aztec/constants": "0.0.1-commit.d1f2d6c",
|
|
70
|
+
"@aztec/epoch-cache": "0.0.1-commit.d1f2d6c",
|
|
71
|
+
"@aztec/ethereum": "0.0.1-commit.d1f2d6c",
|
|
72
|
+
"@aztec/foundation": "0.0.1-commit.d1f2d6c",
|
|
73
|
+
"@aztec/node-keystore": "0.0.1-commit.d1f2d6c",
|
|
74
|
+
"@aztec/noir-protocol-circuits-types": "0.0.1-commit.d1f2d6c",
|
|
75
|
+
"@aztec/p2p": "0.0.1-commit.d1f2d6c",
|
|
76
|
+
"@aztec/protocol-contracts": "0.0.1-commit.d1f2d6c",
|
|
77
|
+
"@aztec/prover-client": "0.0.1-commit.d1f2d6c",
|
|
78
|
+
"@aztec/simulator": "0.0.1-commit.d1f2d6c",
|
|
79
|
+
"@aztec/slasher": "0.0.1-commit.d1f2d6c",
|
|
80
|
+
"@aztec/stdlib": "0.0.1-commit.d1f2d6c",
|
|
81
|
+
"@aztec/telemetry-client": "0.0.1-commit.d1f2d6c",
|
|
82
|
+
"@aztec/validator-ha-signer": "0.0.1-commit.d1f2d6c",
|
|
77
83
|
"koa": "^2.16.1",
|
|
78
84
|
"koa-router": "^13.1.1",
|
|
79
85
|
"tslib": "^2.4.0",
|
|
80
|
-
"viem": "npm:@
|
|
86
|
+
"viem": "npm:@aztec/viem@2.38.2"
|
|
81
87
|
},
|
|
82
88
|
"devDependencies": {
|
|
89
|
+
"@electric-sql/pglite": "^0.3.14",
|
|
83
90
|
"@jest/globals": "^30.0.0",
|
|
84
91
|
"@types/jest": "^30.0.0",
|
|
85
92
|
"@types/node": "^22.15.17",
|
|
93
|
+
"@typescript/native-preview": "7.0.0-dev.20260113.1",
|
|
86
94
|
"jest": "^30.0.0",
|
|
87
95
|
"jest-mock-extended": "^4.0.0",
|
|
88
96
|
"ts-node": "^10.9.1",
|
|
@@ -1,19 +1,25 @@
|
|
|
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 { chunkBy } 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
11
|
import { TxProvider } from '@aztec/p2p';
|
|
9
12
|
import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
import
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
import type { L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
14
|
+
import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
15
|
+
import type { ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
16
|
+
import {
|
|
17
|
+
type L1ToL2MessageSource,
|
|
18
|
+
computeCheckpointOutHash,
|
|
19
|
+
computeInHashFromL1ToL2Messages,
|
|
20
|
+
} from '@aztec/stdlib/messaging';
|
|
21
|
+
import type { BlockProposal } from '@aztec/stdlib/p2p';
|
|
22
|
+
import { BlockHeader, type CheckpointGlobalVariables, type FailedTx, type Tx } from '@aztec/stdlib/tx';
|
|
17
23
|
import {
|
|
18
24
|
ReExFailedTxsError,
|
|
19
25
|
ReExStateMismatchError,
|
|
@@ -22,6 +28,7 @@ import {
|
|
|
22
28
|
} from '@aztec/stdlib/validators';
|
|
23
29
|
import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
24
30
|
|
|
31
|
+
import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
25
32
|
import type { ValidatorMetrics } from './metrics.js';
|
|
26
33
|
|
|
27
34
|
export type BlockProposalValidationFailureReason =
|
|
@@ -29,6 +36,7 @@ export type BlockProposalValidationFailureReason =
|
|
|
29
36
|
| 'parent_block_not_found'
|
|
30
37
|
| 'parent_block_wrong_slot'
|
|
31
38
|
| 'in_hash_mismatch'
|
|
39
|
+
| 'global_variables_mismatch'
|
|
32
40
|
| 'block_number_already_exists'
|
|
33
41
|
| 'txs_not_available'
|
|
34
42
|
| 'state_mismatch'
|
|
@@ -45,58 +53,71 @@ 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
81
|
private txProvider: TxProvider,
|
|
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
96
|
registerForReexecution(p2pClient: P2P): BlockProposalHandler {
|
|
80
|
-
|
|
97
|
+
// Non-validator handler that 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
101
|
const result = await this.handleBlockProposal(proposal, proposalSender, true);
|
|
83
102
|
if (result.isValid) {
|
|
84
|
-
this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber
|
|
103
|
+
this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber}`, {
|
|
85
104
|
blockNumber: result.blockNumber,
|
|
86
105
|
reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
|
|
87
106
|
totalManaUsed: result.reexecutionResult?.totalManaUsed,
|
|
88
107
|
numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
|
|
89
108
|
});
|
|
109
|
+
return true;
|
|
90
110
|
} else {
|
|
91
|
-
this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber
|
|
111
|
+
this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
|
|
92
112
|
blockNumber: result.blockNumber,
|
|
93
113
|
reason: result.reason,
|
|
94
114
|
});
|
|
115
|
+
return false;
|
|
95
116
|
}
|
|
96
117
|
} catch (error) {
|
|
97
118
|
this.log.error('Error processing block proposal in non-validator handler', error);
|
|
119
|
+
return false;
|
|
98
120
|
}
|
|
99
|
-
return undefined; // Non-validator nodes don't return attestations
|
|
100
121
|
};
|
|
101
122
|
|
|
102
123
|
p2pClient.registerBlockProposalHandler(handler);
|
|
@@ -108,9 +129,9 @@ export class BlockProposalHandler {
|
|
|
108
129
|
proposalSender: PeerId,
|
|
109
130
|
shouldReexecute: boolean,
|
|
110
131
|
): Promise<BlockProposalValidationResult> {
|
|
111
|
-
const slotNumber = proposal.slotNumber
|
|
132
|
+
const slotNumber = proposal.slotNumber;
|
|
112
133
|
const proposer = proposal.getSender();
|
|
113
|
-
const config = this.
|
|
134
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
114
135
|
|
|
115
136
|
// Reject proposals with invalid signatures
|
|
116
137
|
if (!proposer) {
|
|
@@ -126,8 +147,8 @@ export class BlockProposalHandler {
|
|
|
126
147
|
|
|
127
148
|
// Check that the proposal is from the current proposer, or the next proposer
|
|
128
149
|
// This should have been handled by the p2p layer, but we double check here out of caution
|
|
129
|
-
const
|
|
130
|
-
if (
|
|
150
|
+
const validationResult = await this.blockProposalValidator.validate(proposal);
|
|
151
|
+
if (validationResult.result !== 'accept') {
|
|
131
152
|
this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
|
|
132
153
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
133
154
|
}
|
|
@@ -150,7 +171,10 @@ export class BlockProposalHandler {
|
|
|
150
171
|
}
|
|
151
172
|
|
|
152
173
|
// Compute the block number based on the parent block
|
|
153
|
-
const blockNumber =
|
|
174
|
+
const blockNumber =
|
|
175
|
+
parentBlockHeader === 'genesis'
|
|
176
|
+
? BlockNumber(INITIAL_L2_BLOCK_NUM)
|
|
177
|
+
: BlockNumber(parentBlockHeader.getBlockNumber() + 1);
|
|
154
178
|
|
|
155
179
|
// Check that this block number does not exist already
|
|
156
180
|
const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
|
|
@@ -166,10 +190,17 @@ export class BlockProposalHandler {
|
|
|
166
190
|
deadline: this.getReexecutionDeadline(slotNumber, config),
|
|
167
191
|
});
|
|
168
192
|
|
|
193
|
+
// Compute the checkpoint number for this block and validate checkpoint consistency
|
|
194
|
+
const checkpointResult = await this.computeCheckpointNumber(proposal, parentBlockHeader, proposalInfo);
|
|
195
|
+
if (checkpointResult.reason) {
|
|
196
|
+
return { isValid: false, blockNumber, reason: checkpointResult.reason };
|
|
197
|
+
}
|
|
198
|
+
const checkpointNumber = checkpointResult.checkpointNumber;
|
|
199
|
+
|
|
169
200
|
// 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.
|
|
201
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
202
|
+
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
203
|
+
const proposalInHash = proposal.inHash;
|
|
173
204
|
if (!computedInHash.equals(proposalInHash)) {
|
|
174
205
|
this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
|
|
175
206
|
proposalInHash: proposalInHash.toString(),
|
|
@@ -188,9 +219,28 @@ export class BlockProposalHandler {
|
|
|
188
219
|
// Try re-executing the transactions in the proposal if needed
|
|
189
220
|
let reexecutionResult;
|
|
190
221
|
if (shouldReexecute) {
|
|
222
|
+
// Compute the previous checkpoint out hashes for the epoch.
|
|
223
|
+
// TODO(leila/mbps): There can be a more efficient way to get the previous checkpoint out
|
|
224
|
+
// hashes without having to fetch all the blocks.
|
|
225
|
+
const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
|
|
226
|
+
const checkpointedBlocks = (await this.blockSource.getCheckpointedBlocksForEpoch(epoch))
|
|
227
|
+
.filter(b => b.block.number < blockNumber)
|
|
228
|
+
.sort((a, b) => a.block.number - b.block.number);
|
|
229
|
+
const blocksByCheckpoint = chunkBy(checkpointedBlocks, b => b.checkpointNumber);
|
|
230
|
+
const previousCheckpointOutHashes = blocksByCheckpoint.map(checkpointBlocks =>
|
|
231
|
+
computeCheckpointOutHash(checkpointBlocks.map(b => b.block.body.txEffects.map(tx => tx.l2ToL1Msgs))),
|
|
232
|
+
);
|
|
233
|
+
|
|
191
234
|
try {
|
|
192
235
|
this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
|
|
193
|
-
reexecutionResult = await this.reexecuteTransactions(
|
|
236
|
+
reexecutionResult = await this.reexecuteTransactions(
|
|
237
|
+
proposal,
|
|
238
|
+
blockNumber,
|
|
239
|
+
checkpointNumber,
|
|
240
|
+
txs,
|
|
241
|
+
l1ToL2Messages,
|
|
242
|
+
previousCheckpointOutHashes,
|
|
243
|
+
);
|
|
194
244
|
} catch (error) {
|
|
195
245
|
this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
|
|
196
246
|
const reason = this.getReexecuteFailureReason(error);
|
|
@@ -198,14 +248,23 @@ export class BlockProposalHandler {
|
|
|
198
248
|
}
|
|
199
249
|
}
|
|
200
250
|
|
|
201
|
-
this
|
|
251
|
+
// If we succeeded, push this block into the archiver (unless disabled)
|
|
252
|
+
if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
|
|
253
|
+
await this.blockSource.addBlock(reexecutionResult?.block);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
this.log.info(
|
|
257
|
+
`Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
|
|
258
|
+
proposalInfo,
|
|
259
|
+
);
|
|
260
|
+
|
|
202
261
|
return { isValid: true, blockNumber, reexecutionResult };
|
|
203
262
|
}
|
|
204
263
|
|
|
205
264
|
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockHeader | undefined> {
|
|
206
|
-
const parentArchive = proposal.
|
|
207
|
-
const slot = proposal.slotNumber
|
|
208
|
-
const config = this.
|
|
265
|
+
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
266
|
+
const slot = proposal.slotNumber;
|
|
267
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
209
268
|
const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
|
|
210
269
|
|
|
211
270
|
if (parentArchive.equals(genesisArchiveRoot)) {
|
|
@@ -239,10 +298,142 @@ export class BlockProposalHandler {
|
|
|
239
298
|
}
|
|
240
299
|
}
|
|
241
300
|
|
|
242
|
-
private
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
301
|
+
private async computeCheckpointNumber(
|
|
302
|
+
proposal: BlockProposal,
|
|
303
|
+
parentBlockHeader: 'genesis' | BlockHeader,
|
|
304
|
+
proposalInfo: object,
|
|
305
|
+
): Promise<CheckpointComputationResult> {
|
|
306
|
+
if (parentBlockHeader === 'genesis') {
|
|
307
|
+
// First block is in checkpoint 1
|
|
308
|
+
if (proposal.indexWithinCheckpoint !== 0) {
|
|
309
|
+
this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
|
|
310
|
+
return { reason: 'invalid_proposal' };
|
|
311
|
+
}
|
|
312
|
+
return { checkpointNumber: CheckpointNumber.INITIAL };
|
|
313
|
+
}
|
|
314
|
+
|
|
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.getL2Block(parentBlockNumber);
|
|
320
|
+
if (!parentBlock) {
|
|
321
|
+
this.log.warn(`Parent block ${parentBlockNumber} not found in archiver`, proposalInfo);
|
|
322
|
+
return { reason: 'invalid_proposal' };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (proposal.indexWithinCheckpoint === 0) {
|
|
326
|
+
// If this is the first block in a new checkpoint, increment the checkpoint number
|
|
327
|
+
if (!(proposal.blockHeader.getSlot() > parentBlockHeader.getSlot())) {
|
|
328
|
+
this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
|
|
329
|
+
return { reason: 'invalid_proposal' };
|
|
330
|
+
}
|
|
331
|
+
return { checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1) };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Otherwise it should follow the previous block in the same checkpoint
|
|
335
|
+
if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
|
|
336
|
+
this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
|
|
337
|
+
return { reason: 'invalid_proposal' };
|
|
338
|
+
}
|
|
339
|
+
if (proposal.blockHeader.getSlot() !== parentBlockHeader.getSlot()) {
|
|
340
|
+
this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
|
|
341
|
+
return { reason: 'invalid_proposal' };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
|
|
345
|
+
const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
|
|
346
|
+
if (validationResult) {
|
|
347
|
+
return validationResult;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return { checkpointNumber: parentBlock.checkpointNumber };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Validates that a non-first block in a checkpoint has consistent global variables with its parent.
|
|
355
|
+
* For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
|
|
356
|
+
* @returns A failure result if validation fails, undefined if validation passes
|
|
357
|
+
*/
|
|
358
|
+
private validateNonFirstBlockInCheckpoint(
|
|
359
|
+
proposal: BlockProposal,
|
|
360
|
+
parentBlock: L2Block,
|
|
361
|
+
proposalInfo: object,
|
|
362
|
+
): CheckpointComputationResult | undefined {
|
|
363
|
+
const proposalGlobals = proposal.blockHeader.globalVariables;
|
|
364
|
+
const parentGlobals = parentBlock.header.globalVariables;
|
|
365
|
+
|
|
366
|
+
// All global variables except blockNumber should match the parent
|
|
367
|
+
// blockNumber naturally increments between blocks
|
|
368
|
+
if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
|
|
369
|
+
this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
|
|
370
|
+
...proposalInfo,
|
|
371
|
+
proposalChainId: proposalGlobals.chainId.toString(),
|
|
372
|
+
parentChainId: parentGlobals.chainId.toString(),
|
|
373
|
+
});
|
|
374
|
+
return { reason: 'global_variables_mismatch' };
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (!proposalGlobals.version.equals(parentGlobals.version)) {
|
|
378
|
+
this.log.warn(`Non-first block in checkpoint has mismatched version`, {
|
|
379
|
+
...proposalInfo,
|
|
380
|
+
proposalVersion: proposalGlobals.version.toString(),
|
|
381
|
+
parentVersion: parentGlobals.version.toString(),
|
|
382
|
+
});
|
|
383
|
+
return { reason: 'global_variables_mismatch' };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
|
|
387
|
+
this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
|
|
388
|
+
...proposalInfo,
|
|
389
|
+
proposalSlotNumber: proposalGlobals.slotNumber,
|
|
390
|
+
parentSlotNumber: parentGlobals.slotNumber,
|
|
391
|
+
});
|
|
392
|
+
return { reason: 'global_variables_mismatch' };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
|
|
396
|
+
this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
|
|
397
|
+
...proposalInfo,
|
|
398
|
+
proposalTimestamp: proposalGlobals.timestamp.toString(),
|
|
399
|
+
parentTimestamp: parentGlobals.timestamp.toString(),
|
|
400
|
+
});
|
|
401
|
+
return { reason: 'global_variables_mismatch' };
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
|
|
405
|
+
this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
|
|
406
|
+
...proposalInfo,
|
|
407
|
+
proposalCoinbase: proposalGlobals.coinbase.toString(),
|
|
408
|
+
parentCoinbase: parentGlobals.coinbase.toString(),
|
|
409
|
+
});
|
|
410
|
+
return { reason: 'global_variables_mismatch' };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
|
|
414
|
+
this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
|
|
415
|
+
...proposalInfo,
|
|
416
|
+
proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
|
|
417
|
+
parentFeeRecipient: parentGlobals.feeRecipient.toString(),
|
|
418
|
+
});
|
|
419
|
+
return { reason: 'global_variables_mismatch' };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
|
|
423
|
+
this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
|
|
424
|
+
...proposalInfo,
|
|
425
|
+
proposalGasFees: proposalGlobals.gasFees.toInspect(),
|
|
426
|
+
parentGasFees: parentGlobals.gasFees.toInspect(),
|
|
427
|
+
});
|
|
428
|
+
return { reason: 'global_variables_mismatch' };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return undefined;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
|
|
435
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
436
|
+
return new Date(nextSlotTimestampSeconds * 1000);
|
|
246
437
|
}
|
|
247
438
|
|
|
248
439
|
private getReexecuteFailureReason(err: any) {
|
|
@@ -259,12 +450,13 @@ export class BlockProposalHandler {
|
|
|
259
450
|
|
|
260
451
|
async reexecuteTransactions(
|
|
261
452
|
proposal: BlockProposal,
|
|
262
|
-
blockNumber:
|
|
453
|
+
blockNumber: BlockNumber,
|
|
454
|
+
checkpointNumber: CheckpointNumber,
|
|
263
455
|
txs: Tx[],
|
|
264
456
|
l1ToL2Messages: Fr[],
|
|
457
|
+
previousCheckpointOutHashes: Fr[],
|
|
265
458
|
): Promise<ReexecuteTransactionsResult> {
|
|
266
|
-
const {
|
|
267
|
-
const { txHashes } = proposal;
|
|
459
|
+
const { blockHeader, txHashes } = proposal;
|
|
268
460
|
|
|
269
461
|
// If we do not have all of the transactions, then we should fail
|
|
270
462
|
if (txs.length !== txHashes.length) {
|
|
@@ -273,28 +465,48 @@ export class BlockProposalHandler {
|
|
|
273
465
|
throw new TransactionsNotAvailableError(missingTxHashes);
|
|
274
466
|
}
|
|
275
467
|
|
|
276
|
-
// Use the sequencer's block building logic to re-execute the transactions
|
|
277
468
|
const timer = new Timer();
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
469
|
+
const slot = proposal.slotNumber;
|
|
470
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
471
|
+
|
|
472
|
+
// Get prior blocks in this checkpoint (same slot before current block)
|
|
473
|
+
const allBlocksInSlot = await this.blockSource.getBlocksForSlot(slot);
|
|
474
|
+
const priorBlocks = allBlocksInSlot.filter(b => b.number < blockNumber && b.header.getSlot() === slot);
|
|
475
|
+
|
|
476
|
+
// Fork before the block to be built
|
|
477
|
+
const parentBlockNumber = BlockNumber(blockNumber - 1);
|
|
478
|
+
using fork = await this.worldState.fork(parentBlockNumber);
|
|
479
|
+
|
|
480
|
+
// Build checkpoint constants from proposal (excludes blockNumber and timestamp which are per-block)
|
|
481
|
+
const constants: CheckpointGlobalVariables = {
|
|
288
482
|
chainId: new Fr(config.l1ChainId),
|
|
289
483
|
version: new Fr(config.rollupVersion),
|
|
290
|
-
|
|
484
|
+
slotNumber: slot,
|
|
485
|
+
coinbase: blockHeader.globalVariables.coinbase,
|
|
486
|
+
feeRecipient: blockHeader.globalVariables.feeRecipient,
|
|
487
|
+
gasFees: blockHeader.globalVariables.gasFees,
|
|
488
|
+
};
|
|
291
489
|
|
|
292
|
-
|
|
293
|
-
|
|
490
|
+
// Create checkpoint builder with prior blocks
|
|
491
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
492
|
+
checkpointNumber,
|
|
493
|
+
constants,
|
|
494
|
+
l1ToL2Messages,
|
|
495
|
+
previousCheckpointOutHashes,
|
|
496
|
+
fork,
|
|
497
|
+
priorBlocks,
|
|
498
|
+
);
|
|
499
|
+
|
|
500
|
+
// Build the new block
|
|
501
|
+
const deadline = this.getReexecutionDeadline(slot, config);
|
|
502
|
+
const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
|
|
503
|
+
deadline,
|
|
504
|
+
expectedEndState: blockHeader.state,
|
|
294
505
|
});
|
|
295
506
|
|
|
507
|
+
const { block, failedTxs } = result;
|
|
296
508
|
const numFailedTxs = failedTxs.length;
|
|
297
|
-
|
|
509
|
+
|
|
298
510
|
this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
|
|
299
511
|
numFailedTxs,
|
|
300
512
|
numProposalTxs: txHashes.length,
|
|
@@ -313,19 +525,18 @@ export class BlockProposalHandler {
|
|
|
313
525
|
}
|
|
314
526
|
|
|
315
527
|
// Throw a ReExStateMismatchError error if state updates do not match
|
|
316
|
-
|
|
317
|
-
|
|
528
|
+
// Compare the full block structure (archive and header) from the built block with the proposal
|
|
529
|
+
const archiveMatches = proposal.archive.equals(block.archive.root);
|
|
530
|
+
const headerMatches = proposal.blockHeader.equals(block.header);
|
|
531
|
+
if (!archiveMatches || !headerMatches) {
|
|
318
532
|
this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
|
|
319
|
-
|
|
320
|
-
|
|
533
|
+
expectedArchive: block.archive.root.toString(),
|
|
534
|
+
actualArchive: proposal.archive.toString(),
|
|
535
|
+
expectedHeader: block.header.toInspect(),
|
|
536
|
+
actualHeader: proposal.blockHeader.toInspect(),
|
|
321
537
|
});
|
|
322
538
|
this.metrics?.recordFailedReexecution(proposal);
|
|
323
|
-
throw new ReExStateMismatchError(
|
|
324
|
-
proposal.archive,
|
|
325
|
-
block.archive.root,
|
|
326
|
-
proposal.payload.stateReference,
|
|
327
|
-
block.header.state,
|
|
328
|
-
);
|
|
539
|
+
throw new ReExStateMismatchError(proposal.archive, block.archive.root);
|
|
329
540
|
}
|
|
330
541
|
|
|
331
542
|
const reexecutionTimeMs = timer.ms();
|