@aztec/validator-client 0.0.1-commit.1142ef1 → 0.0.1-commit.125b3452
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 +96 -24
- package/dest/block_proposal_handler.d.ts +12 -11
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +132 -77
- package/dest/checkpoint_builder.d.ts +32 -25
- package/dest/checkpoint_builder.d.ts.map +1 -1
- package/dest/checkpoint_builder.js +144 -49
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +33 -14
- package/dest/duties/validation_service.d.ts +20 -7
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +69 -22
- package/dest/factory.d.ts +2 -2
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +3 -2
- package/dest/index.d.ts +1 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +0 -1
- package/dest/key_store/ha_key_store.d.ts +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 +8 -4
- 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 +8 -4
- package/dest/metrics.d.ts +12 -3
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +46 -5
- package/dest/validator.d.ts +45 -18
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +263 -99
- package/package.json +21 -17
- package/src/block_proposal_handler.ts +163 -95
- package/src/checkpoint_builder.ts +206 -61
- package/src/config.ts +32 -13
- package/src/duties/validation_service.ts +94 -25
- package/src/factory.ts +2 -0
- package/src/index.ts +0 -1
- 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 +13 -4
- package/src/key_store/node_keystore_adapter.ts +27 -4
- package/src/key_store/web3signer_key_store.ts +17 -4
- package/src/metrics.ts +63 -6
- package/src/validator.ts +329 -118
- package/dest/tx_validator/index.d.ts +0 -3
- package/dest/tx_validator/index.d.ts.map +0 -1
- package/dest/tx_validator/index.js +0 -2
- package/dest/tx_validator/nullifier_cache.d.ts +0 -14
- package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
- package/dest/tx_validator/nullifier_cache.js +0 -24
- package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
- package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
- package/dest/tx_validator/tx_validator_factory.js +0 -53
- package/src/tx_validator/index.ts +0 -2
- package/src/tx_validator/nullifier_cache.ts +0 -30
- package/src/tx_validator/tx_validator_factory.ts +0 -133
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.125b3452",
|
|
4
4
|
"main": "dest/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -64,31 +64,35 @@
|
|
|
64
64
|
]
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@aztec/blob-client": "0.0.1-commit.
|
|
68
|
-
"@aztec/blob-lib": "0.0.1-commit.
|
|
69
|
-
"@aztec/constants": "0.0.1-commit.
|
|
70
|
-
"@aztec/epoch-cache": "0.0.1-commit.
|
|
71
|
-
"@aztec/ethereum": "0.0.1-commit.
|
|
72
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
73
|
-
"@aztec/node-keystore": "0.0.1-commit.
|
|
74
|
-
"@aztec/noir-protocol-circuits-types": "0.0.1-commit.
|
|
75
|
-
"@aztec/p2p": "0.0.1-commit.
|
|
76
|
-
"@aztec/protocol-contracts": "0.0.1-commit.
|
|
77
|
-
"@aztec/prover-client": "0.0.1-commit.
|
|
78
|
-
"@aztec/simulator": "0.0.1-commit.
|
|
79
|
-
"@aztec/slasher": "0.0.1-commit.
|
|
80
|
-
"@aztec/stdlib": "0.0.1-commit.
|
|
81
|
-
"@aztec/telemetry-client": "0.0.1-commit.
|
|
67
|
+
"@aztec/blob-client": "0.0.1-commit.125b3452",
|
|
68
|
+
"@aztec/blob-lib": "0.0.1-commit.125b3452",
|
|
69
|
+
"@aztec/constants": "0.0.1-commit.125b3452",
|
|
70
|
+
"@aztec/epoch-cache": "0.0.1-commit.125b3452",
|
|
71
|
+
"@aztec/ethereum": "0.0.1-commit.125b3452",
|
|
72
|
+
"@aztec/foundation": "0.0.1-commit.125b3452",
|
|
73
|
+
"@aztec/node-keystore": "0.0.1-commit.125b3452",
|
|
74
|
+
"@aztec/noir-protocol-circuits-types": "0.0.1-commit.125b3452",
|
|
75
|
+
"@aztec/p2p": "0.0.1-commit.125b3452",
|
|
76
|
+
"@aztec/protocol-contracts": "0.0.1-commit.125b3452",
|
|
77
|
+
"@aztec/prover-client": "0.0.1-commit.125b3452",
|
|
78
|
+
"@aztec/simulator": "0.0.1-commit.125b3452",
|
|
79
|
+
"@aztec/slasher": "0.0.1-commit.125b3452",
|
|
80
|
+
"@aztec/stdlib": "0.0.1-commit.125b3452",
|
|
81
|
+
"@aztec/telemetry-client": "0.0.1-commit.125b3452",
|
|
82
|
+
"@aztec/validator-ha-signer": "0.0.1-commit.125b3452",
|
|
82
83
|
"koa": "^2.16.1",
|
|
83
84
|
"koa-router": "^13.1.1",
|
|
84
85
|
"tslib": "^2.4.0",
|
|
85
86
|
"viem": "npm:@aztec/viem@2.38.2"
|
|
86
87
|
},
|
|
87
88
|
"devDependencies": {
|
|
89
|
+
"@aztec/archiver": "0.0.1-commit.125b3452",
|
|
90
|
+
"@aztec/world-state": "0.0.1-commit.125b3452",
|
|
91
|
+
"@electric-sql/pglite": "^0.3.14",
|
|
88
92
|
"@jest/globals": "^30.0.0",
|
|
89
93
|
"@types/jest": "^30.0.0",
|
|
90
94
|
"@types/node": "^22.15.17",
|
|
91
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
95
|
+
"@typescript/native-preview": "7.0.0-dev.20260113.1",
|
|
92
96
|
"jest": "^30.0.0",
|
|
93
97
|
"jest-mock-extended": "^4.0.0",
|
|
94
98
|
"ts-node": "^10.9.1",
|
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import type { EpochCache } from '@aztec/epoch-cache';
|
|
2
3
|
import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
|
+
import { pick } from '@aztec/foundation/collection';
|
|
3
5
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
6
|
import { TimeoutError } from '@aztec/foundation/error';
|
|
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 {
|
|
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
17
|
import type { BlockProposal } from '@aztec/stdlib/p2p';
|
|
16
|
-
import {
|
|
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,
|
|
@@ -28,6 +32,7 @@ import type { ValidatorMetrics } from './metrics.js';
|
|
|
28
32
|
export type BlockProposalValidationFailureReason =
|
|
29
33
|
| 'invalid_proposal'
|
|
30
34
|
| 'parent_block_not_found'
|
|
35
|
+
| 'block_source_not_synced'
|
|
31
36
|
| 'parent_block_wrong_slot'
|
|
32
37
|
| 'in_hash_mismatch'
|
|
33
38
|
| 'global_variables_mismatch'
|
|
@@ -35,11 +40,12 @@ export type BlockProposalValidationFailureReason =
|
|
|
35
40
|
| 'txs_not_available'
|
|
36
41
|
| 'state_mismatch'
|
|
37
42
|
| 'failed_txs'
|
|
43
|
+
| 'initial_state_mismatch'
|
|
38
44
|
| 'timeout'
|
|
39
45
|
| 'unknown_error';
|
|
40
46
|
|
|
41
47
|
type ReexecuteTransactionsResult = {
|
|
42
|
-
block:
|
|
48
|
+
block: L2Block;
|
|
43
49
|
failedTxs: FailedTx[];
|
|
44
50
|
reexecutionTimeMs: number;
|
|
45
51
|
totalManaUsed: number;
|
|
@@ -72,8 +78,9 @@ export class BlockProposalHandler {
|
|
|
72
78
|
private worldState: WorldStateSynchronizer,
|
|
73
79
|
private blockSource: L2BlockSource & L2BlockSink,
|
|
74
80
|
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
75
|
-
private txProvider:
|
|
81
|
+
private txProvider: ITxProvider,
|
|
76
82
|
private blockProposalValidator: BlockProposalValidator,
|
|
83
|
+
private epochCache: EpochCache,
|
|
77
84
|
private config: ValidatorClientFullConfig,
|
|
78
85
|
private metrics?: ValidatorMetrics,
|
|
79
86
|
private dateProvider: DateProvider = new DateProvider(),
|
|
@@ -86,25 +93,28 @@ export class BlockProposalHandler {
|
|
|
86
93
|
this.tracer = telemetry.getTracer('BlockProposalHandler');
|
|
87
94
|
}
|
|
88
95
|
|
|
89
|
-
|
|
90
|
-
// Non-validator handler that re-executes for monitoring but does not attest.
|
|
96
|
+
register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
|
|
97
|
+
// Non-validator handler that processes or re-executes for monitoring but does not attest.
|
|
91
98
|
// Returns boolean indicating whether the proposal was valid.
|
|
92
99
|
const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
|
|
93
100
|
try {
|
|
94
|
-
const
|
|
101
|
+
const { slotNumber, blockNumber } = proposal;
|
|
102
|
+
const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
|
|
95
103
|
if (result.isValid) {
|
|
96
|
-
this.log.info(`Non-validator
|
|
104
|
+
this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
|
|
97
105
|
blockNumber: result.blockNumber,
|
|
106
|
+
slotNumber,
|
|
98
107
|
reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
|
|
99
108
|
totalManaUsed: result.reexecutionResult?.totalManaUsed,
|
|
100
109
|
numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
|
|
110
|
+
reexecuted: shouldReexecute,
|
|
101
111
|
});
|
|
102
112
|
return true;
|
|
103
113
|
} else {
|
|
104
|
-
this.log.warn(
|
|
105
|
-
blockNumber
|
|
106
|
-
reason: result.reason,
|
|
107
|
-
|
|
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
|
+
);
|
|
108
118
|
return false;
|
|
109
119
|
}
|
|
110
120
|
} catch (error) {
|
|
@@ -132,7 +142,13 @@ export class BlockProposalHandler {
|
|
|
132
142
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
133
143
|
}
|
|
134
144
|
|
|
135
|
-
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
|
+
|
|
136
152
|
this.log.info(`Processing proposal for slot ${slotNumber}`, {
|
|
137
153
|
...proposalInfo,
|
|
138
154
|
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
@@ -140,23 +156,36 @@ export class BlockProposalHandler {
|
|
|
140
156
|
|
|
141
157
|
// Check that the proposal is from the current proposer, or the next proposer
|
|
142
158
|
// This should have been handled by the p2p layer, but we double check here out of caution
|
|
143
|
-
const
|
|
144
|
-
if (
|
|
159
|
+
const validationResult = await this.blockProposalValidator.validate(proposal);
|
|
160
|
+
if (validationResult.result !== 'accept') {
|
|
145
161
|
this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
|
|
146
162
|
return { isValid: false, reason: 'invalid_proposal' };
|
|
147
163
|
}
|
|
148
164
|
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
|
|
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
|
+
// TODO(@Maddiaa0): This may break staggered slots.
|
|
170
|
+
const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
|
|
171
|
+
if (!blockSourceSync) {
|
|
172
|
+
this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
|
|
173
|
+
return { isValid: false, reason: 'block_source_not_synced' };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Check that the parent proposal is a block we know, otherwise reexecution would fail.
|
|
177
|
+
// If we don't find it immediately, we keep retrying for a while; it may be we still
|
|
178
|
+
// need to process other block proposals to get to it.
|
|
179
|
+
const parentBlock = await this.getParentBlock(proposal);
|
|
180
|
+
if (parentBlock === undefined) {
|
|
152
181
|
this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
|
|
153
182
|
return { isValid: false, reason: 'parent_block_not_found' };
|
|
154
183
|
}
|
|
155
184
|
|
|
156
|
-
// Check that the parent block's slot is
|
|
157
|
-
if (
|
|
158
|
-
this.log.warn(`Parent block slot is greater than
|
|
159
|
-
parentBlockSlot:
|
|
185
|
+
// Check that the parent block's slot is not greater than the proposal's slot.
|
|
186
|
+
if (parentBlock !== 'genesis' && parentBlock.header.getSlot() > slotNumber) {
|
|
187
|
+
this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
|
|
188
|
+
parentBlockSlot: parentBlock.header.getSlot().toString(),
|
|
160
189
|
proposalSlot: slotNumber.toString(),
|
|
161
190
|
...proposalInfo,
|
|
162
191
|
});
|
|
@@ -165,9 +194,10 @@ export class BlockProposalHandler {
|
|
|
165
194
|
|
|
166
195
|
// Compute the block number based on the parent block
|
|
167
196
|
const blockNumber =
|
|
168
|
-
|
|
197
|
+
parentBlock === 'genesis'
|
|
169
198
|
? BlockNumber(INITIAL_L2_BLOCK_NUM)
|
|
170
|
-
: BlockNumber(
|
|
199
|
+
: BlockNumber(parentBlock.header.getBlockNumber() + 1);
|
|
200
|
+
proposalInfo.blockNumber = blockNumber;
|
|
171
201
|
|
|
172
202
|
// Check that this block number does not exist already
|
|
173
203
|
const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
|
|
@@ -183,12 +213,22 @@ export class BlockProposalHandler {
|
|
|
183
213
|
deadline: this.getReexecutionDeadline(slotNumber, config),
|
|
184
214
|
});
|
|
185
215
|
|
|
216
|
+
// If reexecution is disabled, bail. We were just interested in triggering tx collection.
|
|
217
|
+
if (!shouldReexecute) {
|
|
218
|
+
this.log.info(
|
|
219
|
+
`Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
|
|
220
|
+
proposalInfo,
|
|
221
|
+
);
|
|
222
|
+
return { isValid: true, blockNumber };
|
|
223
|
+
}
|
|
224
|
+
|
|
186
225
|
// Compute the checkpoint number for this block and validate checkpoint consistency
|
|
187
|
-
const checkpointResult =
|
|
226
|
+
const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
|
|
188
227
|
if (checkpointResult.reason) {
|
|
189
228
|
return { isValid: false, blockNumber, reason: checkpointResult.reason };
|
|
190
229
|
}
|
|
191
230
|
const checkpointNumber = checkpointResult.checkpointNumber;
|
|
231
|
+
proposalInfo.checkpointNumber = checkpointNumber;
|
|
192
232
|
|
|
193
233
|
// Check that I have the same set of l1ToL2Messages as the proposal
|
|
194
234
|
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
@@ -209,40 +249,44 @@ export class BlockProposalHandler {
|
|
|
209
249
|
return { isValid: false, blockNumber, reason: 'txs_not_available' };
|
|
210
250
|
}
|
|
211
251
|
|
|
252
|
+
// Collect the out hashes of all the checkpoints before this one in the same epoch
|
|
253
|
+
const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
|
|
254
|
+
const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
|
|
255
|
+
.filter(c => c.checkpointNumber < checkpointNumber)
|
|
256
|
+
.map(c => c.checkpointOutHash);
|
|
257
|
+
|
|
212
258
|
// Try re-executing the transactions in the proposal if needed
|
|
213
259
|
let reexecutionResult;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
260
|
+
try {
|
|
261
|
+
this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
|
|
262
|
+
reexecutionResult = await this.reexecuteTransactions(
|
|
263
|
+
proposal,
|
|
264
|
+
blockNumber,
|
|
265
|
+
checkpointNumber,
|
|
266
|
+
txs,
|
|
267
|
+
l1ToL2Messages,
|
|
268
|
+
previousCheckpointOutHashes,
|
|
269
|
+
);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
|
|
272
|
+
const reason = this.getReexecuteFailureReason(error);
|
|
273
|
+
return { isValid: false, blockNumber, reason, reexecutionResult };
|
|
229
274
|
}
|
|
230
275
|
|
|
231
276
|
// If we succeeded, push this block into the archiver (unless disabled)
|
|
232
|
-
// TODO(palla/mbps): Change default to false once block sync is stable.
|
|
233
277
|
if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
|
|
234
278
|
await this.blockSource.addBlock(reexecutionResult?.block);
|
|
235
279
|
}
|
|
236
280
|
|
|
237
281
|
this.log.info(
|
|
238
|
-
`Successfully
|
|
239
|
-
proposalInfo,
|
|
282
|
+
`Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
|
|
283
|
+
{ ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
|
|
240
284
|
);
|
|
241
285
|
|
|
242
286
|
return { isValid: true, blockNumber, reexecutionResult };
|
|
243
287
|
}
|
|
244
288
|
|
|
245
|
-
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' |
|
|
289
|
+
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
|
|
246
290
|
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
247
291
|
const slot = proposal.slotNumber;
|
|
248
292
|
const config = this.checkpointsBuilder.getConfig();
|
|
@@ -258,12 +302,11 @@ export class BlockProposalHandler {
|
|
|
258
302
|
|
|
259
303
|
try {
|
|
260
304
|
return (
|
|
261
|
-
(await this.blockSource.
|
|
305
|
+
(await this.blockSource.getBlockDataByArchive(parentArchive)) ??
|
|
262
306
|
(timeoutDurationMs <= 0
|
|
263
307
|
? undefined
|
|
264
308
|
: await retryUntil(
|
|
265
|
-
() =>
|
|
266
|
-
this.blockSource.syncImmediate().then(() => this.blockSource.getBlockHeaderByArchive(parentArchive)),
|
|
309
|
+
() => this.blockSource.syncImmediate().then(() => this.blockSource.getBlockDataByArchive(parentArchive)),
|
|
267
310
|
'force archiver sync',
|
|
268
311
|
timeoutDurationMs / 1000,
|
|
269
312
|
0.5,
|
|
@@ -279,12 +322,12 @@ export class BlockProposalHandler {
|
|
|
279
322
|
}
|
|
280
323
|
}
|
|
281
324
|
|
|
282
|
-
private
|
|
325
|
+
private computeCheckpointNumber(
|
|
283
326
|
proposal: BlockProposal,
|
|
284
|
-
|
|
327
|
+
parentBlock: 'genesis' | BlockData,
|
|
285
328
|
proposalInfo: object,
|
|
286
|
-
):
|
|
287
|
-
if (
|
|
329
|
+
): CheckpointComputationResult {
|
|
330
|
+
if (parentBlock === 'genesis') {
|
|
288
331
|
// First block is in checkpoint 1
|
|
289
332
|
if (proposal.indexWithinCheckpoint !== 0) {
|
|
290
333
|
this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
|
|
@@ -293,19 +336,9 @@ export class BlockProposalHandler {
|
|
|
293
336
|
return { checkpointNumber: CheckpointNumber.INITIAL };
|
|
294
337
|
}
|
|
295
338
|
|
|
296
|
-
// Get the parent block to find its checkpoint number
|
|
297
|
-
// TODO(palla/mbps): The block header should include the checkpoint number to avoid this lookup,
|
|
298
|
-
// or at least the L2BlockSource should return a different struct that includes it.
|
|
299
|
-
const parentBlockNumber = parentBlockHeader.getBlockNumber();
|
|
300
|
-
const parentBlock = await this.blockSource.getL2BlockNew(parentBlockNumber);
|
|
301
|
-
if (!parentBlock) {
|
|
302
|
-
this.log.warn(`Parent block ${parentBlockNumber} not found in archiver`, proposalInfo);
|
|
303
|
-
return { reason: 'invalid_proposal' };
|
|
304
|
-
}
|
|
305
|
-
|
|
306
339
|
if (proposal.indexWithinCheckpoint === 0) {
|
|
307
340
|
// If this is the first block in a new checkpoint, increment the checkpoint number
|
|
308
|
-
if (!(proposal.blockHeader.getSlot() >
|
|
341
|
+
if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
|
|
309
342
|
this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
|
|
310
343
|
return { reason: 'invalid_proposal' };
|
|
311
344
|
}
|
|
@@ -317,7 +350,7 @@ export class BlockProposalHandler {
|
|
|
317
350
|
this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
|
|
318
351
|
return { reason: 'invalid_proposal' };
|
|
319
352
|
}
|
|
320
|
-
if (proposal.blockHeader.getSlot() !==
|
|
353
|
+
if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
|
|
321
354
|
this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
|
|
322
355
|
return { reason: 'invalid_proposal' };
|
|
323
356
|
}
|
|
@@ -338,7 +371,7 @@ export class BlockProposalHandler {
|
|
|
338
371
|
*/
|
|
339
372
|
private validateNonFirstBlockInCheckpoint(
|
|
340
373
|
proposal: BlockProposal,
|
|
341
|
-
parentBlock:
|
|
374
|
+
parentBlock: BlockData,
|
|
342
375
|
proposalInfo: object,
|
|
343
376
|
): CheckpointComputationResult | undefined {
|
|
344
377
|
const proposalGlobals = proposal.blockHeader.globalVariables;
|
|
@@ -414,35 +447,49 @@ export class BlockProposalHandler {
|
|
|
414
447
|
|
|
415
448
|
private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
|
|
416
449
|
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
417
|
-
|
|
418
|
-
return new Date(nextSlotTimestampSeconds * 1000 - msNeededForPropagationAndPublishing);
|
|
450
|
+
return new Date(nextSlotTimestampSeconds * 1000);
|
|
419
451
|
}
|
|
420
452
|
|
|
421
|
-
/**
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
slot
|
|
426
|
-
|
|
427
|
-
checkpointNumber: CheckpointNumber,
|
|
428
|
-
): Promise<L2BlockNew[]> {
|
|
429
|
-
const blocks: L2BlockNew[] = [];
|
|
430
|
-
let currentBlockNumber = BlockNumber(upToBlockNumber - 1);
|
|
431
|
-
|
|
432
|
-
while (currentBlockNumber >= INITIAL_L2_BLOCK_NUM) {
|
|
433
|
-
const block = await this.blockSource.getL2BlockNew(currentBlockNumber);
|
|
434
|
-
if (!block || block.header.getSlot() !== slot || block.checkpointNumber !== checkpointNumber) {
|
|
435
|
-
break;
|
|
436
|
-
}
|
|
437
|
-
blocks.unshift(block);
|
|
438
|
-
currentBlockNumber = BlockNumber(currentBlockNumber - 1);
|
|
453
|
+
/** Waits for the block source to sync L1 data up to at least the slot before the given one. */
|
|
454
|
+
private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
|
|
455
|
+
const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
|
|
456
|
+
const timeoutMs = deadline.getTime() - this.dateProvider.now();
|
|
457
|
+
if (slot === 0) {
|
|
458
|
+
return true;
|
|
439
459
|
}
|
|
440
460
|
|
|
441
|
-
|
|
461
|
+
// Make a quick check before triggering an archiver sync
|
|
462
|
+
const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
|
|
463
|
+
if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
|
|
464
|
+
return true;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
try {
|
|
468
|
+
// Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
|
|
469
|
+
return await retryUntil(
|
|
470
|
+
async () => {
|
|
471
|
+
await this.blockSource.syncImmediate();
|
|
472
|
+
const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
|
|
473
|
+
return syncedSlot !== undefined && syncedSlot + 1 >= slot;
|
|
474
|
+
},
|
|
475
|
+
'wait for block source sync',
|
|
476
|
+
timeoutMs / 1000,
|
|
477
|
+
0.5,
|
|
478
|
+
);
|
|
479
|
+
} catch (err) {
|
|
480
|
+
if (err instanceof TimeoutError) {
|
|
481
|
+
this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
|
|
482
|
+
return false;
|
|
483
|
+
} else {
|
|
484
|
+
throw err;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
442
487
|
}
|
|
443
488
|
|
|
444
|
-
private getReexecuteFailureReason(err: any) {
|
|
445
|
-
if (err instanceof
|
|
489
|
+
private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
|
|
490
|
+
if (err instanceof ReExInitialStateMismatchError) {
|
|
491
|
+
return 'initial_state_mismatch';
|
|
492
|
+
} else if (err instanceof ReExStateMismatchError) {
|
|
446
493
|
return 'state_mismatch';
|
|
447
494
|
} else if (err instanceof ReExFailedTxsError) {
|
|
448
495
|
return 'failed_txs';
|
|
@@ -459,6 +506,7 @@ export class BlockProposalHandler {
|
|
|
459
506
|
checkpointNumber: CheckpointNumber,
|
|
460
507
|
txs: Tx[],
|
|
461
508
|
l1ToL2Messages: Fr[],
|
|
509
|
+
previousCheckpointOutHashes: Fr[],
|
|
462
510
|
): Promise<ReexecuteTransactionsResult> {
|
|
463
511
|
const { blockHeader, txHashes } = proposal;
|
|
464
512
|
|
|
@@ -473,18 +521,28 @@ export class BlockProposalHandler {
|
|
|
473
521
|
const slot = proposal.slotNumber;
|
|
474
522
|
const config = this.checkpointsBuilder.getConfig();
|
|
475
523
|
|
|
476
|
-
// Get prior blocks in this checkpoint (same slot
|
|
477
|
-
const
|
|
524
|
+
// Get prior blocks in this checkpoint (same slot before current block)
|
|
525
|
+
const allBlocksInSlot = await this.blockSource.getBlocksForSlot(slot);
|
|
526
|
+
const priorBlocks = allBlocksInSlot.filter(b => b.number < blockNumber && b.header.getSlot() === slot);
|
|
478
527
|
|
|
479
528
|
// Fork before the block to be built
|
|
480
529
|
const parentBlockNumber = BlockNumber(blockNumber - 1);
|
|
481
|
-
|
|
530
|
+
await this.worldState.syncImmediate(parentBlockNumber);
|
|
531
|
+
await using fork = await this.worldState.fork(parentBlockNumber);
|
|
532
|
+
|
|
533
|
+
// Verify the fork's archive root matches the proposal's expected last archive.
|
|
534
|
+
// If they don't match, our world state synced to a different chain and reexecution would fail.
|
|
535
|
+
const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
|
|
536
|
+
if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
|
|
537
|
+
throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
|
|
538
|
+
}
|
|
482
539
|
|
|
483
|
-
// Build checkpoint constants from proposal (excludes blockNumber
|
|
540
|
+
// Build checkpoint constants from proposal (excludes blockNumber which is per-block)
|
|
484
541
|
const constants: CheckpointGlobalVariables = {
|
|
485
542
|
chainId: new Fr(config.l1ChainId),
|
|
486
543
|
version: new Fr(config.rollupVersion),
|
|
487
544
|
slotNumber: slot,
|
|
545
|
+
timestamp: blockHeader.globalVariables.timestamp,
|
|
488
546
|
coinbase: blockHeader.globalVariables.coinbase,
|
|
489
547
|
feeRecipient: blockHeader.globalVariables.feeRecipient,
|
|
490
548
|
gasFees: blockHeader.globalVariables.gasFees,
|
|
@@ -494,25 +552,35 @@ export class BlockProposalHandler {
|
|
|
494
552
|
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
495
553
|
checkpointNumber,
|
|
496
554
|
constants,
|
|
555
|
+
0n, // only takes effect in the following checkpoint.
|
|
497
556
|
l1ToL2Messages,
|
|
557
|
+
previousCheckpointOutHashes,
|
|
498
558
|
fork,
|
|
499
559
|
priorBlocks,
|
|
560
|
+
this.log.getBindings(),
|
|
500
561
|
);
|
|
501
562
|
|
|
502
563
|
// Build the new block
|
|
503
564
|
const deadline = this.getReexecutionDeadline(slot, config);
|
|
565
|
+
const maxBlockGas =
|
|
566
|
+
this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
|
|
567
|
+
? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
|
|
568
|
+
: undefined;
|
|
504
569
|
const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
|
|
505
570
|
deadline,
|
|
506
571
|
expectedEndState: blockHeader.state,
|
|
572
|
+
maxTransactions: this.config.validateMaxTxsPerBlock,
|
|
573
|
+
maxBlockGas,
|
|
507
574
|
});
|
|
508
575
|
|
|
509
576
|
const { block, failedTxs } = result;
|
|
510
577
|
const numFailedTxs = failedTxs.length;
|
|
511
578
|
|
|
512
|
-
this.log.verbose(`
|
|
579
|
+
this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
|
|
513
580
|
numFailedTxs,
|
|
514
581
|
numProposalTxs: txHashes.length,
|
|
515
582
|
numProcessedTxs: block.body.txEffects.length,
|
|
583
|
+
blockNumber,
|
|
516
584
|
slot,
|
|
517
585
|
});
|
|
518
586
|
|