@aztec/world-state 0.1.0-alpha11
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/.eslintrc.cjs +1 -0
- package/.tsbuildinfo +1 -0
- package/README.md +40 -0
- package/dest/index.d.ts +4 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +4 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.d.ts +117 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.d.ts.map +1 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.js +133 -0
- package/dest/synchroniser/config.d.ts +19 -0
- package/dest/synchroniser/config.d.ts.map +1 -0
- package/dest/synchroniser/config.js +13 -0
- package/dest/synchroniser/index.d.ts +3 -0
- package/dest/synchroniser/index.d.ts.map +1 -0
- package/dest/synchroniser/index.js +3 -0
- package/dest/synchroniser/server_world_state_synchroniser.d.ts +62 -0
- package/dest/synchroniser/server_world_state_synchroniser.d.ts.map +1 -0
- package/dest/synchroniser/server_world_state_synchroniser.js +134 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.d.ts +2 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.d.ts.map +1 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.js +215 -0
- package/dest/synchroniser/world_state_synchroniser.d.ts +34 -0
- package/dest/synchroniser/world_state_synchroniser.d.ts.map +1 -0
- package/dest/synchroniser/world_state_synchroniser.js +11 -0
- package/dest/utils.d.ts +12 -0
- package/dest/utils.d.ts.map +1 -0
- package/dest/utils.js +14 -0
- package/dest/world-state-db/index.d.ts +165 -0
- package/dest/world-state-db/index.d.ts.map +1 -0
- package/dest/world-state-db/index.js +21 -0
- package/dest/world-state-db/merkle_trees.d.ts +200 -0
- package/dest/world-state-db/merkle_trees.d.ts.map +1 -0
- package/dest/world-state-db/merkle_trees.js +373 -0
- package/package.json +15 -0
- package/src/index.ts +3 -0
- package/src/merkle-tree/merkle_tree_operations_facade.ts +164 -0
- package/src/synchroniser/config.ts +27 -0
- package/src/synchroniser/index.ts +2 -0
- package/src/synchroniser/server_world_state_synchroniser.test.ts +281 -0
- package/src/synchroniser/server_world_state_synchroniser.ts +151 -0
- package/src/synchroniser/world_state_synchroniser.ts +36 -0
- package/src/utils.ts +24 -0
- package/src/world-state-db/index.ts +215 -0
- package/src/world-state-db/merkle_trees.ts +547 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AppendOnlyTreeSnapshot,
|
|
3
|
+
CircuitsWasm,
|
|
4
|
+
GlobalVariables,
|
|
5
|
+
KERNEL_NEW_COMMITMENTS_LENGTH,
|
|
6
|
+
KERNEL_NEW_CONTRACTS_LENGTH,
|
|
7
|
+
KERNEL_NEW_NULLIFIERS_LENGTH,
|
|
8
|
+
KERNEL_PUBLIC_DATA_UPDATE_REQUESTS_LENGTH,
|
|
9
|
+
NEW_L2_TO_L1_MSGS_LENGTH,
|
|
10
|
+
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
|
|
11
|
+
} from '@aztec/circuits.js';
|
|
12
|
+
import { INITIAL_LEAF, Pedersen, SiblingPath } from '@aztec/merkle-tree';
|
|
13
|
+
import { ContractData, L2Block, L2BlockL2Logs, L2BlockSource, MerkleTreeId, PublicDataWrite } from '@aztec/types';
|
|
14
|
+
import { jest } from '@jest/globals';
|
|
15
|
+
import { MerkleTreeDb } from '../index.js';
|
|
16
|
+
import { ServerWorldStateSynchroniser } from './server_world_state_synchroniser.js';
|
|
17
|
+
import { WorldStateRunningState } from './world_state_synchroniser.js';
|
|
18
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
19
|
+
import { sleep } from '@aztec/foundation/sleep';
|
|
20
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
21
|
+
import times from 'lodash.times';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Generic mock implementation.
|
|
25
|
+
*/
|
|
26
|
+
type Mockify<T> = {
|
|
27
|
+
[P in keyof T]: jest.Mock;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const LATEST_BLOCK_NUMBER = 5;
|
|
31
|
+
const getLatestBlockNumber = () => LATEST_BLOCK_NUMBER;
|
|
32
|
+
let nextBlocks: L2Block[] = [];
|
|
33
|
+
const consumeNextBlocks = () => {
|
|
34
|
+
const blocks = nextBlocks;
|
|
35
|
+
nextBlocks = [];
|
|
36
|
+
return Promise.resolve(blocks);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const getMockTreeSnapshot = () => {
|
|
40
|
+
return new AppendOnlyTreeSnapshot(Fr.random(), 16);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const getMockContractData = () => {
|
|
44
|
+
return ContractData.random();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const getMockGlobalVariables = () => {
|
|
48
|
+
return GlobalVariables.from({
|
|
49
|
+
chainId: Fr.random(),
|
|
50
|
+
version: Fr.random(),
|
|
51
|
+
blockNumber: Fr.random(),
|
|
52
|
+
timestamp: Fr.random(),
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const getMockL1ToL2MessagesData = () => {
|
|
57
|
+
return new Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).map(() => Fr.random());
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const getMockBlock = (blockNumber: number, newContractsCommitments?: Buffer[]) => {
|
|
61
|
+
const newEncryptedLogs = L2BlockL2Logs.random(1, 2, 3);
|
|
62
|
+
const block = L2Block.fromFields({
|
|
63
|
+
number: blockNumber,
|
|
64
|
+
globalVariables: getMockGlobalVariables(),
|
|
65
|
+
startPrivateDataTreeSnapshot: getMockTreeSnapshot(),
|
|
66
|
+
startNullifierTreeSnapshot: getMockTreeSnapshot(),
|
|
67
|
+
startContractTreeSnapshot: getMockTreeSnapshot(),
|
|
68
|
+
startTreeOfHistoricPrivateDataTreeRootsSnapshot: getMockTreeSnapshot(),
|
|
69
|
+
startTreeOfHistoricContractTreeRootsSnapshot: getMockTreeSnapshot(),
|
|
70
|
+
startPublicDataTreeRoot: Fr.random(),
|
|
71
|
+
startL1ToL2MessageTreeSnapshot: getMockTreeSnapshot(),
|
|
72
|
+
startTreeOfHistoricL1ToL2MessageTreeRootsSnapshot: getMockTreeSnapshot(),
|
|
73
|
+
endPrivateDataTreeSnapshot: getMockTreeSnapshot(),
|
|
74
|
+
endNullifierTreeSnapshot: getMockTreeSnapshot(),
|
|
75
|
+
endContractTreeSnapshot: getMockTreeSnapshot(),
|
|
76
|
+
endTreeOfHistoricPrivateDataTreeRootsSnapshot: getMockTreeSnapshot(),
|
|
77
|
+
endTreeOfHistoricContractTreeRootsSnapshot: getMockTreeSnapshot(),
|
|
78
|
+
endPublicDataTreeRoot: Fr.random(),
|
|
79
|
+
endL1ToL2MessageTreeSnapshot: getMockTreeSnapshot(),
|
|
80
|
+
endTreeOfHistoricL1ToL2MessageTreeRootsSnapshot: getMockTreeSnapshot(),
|
|
81
|
+
newCommitments: times(KERNEL_NEW_COMMITMENTS_LENGTH, Fr.random),
|
|
82
|
+
newNullifiers: times(KERNEL_NEW_NULLIFIERS_LENGTH, Fr.random),
|
|
83
|
+
newContracts: newContractsCommitments?.map(x => Fr.fromBuffer(x)) ?? [Fr.random()],
|
|
84
|
+
newContractData: times(KERNEL_NEW_CONTRACTS_LENGTH, getMockContractData),
|
|
85
|
+
newPublicDataWrites: times(KERNEL_PUBLIC_DATA_UPDATE_REQUESTS_LENGTH, PublicDataWrite.random),
|
|
86
|
+
newL1ToL2Messages: getMockL1ToL2MessagesData(),
|
|
87
|
+
newL2ToL1Msgs: times(NEW_L2_TO_L1_MSGS_LENGTH, Fr.random),
|
|
88
|
+
newEncryptedLogs,
|
|
89
|
+
});
|
|
90
|
+
return block;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const createSynchroniser = (merkleTreeDb: any, rollupSource: any) =>
|
|
94
|
+
new ServerWorldStateSynchroniser(merkleTreeDb as MerkleTreeDb, rollupSource as L2BlockSource);
|
|
95
|
+
|
|
96
|
+
const log = createLogger('aztec:server_world_state_synchroniser_test');
|
|
97
|
+
|
|
98
|
+
describe('server_world_state_synchroniser', () => {
|
|
99
|
+
const rollupSource: Mockify<Pick<L2BlockSource, 'getBlockHeight' | 'getL2Blocks'>> = {
|
|
100
|
+
getBlockHeight: jest.fn().mockImplementation(getLatestBlockNumber),
|
|
101
|
+
getL2Blocks: jest.fn().mockImplementation(consumeNextBlocks),
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const merkleTreeDb: Mockify<MerkleTreeDb> = {
|
|
105
|
+
getTreeInfo: jest
|
|
106
|
+
.fn()
|
|
107
|
+
.mockImplementation(() =>
|
|
108
|
+
Promise.resolve({ treeId: MerkleTreeId.CONTRACT_TREE, root: Buffer.alloc(32, 0), size: 0n }),
|
|
109
|
+
),
|
|
110
|
+
appendLeaves: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
111
|
+
updateLeaf: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
112
|
+
getSiblingPath: jest.fn().mockImplementation(() => {
|
|
113
|
+
return async () => {
|
|
114
|
+
const wasm = await CircuitsWasm.get();
|
|
115
|
+
const pedersen: Pedersen = new Pedersen(wasm);
|
|
116
|
+
SiblingPath.ZERO(32, INITIAL_LEAF, pedersen);
|
|
117
|
+
}; //Promise.resolve();
|
|
118
|
+
}),
|
|
119
|
+
updateHistoricRootsTrees: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
120
|
+
commit: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
121
|
+
rollback: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
122
|
+
handleL2Block: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
123
|
+
} as any;
|
|
124
|
+
|
|
125
|
+
it('can be constructed', () => {
|
|
126
|
+
expect(() => createSynchroniser(merkleTreeDb, rollupSource)).not.toThrow();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('updates sync progress', async () => {
|
|
130
|
+
const server = createSynchroniser(merkleTreeDb, rollupSource);
|
|
131
|
+
|
|
132
|
+
// test initial state
|
|
133
|
+
let status = await server.status();
|
|
134
|
+
expect(status.syncedToL2Block).toEqual(0);
|
|
135
|
+
expect(status.state).toEqual(WorldStateRunningState.IDLE);
|
|
136
|
+
|
|
137
|
+
// create an initial block
|
|
138
|
+
let currentBlockNumber = 0;
|
|
139
|
+
nextBlocks = [getMockBlock(currentBlockNumber + 1)];
|
|
140
|
+
|
|
141
|
+
// start the sync process but don't await
|
|
142
|
+
server.start().catch(err => log('Sync not completed: ', err));
|
|
143
|
+
|
|
144
|
+
// now setup a loop to monitor the sync progress and push new blocks in
|
|
145
|
+
while (currentBlockNumber <= LATEST_BLOCK_NUMBER) {
|
|
146
|
+
status = await server.status();
|
|
147
|
+
expect(
|
|
148
|
+
status.syncedToL2Block >= currentBlockNumber || status.syncedToL2Block <= currentBlockNumber + 1,
|
|
149
|
+
).toBeTruthy();
|
|
150
|
+
if (status.syncedToL2Block === LATEST_BLOCK_NUMBER) {
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
expect(
|
|
154
|
+
status.state >= WorldStateRunningState.IDLE || status.state <= WorldStateRunningState.SYNCHING,
|
|
155
|
+
).toBeTruthy();
|
|
156
|
+
if (status.syncedToL2Block === currentBlockNumber) {
|
|
157
|
+
await sleep(100);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
currentBlockNumber++;
|
|
161
|
+
nextBlocks = [getMockBlock(currentBlockNumber + 1)];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// check the status agian, should be fully synced
|
|
165
|
+
status = await server.status();
|
|
166
|
+
expect(status.state).toEqual(WorldStateRunningState.RUNNING);
|
|
167
|
+
expect(status.syncedToL2Block).toEqual(LATEST_BLOCK_NUMBER);
|
|
168
|
+
|
|
169
|
+
// stop the synchroniser
|
|
170
|
+
await server.stop();
|
|
171
|
+
|
|
172
|
+
// check the final status
|
|
173
|
+
status = await server.status();
|
|
174
|
+
expect(status.state).toEqual(WorldStateRunningState.STOPPED);
|
|
175
|
+
expect(status.syncedToL2Block).toEqual(LATEST_BLOCK_NUMBER);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('enables blocking until synced', async () => {
|
|
179
|
+
const server = createSynchroniser(merkleTreeDb, rollupSource);
|
|
180
|
+
let currentBlockNumber = 0;
|
|
181
|
+
|
|
182
|
+
const newBlocks = async () => {
|
|
183
|
+
while (currentBlockNumber <= LATEST_BLOCK_NUMBER) {
|
|
184
|
+
await sleep(100);
|
|
185
|
+
nextBlocks = [...nextBlocks, getMockBlock(++currentBlockNumber)];
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// kick off the background queueing of blocks
|
|
190
|
+
const newBlockPromise = newBlocks();
|
|
191
|
+
|
|
192
|
+
// kick off the synching
|
|
193
|
+
const syncPromise = server.start();
|
|
194
|
+
|
|
195
|
+
// await the synching
|
|
196
|
+
await syncPromise;
|
|
197
|
+
|
|
198
|
+
await newBlockPromise;
|
|
199
|
+
|
|
200
|
+
let status = await server.status();
|
|
201
|
+
expect(status.state).toEqual(WorldStateRunningState.RUNNING);
|
|
202
|
+
expect(status.syncedToL2Block).toEqual(LATEST_BLOCK_NUMBER);
|
|
203
|
+
await server.stop();
|
|
204
|
+
status = await server.status();
|
|
205
|
+
expect(status.state).toEqual(WorldStateRunningState.STOPPED);
|
|
206
|
+
expect(status.syncedToL2Block).toEqual(LATEST_BLOCK_NUMBER);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('handles multiple calls to start', async () => {
|
|
210
|
+
const server = createSynchroniser(merkleTreeDb, rollupSource);
|
|
211
|
+
let currentBlockNumber = 0;
|
|
212
|
+
|
|
213
|
+
const newBlocks = async () => {
|
|
214
|
+
while (currentBlockNumber < LATEST_BLOCK_NUMBER) {
|
|
215
|
+
await sleep(100);
|
|
216
|
+
const newBlock = getMockBlock(++currentBlockNumber);
|
|
217
|
+
nextBlocks = [...nextBlocks, newBlock];
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// kick off the background queueing of blocks
|
|
222
|
+
const newBlockPromise = newBlocks();
|
|
223
|
+
|
|
224
|
+
// kick off the synching
|
|
225
|
+
await server.start();
|
|
226
|
+
|
|
227
|
+
// call start again, should get back the same promise
|
|
228
|
+
await server.start();
|
|
229
|
+
|
|
230
|
+
// wait until the block production has finished
|
|
231
|
+
await newBlockPromise;
|
|
232
|
+
|
|
233
|
+
await server.stop();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('immediately syncs if no new blocks', async () => {
|
|
237
|
+
const server = createSynchroniser(merkleTreeDb, rollupSource);
|
|
238
|
+
rollupSource.getBlockHeight.mockImplementationOnce(() => {
|
|
239
|
+
return Promise.resolve(0);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// kick off the synching
|
|
243
|
+
const syncPromise = server.start();
|
|
244
|
+
|
|
245
|
+
// it should already be synced, no need to push new blocks
|
|
246
|
+
await syncPromise;
|
|
247
|
+
|
|
248
|
+
const status = await server.status();
|
|
249
|
+
expect(status.state).toBe(WorldStateRunningState.RUNNING);
|
|
250
|
+
expect(status.syncedToL2Block).toBe(0);
|
|
251
|
+
await server.stop();
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("can't be started if already stopped", async () => {
|
|
255
|
+
const server = createSynchroniser(merkleTreeDb, rollupSource);
|
|
256
|
+
rollupSource.getBlockHeight.mockImplementationOnce(() => {
|
|
257
|
+
return Promise.resolve(0);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// kick off the synching
|
|
261
|
+
const syncPromise = server.start();
|
|
262
|
+
await syncPromise;
|
|
263
|
+
await server.stop();
|
|
264
|
+
|
|
265
|
+
await expect(server.start()).rejects.toThrow();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('adds the received L2 blocks', async () => {
|
|
269
|
+
merkleTreeDb.handleL2Block.mockReset();
|
|
270
|
+
const server = createSynchroniser(merkleTreeDb, rollupSource);
|
|
271
|
+
const totalBlocks = LATEST_BLOCK_NUMBER + 1;
|
|
272
|
+
nextBlocks = Array(totalBlocks)
|
|
273
|
+
.fill(0)
|
|
274
|
+
.map((_, index) => getMockBlock(index, [Buffer.alloc(32, index)]));
|
|
275
|
+
// sync the server
|
|
276
|
+
await server.start();
|
|
277
|
+
|
|
278
|
+
expect(merkleTreeDb.handleL2Block).toHaveBeenCalledTimes(totalBlocks);
|
|
279
|
+
await server.stop();
|
|
280
|
+
});
|
|
281
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { L2Block, L2BlockDownloader, L2BlockSource } from '@aztec/types';
|
|
2
|
+
import { MerkleTreeDb, MerkleTreeOperations } from '../index.js';
|
|
3
|
+
import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js';
|
|
4
|
+
import { WorldStateRunningState, WorldStateStatus, WorldStateSynchroniser } from './world_state_synchroniser.js';
|
|
5
|
+
import { getConfigEnvVars } from './config.js';
|
|
6
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Synchronises the world state with the L2 blocks from a L2BlockSource.
|
|
10
|
+
* The synchroniser will download the L2 blocks from the L2BlockSource and insert the new commitments into the merkle
|
|
11
|
+
* tree.
|
|
12
|
+
*/
|
|
13
|
+
export class ServerWorldStateSynchroniser implements WorldStateSynchroniser {
|
|
14
|
+
private currentL2BlockNum = 0;
|
|
15
|
+
private latestBlockNumberAtStart = 0;
|
|
16
|
+
private l2BlockDownloader: L2BlockDownloader;
|
|
17
|
+
private syncPromise: Promise<void> = Promise.resolve();
|
|
18
|
+
private syncResolve?: () => void = undefined;
|
|
19
|
+
private stopping = false;
|
|
20
|
+
private runningPromise: Promise<void> = Promise.resolve();
|
|
21
|
+
private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
private merkleTreeDb: MerkleTreeDb,
|
|
25
|
+
private l2BlockSource: L2BlockSource,
|
|
26
|
+
private log = createDebugLogger('aztec:world_state'),
|
|
27
|
+
) {
|
|
28
|
+
const config = getConfigEnvVars();
|
|
29
|
+
this.l2BlockDownloader = new L2BlockDownloader(l2BlockSource, config.l2QueueSize, config.checkInterval);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns an instance of MerkleTreeOperations that will include uncommitted data.
|
|
34
|
+
* @returns An instance of MerkleTreeOperations that will include uncommitted data.
|
|
35
|
+
*/
|
|
36
|
+
public getLatest(): MerkleTreeOperations {
|
|
37
|
+
return new MerkleTreeOperationsFacade(this.merkleTreeDb, true);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Returns an instance of MerkleTreeOperations that will not include uncommitted data.
|
|
42
|
+
* @returns An instance of MerkleTreeOperations that will not include uncommitted data.
|
|
43
|
+
*/
|
|
44
|
+
public getCommitted(): MerkleTreeOperations {
|
|
45
|
+
return new MerkleTreeOperationsFacade(this.merkleTreeDb, false);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Starts the synchroniser.
|
|
50
|
+
* @returns A promise that resolves once the initial sync is completed.
|
|
51
|
+
*/
|
|
52
|
+
public async start() {
|
|
53
|
+
if (this.currentState === WorldStateRunningState.STOPPED) {
|
|
54
|
+
throw new Error('Synchroniser already stopped');
|
|
55
|
+
}
|
|
56
|
+
if (this.currentState !== WorldStateRunningState.IDLE) {
|
|
57
|
+
return this.syncPromise;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// get the current latest block number
|
|
61
|
+
this.latestBlockNumberAtStart = await this.l2BlockSource.getBlockHeight();
|
|
62
|
+
|
|
63
|
+
const blockToDownloadFrom = this.currentL2BlockNum + 1;
|
|
64
|
+
|
|
65
|
+
// if there are blocks to be retrieved, go to a synching state
|
|
66
|
+
if (blockToDownloadFrom <= this.latestBlockNumberAtStart) {
|
|
67
|
+
this.setCurrentState(WorldStateRunningState.SYNCHING);
|
|
68
|
+
this.syncPromise = new Promise(resolve => {
|
|
69
|
+
this.syncResolve = resolve;
|
|
70
|
+
});
|
|
71
|
+
this.log(`Starting sync from ${blockToDownloadFrom}, latest block ${this.latestBlockNumberAtStart}`);
|
|
72
|
+
} else {
|
|
73
|
+
// if no blocks to be retrieved, go straight to running
|
|
74
|
+
this.setCurrentState(WorldStateRunningState.RUNNING);
|
|
75
|
+
this.syncPromise = Promise.resolve();
|
|
76
|
+
this.log(`Next block ${blockToDownloadFrom} already beyond latest block at ${this.latestBlockNumberAtStart}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// start looking for further blocks
|
|
80
|
+
const blockProcess = async () => {
|
|
81
|
+
while (!this.stopping) {
|
|
82
|
+
const blocks = await this.l2BlockDownloader.getL2Blocks();
|
|
83
|
+
await this.handleL2Blocks(blocks);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
this.runningPromise = blockProcess();
|
|
87
|
+
this.l2BlockDownloader.start(blockToDownloadFrom);
|
|
88
|
+
this.log(`Started block downloader from block ${blockToDownloadFrom}`);
|
|
89
|
+
return this.syncPromise;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Stops the synchroniser.
|
|
94
|
+
*/
|
|
95
|
+
public async stop() {
|
|
96
|
+
this.log('Stopping world state...');
|
|
97
|
+
this.stopping = true;
|
|
98
|
+
await this.l2BlockDownloader.stop();
|
|
99
|
+
await this.runningPromise;
|
|
100
|
+
this.setCurrentState(WorldStateRunningState.STOPPED);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns the current status of the synchroniser.
|
|
105
|
+
* @returns The current status of the synchroniser.
|
|
106
|
+
*/
|
|
107
|
+
public status(): Promise<WorldStateStatus> {
|
|
108
|
+
const status = {
|
|
109
|
+
syncedToL2Block: this.currentL2BlockNum,
|
|
110
|
+
state: this.currentState,
|
|
111
|
+
} as WorldStateStatus;
|
|
112
|
+
return Promise.resolve(status);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Handles a list of L2 blocks (i.e. Inserts the new commitments into the merkle tree).
|
|
117
|
+
* @param l2Blocks - The L2 blocks to handle.
|
|
118
|
+
*/
|
|
119
|
+
private async handleL2Blocks(l2Blocks: L2Block[]) {
|
|
120
|
+
for (const l2Block of l2Blocks) {
|
|
121
|
+
await this.handleL2Block(l2Block);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
|
|
127
|
+
* @param l2Block - The L2 block to handle.
|
|
128
|
+
*/
|
|
129
|
+
private async handleL2Block(l2Block: L2Block) {
|
|
130
|
+
await this.merkleTreeDb.handleL2Block(l2Block);
|
|
131
|
+
this.currentL2BlockNum = l2Block.number;
|
|
132
|
+
if (
|
|
133
|
+
this.currentState === WorldStateRunningState.SYNCHING &&
|
|
134
|
+
this.currentL2BlockNum >= this.latestBlockNumberAtStart
|
|
135
|
+
) {
|
|
136
|
+
this.setCurrentState(WorldStateRunningState.RUNNING);
|
|
137
|
+
if (this.syncResolve !== undefined) {
|
|
138
|
+
this.syncResolve();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Method to set the value of the current state.
|
|
145
|
+
* @param newState - New state value.
|
|
146
|
+
*/
|
|
147
|
+
private setCurrentState(newState: WorldStateRunningState) {
|
|
148
|
+
this.currentState = newState;
|
|
149
|
+
this.log(`Moved to state ${WorldStateRunningState[this.currentState]}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { MerkleTreeOperations } from '../index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Defines the possible states of the world state synchroniser.
|
|
5
|
+
*/
|
|
6
|
+
export enum WorldStateRunningState {
|
|
7
|
+
IDLE,
|
|
8
|
+
SYNCHING,
|
|
9
|
+
RUNNING,
|
|
10
|
+
STOPPED,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Defines the status of the world state synchroniser.
|
|
15
|
+
*/
|
|
16
|
+
export interface WorldStateStatus {
|
|
17
|
+
/**
|
|
18
|
+
* The current state of the world state synchroniser.
|
|
19
|
+
*/
|
|
20
|
+
state: WorldStateRunningState;
|
|
21
|
+
/**
|
|
22
|
+
* The block number that the world state synchroniser is synced to.
|
|
23
|
+
*/
|
|
24
|
+
syncedToL2Block: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Defines the interface for a world state synchroniser.
|
|
29
|
+
*/
|
|
30
|
+
export interface WorldStateSynchroniser {
|
|
31
|
+
start(): void;
|
|
32
|
+
status(): Promise<WorldStateStatus>;
|
|
33
|
+
stop(): Promise<void>;
|
|
34
|
+
getLatest(): MerkleTreeOperations;
|
|
35
|
+
getCommitted(): MerkleTreeOperations;
|
|
36
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { GeneratorIndex } from '@aztec/circuits.js';
|
|
2
|
+
import { pedersenCompressWithHashIndex } from '@aztec/circuits.js/barretenberg';
|
|
3
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
4
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
5
|
+
import { toBigInt } from '@aztec/foundation/serialize';
|
|
6
|
+
|
|
7
|
+
import { IWasmModule } from '@aztec/foundation/wasm';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Computes the index in the public data tree for a given contract and storage slot.
|
|
11
|
+
* @param contract - Address of the contract who owns the storage.
|
|
12
|
+
* @param slot - Slot within the contract storage.
|
|
13
|
+
* @param bbWasm - Wasm module for computing the hash.
|
|
14
|
+
* @returns The leaf index of the public data tree that maps to this storage slot.
|
|
15
|
+
*/
|
|
16
|
+
export function computePublicDataTreeLeafIndex(contract: AztecAddress, slot: Fr, wasm: IWasmModule): bigint {
|
|
17
|
+
return toBigInt(
|
|
18
|
+
pedersenCompressWithHashIndex(
|
|
19
|
+
wasm,
|
|
20
|
+
[contract, slot].map(f => f.toBuffer()),
|
|
21
|
+
GeneratorIndex.PUBLIC_LEAF_INDEX,
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
}
|