@aztec/validator-client 0.0.0-test.0 → 0.0.1-commit.0208eb9
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 +63 -0
- package/dest/block_proposal_handler.d.ts.map +1 -0
- package/dest/block_proposal_handler.js +546 -0
- package/dest/checkpoint_builder.d.ts +66 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +173 -0
- package/dest/config.d.ts +3 -14
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +41 -8
- package/dest/duties/validation_service.d.ts +50 -13
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +123 -17
- package/dest/factory.d.ts +28 -6
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +13 -6
- package/dest/index.d.ts +6 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +5 -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 +4 -1
- package/dest/key_store/index.d.ts.map +1 -1
- package/dest/key_store/index.js +3 -0
- package/dest/key_store/interface.d.ts +85 -6
- package/dest/key_store/interface.d.ts.map +1 -1
- package/dest/key_store/interface.js +3 -3
- package/dest/key_store/local_key_store.d.ts +46 -11
- package/dest/key_store/local_key_store.d.ts.map +1 -1
- package/dest/key_store/local_key_store.js +68 -17
- package/dest/key_store/node_keystore_adapter.d.ts +151 -0
- package/dest/key_store/node_keystore_adapter.d.ts.map +1 -0
- package/dest/key_store/node_keystore_adapter.js +330 -0
- package/dest/key_store/web3signer_key_store.d.ts +66 -0
- package/dest/key_store/web3signer_key_store.d.ts.map +1 -0
- package/dest/key_store/web3signer_key_store.js +156 -0
- package/dest/metrics.d.ts +13 -5
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +63 -22
- 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 +19 -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 +73 -58
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +559 -166
- package/package.json +37 -21
- package/src/block_proposal_handler.ts +555 -0
- package/src/checkpoint_builder.ts +314 -0
- package/src/config.ts +52 -22
- package/src/duties/validation_service.ts +193 -19
- package/src/factory.ts +65 -11
- package/src/index.ts +5 -1
- package/src/key_store/ha_key_store.ts +269 -0
- package/src/key_store/index.ts +3 -0
- package/src/key_store/interface.ts +100 -5
- package/src/key_store/local_key_store.ts +77 -18
- package/src/key_store/node_keystore_adapter.ts +398 -0
- package/src/key_store/web3signer_key_store.ts +205 -0
- package/src/metrics.ts +86 -23
- 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 +154 -0
- package/src/validator.ts +757 -221
- package/dest/errors/index.d.ts +0 -2
- package/dest/errors/index.d.ts.map +0 -1
- package/dest/errors/index.js +0 -1
- package/dest/errors/validator.error.d.ts +0 -29
- package/dest/errors/validator.error.d.ts.map +0 -1
- package/dest/errors/validator.error.js +0 -45
- package/src/errors/index.ts +0 -1
- package/src/errors/validator.error.ts +0 -55
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/validator-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1-commit.0208eb9",
|
|
4
4
|
"main": "dest/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -18,11 +18,9 @@
|
|
|
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
|
-
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
|
|
25
|
-
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
|
|
26
24
|
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
|
|
27
25
|
},
|
|
28
26
|
"inherits": [
|
|
@@ -59,28 +57,46 @@
|
|
|
59
57
|
"testTimeout": 120000,
|
|
60
58
|
"setupFiles": [
|
|
61
59
|
"../../foundation/src/jest/setup.mjs"
|
|
60
|
+
],
|
|
61
|
+
"testEnvironment": "../../foundation/src/jest/env.mjs",
|
|
62
|
+
"setupFilesAfterEnv": [
|
|
63
|
+
"../../foundation/src/jest/setupAfterEnv.mjs"
|
|
62
64
|
]
|
|
63
65
|
},
|
|
64
66
|
"dependencies": {
|
|
65
|
-
"@aztec/
|
|
66
|
-
"@aztec/
|
|
67
|
-
"@aztec/
|
|
68
|
-
"@aztec/
|
|
69
|
-
"@aztec/
|
|
70
|
-
"@aztec/
|
|
71
|
-
"
|
|
72
|
-
"
|
|
67
|
+
"@aztec/blob-client": "0.0.1-commit.0208eb9",
|
|
68
|
+
"@aztec/blob-lib": "0.0.1-commit.0208eb9",
|
|
69
|
+
"@aztec/constants": "0.0.1-commit.0208eb9",
|
|
70
|
+
"@aztec/epoch-cache": "0.0.1-commit.0208eb9",
|
|
71
|
+
"@aztec/ethereum": "0.0.1-commit.0208eb9",
|
|
72
|
+
"@aztec/foundation": "0.0.1-commit.0208eb9",
|
|
73
|
+
"@aztec/node-keystore": "0.0.1-commit.0208eb9",
|
|
74
|
+
"@aztec/noir-protocol-circuits-types": "0.0.1-commit.0208eb9",
|
|
75
|
+
"@aztec/p2p": "0.0.1-commit.0208eb9",
|
|
76
|
+
"@aztec/protocol-contracts": "0.0.1-commit.0208eb9",
|
|
77
|
+
"@aztec/prover-client": "0.0.1-commit.0208eb9",
|
|
78
|
+
"@aztec/simulator": "0.0.1-commit.0208eb9",
|
|
79
|
+
"@aztec/slasher": "0.0.1-commit.0208eb9",
|
|
80
|
+
"@aztec/stdlib": "0.0.1-commit.0208eb9",
|
|
81
|
+
"@aztec/telemetry-client": "0.0.1-commit.0208eb9",
|
|
82
|
+
"@aztec/validator-ha-signer": "0.0.1-commit.0208eb9",
|
|
83
|
+
"koa": "^2.16.1",
|
|
84
|
+
"koa-router": "^13.1.1",
|
|
73
85
|
"tslib": "^2.4.0",
|
|
74
|
-
"viem": "2.
|
|
86
|
+
"viem": "npm:@aztec/viem@2.38.2"
|
|
75
87
|
},
|
|
76
88
|
"devDependencies": {
|
|
77
|
-
"@
|
|
78
|
-
"@
|
|
79
|
-
"@
|
|
80
|
-
"jest": "^
|
|
81
|
-
"jest
|
|
89
|
+
"@aztec/archiver": "0.0.1-commit.0208eb9",
|
|
90
|
+
"@aztec/world-state": "0.0.1-commit.0208eb9",
|
|
91
|
+
"@electric-sql/pglite": "^0.3.14",
|
|
92
|
+
"@jest/globals": "^30.0.0",
|
|
93
|
+
"@types/jest": "^30.0.0",
|
|
94
|
+
"@types/node": "^22.15.17",
|
|
95
|
+
"@typescript/native-preview": "7.0.0-dev.20260113.1",
|
|
96
|
+
"jest": "^30.0.0",
|
|
97
|
+
"jest-mock-extended": "^4.0.0",
|
|
82
98
|
"ts-node": "^10.9.1",
|
|
83
|
-
"typescript": "^5.
|
|
99
|
+
"typescript": "^5.3.3"
|
|
84
100
|
},
|
|
85
101
|
"files": [
|
|
86
102
|
"dest",
|
|
@@ -89,6 +105,6 @@
|
|
|
89
105
|
],
|
|
90
106
|
"types": "./dest/index.d.ts",
|
|
91
107
|
"engines": {
|
|
92
|
-
"node": ">=
|
|
108
|
+
"node": ">=20.10"
|
|
93
109
|
}
|
|
94
110
|
}
|
|
@@ -0,0 +1,555 @@
|
|
|
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';
|
|
6
|
+
import { TimeoutError } from '@aztec/foundation/error';
|
|
7
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
8
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
9
|
+
import { DateProvider, Timer } from '@aztec/foundation/timer';
|
|
10
|
+
import type { P2P, PeerId } from '@aztec/p2p';
|
|
11
|
+
import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
|
|
12
|
+
import type { L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
13
|
+
import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
14
|
+
import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
15
|
+
import {
|
|
16
|
+
type L1ToL2MessageSource,
|
|
17
|
+
computeCheckpointOutHash,
|
|
18
|
+
computeInHashFromL1ToL2Messages,
|
|
19
|
+
} from '@aztec/stdlib/messaging';
|
|
20
|
+
import type { BlockProposal } from '@aztec/stdlib/p2p';
|
|
21
|
+
import { BlockHeader, type CheckpointGlobalVariables, type FailedTx, type Tx } from '@aztec/stdlib/tx';
|
|
22
|
+
import {
|
|
23
|
+
ReExFailedTxsError,
|
|
24
|
+
ReExStateMismatchError,
|
|
25
|
+
ReExTimeoutError,
|
|
26
|
+
TransactionsNotAvailableError,
|
|
27
|
+
} from '@aztec/stdlib/validators';
|
|
28
|
+
import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
29
|
+
|
|
30
|
+
import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
31
|
+
import type { ValidatorMetrics } from './metrics.js';
|
|
32
|
+
|
|
33
|
+
export type BlockProposalValidationFailureReason =
|
|
34
|
+
| 'invalid_proposal'
|
|
35
|
+
| 'parent_block_not_found'
|
|
36
|
+
| 'parent_block_wrong_slot'
|
|
37
|
+
| 'in_hash_mismatch'
|
|
38
|
+
| 'global_variables_mismatch'
|
|
39
|
+
| 'block_number_already_exists'
|
|
40
|
+
| 'txs_not_available'
|
|
41
|
+
| 'state_mismatch'
|
|
42
|
+
| 'failed_txs'
|
|
43
|
+
| 'timeout'
|
|
44
|
+
| 'unknown_error';
|
|
45
|
+
|
|
46
|
+
type ReexecuteTransactionsResult = {
|
|
47
|
+
block: L2Block;
|
|
48
|
+
failedTxs: FailedTx[];
|
|
49
|
+
reexecutionTimeMs: number;
|
|
50
|
+
totalManaUsed: number;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type BlockProposalValidationSuccessResult = {
|
|
54
|
+
isValid: true;
|
|
55
|
+
blockNumber: BlockNumber;
|
|
56
|
+
reexecutionResult?: ReexecuteTransactionsResult;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type BlockProposalValidationFailureResult = {
|
|
60
|
+
isValid: false;
|
|
61
|
+
reason: BlockProposalValidationFailureReason;
|
|
62
|
+
blockNumber?: BlockNumber;
|
|
63
|
+
reexecutionResult?: ReexecuteTransactionsResult;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
|
|
67
|
+
|
|
68
|
+
type CheckpointComputationResult =
|
|
69
|
+
| { checkpointNumber: CheckpointNumber; reason?: undefined }
|
|
70
|
+
| { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
|
|
71
|
+
|
|
72
|
+
export class BlockProposalHandler {
|
|
73
|
+
public readonly tracer: Tracer;
|
|
74
|
+
|
|
75
|
+
constructor(
|
|
76
|
+
private checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
77
|
+
private worldState: WorldStateSynchronizer,
|
|
78
|
+
private blockSource: L2BlockSource & L2BlockSink,
|
|
79
|
+
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
80
|
+
private txProvider: ITxProvider,
|
|
81
|
+
private blockProposalValidator: BlockProposalValidator,
|
|
82
|
+
private epochCache: EpochCache,
|
|
83
|
+
private config: ValidatorClientFullConfig,
|
|
84
|
+
private metrics?: ValidatorMetrics,
|
|
85
|
+
private dateProvider: DateProvider = new DateProvider(),
|
|
86
|
+
telemetry: TelemetryClient = getTelemetryClient(),
|
|
87
|
+
private log = createLogger('validator:block-proposal-handler'),
|
|
88
|
+
) {
|
|
89
|
+
if (config.fishermanMode) {
|
|
90
|
+
this.log = this.log.createChild('[FISHERMAN]');
|
|
91
|
+
}
|
|
92
|
+
this.tracer = telemetry.getTracer('BlockProposalHandler');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
registerForReexecution(p2pClient: P2P): BlockProposalHandler {
|
|
96
|
+
// Non-validator handler that re-executes for monitoring but does not attest.
|
|
97
|
+
// Returns boolean indicating whether the proposal was valid.
|
|
98
|
+
const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
|
|
99
|
+
try {
|
|
100
|
+
const result = await this.handleBlockProposal(proposal, proposalSender, true);
|
|
101
|
+
if (result.isValid) {
|
|
102
|
+
this.log.info(`Non-validator reexecution completed for slot ${proposal.slotNumber}`, {
|
|
103
|
+
blockNumber: result.blockNumber,
|
|
104
|
+
reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
|
|
105
|
+
totalManaUsed: result.reexecutionResult?.totalManaUsed,
|
|
106
|
+
numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
|
|
107
|
+
});
|
|
108
|
+
return true;
|
|
109
|
+
} else {
|
|
110
|
+
this.log.warn(`Non-validator reexecution failed for slot ${proposal.slotNumber}`, {
|
|
111
|
+
blockNumber: result.blockNumber,
|
|
112
|
+
reason: result.reason,
|
|
113
|
+
});
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
} catch (error) {
|
|
117
|
+
this.log.error('Error processing block proposal in non-validator handler', error);
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
p2pClient.registerBlockProposalHandler(handler);
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async handleBlockProposal(
|
|
127
|
+
proposal: BlockProposal,
|
|
128
|
+
proposalSender: PeerId,
|
|
129
|
+
shouldReexecute: boolean,
|
|
130
|
+
): Promise<BlockProposalValidationResult> {
|
|
131
|
+
const slotNumber = proposal.slotNumber;
|
|
132
|
+
const proposer = proposal.getSender();
|
|
133
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
134
|
+
|
|
135
|
+
// Reject proposals with invalid signatures
|
|
136
|
+
if (!proposer) {
|
|
137
|
+
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
138
|
+
return { isValid: false, reason: 'invalid_proposal' };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
|
|
142
|
+
this.log.info(`Processing proposal for slot ${slotNumber}`, {
|
|
143
|
+
...proposalInfo,
|
|
144
|
+
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Check that the proposal is from the current proposer, or the next proposer
|
|
148
|
+
// This should have been handled by the p2p layer, but we double check here out of caution
|
|
149
|
+
const validationResult = await this.blockProposalValidator.validate(proposal);
|
|
150
|
+
if (validationResult.result !== 'accept') {
|
|
151
|
+
this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
|
|
152
|
+
return { isValid: false, reason: 'invalid_proposal' };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Check that the parent proposal is a block we know, otherwise reexecution would fail
|
|
156
|
+
const parentBlockHeader = await this.getParentBlock(proposal);
|
|
157
|
+
if (parentBlockHeader === undefined) {
|
|
158
|
+
this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
|
|
159
|
+
return { isValid: false, reason: 'parent_block_not_found' };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Check that the parent block's slot is not greater than the proposal's slot.
|
|
163
|
+
if (parentBlockHeader !== 'genesis' && parentBlockHeader.getSlot() > slotNumber) {
|
|
164
|
+
this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
|
|
165
|
+
parentBlockSlot: parentBlockHeader.getSlot().toString(),
|
|
166
|
+
proposalSlot: slotNumber.toString(),
|
|
167
|
+
...proposalInfo,
|
|
168
|
+
});
|
|
169
|
+
return { isValid: false, reason: 'parent_block_wrong_slot' };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Compute the block number based on the parent block
|
|
173
|
+
const blockNumber =
|
|
174
|
+
parentBlockHeader === 'genesis'
|
|
175
|
+
? BlockNumber(INITIAL_L2_BLOCK_NUM)
|
|
176
|
+
: BlockNumber(parentBlockHeader.getBlockNumber() + 1);
|
|
177
|
+
|
|
178
|
+
// Check that this block number does not exist already
|
|
179
|
+
const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
|
|
180
|
+
if (existingBlock) {
|
|
181
|
+
this.log.warn(`Block number ${blockNumber} already exists, skipping processing`, proposalInfo);
|
|
182
|
+
return { isValid: false, blockNumber, reason: 'block_number_already_exists' };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Collect txs from the proposal. We start doing this as early as possible,
|
|
186
|
+
// and we do it even if we don't plan to re-execute the txs, so that we have them if another node needs them.
|
|
187
|
+
const { txs, missingTxs } = await this.txProvider.getTxsForBlockProposal(proposal, blockNumber, {
|
|
188
|
+
pinnedPeer: proposalSender,
|
|
189
|
+
deadline: this.getReexecutionDeadline(slotNumber, config),
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Compute the checkpoint number for this block and validate checkpoint consistency
|
|
193
|
+
const checkpointResult = await this.computeCheckpointNumber(proposal, parentBlockHeader, proposalInfo);
|
|
194
|
+
if (checkpointResult.reason) {
|
|
195
|
+
return { isValid: false, blockNumber, reason: checkpointResult.reason };
|
|
196
|
+
}
|
|
197
|
+
const checkpointNumber = checkpointResult.checkpointNumber;
|
|
198
|
+
|
|
199
|
+
// Check that I have the same set of l1ToL2Messages as the proposal
|
|
200
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
201
|
+
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
202
|
+
const proposalInHash = proposal.inHash;
|
|
203
|
+
if (!computedInHash.equals(proposalInHash)) {
|
|
204
|
+
this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
|
|
205
|
+
proposalInHash: proposalInHash.toString(),
|
|
206
|
+
computedInHash: computedInHash.toString(),
|
|
207
|
+
...proposalInfo,
|
|
208
|
+
});
|
|
209
|
+
return { isValid: false, blockNumber, reason: 'in_hash_mismatch' };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Check that all of the transactions in the proposal are available
|
|
213
|
+
if (missingTxs.length > 0) {
|
|
214
|
+
this.log.warn(`Missing ${missingTxs.length} txs to process proposal`, { ...proposalInfo, missingTxs });
|
|
215
|
+
return { isValid: false, blockNumber, reason: 'txs_not_available' };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Try re-executing the transactions in the proposal if needed
|
|
219
|
+
let reexecutionResult;
|
|
220
|
+
if (shouldReexecute) {
|
|
221
|
+
// Compute the previous checkpoint out hashes for the epoch.
|
|
222
|
+
// TODO(leila/mbps): There can be a more efficient way to get the previous checkpoint out
|
|
223
|
+
// hashes without having to fetch all the blocks.
|
|
224
|
+
const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
|
|
225
|
+
const checkpointedBlocks = (await this.blockSource.getCheckpointedBlocksForEpoch(epoch))
|
|
226
|
+
.filter(b => b.block.number < blockNumber)
|
|
227
|
+
.sort((a, b) => a.block.number - b.block.number);
|
|
228
|
+
const blocksByCheckpoint = chunkBy(checkpointedBlocks, b => b.checkpointNumber);
|
|
229
|
+
const previousCheckpointOutHashes = blocksByCheckpoint.map(checkpointBlocks =>
|
|
230
|
+
computeCheckpointOutHash(checkpointBlocks.map(b => b.block.body.txEffects.map(tx => tx.l2ToL1Msgs))),
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
|
|
235
|
+
reexecutionResult = await this.reexecuteTransactions(
|
|
236
|
+
proposal,
|
|
237
|
+
blockNumber,
|
|
238
|
+
checkpointNumber,
|
|
239
|
+
txs,
|
|
240
|
+
l1ToL2Messages,
|
|
241
|
+
previousCheckpointOutHashes,
|
|
242
|
+
);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
|
|
245
|
+
const reason = this.getReexecuteFailureReason(error);
|
|
246
|
+
return { isValid: false, blockNumber, reason, reexecutionResult };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// If we succeeded, push this block into the archiver (unless disabled)
|
|
251
|
+
if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
|
|
252
|
+
await this.blockSource.addBlock(reexecutionResult?.block);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
this.log.info(
|
|
256
|
+
`Successfully processed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
|
|
257
|
+
proposalInfo,
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
return { isValid: true, blockNumber, reexecutionResult };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockHeader | undefined> {
|
|
264
|
+
const parentArchive = proposal.blockHeader.lastArchive.root;
|
|
265
|
+
const slot = proposal.slotNumber;
|
|
266
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
267
|
+
const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
|
|
268
|
+
|
|
269
|
+
if (parentArchive.equals(genesisArchiveRoot)) {
|
|
270
|
+
return 'genesis';
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const deadline = this.getReexecutionDeadline(slot, config);
|
|
274
|
+
const currentTime = this.dateProvider.now();
|
|
275
|
+
const timeoutDurationMs = deadline.getTime() - currentTime;
|
|
276
|
+
|
|
277
|
+
try {
|
|
278
|
+
return (
|
|
279
|
+
(await this.blockSource.getBlockHeaderByArchive(parentArchive)) ??
|
|
280
|
+
(timeoutDurationMs <= 0
|
|
281
|
+
? undefined
|
|
282
|
+
: await retryUntil(
|
|
283
|
+
() =>
|
|
284
|
+
this.blockSource.syncImmediate().then(() => this.blockSource.getBlockHeaderByArchive(parentArchive)),
|
|
285
|
+
'force archiver sync',
|
|
286
|
+
timeoutDurationMs / 1000,
|
|
287
|
+
0.5,
|
|
288
|
+
))
|
|
289
|
+
);
|
|
290
|
+
} catch (err) {
|
|
291
|
+
if (err instanceof TimeoutError) {
|
|
292
|
+
this.log.debug(`Timed out getting parent block by archive root`, { parentArchive });
|
|
293
|
+
} else {
|
|
294
|
+
this.log.error('Error getting parent block by archive root', err, { parentArchive });
|
|
295
|
+
}
|
|
296
|
+
return undefined;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private async computeCheckpointNumber(
|
|
301
|
+
proposal: BlockProposal,
|
|
302
|
+
parentBlockHeader: 'genesis' | BlockHeader,
|
|
303
|
+
proposalInfo: object,
|
|
304
|
+
): Promise<CheckpointComputationResult> {
|
|
305
|
+
if (parentBlockHeader === 'genesis') {
|
|
306
|
+
// First block is in checkpoint 1
|
|
307
|
+
if (proposal.indexWithinCheckpoint !== 0) {
|
|
308
|
+
this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
|
|
309
|
+
return { reason: 'invalid_proposal' };
|
|
310
|
+
}
|
|
311
|
+
return { checkpointNumber: CheckpointNumber.INITIAL };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Get the parent block to find its checkpoint number
|
|
315
|
+
// TODO(palla/mbps): The block header should include the checkpoint number to avoid this lookup,
|
|
316
|
+
// or at least the L2BlockSource should return a different struct that includes it.
|
|
317
|
+
const parentBlockNumber = parentBlockHeader.getBlockNumber();
|
|
318
|
+
const parentBlock = await this.blockSource.getL2Block(parentBlockNumber);
|
|
319
|
+
if (!parentBlock) {
|
|
320
|
+
this.log.warn(`Parent block ${parentBlockNumber} not found in archiver`, proposalInfo);
|
|
321
|
+
return { reason: 'invalid_proposal' };
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (proposal.indexWithinCheckpoint === 0) {
|
|
325
|
+
// If this is the first block in a new checkpoint, increment the checkpoint number
|
|
326
|
+
if (!(proposal.blockHeader.getSlot() > parentBlockHeader.getSlot())) {
|
|
327
|
+
this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
|
|
328
|
+
return { reason: 'invalid_proposal' };
|
|
329
|
+
}
|
|
330
|
+
return { checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1) };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Otherwise it should follow the previous block in the same checkpoint
|
|
334
|
+
if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
|
|
335
|
+
this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
|
|
336
|
+
return { reason: 'invalid_proposal' };
|
|
337
|
+
}
|
|
338
|
+
if (proposal.blockHeader.getSlot() !== parentBlockHeader.getSlot()) {
|
|
339
|
+
this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
|
|
340
|
+
return { reason: 'invalid_proposal' };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
|
|
344
|
+
const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
|
|
345
|
+
if (validationResult) {
|
|
346
|
+
return validationResult;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return { checkpointNumber: parentBlock.checkpointNumber };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Validates that a non-first block in a checkpoint has consistent global variables with its parent.
|
|
354
|
+
* For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
|
|
355
|
+
* @returns A failure result if validation fails, undefined if validation passes
|
|
356
|
+
*/
|
|
357
|
+
private validateNonFirstBlockInCheckpoint(
|
|
358
|
+
proposal: BlockProposal,
|
|
359
|
+
parentBlock: L2Block,
|
|
360
|
+
proposalInfo: object,
|
|
361
|
+
): CheckpointComputationResult | undefined {
|
|
362
|
+
const proposalGlobals = proposal.blockHeader.globalVariables;
|
|
363
|
+
const parentGlobals = parentBlock.header.globalVariables;
|
|
364
|
+
|
|
365
|
+
// All global variables except blockNumber should match the parent
|
|
366
|
+
// blockNumber naturally increments between blocks
|
|
367
|
+
if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
|
|
368
|
+
this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
|
|
369
|
+
...proposalInfo,
|
|
370
|
+
proposalChainId: proposalGlobals.chainId.toString(),
|
|
371
|
+
parentChainId: parentGlobals.chainId.toString(),
|
|
372
|
+
});
|
|
373
|
+
return { reason: 'global_variables_mismatch' };
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (!proposalGlobals.version.equals(parentGlobals.version)) {
|
|
377
|
+
this.log.warn(`Non-first block in checkpoint has mismatched version`, {
|
|
378
|
+
...proposalInfo,
|
|
379
|
+
proposalVersion: proposalGlobals.version.toString(),
|
|
380
|
+
parentVersion: parentGlobals.version.toString(),
|
|
381
|
+
});
|
|
382
|
+
return { reason: 'global_variables_mismatch' };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
|
|
386
|
+
this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
|
|
387
|
+
...proposalInfo,
|
|
388
|
+
proposalSlotNumber: proposalGlobals.slotNumber,
|
|
389
|
+
parentSlotNumber: parentGlobals.slotNumber,
|
|
390
|
+
});
|
|
391
|
+
return { reason: 'global_variables_mismatch' };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
|
|
395
|
+
this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
|
|
396
|
+
...proposalInfo,
|
|
397
|
+
proposalTimestamp: proposalGlobals.timestamp.toString(),
|
|
398
|
+
parentTimestamp: parentGlobals.timestamp.toString(),
|
|
399
|
+
});
|
|
400
|
+
return { reason: 'global_variables_mismatch' };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
|
|
404
|
+
this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
|
|
405
|
+
...proposalInfo,
|
|
406
|
+
proposalCoinbase: proposalGlobals.coinbase.toString(),
|
|
407
|
+
parentCoinbase: parentGlobals.coinbase.toString(),
|
|
408
|
+
});
|
|
409
|
+
return { reason: 'global_variables_mismatch' };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
|
|
413
|
+
this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
|
|
414
|
+
...proposalInfo,
|
|
415
|
+
proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
|
|
416
|
+
parentFeeRecipient: parentGlobals.feeRecipient.toString(),
|
|
417
|
+
});
|
|
418
|
+
return { reason: 'global_variables_mismatch' };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
|
|
422
|
+
this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
|
|
423
|
+
...proposalInfo,
|
|
424
|
+
proposalGasFees: proposalGlobals.gasFees.toInspect(),
|
|
425
|
+
parentGasFees: parentGlobals.gasFees.toInspect(),
|
|
426
|
+
});
|
|
427
|
+
return { reason: 'global_variables_mismatch' };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return undefined;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
|
|
434
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
435
|
+
return new Date(nextSlotTimestampSeconds * 1000);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
private getReexecuteFailureReason(err: any) {
|
|
439
|
+
if (err instanceof ReExStateMismatchError) {
|
|
440
|
+
return 'state_mismatch';
|
|
441
|
+
} else if (err instanceof ReExFailedTxsError) {
|
|
442
|
+
return 'failed_txs';
|
|
443
|
+
} else if (err instanceof ReExTimeoutError) {
|
|
444
|
+
return 'timeout';
|
|
445
|
+
} else {
|
|
446
|
+
return 'unknown_error';
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
async reexecuteTransactions(
|
|
451
|
+
proposal: BlockProposal,
|
|
452
|
+
blockNumber: BlockNumber,
|
|
453
|
+
checkpointNumber: CheckpointNumber,
|
|
454
|
+
txs: Tx[],
|
|
455
|
+
l1ToL2Messages: Fr[],
|
|
456
|
+
previousCheckpointOutHashes: Fr[],
|
|
457
|
+
): Promise<ReexecuteTransactionsResult> {
|
|
458
|
+
const { blockHeader, txHashes } = proposal;
|
|
459
|
+
|
|
460
|
+
// If we do not have all of the transactions, then we should fail
|
|
461
|
+
if (txs.length !== txHashes.length) {
|
|
462
|
+
const foundTxHashes = txs.map(tx => tx.getTxHash());
|
|
463
|
+
const missingTxHashes = txHashes.filter(txHash => !foundTxHashes.includes(txHash));
|
|
464
|
+
throw new TransactionsNotAvailableError(missingTxHashes);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const timer = new Timer();
|
|
468
|
+
const slot = proposal.slotNumber;
|
|
469
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
470
|
+
|
|
471
|
+
// Get prior blocks in this checkpoint (same slot before current block)
|
|
472
|
+
const allBlocksInSlot = await this.blockSource.getBlocksForSlot(slot);
|
|
473
|
+
const priorBlocks = allBlocksInSlot.filter(b => b.number < blockNumber && b.header.getSlot() === slot);
|
|
474
|
+
|
|
475
|
+
// Fork before the block to be built
|
|
476
|
+
const parentBlockNumber = BlockNumber(blockNumber - 1);
|
|
477
|
+
await this.worldState.syncImmediate(parentBlockNumber);
|
|
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 = {
|
|
482
|
+
chainId: new Fr(config.l1ChainId),
|
|
483
|
+
version: new Fr(config.rollupVersion),
|
|
484
|
+
slotNumber: slot,
|
|
485
|
+
coinbase: blockHeader.globalVariables.coinbase,
|
|
486
|
+
feeRecipient: blockHeader.globalVariables.feeRecipient,
|
|
487
|
+
gasFees: blockHeader.globalVariables.gasFees,
|
|
488
|
+
};
|
|
489
|
+
|
|
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
|
+
this.log.getBindings(),
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
// Build the new block
|
|
502
|
+
const deadline = this.getReexecutionDeadline(slot, config);
|
|
503
|
+
const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
|
|
504
|
+
deadline,
|
|
505
|
+
expectedEndState: blockHeader.state,
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
const { block, failedTxs } = result;
|
|
509
|
+
const numFailedTxs = failedTxs.length;
|
|
510
|
+
|
|
511
|
+
this.log.verbose(`Transaction re-execution complete for slot ${slot}`, {
|
|
512
|
+
numFailedTxs,
|
|
513
|
+
numProposalTxs: txHashes.length,
|
|
514
|
+
numProcessedTxs: block.body.txEffects.length,
|
|
515
|
+
slot,
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
if (numFailedTxs > 0) {
|
|
519
|
+
this.metrics?.recordFailedReexecution(proposal);
|
|
520
|
+
throw new ReExFailedTxsError(numFailedTxs);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (block.body.txEffects.length !== txHashes.length) {
|
|
524
|
+
this.metrics?.recordFailedReexecution(proposal);
|
|
525
|
+
throw new ReExTimeoutError();
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// Throw a ReExStateMismatchError error if state updates do not match
|
|
529
|
+
// Compare the full block structure (archive and header) from the built block with the proposal
|
|
530
|
+
const archiveMatches = proposal.archive.equals(block.archive.root);
|
|
531
|
+
const headerMatches = proposal.blockHeader.equals(block.header);
|
|
532
|
+
if (!archiveMatches || !headerMatches) {
|
|
533
|
+
this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
|
|
534
|
+
expectedArchive: block.archive.root.toString(),
|
|
535
|
+
actualArchive: proposal.archive.toString(),
|
|
536
|
+
expectedHeader: block.header.toInspect(),
|
|
537
|
+
actualHeader: proposal.blockHeader.toInspect(),
|
|
538
|
+
});
|
|
539
|
+
this.metrics?.recordFailedReexecution(proposal);
|
|
540
|
+
throw new ReExStateMismatchError(proposal.archive, block.archive.root);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const reexecutionTimeMs = timer.ms();
|
|
544
|
+
const totalManaUsed = block.header.totalManaUsed.toNumber() / 1e6;
|
|
545
|
+
|
|
546
|
+
this.metrics?.recordReex(reexecutionTimeMs, txs.length, totalManaUsed);
|
|
547
|
+
|
|
548
|
+
return {
|
|
549
|
+
block,
|
|
550
|
+
failedTxs,
|
|
551
|
+
reexecutionTimeMs,
|
|
552
|
+
totalManaUsed,
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
}
|