@aztec/aztec-node 5.0.0-rc.1 → 5.0.0-rc.2
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/dest/aztec-node/config.d.ts +3 -1
- package/dest/aztec-node/config.d.ts.map +1 -1
- package/dest/aztec-node/config.js +5 -0
- package/dest/aztec-node/node_public_calls_simulator.d.ts +90 -0
- package/dest/aztec-node/node_public_calls_simulator.d.ts.map +1 -0
- package/dest/aztec-node/node_public_calls_simulator.js +346 -0
- package/dest/aztec-node/server.d.ts +68 -65
- package/dest/aztec-node/server.d.ts.map +1 -1
- package/dest/aztec-node/server.js +153 -1161
- package/dest/bin/index.js +2 -2
- package/dest/factory.d.ts +33 -0
- package/dest/factory.d.ts.map +1 -0
- package/dest/factory.js +496 -0
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/modules/block_parameter.d.ts +25 -0
- package/dest/modules/block_parameter.d.ts.map +1 -0
- package/dest/modules/block_parameter.js +100 -0
- package/dest/modules/node_block_provider.d.ts +19 -0
- package/dest/modules/node_block_provider.d.ts.map +1 -0
- package/dest/modules/node_block_provider.js +112 -0
- package/dest/modules/node_tx_receipt.d.ts +24 -0
- package/dest/modules/node_tx_receipt.d.ts.map +1 -0
- package/dest/modules/node_tx_receipt.js +70 -0
- package/dest/modules/node_world_state_queries.d.ts +61 -0
- package/dest/modules/node_world_state_queries.d.ts.map +1 -0
- package/dest/modules/node_world_state_queries.js +257 -0
- package/dest/sentinel/factory.d.ts +3 -3
- package/dest/sentinel/factory.d.ts.map +1 -1
- package/dest/sentinel/factory.js +8 -1
- package/dest/sentinel/sentinel.d.ts +21 -21
- package/dest/sentinel/sentinel.d.ts.map +1 -1
- package/dest/sentinel/sentinel.js +27 -47
- package/package.json +28 -27
- package/src/aztec-node/config.ts +7 -0
- package/src/aztec-node/node_public_calls_simulator.ts +383 -0
- package/src/aztec-node/server.ts +221 -1342
- package/src/bin/index.ts +7 -2
- package/src/factory.ts +656 -0
- package/src/index.ts +1 -0
- package/src/modules/block_parameter.ts +93 -0
- package/src/modules/node_block_provider.ts +149 -0
- package/src/modules/node_tx_receipt.ts +115 -0
- package/src/modules/node_world_state_queries.ts +360 -0
- package/src/sentinel/README.md +3 -3
- package/src/sentinel/factory.ts +15 -3
- package/src/sentinel/sentinel.ts +34 -72
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { ARCHIVE_HEIGHT, type L1_TO_L2_MSG_TREE_HEIGHT, type NOTE_HASH_TREE_HEIGHT } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, type CheckpointNumber, type EpochNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { chunkBy } from '@aztec/foundation/collection';
|
|
4
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
5
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
6
|
+
import { MembershipWitness, type SiblingPath } from '@aztec/foundation/trees';
|
|
7
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
8
|
+
import {
|
|
9
|
+
type BlockHash,
|
|
10
|
+
type BlockParameter,
|
|
11
|
+
type DataInBlock,
|
|
12
|
+
type L2BlockSource,
|
|
13
|
+
inspectBlockParameter,
|
|
14
|
+
} from '@aztec/stdlib/block';
|
|
15
|
+
import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
|
|
16
|
+
import type { WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
17
|
+
import { InboxLeaf, type L1ToL2MessageSource, type L2ToL1MembershipWitness } from '@aztec/stdlib/messaging';
|
|
18
|
+
import {
|
|
19
|
+
MerkleTreeId,
|
|
20
|
+
type NullifierLeafPreimage,
|
|
21
|
+
NullifierMembershipWitness,
|
|
22
|
+
type PublicDataTreeLeafPreimage,
|
|
23
|
+
PublicDataWitness,
|
|
24
|
+
} from '@aztec/stdlib/trees';
|
|
25
|
+
import type { TxHash } from '@aztec/stdlib/tx';
|
|
26
|
+
|
|
27
|
+
import { normalizeBlockParameter } from './block_parameter.js';
|
|
28
|
+
|
|
29
|
+
/** Dependencies required to build a {@link NodeWorldStateQueries}. */
|
|
30
|
+
export interface NodeWorldStateQueriesDeps {
|
|
31
|
+
worldStateSynchronizer: WorldStateSynchronizer;
|
|
32
|
+
blockSource: L2BlockSource;
|
|
33
|
+
l1ToL2MessageSource: L1ToL2MessageSource;
|
|
34
|
+
log?: Logger;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Serves the node's Merkle-tree and membership-witness queries against committed world-state at a
|
|
39
|
+
* requested block. Extracted from `AztecNodeService` so the block-resolution and reorg-aware sync logic
|
|
40
|
+
* can be unit-tested without standing up the whole node, and to keep `server.ts` smaller.
|
|
41
|
+
*/
|
|
42
|
+
export class NodeWorldStateQueries {
|
|
43
|
+
private readonly worldStateSynchronizer: WorldStateSynchronizer;
|
|
44
|
+
private readonly blockSource: L2BlockSource;
|
|
45
|
+
private readonly l1ToL2MessageSource: L1ToL2MessageSource;
|
|
46
|
+
private readonly log: Logger;
|
|
47
|
+
|
|
48
|
+
constructor(deps: NodeWorldStateQueriesDeps) {
|
|
49
|
+
this.worldStateSynchronizer = deps.worldStateSynchronizer;
|
|
50
|
+
this.blockSource = deps.blockSource;
|
|
51
|
+
this.l1ToL2MessageSource = deps.l1ToL2MessageSource;
|
|
52
|
+
this.log = deps.log ?? createLogger('node:world-state-queries');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public async findLeavesIndexes(
|
|
56
|
+
referenceBlock: BlockParameter,
|
|
57
|
+
treeId: MerkleTreeId,
|
|
58
|
+
leafValues: Fr[],
|
|
59
|
+
): Promise<(DataInBlock<bigint> | undefined)[]> {
|
|
60
|
+
const committedDb = await this.getWorldState(referenceBlock);
|
|
61
|
+
const maybeIndices = await committedDb.findLeafIndices(
|
|
62
|
+
treeId,
|
|
63
|
+
leafValues.map(x => x.toBuffer()),
|
|
64
|
+
);
|
|
65
|
+
// Filter out undefined values to query block numbers only for found leaves
|
|
66
|
+
const definedIndices = maybeIndices.filter(x => x !== undefined);
|
|
67
|
+
|
|
68
|
+
// Now we find the block numbers for the defined indices
|
|
69
|
+
const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, definedIndices);
|
|
70
|
+
|
|
71
|
+
// Build a map from leaf index to block number
|
|
72
|
+
const indexToBlockNumber = new Map<bigint, BlockNumber>();
|
|
73
|
+
for (let i = 0; i < definedIndices.length; i++) {
|
|
74
|
+
const blockNumber = blockNumbers[i];
|
|
75
|
+
if (blockNumber === undefined) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`Block number is undefined for leaf index ${definedIndices[i]} in tree ${MerkleTreeId[treeId]}`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
indexToBlockNumber.set(definedIndices[i], blockNumber);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Get unique block numbers in order to optimize num calls to getLeafValue function.
|
|
84
|
+
const uniqueBlockNumbers = [...new Set(indexToBlockNumber.values())];
|
|
85
|
+
|
|
86
|
+
// Now we obtain the block hashes from the archive tree (block number = leaf index in archive tree).
|
|
87
|
+
const blockHashes = await Promise.all(
|
|
88
|
+
uniqueBlockNumbers.map(blockNumber => {
|
|
89
|
+
return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// Build a map from block number to block hash
|
|
94
|
+
const blockNumberToHash = new Map<BlockNumber, BlockHash>();
|
|
95
|
+
for (let i = 0; i < uniqueBlockNumbers.length; i++) {
|
|
96
|
+
const blockHash = blockHashes[i];
|
|
97
|
+
if (blockHash === undefined) {
|
|
98
|
+
throw new Error(`Block hash is undefined for block number ${uniqueBlockNumbers[i]}`);
|
|
99
|
+
}
|
|
100
|
+
blockNumberToHash.set(uniqueBlockNumbers[i], blockHash);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Create DataInBlock objects by combining indices, blockNumbers and blockHashes and return them.
|
|
104
|
+
return maybeIndices.map(index => {
|
|
105
|
+
if (index === undefined) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
const blockNumber = indexToBlockNumber.get(index);
|
|
109
|
+
if (blockNumber === undefined) {
|
|
110
|
+
throw new Error(`Block number not found for leaf index ${index} in tree ${MerkleTreeId[treeId]}`);
|
|
111
|
+
}
|
|
112
|
+
const l2BlockHash = blockNumberToHash.get(blockNumber);
|
|
113
|
+
if (l2BlockHash === undefined) {
|
|
114
|
+
throw new Error(`Block hash not found for block number ${blockNumber}`);
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
l2BlockNumber: blockNumber,
|
|
118
|
+
l2BlockHash,
|
|
119
|
+
data: index,
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public async getBlockHashMembershipWitness(
|
|
125
|
+
referenceBlock: BlockParameter,
|
|
126
|
+
blockHash: BlockHash,
|
|
127
|
+
): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
|
|
128
|
+
// The Noir circuit checks the archive membership proof against `anchor_block_header.last_archive.root`,
|
|
129
|
+
// which is the archive tree root BEFORE the anchor block was added (i.e. the state after block N-1).
|
|
130
|
+
// So we need the world state at block N-1, not block N, to produce a sibling path matching that root.
|
|
131
|
+
const referenceBlockNumber = await this.resolveBlockNumber(referenceBlock);
|
|
132
|
+
if (referenceBlockNumber === BlockNumber.ZERO) {
|
|
133
|
+
// Block 0 (the initial block) has an empty archive, so no membership witness can exist.
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
const committedDb = await this.getWorldState(BlockNumber(referenceBlockNumber - 1));
|
|
137
|
+
const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [blockHash]);
|
|
138
|
+
return pathAndIndex === undefined
|
|
139
|
+
? undefined
|
|
140
|
+
: MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public async getNoteHashMembershipWitness(
|
|
144
|
+
referenceBlock: BlockParameter,
|
|
145
|
+
noteHash: Fr,
|
|
146
|
+
): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
|
|
147
|
+
const committedDb = await this.getWorldState(referenceBlock);
|
|
148
|
+
const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.NOTE_HASH_TREE>(
|
|
149
|
+
MerkleTreeId.NOTE_HASH_TREE,
|
|
150
|
+
[noteHash],
|
|
151
|
+
);
|
|
152
|
+
return pathAndIndex === undefined
|
|
153
|
+
? undefined
|
|
154
|
+
: MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
public async getL1ToL2MessageMembershipWitness(
|
|
158
|
+
referenceBlock: BlockParameter,
|
|
159
|
+
l1ToL2Message: Fr,
|
|
160
|
+
): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
|
|
161
|
+
const db = await this.getWorldState(referenceBlock);
|
|
162
|
+
const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [l1ToL2Message]);
|
|
163
|
+
if (!witness) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// REFACTOR: Return a MembershipWitness object
|
|
168
|
+
return [witness.index, witness.path];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public async getL1ToL2MessageCheckpoint(l1ToL2Message: Fr): Promise<CheckpointNumber | undefined> {
|
|
172
|
+
const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
|
|
173
|
+
return messageIndex !== undefined ? InboxLeaf.checkpointNumberFromIndex(messageIndex) : undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Returns all the L2 to L1 messages in an epoch (empty array if the epoch is not found). The public
|
|
178
|
+
* `AztecNodeService.getL2ToL1Messages` that delegates here is deprecated in favor of
|
|
179
|
+
* {@link getL2ToL1MembershipWitness}.
|
|
180
|
+
* @param epoch - The epoch at which to get the data.
|
|
181
|
+
*/
|
|
182
|
+
public async getL2ToL1Messages(epoch: EpochNumber): Promise<Fr[][][][]> {
|
|
183
|
+
const blocks = await this.blockSource.getBlocks({ epoch, onlyCheckpointed: true });
|
|
184
|
+
const blocksInCheckpoints = chunkBy(blocks, block => block.header.globalVariables.slotNumber);
|
|
185
|
+
return blocksInCheckpoints.map(slotBlocks =>
|
|
186
|
+
slotBlocks.map(block => block.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs)),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Returns the L2-to-L1 membership witness for a message in `txHash`. Passthrough to the
|
|
192
|
+
* archiver's locally-cached resolver — see {@link Archiver.getL2ToL1MembershipWitness}.
|
|
193
|
+
*/
|
|
194
|
+
public getL2ToL1MembershipWitness(
|
|
195
|
+
txHash: TxHash,
|
|
196
|
+
message: Fr,
|
|
197
|
+
messageIndexInTx?: number,
|
|
198
|
+
): Promise<L2ToL1MembershipWitness | undefined> {
|
|
199
|
+
return this.blockSource.getL2ToL1MembershipWitness(txHash, message, messageIndexInTx);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public async getNullifierMembershipWitness(
|
|
203
|
+
referenceBlock: BlockParameter,
|
|
204
|
+
nullifier: Fr,
|
|
205
|
+
): Promise<NullifierMembershipWitness | undefined> {
|
|
206
|
+
const db = await this.getWorldState(referenceBlock);
|
|
207
|
+
const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]);
|
|
208
|
+
if (!witness) {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const { index, path } = witness;
|
|
213
|
+
const leafPreimage = await db.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
|
|
214
|
+
if (!leafPreimage) {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return new NullifierMembershipWitness(index, leafPreimage as NullifierLeafPreimage, path);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public async getLowNullifierMembershipWitness(
|
|
222
|
+
referenceBlock: BlockParameter,
|
|
223
|
+
nullifier: Fr,
|
|
224
|
+
): Promise<NullifierMembershipWitness | undefined> {
|
|
225
|
+
const committedDb = await this.getWorldState(referenceBlock);
|
|
226
|
+
const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
|
|
227
|
+
if (!findResult) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
const { index, alreadyPresent } = findResult;
|
|
231
|
+
if (alreadyPresent) {
|
|
232
|
+
throw new Error(
|
|
233
|
+
`Cannot prove nullifier non-inclusion: nullifier ${nullifier.toBigInt()} already exists in the tree`,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
const preimageData = (await committedDb.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index))!;
|
|
237
|
+
|
|
238
|
+
const siblingPath = await committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, BigInt(index));
|
|
239
|
+
return new NullifierMembershipWitness(BigInt(index), preimageData as NullifierLeafPreimage, siblingPath);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
|
|
243
|
+
const committedDb = await this.getWorldState(referenceBlock);
|
|
244
|
+
const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
|
|
245
|
+
if (!lowLeafResult) {
|
|
246
|
+
return undefined;
|
|
247
|
+
} else {
|
|
248
|
+
const preimage = (await committedDb.getLeafPreimage(
|
|
249
|
+
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
250
|
+
lowLeafResult.index,
|
|
251
|
+
)) as PublicDataTreeLeafPreimage;
|
|
252
|
+
const path = await committedDb.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index);
|
|
253
|
+
return new PublicDataWitness(lowLeafResult.index, preimage, path);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
public async getPublicStorageAt(referenceBlock: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
|
|
258
|
+
const committedDb = await this.getWorldState(referenceBlock);
|
|
259
|
+
const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
|
|
260
|
+
|
|
261
|
+
const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
|
|
262
|
+
if (!lowLeafResult || !lowLeafResult.alreadyPresent) {
|
|
263
|
+
return Fr.ZERO;
|
|
264
|
+
}
|
|
265
|
+
const preimage = (await committedDb.getLeafPreimage(
|
|
266
|
+
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
267
|
+
lowLeafResult.index,
|
|
268
|
+
)) as PublicDataTreeLeafPreimage;
|
|
269
|
+
return preimage.leaf.value;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
|
|
274
|
+
* @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
|
|
275
|
+
* @returns An instance of a committed MerkleTreeOperations
|
|
276
|
+
*/
|
|
277
|
+
public async getWorldState(block: BlockParameter) {
|
|
278
|
+
const query = normalizeBlockParameter(block);
|
|
279
|
+
|
|
280
|
+
// When the request anchors on a specific block hash, resolve it against the archiver up front and
|
|
281
|
+
// drive the world-state sync to that exact block number and hash. Resolving against the archiver
|
|
282
|
+
// first fails fast with a clear reorg error if the hash is unknown, and passing the hash to the
|
|
283
|
+
// synchronizer makes the sync reorg-aware: it barriers until the archive-tree commit for that block
|
|
284
|
+
// has landed and verifies it matches the requested fork, instead of syncing to bare latest height
|
|
285
|
+
// and then racing the snapshot read below against an in-flight archive-tree write.
|
|
286
|
+
const requestedHash = 'hash' in query ? query.hash : undefined;
|
|
287
|
+
const anchorBlockNumber = requestedHash !== undefined ? await this.resolveBlockNumber(query) : undefined;
|
|
288
|
+
|
|
289
|
+
let blockSyncedTo: BlockNumber = BlockNumber.ZERO;
|
|
290
|
+
try {
|
|
291
|
+
// Attempt to sync the world state if necessary
|
|
292
|
+
blockSyncedTo = await this.#syncWorldState(anchorBlockNumber, requestedHash);
|
|
293
|
+
} catch (err) {
|
|
294
|
+
this.log.error(`Error getting world state: ${err}`);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if ('tag' in query && query.tag === 'proposed') {
|
|
298
|
+
this.log.debug(`Using committed db for latest block, world state synced upto ${blockSyncedTo}`);
|
|
299
|
+
return this.worldStateSynchronizer.getCommitted();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const blockNumber = anchorBlockNumber ?? (await this.resolveBlockNumber(query));
|
|
303
|
+
|
|
304
|
+
// Check it's within world state sync range
|
|
305
|
+
if (blockNumber > blockSyncedTo) {
|
|
306
|
+
throw new Error(
|
|
307
|
+
`Queried block ${inspectBlockParameter(block)} not yet synced by the node (node is synced upto ${blockSyncedTo}).`,
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
|
|
311
|
+
|
|
312
|
+
const snapshot = this.worldStateSynchronizer.getSnapshot(blockNumber);
|
|
313
|
+
|
|
314
|
+
// Double-check world-state synced to the same block hash as was requested.
|
|
315
|
+
// Block 0 is skipped: the snapshot returned by `getSnapshot(0)` is the *pre*-genesis archive
|
|
316
|
+
// (size 0), so leaf 0 is not yet inserted from that snapshot's view even though block 0's hash
|
|
317
|
+
// does live at archive index 0 in the committed tree. The genesis hash is already validated by
|
|
318
|
+
// the archiver when it resolves the hash query to block number 0.
|
|
319
|
+
if (requestedHash !== undefined && blockNumber !== BlockNumber.ZERO) {
|
|
320
|
+
const blockHash = await snapshot.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
|
|
321
|
+
if (!blockHash || !requestedHash.equals(blockHash)) {
|
|
322
|
+
throw new Error(
|
|
323
|
+
`Block hash ${requestedHash.toString()} not found in world state at block number ${blockNumber} (world state has ${blockHash?.toString() ?? 'no hash'} at that index, genesis header hash is ${this.blockSource.getGenesisBlockHash().toString()}). If the node API has been queried with anchor block hash possibly a reorg has occurred.`,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return snapshot;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Resolves any {@link BlockParameter} variant to a concrete block number. */
|
|
332
|
+
public async resolveBlockNumber(block: BlockParameter): Promise<BlockNumber> {
|
|
333
|
+
const query = normalizeBlockParameter(block);
|
|
334
|
+
const blockNumber = await this.blockSource.getBlockNumber(query);
|
|
335
|
+
if (blockNumber === undefined) {
|
|
336
|
+
if ('hash' in query) {
|
|
337
|
+
throw new Error(
|
|
338
|
+
`Block hash ${query.hash.toString()} not found when querying world state. If the node API has been queried with anchor block hash possibly a reorg has occurred.`,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
if ('archive' in query) {
|
|
342
|
+
throw new Error(`Block with archive ${query.archive.toString()} not found.`);
|
|
343
|
+
}
|
|
344
|
+
throw new Error(`Block not found for ${inspectBlockParameter(block)}.`);
|
|
345
|
+
}
|
|
346
|
+
return blockNumber;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Ensure the world state is synced.
|
|
351
|
+
* @param targetBlockNumber - Block to sync up to. Defaults to the latest block known to the archiver.
|
|
352
|
+
* @param blockHash - If provided, the synchronizer verifies the block at `targetBlockNumber` matches this
|
|
353
|
+
* hash, resyncing (and so detecting reorgs) if it does not yet match or has been reorged away.
|
|
354
|
+
* @returns A promise that fulfils once the world state is synced
|
|
355
|
+
*/
|
|
356
|
+
async #syncWorldState(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
|
|
357
|
+
const target = targetBlockNumber ?? (await this.blockSource.getBlockNumber());
|
|
358
|
+
return await this.worldStateSynchronizer.syncImmediate(target, blockHash);
|
|
359
|
+
}
|
|
360
|
+
}
|
package/src/sentinel/README.md
CHANGED
|
@@ -17,10 +17,10 @@ The sentinel is one of several watchers registered with the slasher; it does not
|
|
|
17
17
|
| Source | What it provides |
|
|
18
18
|
|---|---|
|
|
19
19
|
| `EpochCache` | Slot/epoch helpers, committee + proposer for a slot, escape-hatch state |
|
|
20
|
-
| `L2BlockSource` (archiver) | Synced slot, `
|
|
20
|
+
| `L2BlockSource` (archiver) | Synced slot, `getCheckpoint({ slot })`, `getL2Tips()`, block headers |
|
|
21
21
|
| `P2PClient` | `getCheckpointAttestationsForSlot(slot, payloadHash)`, `hasBlockProposalsForSlot(slot)` |
|
|
22
22
|
| `CheckpointReexecutionTracker` | Local re-execution outcome for the proposal at each slot (`valid` / `invalid` / `unvalidated`) — populated by the validator client's `ProposalHandler` |
|
|
23
|
-
| L1-
|
|
23
|
+
| L1-confirmed checkpoints | Fetched on demand per slot via `archiver.getCheckpoint({ slot })`, yielding the canonical attestor set |
|
|
24
24
|
|
|
25
25
|
## Two cadences
|
|
26
26
|
|
|
@@ -49,7 +49,7 @@ For each slot, the proposer is assigned one of six statuses, ranked highest-conf
|
|
|
49
49
|
|
|
50
50
|
| # | Status | Trigger | Inactive party |
|
|
51
51
|
|---|---|---|---|
|
|
52
|
-
| 6 | `checkpoint-mined` | `
|
|
52
|
+
| 6 | `checkpoint-mined` | `archiver.getCheckpoint({ slot })` returns a checkpoint (one covering this slot has landed on L1) | Attestors who didn't attest |
|
|
53
53
|
| 5 | `checkpoint-valid` | `tracker.getOutcomeForSlot(slot) === 'valid'` | Attestors who didn't attest |
|
|
54
54
|
| 4 | `checkpoint-invalid` | `tracker.getOutcomeForSlot(slot) === 'invalid'` (re-executed and rejected) | Proposer |
|
|
55
55
|
| 3 | `checkpoint-unvalidated` | `tracker.getOutcomeForSlot(slot) === 'unvalidated'` (validation aborted: missing data, timeout, etc.) | Proposer |
|
package/src/sentinel/factory.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { P2PClient } from '@aztec/p2p';
|
|
|
5
5
|
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
6
6
|
import type { CheckpointReexecutionTracker } from '@aztec/stdlib/checkpoint';
|
|
7
7
|
import type { ChainConfig } from '@aztec/stdlib/config';
|
|
8
|
-
import type { SlasherConfig } from '@aztec/stdlib/interfaces/server';
|
|
8
|
+
import type { SlasherConfig, ValidatorClientConfig } from '@aztec/stdlib/interfaces/server';
|
|
9
9
|
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
10
10
|
|
|
11
11
|
import type { SentinelConfig } from './config.js';
|
|
@@ -17,12 +17,24 @@ export async function createSentinel(
|
|
|
17
17
|
archiver: L2BlockSource,
|
|
18
18
|
p2p: P2PClient,
|
|
19
19
|
reexecutionTracker: CheckpointReexecutionTracker,
|
|
20
|
-
config: SentinelConfig &
|
|
20
|
+
config: SentinelConfig &
|
|
21
|
+
DataStoreConfig &
|
|
22
|
+
SlasherConfig &
|
|
23
|
+
Pick<ChainConfig, 'l1ChainId' | 'rollupAddress'> &
|
|
24
|
+
Pick<ValidatorClientConfig, 'disableValidator'>,
|
|
21
25
|
logger = createLogger('node:sentinel'),
|
|
22
26
|
): Promise<Sentinel | undefined> {
|
|
23
|
-
|
|
27
|
+
const runsValidator = !config.disableValidator;
|
|
28
|
+
if (!runsValidator && !config.sentinelEnabled) {
|
|
29
|
+
logger.verbose('Sentinel is disabled');
|
|
24
30
|
return undefined;
|
|
25
31
|
}
|
|
32
|
+
if (runsValidator) {
|
|
33
|
+
logger.info('Enabling sentinel since this node runs a validator');
|
|
34
|
+
} else {
|
|
35
|
+
logger.info('Enabling sentinel from SENTINEL_ENABLED configuration');
|
|
36
|
+
}
|
|
37
|
+
|
|
26
38
|
const kvStore = await createStore('sentinel', SentinelStore.SCHEMA_VERSION, config, logger.getBindings());
|
|
27
39
|
const storeHistoryLength = config.sentinelHistoryLengthInEpochs * epochCache.getL1Constants().epochDuration;
|
|
28
40
|
const storeHistoricEpochPerformanceLength = config.sentinelHistoricEpochPerformanceLengthInEpochs;
|
package/src/sentinel/sentinel.ts
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
2
|
-
import {
|
|
3
|
-
BlockNumber,
|
|
4
|
-
CheckpointNumber,
|
|
5
|
-
CheckpointProposalHash,
|
|
6
|
-
EpochNumber,
|
|
7
|
-
SlotNumber,
|
|
8
|
-
} from '@aztec/foundation/branded-types';
|
|
2
|
+
import { CheckpointNumber, CheckpointProposalHash, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
9
3
|
import { countWhile, filterAsync, fromEntries, getEntries, mapValues } from '@aztec/foundation/collection';
|
|
10
4
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
11
5
|
import { createLogger } from '@aztec/foundation/log';
|
|
12
6
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
13
|
-
import { L2TipsMemoryStore, type L2TipsStore } from '@aztec/kv-store/stores';
|
|
14
7
|
import type { P2PClient } from '@aztec/p2p';
|
|
15
8
|
import {
|
|
16
9
|
OffenseType,
|
|
@@ -21,13 +14,7 @@ import {
|
|
|
21
14
|
getOffenseTypeName,
|
|
22
15
|
} from '@aztec/slasher';
|
|
23
16
|
import type { SlasherConfig } from '@aztec/slasher/config';
|
|
24
|
-
import {
|
|
25
|
-
type L2BlockSource,
|
|
26
|
-
L2BlockStream,
|
|
27
|
-
type L2BlockStreamEvent,
|
|
28
|
-
type L2BlockStreamEventHandler,
|
|
29
|
-
getAttestationInfoFromPublishedCheckpoint,
|
|
30
|
-
} from '@aztec/stdlib/block';
|
|
17
|
+
import { type L2BlockSource, getAttestationInfoFromPublishedCheckpoint } from '@aztec/stdlib/block';
|
|
31
18
|
import type { CheckpointReexecutionTracker } from '@aztec/stdlib/checkpoint';
|
|
32
19
|
import type { ChainConfig } from '@aztec/stdlib/config';
|
|
33
20
|
import { getEpochAtSlot, getSlotRangeForEpoch, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
@@ -97,7 +84,7 @@ function statusToCategory(status: ValidatorStatusInSlot): ValidatorStatusType {
|
|
|
97
84
|
* first:
|
|
98
85
|
*
|
|
99
86
|
* - `checkpoint-mined` — a checkpoint covering this slot has landed on L1
|
|
100
|
-
* (
|
|
87
|
+
* (fetched on demand via `archiver.getCheckpoint({ slot })`).
|
|
101
88
|
* - `checkpoint-valid` — the local node re-executed a checkpoint proposal for this slot
|
|
102
89
|
* successfully (consulted via `CheckpointReexecutionTracker`).
|
|
103
90
|
* - `checkpoint-invalid` — the local node re-executed a checkpoint proposal for this slot
|
|
@@ -135,25 +122,13 @@ function statusToCategory(status: ValidatorStatusInSlot): ValidatorStatusType {
|
|
|
135
122
|
* (no history entries for that slot) and per-epoch evaluation writes an empty performance map
|
|
136
123
|
* (no slashing).
|
|
137
124
|
*/
|
|
138
|
-
export class Sentinel extends (EventEmitter as new () => WatcherEmitter) implements
|
|
125
|
+
export class Sentinel extends (EventEmitter as new () => WatcherEmitter) implements Watcher {
|
|
139
126
|
protected runningPromise: RunningPromise;
|
|
140
|
-
protected blockStream!: L2BlockStream;
|
|
141
|
-
protected l2TipsStore: L2TipsStore;
|
|
142
127
|
|
|
143
128
|
protected initialSlot: SlotNumber | undefined;
|
|
144
129
|
protected lastProcessedSlot: SlotNumber | undefined;
|
|
145
130
|
/** Largest epoch number for which the end-of-epoch aggregator has run. */
|
|
146
131
|
protected lastEvaluatedEpoch: EpochNumber | undefined;
|
|
147
|
-
protected slotNumberToCheckpoint: Map<
|
|
148
|
-
SlotNumber,
|
|
149
|
-
{
|
|
150
|
-
checkpointNumber: CheckpointNumber;
|
|
151
|
-
archive: string;
|
|
152
|
-
/** Hex keccak256 of the consensus payload bytes; used to fetch matching p2p attestations. */
|
|
153
|
-
proposalPayloadHash: CheckpointProposalHash;
|
|
154
|
-
attestors: EthAddress[];
|
|
155
|
-
}
|
|
156
|
-
> = new Map();
|
|
157
132
|
|
|
158
133
|
constructor(
|
|
159
134
|
protected epochCache: EpochCache,
|
|
@@ -165,7 +140,6 @@ export class Sentinel extends (EventEmitter as new () => WatcherEmitter) impleme
|
|
|
165
140
|
protected logger = createLogger('node:sentinel'),
|
|
166
141
|
) {
|
|
167
142
|
super();
|
|
168
|
-
this.l2TipsStore = new L2TipsMemoryStore(archiver.getGenesisBlockHash());
|
|
169
143
|
const interval = (epochCache.getL1Constants().ethereumSlotDuration * 1000) / 4;
|
|
170
144
|
this.runningPromise = new RunningPromise(this.work.bind(this), logger, interval);
|
|
171
145
|
}
|
|
@@ -187,17 +161,14 @@ export class Sentinel extends (EventEmitter as new () => WatcherEmitter) impleme
|
|
|
187
161
|
}
|
|
188
162
|
|
|
189
163
|
/**
|
|
190
|
-
* Loads initial slot
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* (cold start).
|
|
164
|
+
* Loads the initial slot. We will not process anything at or before the initial slot. Floors at the
|
|
165
|
+
* archiver's synced L2 slot so the sentinel keeps making forward progress when L1 is advancing but L2 has no
|
|
166
|
+
* activity (the synced slot is driven by L1 sync, not by L2 blocks). Falls back to the wallclock if the
|
|
167
|
+
* archiver isn't ready yet (cold start).
|
|
195
168
|
*/
|
|
196
169
|
protected async init() {
|
|
197
170
|
this.initialSlot = await this.getCurrentSlot();
|
|
198
|
-
|
|
199
|
-
this.logger.info(`Starting validator sentinel with initial slot ${this.initialSlot} and block ${startingBlock}`);
|
|
200
|
-
this.blockStream = new L2BlockStream(this.archiver, this.l2TipsStore, this, this.logger, { startingBlock });
|
|
171
|
+
this.logger.info(`Starting validator sentinel with initial slot ${this.initialSlot}`);
|
|
201
172
|
}
|
|
202
173
|
|
|
203
174
|
/**
|
|
@@ -215,44 +186,38 @@ export class Sentinel extends (EventEmitter as new () => WatcherEmitter) impleme
|
|
|
215
186
|
return this.runningPromise.stop();
|
|
216
187
|
}
|
|
217
188
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
protected
|
|
226
|
-
|
|
227
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Fetches the L1-confirmed checkpoint covering a slot (if any) and derives the slot-level data the
|
|
191
|
+
* activity classifier needs: the checkpoint number, archive root, consensus payload hash (used to fetch
|
|
192
|
+
* matching p2p attestations regardless of feeAssetPriceModifier variants), and the recovered attestor set.
|
|
193
|
+
* Reads on demand so the result is always against the canonical chain — a reorged-out checkpoint simply
|
|
194
|
+
* stops being returned, with no stale mapping to clean up.
|
|
195
|
+
*/
|
|
196
|
+
protected async getCheckpointForSlot(slot: SlotNumber): Promise<
|
|
197
|
+
| {
|
|
198
|
+
checkpointNumber: CheckpointNumber;
|
|
199
|
+
archive: string;
|
|
200
|
+
proposalPayloadHash: CheckpointProposalHash;
|
|
201
|
+
attestors: EthAddress[];
|
|
202
|
+
}
|
|
203
|
+
| undefined
|
|
204
|
+
> {
|
|
205
|
+
const checkpoint = await this.archiver.getCheckpoint({ slot });
|
|
206
|
+
if (!checkpoint) {
|
|
207
|
+
return undefined;
|
|
228
208
|
}
|
|
229
|
-
const checkpoint = event.checkpoint;
|
|
230
|
-
|
|
231
|
-
// Store mapping from slot to archive, checkpoint number, attestors, and the consensus payload
|
|
232
|
-
// hash (used to query matching p2p attestations regardless of feeAssetPriceModifier variants).
|
|
233
209
|
const signatureContext = this.getSignatureContext();
|
|
234
210
|
const proposalPayloadHash = CheckpointProposalHash.fromBuffer(
|
|
235
211
|
ConsensusPayload.fromCheckpoint(checkpoint.checkpoint, signatureContext).getPayloadHash(),
|
|
236
212
|
);
|
|
237
|
-
|
|
213
|
+
return {
|
|
238
214
|
checkpointNumber: checkpoint.checkpoint.number,
|
|
239
215
|
archive: checkpoint.checkpoint.archive.root.toString(),
|
|
240
216
|
proposalPayloadHash,
|
|
241
217
|
attestors: getAttestationInfoFromPublishedCheckpoint(checkpoint, signatureContext)
|
|
242
218
|
.filter(a => a.status === 'recovered-from-signature')
|
|
243
219
|
.map(a => a.address!),
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// Prune the archive map to only keep at most N entries
|
|
247
|
-
const historyLength = this.store.getHistoryLength();
|
|
248
|
-
if (this.slotNumberToCheckpoint.size > historyLength) {
|
|
249
|
-
const toDelete = Array.from(this.slotNumberToCheckpoint.keys())
|
|
250
|
-
.sort((a, b) => Number(a - b))
|
|
251
|
-
.slice(0, this.slotNumberToCheckpoint.size - historyLength);
|
|
252
|
-
for (const key of toDelete) {
|
|
253
|
-
this.slotNumberToCheckpoint.delete(key);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
220
|
+
};
|
|
256
221
|
}
|
|
257
222
|
|
|
258
223
|
/**
|
|
@@ -387,10 +352,6 @@ export class Sentinel extends (EventEmitter as new () => WatcherEmitter) impleme
|
|
|
387
352
|
public async work() {
|
|
388
353
|
const currentSlot = await this.getCurrentSlot();
|
|
389
354
|
try {
|
|
390
|
-
// Manually sync the block stream to ensure we have the latest data.
|
|
391
|
-
// Note we never `start` the blockstream, so it loops at the same pace as we do.
|
|
392
|
-
await this.blockStream.sync();
|
|
393
|
-
|
|
394
355
|
// Per-slot activity recording (lag = 2 slots for P2P attestation settlement).
|
|
395
356
|
const targetSlot = await this.isReadyToProcess(currentSlot);
|
|
396
357
|
if (targetSlot !== false) {
|
|
@@ -478,7 +439,7 @@ export class Sentinel extends (EventEmitter as new () => WatcherEmitter) impleme
|
|
|
478
439
|
return false;
|
|
479
440
|
}
|
|
480
441
|
|
|
481
|
-
const archiverLastBlockHash = await this.
|
|
442
|
+
const archiverLastBlockHash = await this.archiver.getL2Tips().then(tip => tip.proposed.hash);
|
|
482
443
|
const p2pLastBlockHash = await this.p2p.getL2Tips().then(tips => tips.proposed.hash);
|
|
483
444
|
const isP2pSynced = archiverLastBlockHash === p2pLastBlockHash;
|
|
484
445
|
if (!isP2pSynced) {
|
|
@@ -534,8 +495,9 @@ export class Sentinel extends (EventEmitter as new () => WatcherEmitter) impleme
|
|
|
534
495
|
this.logger.debug(`Computing stats for slot ${slot} at epoch ${epoch}`, { slot, epoch, proposer, committee });
|
|
535
496
|
|
|
536
497
|
// Gather attestors from both p2p (live attestations) and the archiver (signers on the
|
|
537
|
-
// checkpoint if one has landed on L1).
|
|
538
|
-
|
|
498
|
+
// checkpoint if one has landed on L1). Fetched on demand so it always reflects the canonical chain.
|
|
499
|
+
// Used regardless of which case applies.
|
|
500
|
+
const checkpoint = await this.getCheckpointForSlot(slot);
|
|
539
501
|
const p2pAttested = await this.p2p.getCheckpointAttestationsForSlot(slot, checkpoint?.proposalPayloadHash);
|
|
540
502
|
const p2pAttestors = p2pAttested.map(a => a.getSender()).filter((s): s is EthAddress => s !== undefined);
|
|
541
503
|
const attestors = new Set(
|