@aztec/world-state 0.0.1-commit.b655e406 → 0.0.1-commit.b9865e97

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.
Files changed (69) hide show
  1. package/dest/index.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts +4 -3
  3. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  4. package/dest/instrumentation/instrumentation.js +21 -50
  5. package/dest/native/bench_metrics.d.ts +1 -1
  6. package/dest/native/bench_metrics.d.ts.map +1 -1
  7. package/dest/native/fork_checkpoint.d.ts +7 -1
  8. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  9. package/dest/native/fork_checkpoint.js +15 -3
  10. package/dest/native/index.d.ts +1 -1
  11. package/dest/native/ipc_world_state_instance.d.ts +87 -0
  12. package/dest/native/ipc_world_state_instance.d.ts.map +1 -0
  13. package/dest/native/ipc_world_state_instance.js +660 -0
  14. package/dest/native/merkle_trees_facade.d.ts +18 -8
  15. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  16. package/dest/native/merkle_trees_facade.js +90 -111
  17. package/dest/native/message.d.ts +22 -228
  18. package/dest/native/message.d.ts.map +1 -1
  19. package/dest/native/message.js +14 -55
  20. package/dest/native/native_world_state.d.ts +30 -15
  21. package/dest/native/native_world_state.d.ts.map +1 -1
  22. package/dest/native/native_world_state.js +126 -66
  23. package/dest/native/native_world_state_instance.d.ts +58 -32
  24. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  25. package/dest/native/native_world_state_instance.js +5 -181
  26. package/dest/native/world_state_operation.d.ts +7 -0
  27. package/dest/native/world_state_operation.d.ts.map +1 -0
  28. package/dest/native/world_state_operation.js +5 -0
  29. package/dest/synchronizer/config.d.ts +3 -5
  30. package/dest/synchronizer/config.d.ts.map +1 -1
  31. package/dest/synchronizer/config.js +15 -18
  32. package/dest/synchronizer/errors.d.ts +1 -1
  33. package/dest/synchronizer/errors.d.ts.map +1 -1
  34. package/dest/synchronizer/factory.d.ts +6 -5
  35. package/dest/synchronizer/factory.d.ts.map +1 -1
  36. package/dest/synchronizer/factory.js +7 -6
  37. package/dest/synchronizer/index.d.ts +1 -1
  38. package/dest/synchronizer/server_world_state_synchronizer.d.ts +11 -28
  39. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  40. package/dest/synchronizer/server_world_state_synchronizer.js +165 -81
  41. package/dest/test/index.d.ts +1 -1
  42. package/dest/test/utils.d.ts +12 -5
  43. package/dest/test/utils.d.ts.map +1 -1
  44. package/dest/test/utils.js +54 -50
  45. package/dest/testing.d.ts +5 -4
  46. package/dest/testing.d.ts.map +1 -1
  47. package/dest/testing.js +11 -7
  48. package/dest/world-state-db/index.d.ts +1 -1
  49. package/dest/world-state-db/merkle_tree_db.d.ts +9 -19
  50. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  51. package/package.json +17 -13
  52. package/src/instrumentation/instrumentation.ts +23 -59
  53. package/src/native/fork_checkpoint.ts +19 -3
  54. package/src/native/ipc_world_state_instance.ts +830 -0
  55. package/src/native/merkle_trees_facade.ts +118 -104
  56. package/src/native/message.ts +33 -305
  57. package/src/native/native_world_state.ts +164 -108
  58. package/src/native/native_world_state_instance.ts +96 -250
  59. package/src/native/world_state_operation.ts +33 -0
  60. package/src/synchronizer/config.ts +16 -23
  61. package/src/synchronizer/factory.ts +18 -11
  62. package/src/synchronizer/server_world_state_synchronizer.ts +181 -106
  63. package/src/test/utils.ts +87 -92
  64. package/src/testing.ts +9 -10
  65. package/src/world-state-db/merkle_tree_db.ts +12 -19
  66. package/dest/native/world_state_ops_queue.d.ts +0 -19
  67. package/dest/native/world_state_ops_queue.d.ts.map +0 -1
  68. package/dest/native/world_state_ops_queue.js +0 -146
  69. package/src/native/world_state_ops_queue.ts +0 -190
package/src/test/utils.ts CHANGED
@@ -4,122 +4,100 @@ import {
4
4
  NULLIFIER_SUBTREE_HEIGHT,
5
5
  NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
6
6
  } from '@aztec/constants';
7
+ import { asyncMap } from '@aztec/foundation/async-map';
8
+ import { BlockNumber, type CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
7
9
  import { padArrayEnd } from '@aztec/foundation/collection';
8
- import { Fr } from '@aztec/foundation/fields';
10
+ import { Fr } from '@aztec/foundation/curves/bn254';
9
11
  import { L2Block } from '@aztec/stdlib/block';
10
12
  import type {
11
13
  IndexedTreeId,
12
14
  MerkleTreeReadOperations,
13
15
  MerkleTreeWriteOperations,
14
16
  } from '@aztec/stdlib/interfaces/server';
17
+ import { mockCheckpointAndMessages, mockL1ToL2Messages } from '@aztec/stdlib/testing';
15
18
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
19
+ import { BlockHeader } from '@aztec/stdlib/tx';
16
20
 
17
21
  import type { NativeWorldStateService } from '../native/native_world_state.js';
18
22
 
19
- export async function mockBlock(
20
- blockNum: number,
21
- size: number,
22
- fork: MerkleTreeWriteOperations,
23
- maxEffects: number | undefined = undefined,
24
- ) {
25
- const l2Block = await L2Block.random(blockNum, size, maxEffects);
26
- const l1ToL2Messages = Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(0).map(Fr.random);
27
-
28
- {
29
- const insertData = async (
30
- treeId: IndexedTreeId,
31
- data: Buffer[][],
32
- subTreeHeight: number,
33
- fork: MerkleTreeWriteOperations,
34
- ) => {
35
- for (const dataBatch of data) {
36
- await fork.batchInsert(treeId, dataBatch, subTreeHeight);
37
- }
38
- };
39
-
40
- const publicDataInsert = insertData(
41
- MerkleTreeId.PUBLIC_DATA_TREE,
42
- l2Block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
43
- 0,
44
- fork,
45
- );
46
- const nullifierInsert = insertData(
47
- MerkleTreeId.NULLIFIER_TREE,
48
- l2Block.body.txEffects.map(txEffect =>
49
- padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
50
- ),
51
- NULLIFIER_SUBTREE_HEIGHT,
52
- fork,
53
- );
54
- const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
55
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
56
- );
57
-
58
- const l1ToL2MessagesPadded = padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
59
-
60
- const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
61
- const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
62
- await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
63
- }
23
+ export async function updateBlockState(block: L2Block, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations) {
24
+ const insertData = async (
25
+ treeId: IndexedTreeId,
26
+ data: Buffer[][],
27
+ subTreeHeight: number,
28
+ fork: MerkleTreeWriteOperations,
29
+ ) => {
30
+ for (const dataBatch of data) {
31
+ await fork.batchInsert(treeId, dataBatch, subTreeHeight);
32
+ }
33
+ };
34
+
35
+ const publicDataInsert = insertData(
36
+ MerkleTreeId.PUBLIC_DATA_TREE,
37
+ block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
38
+ 0,
39
+ fork,
40
+ );
41
+ const nullifierInsert = insertData(
42
+ MerkleTreeId.NULLIFIER_TREE,
43
+ block.body.txEffects.map(txEffect =>
44
+ padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
45
+ ),
46
+ NULLIFIER_SUBTREE_HEIGHT,
47
+ fork,
48
+ );
49
+ const noteHashesPadded = block.body.txEffects.flatMap(txEffect =>
50
+ padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
51
+ );
52
+
53
+ const l1ToL2MessagesPadded =
54
+ block.indexWithinCheckpoint === 0
55
+ ? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
56
+ : l1ToL2Messages;
57
+
58
+ const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
59
+ const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
60
+ await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
64
61
 
65
62
  const state = await fork.getStateReference();
66
- l2Block.header.state = state;
67
- await fork.updateArchive(l2Block.getBlockHeader());
63
+ block.header = BlockHeader.from({ ...block.header, state });
64
+ await fork.updateArchive(block.header);
68
65
 
69
66
  const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
70
67
 
71
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
68
+ block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
69
+ }
70
+
71
+ export async function mockBlock(
72
+ blockNum: BlockNumber,
73
+ size: number,
74
+ fork: MerkleTreeWriteOperations,
75
+ maxEffects: number | undefined = 1000, // Defaults to the maximum tx effects.
76
+ numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
77
+ isFirstBlockInCheckpoint: boolean = true,
78
+ ) {
79
+ const block = await L2Block.random(blockNum, {
80
+ indexWithinCheckpoint: isFirstBlockInCheckpoint ? IndexWithinCheckpoint(0) : IndexWithinCheckpoint(1),
81
+ txsPerBlock: size,
82
+ txOptions: { maxEffects },
83
+ });
84
+ const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
85
+
86
+ await updateBlockState(block, l1ToL2Messages, fork);
72
87
 
73
88
  return {
74
- block: l2Block,
89
+ block,
75
90
  messages: l1ToL2Messages,
76
91
  };
77
92
  }
78
93
 
79
- export async function mockEmptyBlock(blockNum: number, fork: MerkleTreeWriteOperations) {
94
+ export async function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations) {
80
95
  const l2Block = L2Block.empty();
81
96
  const l1ToL2Messages = Array(16).fill(0).map(Fr.zero);
82
97
 
83
98
  l2Block.header.globalVariables.blockNumber = blockNum;
84
99
 
85
- // Sync the append only trees
86
- {
87
- const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
88
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
89
- );
90
- await fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
91
-
92
- const l1ToL2MessagesPadded = padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
93
- await fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
94
- }
95
-
96
- // Sync the indexed trees
97
- {
98
- // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
99
- for (const txEffect of l2Block.body.txEffects) {
100
- await fork.batchInsert(
101
- MerkleTreeId.PUBLIC_DATA_TREE,
102
- txEffect.publicDataWrites.map(write => write.toBuffer()),
103
- 0,
104
- );
105
-
106
- const nullifiersPadded = padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX);
107
-
108
- await fork.batchInsert(
109
- MerkleTreeId.NULLIFIER_TREE,
110
- nullifiersPadded.map(nullifier => nullifier.toBuffer()),
111
- NULLIFIER_SUBTREE_HEIGHT,
112
- );
113
- }
114
- }
115
-
116
- const state = await fork.getStateReference();
117
- l2Block.header.state = state;
118
- await fork.updateArchive(l2Block.getBlockHeader());
119
-
120
- const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
121
-
122
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
100
+ await updateBlockState(l2Block, l1ToL2Messages, fork);
123
101
 
124
102
  return {
125
103
  block: l2Block,
@@ -127,13 +105,18 @@ export async function mockEmptyBlock(blockNum: number, fork: MerkleTreeWriteOper
127
105
  };
128
106
  }
129
107
 
130
- export async function mockBlocks(from: number, count: number, numTxs: number, worldState: NativeWorldStateService) {
131
- const tempFork = await worldState.fork(from - 1);
108
+ export async function mockBlocks(
109
+ from: BlockNumber,
110
+ count: number,
111
+ numTxs: number,
112
+ worldState: NativeWorldStateService,
113
+ ) {
114
+ const tempFork = await worldState.fork(BlockNumber(from - 1));
132
115
 
133
116
  const blocks = [];
134
117
  const messagesArray = [];
135
118
  for (let blockNumber = from; blockNumber < from + count; blockNumber++) {
136
- const { block, messages } = await mockBlock(blockNumber, numTxs, tempFork);
119
+ const { block, messages } = await mockBlock(BlockNumber(blockNumber), numTxs, tempFork);
137
120
  blocks.push(block);
138
121
  messagesArray.push(messages);
139
122
  }
@@ -143,6 +126,18 @@ export async function mockBlocks(from: number, count: number, numTxs: number, wo
143
126
  return { blocks, messages: messagesArray };
144
127
  }
145
128
 
129
+ export async function mockCheckpoint(
130
+ checkpointNumber: CheckpointNumber,
131
+ fork: MerkleTreeWriteOperations,
132
+ options: Partial<Parameters<typeof mockCheckpointAndMessages>[1]> = {},
133
+ ) {
134
+ const { checkpoint, messages } = await mockCheckpointAndMessages(checkpointNumber, options);
135
+ await asyncMap(checkpoint.blocks, async (block, i) => {
136
+ await updateBlockState(block, i === 0 ? messages : [], fork);
137
+ });
138
+ return { checkpoint, messages };
139
+ }
140
+
146
141
  export async function assertSameState(forkA: MerkleTreeReadOperations, forkB: MerkleTreeReadOperations) {
147
142
  const nativeStateRef = await forkA.getStateReference();
148
143
  const nativeArchive = await forkA.getTreeInfo(MerkleTreeId.ARCHIVE);
package/src/testing.ts CHANGED
@@ -1,24 +1,21 @@
1
1
  import { GENESIS_ARCHIVE_ROOT } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { computeFeePayerBalanceLeafSlot } from '@aztec/protocol-contracts/fee-juice';
4
4
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
5
  import { MerkleTreeId, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
6
+ import type { GenesisData } from '@aztec/stdlib/world-state';
6
7
 
7
8
  import { NativeWorldStateService } from './native/index.js';
8
9
 
9
- async function generateGenesisValues(prefilledPublicData: PublicDataTreeLeaf[]) {
10
- if (!prefilledPublicData.length) {
10
+ async function generateGenesisValues(genesis: GenesisData) {
11
+ if (!genesis.prefilledPublicData.length && genesis.genesisTimestamp === 0n) {
11
12
  return {
12
13
  genesisArchiveRoot: new Fr(GENESIS_ARCHIVE_ROOT),
13
14
  };
14
15
  }
15
16
 
16
17
  // Create a temporary world state to compute the genesis values.
17
- const ws = await NativeWorldStateService.tmp(
18
- undefined /* rollupAddress */,
19
- true /* cleanupTmpDir */,
20
- prefilledPublicData,
21
- );
18
+ const ws = await NativeWorldStateService.tmp(/*cleanupTmpDir=*/ true, genesis);
22
19
  const genesisArchiveRoot = new Fr((await ws.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
23
20
  await ws.close();
24
21
 
@@ -33,6 +30,7 @@ export async function getGenesisValues(
33
30
  initialAccounts: AztecAddress[],
34
31
  initialAccountFeeJuice = defaultInitialAccountFeeJuice,
35
32
  genesisPublicData: PublicDataTreeLeaf[] = [],
33
+ genesisTimestamp: bigint = 0n,
36
34
  ) {
37
35
  // Top up the accounts with fee juice.
38
36
  let prefilledPublicData = await Promise.all(
@@ -46,11 +44,12 @@ export async function getGenesisValues(
46
44
 
47
45
  prefilledPublicData.sort((a, b) => (b.slot.lt(a.slot) ? 1 : -1));
48
46
 
49
- const { genesisArchiveRoot } = await generateGenesisValues(prefilledPublicData);
47
+ const genesis: GenesisData = { prefilledPublicData, genesisTimestamp };
48
+ const { genesisArchiveRoot } = await generateGenesisValues(genesis);
50
49
 
51
50
  return {
52
51
  genesisArchiveRoot,
53
- prefilledPublicData,
52
+ genesis,
54
53
  fundingNeeded: BigInt(initialAccounts.length) * initialAccountFeeJuice.toBigInt(),
55
54
  };
56
55
  }
@@ -1,9 +1,12 @@
1
1
  import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
2
- import type { Fr } from '@aztec/foundation/fields';
3
- import type { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
2
+ import type { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import type { Fr } from '@aztec/foundation/curves/bn254';
4
4
  import type { L2Block } from '@aztec/stdlib/block';
5
- import type { ForkMerkleTreeOperations, MerkleTreeReadOperations } from '@aztec/stdlib/interfaces/server';
6
- import type { MerkleTreeId } from '@aztec/stdlib/trees';
5
+ import type {
6
+ ForkMerkleTreeOperations,
7
+ MerkleTreeReadOperations,
8
+ ReadonlyWorldStateAccess,
9
+ } from '@aztec/stdlib/interfaces/server';
7
10
 
8
11
  import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
9
12
 
@@ -26,23 +29,13 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NULLIFIERS_PER_TX;
26
29
 
27
30
  export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
28
31
 
29
- export type TreeSnapshots = {
30
- [MerkleTreeId.NULLIFIER_TREE]: IndexedTreeSnapshot;
31
- [MerkleTreeId.NOTE_HASH_TREE]: TreeSnapshot<Fr>;
32
- [MerkleTreeId.PUBLIC_DATA_TREE]: IndexedTreeSnapshot;
33
- [MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: TreeSnapshot<Fr>;
34
- [MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
35
- };
36
-
37
- export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
32
+ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
38
33
  /**
39
34
  * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
40
35
  * @param block - The L2 block to handle.
41
36
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
42
- * @param isFirstBlock - Whether the block is the first block in a checkpoint. Temporary hack to only insert l1 to l2
43
- * messages for the first block in a checkpoint. TODO(#17027) Remove this.
44
37
  */
45
- handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[], isFirstBlock?: boolean): Promise<WorldStateStatusFull>;
38
+ handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
46
39
 
47
40
  /**
48
41
  * Gets a handle that allows reading the latest committed state
@@ -54,21 +47,21 @@ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
54
47
  * @param toBlockNumber The block number of the new oldest historical block
55
48
  * @returns The new WorldStateStatus
56
49
  */
57
- removeHistoricalBlocks(toBlockNumber: bigint): Promise<WorldStateStatusFull>;
50
+ removeHistoricalBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
58
51
 
59
52
  /**
60
53
  * Removes all pending blocks down to but not including the given block number
61
54
  * @param toBlockNumber The block number of the new tip of the pending chain,
62
55
  * @returns The new WorldStateStatus
63
56
  */
64
- unwindBlocks(toBlockNumber: bigint): Promise<WorldStateStatusFull>;
57
+ unwindBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
65
58
 
66
59
  /**
67
60
  * Advances the finalized block number to be the number provided
68
61
  * @param toBlockNumber The block number that is now the tip of the finalized chain
69
62
  * @returns The new WorldStateStatus
70
63
  */
71
- setFinalized(toBlockNumber: bigint): Promise<WorldStateStatusSummary>;
64
+ setFinalized(toBlockNumber: BlockNumber): Promise<WorldStateStatusSummary>;
72
65
 
73
66
  /**
74
67
  * Gets the current status summary of the database.
@@ -1,19 +0,0 @@
1
- import { WorldStateMessageType } from './message.js';
2
- export declare const MUTATING_MSG_TYPES: Set<WorldStateMessageType>;
3
- export declare class WorldStateOpsQueue {
4
- private requests;
5
- private inFlightMutatingCount;
6
- private inFlightCount;
7
- private stopPromise?;
8
- private stopResolve?;
9
- private requestId;
10
- private ops;
11
- execute(request: () => Promise<any>, messageType: WorldStateMessageType, committedOnly: boolean): Promise<any>;
12
- private executeMutating;
13
- private executeNonMutatingUncommitted;
14
- private executeNonMutatingCommitted;
15
- private checkAndEnqueue;
16
- private sendEnqueuedRequest;
17
- stop(): Promise<void>;
18
- }
19
- //# sourceMappingURL=world_state_ops_queue.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"world_state_ops_queue.d.ts","sourceRoot":"","sources":["../../src/native/world_state_ops_queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAyBrD,eAAO,MAAM,kBAAkB,4BAgB7B,CAAC;AAGH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,qBAAqB,CAAK;IAClC,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,WAAW,CAAC,CAAgB;IACpC,OAAO,CAAC,WAAW,CAAC,CAAa;IACjC,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,GAAG,CAAwC;IAI5C,OAAO,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,EAAE,WAAW,EAAE,qBAAqB,EAAE,aAAa,EAAE,OAAO;IAyBtG,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,6BAA6B;IAYrC,OAAO,CAAC,2BAA2B;IAUnC,OAAO,CAAC,eAAe;IAyCvB,OAAO,CAAC,mBAAmB;IAgBpB,IAAI;CAiBZ"}
@@ -1,146 +0,0 @@
1
- import { promiseWithResolvers } from '@aztec/foundation/promise';
2
- import { WorldStateMessageType } from './message.js';
3
- // These are the set of message types that implement mutating operations
4
- // Messages of these types require exclusive access to their given forks
5
- export const MUTATING_MSG_TYPES = new Set([
6
- WorldStateMessageType.APPEND_LEAVES,
7
- WorldStateMessageType.BATCH_INSERT,
8
- WorldStateMessageType.SEQUENTIAL_INSERT,
9
- WorldStateMessageType.UPDATE_ARCHIVE,
10
- WorldStateMessageType.COMMIT,
11
- WorldStateMessageType.ROLLBACK,
12
- WorldStateMessageType.SYNC_BLOCK,
13
- WorldStateMessageType.CREATE_FORK,
14
- WorldStateMessageType.DELETE_FORK,
15
- WorldStateMessageType.FINALIZE_BLOCKS,
16
- WorldStateMessageType.UNWIND_BLOCKS,
17
- WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
18
- WorldStateMessageType.CREATE_CHECKPOINT,
19
- WorldStateMessageType.COMMIT_CHECKPOINT,
20
- WorldStateMessageType.REVERT_CHECKPOINT
21
- ]);
22
- // This class implements the per-fork operation queue
23
- export class WorldStateOpsQueue {
24
- requests = [];
25
- inFlightMutatingCount = 0;
26
- inFlightCount = 0;
27
- stopPromise;
28
- stopResolve;
29
- requestId = 0;
30
- ops = new Map();
31
- // The primary public api, this is where an operation is queued
32
- // We return a promise that will ultimately be resolved/rejected with the response/error generated by the 'request' argument
33
- execute(request, messageType, committedOnly) {
34
- if (this.stopResolve !== undefined) {
35
- throw new Error('Unable to send request to world state, queue already stopped');
36
- }
37
- const op = {
38
- requestId: this.requestId++,
39
- mutating: MUTATING_MSG_TYPES.has(messageType),
40
- request,
41
- promise: promiseWithResolvers()
42
- };
43
- this.ops.set(op.requestId, op);
44
- // Perform the appropriate action based upon the queueing rules
45
- if (op.mutating) {
46
- this.executeMutating(op);
47
- } else if (committedOnly === false) {
48
- this.executeNonMutatingUncommitted(op);
49
- } else {
50
- this.executeNonMutatingCommitted(op);
51
- }
52
- return op.promise.promise;
53
- }
54
- // Mutating requests need exclusive access
55
- executeMutating(op) {
56
- // If nothing is in flight then we send the request immediately
57
- // Otherwise add to the queue
58
- if (this.inFlightCount === 0) {
59
- this.sendEnqueuedRequest(op);
60
- } else {
61
- this.requests.push(op);
62
- }
63
- }
64
- // Non mutating requests including uncommitted state
65
- executeNonMutatingUncommitted(op) {
66
- // If there are no mutating requests in flight and there is nothing queued
67
- // then send the request immediately
68
- // If a mutating request is in flight then we must wait
69
- // If a mutating request is not in flight but something is queued then it must be a mutating request
70
- if (this.inFlightMutatingCount == 0 && this.requests.length == 0) {
71
- this.sendEnqueuedRequest(op);
72
- } else {
73
- this.requests.push(op);
74
- }
75
- }
76
- executeNonMutatingCommitted(op) {
77
- // This is a non-mutating request for committed data
78
- // It can always be sent
79
- op.request().then(op.promise.resolve, op.promise.reject).finally(()=>{
80
- this.ops.delete(op.requestId);
81
- });
82
- }
83
- checkAndEnqueue(completedOp) {
84
- // As request has completed
85
- // First we decrements the relevant in flight counters
86
- if (completedOp.mutating) {
87
- --this.inFlightMutatingCount;
88
- }
89
- --this.inFlightCount;
90
- // If there are still requests in flight then do nothing further
91
- if (this.inFlightCount != 0) {
92
- return;
93
- }
94
- // No requests in flight, send next queued requests
95
- // We loop and send:
96
- // 1 mutating request if it is next in the queue
97
- // As many non-mutating requests as we encounter until
98
- // we exhaust the queue or we reach a mutating request
99
- while(this.requests.length > 0){
100
- const next = this.requests[0];
101
- if (next.mutating) {
102
- if (this.inFlightCount == 0) {
103
- // send the mutating request
104
- this.requests.shift();
105
- this.sendEnqueuedRequest(next);
106
- }
107
- break;
108
- } else {
109
- // not mutating, send and go round again
110
- this.requests.shift();
111
- this.sendEnqueuedRequest(next);
112
- }
113
- }
114
- // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
115
- if (this.inFlightCount == 0 && this.stopResolve !== undefined) {
116
- this.stopResolve();
117
- }
118
- }
119
- sendEnqueuedRequest(op) {
120
- // Here we increment the in flight counts before sending
121
- ++this.inFlightCount;
122
- if (op.mutating) {
123
- ++this.inFlightMutatingCount;
124
- }
125
- // Make the request and pass the response/error through to the stored promise
126
- op.request().then(op.promise.resolve, op.promise.reject).finally(()=>{
127
- this.checkAndEnqueue(op);
128
- this.ops.delete(op.requestId);
129
- });
130
- }
131
- stop() {
132
- // If there is already a stop promise then return it
133
- if (this.stopPromise) {
134
- return this.stopPromise;
135
- }
136
- // Otherwise create a new one and capture the resolve method
137
- this.stopPromise = new Promise((resolve)=>{
138
- this.stopResolve = resolve;
139
- });
140
- // If no outstanding requests then immediately resolve the promise
141
- if (this.requests.length == 0 && this.inFlightCount == 0 && this.stopResolve !== undefined) {
142
- this.stopResolve();
143
- }
144
- return this.stopPromise;
145
- }
146
- }