@aztec/world-state 0.69.1 → 0.71.0

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 (32) hide show
  1. package/dest/native/merkle_trees_facade.js +2 -2
  2. package/dest/native/message.d.ts +16 -16
  3. package/dest/native/message.d.ts.map +1 -1
  4. package/dest/native/message.js +10 -1
  5. package/dest/native/native_world_state.d.ts.map +1 -1
  6. package/dest/native/native_world_state.js +16 -13
  7. package/dest/native/native_world_state_instance.d.ts +4 -5
  8. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  9. package/dest/native/native_world_state_instance.js +50 -11
  10. package/dest/native/world_state_ops_queue.d.ts +19 -0
  11. package/dest/native/world_state_ops_queue.d.ts.map +1 -0
  12. package/dest/native/world_state_ops_queue.js +154 -0
  13. package/dest/synchronizer/factory.d.ts +1 -1
  14. package/dest/synchronizer/factory.d.ts.map +1 -1
  15. package/dest/synchronizer/factory.js +3 -2
  16. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  17. package/dest/synchronizer/server_world_state_synchronizer.js +3 -4
  18. package/dest/test/utils.d.ts.map +1 -1
  19. package/dest/test/utils.js +4 -5
  20. package/dest/world-state-db/merkle_trees.d.ts +1 -1
  21. package/dest/world-state-db/merkle_trees.d.ts.map +1 -1
  22. package/dest/world-state-db/merkle_trees.js +9 -11
  23. package/package.json +8 -8
  24. package/src/native/merkle_trees_facade.ts +1 -1
  25. package/src/native/message.ts +22 -14
  26. package/src/native/native_world_state.ts +18 -17
  27. package/src/native/native_world_state_instance.ts +78 -22
  28. package/src/native/world_state_ops_queue.ts +187 -0
  29. package/src/synchronizer/factory.ts +2 -2
  30. package/src/synchronizer/server_world_state_synchronizer.ts +2 -3
  31. package/src/test/utils.ts +2 -9
  32. package/src/world-state-db/merkle_trees.ts +8 -15
@@ -0,0 +1,187 @@
1
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
2
+
3
+ import { WorldStateMessageType } from './message.js';
4
+
5
+ /**
6
+ * This is the implementation for queueing requests to the world state.
7
+ * Requests need to be queued for the world state to ensure that writes are correctly ordered
8
+ * and reads return the correct data.
9
+ * Due to the nature of the NAPI we can't really do this there.
10
+ *
11
+ * The rules for queueing are as follows:
12
+ *
13
+ * 1. Reads of committed state never need to be queued. LMDB uses MVCC to ensure readers see a consistent view of the DB.
14
+ * 2. Reads of uncommitted state can happen concurrently with other reads of uncommitted state on the same fork (or reads of committed state)
15
+ * 3. All writes require exclusive access to their respective fork
16
+ *
17
+ */
18
+
19
+ type WorldStateOp = {
20
+ requestId: number;
21
+ mutating: boolean;
22
+ request: () => Promise<any>;
23
+ promise: PromiseWithResolvers<any>;
24
+ };
25
+
26
+ // These are the set of message types that implement mutating operations
27
+ // Messages of these types require exclusive access to their given forks
28
+ export const MUTATING_MSG_TYPES = new Set([
29
+ WorldStateMessageType.APPEND_LEAVES,
30
+ WorldStateMessageType.BATCH_INSERT,
31
+ WorldStateMessageType.SEQUENTIAL_INSERT,
32
+ WorldStateMessageType.UPDATE_ARCHIVE,
33
+ WorldStateMessageType.COMMIT,
34
+ WorldStateMessageType.ROLLBACK,
35
+ WorldStateMessageType.SYNC_BLOCK,
36
+ WorldStateMessageType.CREATE_FORK,
37
+ WorldStateMessageType.DELETE_FORK,
38
+ WorldStateMessageType.FINALISE_BLOCKS,
39
+ WorldStateMessageType.UNWIND_BLOCKS,
40
+ WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
41
+ ]);
42
+
43
+ // This class implements the per-fork operation queue
44
+ export class WorldStateOpsQueue {
45
+ private requests: WorldStateOp[] = [];
46
+ private inFlightMutatingCount = 0;
47
+ private inFlightCount = 0;
48
+ private stopPromise?: Promise<void>;
49
+ private stopResolve?: () => void;
50
+ private requestId = 0;
51
+ private ops: Map<number, WorldStateOp> = new Map();
52
+
53
+ // The primary public api, this is where an operation is queued
54
+ // We return a promise that will ultimately be resolved/rejected with the response/error generated by the 'request' argument
55
+ public execute(request: () => Promise<any>, messageType: WorldStateMessageType, committedOnly: boolean) {
56
+ if (this.stopResolve !== undefined) {
57
+ throw new Error('Unable to send request to world state, queue already stopped');
58
+ }
59
+
60
+ const op: WorldStateOp = {
61
+ requestId: this.requestId++,
62
+ mutating: MUTATING_MSG_TYPES.has(messageType),
63
+ request,
64
+ promise: promiseWithResolvers(),
65
+ };
66
+ this.ops.set(op.requestId, op);
67
+
68
+ // Perform the appropriate action based upon the queueing rules
69
+ if (op.mutating) {
70
+ this.executeMutating(op);
71
+ } else if (committedOnly === false) {
72
+ this.executeNonMutatingUncommitted(op);
73
+ } else {
74
+ this.executeNonMutatingCommitted(op);
75
+ }
76
+ return op.promise.promise;
77
+ }
78
+
79
+ // Mutating requests need exclusive access
80
+ private executeMutating(op: WorldStateOp) {
81
+ // If nothing is in flight then we send the request immediately
82
+ // Otherwise add to the queue
83
+ if (this.inFlightCount === 0) {
84
+ this.sendEnqueuedRequest(op);
85
+ } else {
86
+ this.requests.push(op);
87
+ }
88
+ }
89
+
90
+ // Non mutating requests including uncommitted state
91
+ private executeNonMutatingUncommitted(op: WorldStateOp) {
92
+ // If there are no mutating requests in flight and there is nothing queued
93
+ // then send the request immediately
94
+ // If a mutating request is in flight then we must wait
95
+ // If a mutating request is not in flight but something is queued then it must be a mutating request
96
+ if (this.inFlightMutatingCount == 0 && this.requests.length == 0) {
97
+ this.sendEnqueuedRequest(op);
98
+ } else {
99
+ this.requests.push(op);
100
+ }
101
+ }
102
+
103
+ private executeNonMutatingCommitted(op: WorldStateOp) {
104
+ // This is a non-mutating request for committed data
105
+ // It can always be sent
106
+ op.request()
107
+ .then(op.promise.resolve, op.promise.reject)
108
+ .finally(() => {
109
+ this.ops.delete(op.requestId);
110
+ });
111
+ }
112
+
113
+ private checkAndEnqueue(completedOp: WorldStateOp) {
114
+ // As request has completed
115
+ // First we decrements the relevant in flight counters
116
+ if (completedOp.mutating) {
117
+ --this.inFlightMutatingCount;
118
+ }
119
+ --this.inFlightCount;
120
+
121
+ // If there are still requests in flight then do nothing further
122
+ if (this.inFlightCount != 0) {
123
+ return;
124
+ }
125
+
126
+ // No requests in flight, send next queued requests
127
+ // We loop and send:
128
+ // 1 mutating request if it is next in the queue
129
+ // As many non-mutating requests as we encounter until
130
+ // we exhaust the queue or we reach a mutating request
131
+ while (this.requests.length > 0) {
132
+ const next = this.requests[0];
133
+ if (next.mutating) {
134
+ if (this.inFlightCount == 0) {
135
+ // send the mutating request
136
+ this.requests.shift();
137
+ this.sendEnqueuedRequest(next);
138
+ }
139
+ // this request is mutating, we need to stop here
140
+ break;
141
+ } else {
142
+ // not mutating, send and go round again
143
+ this.requests.shift();
144
+ this.sendEnqueuedRequest(next);
145
+ }
146
+ }
147
+
148
+ // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
149
+ if (this.inFlightCount == 0 && this.stopResolve !== undefined) {
150
+ this.stopResolve();
151
+ }
152
+ }
153
+
154
+ private sendEnqueuedRequest(op: WorldStateOp) {
155
+ // Here we increment the in flight counts before sending
156
+ ++this.inFlightCount;
157
+ if (op.mutating) {
158
+ ++this.inFlightMutatingCount;
159
+ }
160
+
161
+ // Make the request and pass the response/error through to the stored promise
162
+ op.request()
163
+ .then(op.promise.resolve, op.promise.reject)
164
+ .finally(() => {
165
+ this.checkAndEnqueue(op);
166
+ this.ops.delete(op.requestId);
167
+ });
168
+ }
169
+
170
+ public stop() {
171
+ // If there is already a stop promise then return it
172
+ if (this.stopPromise) {
173
+ return this.stopPromise;
174
+ }
175
+
176
+ // Otherwise create a new one and capture the resolve method
177
+ this.stopPromise = new Promise(resolve => {
178
+ this.stopResolve = resolve;
179
+ });
180
+
181
+ // If no outstanding requests then immediately resolve the promise
182
+ if (this.requests.length == 0 && this.inFlightCount == 0 && this.stopResolve !== undefined) {
183
+ this.stopResolve();
184
+ }
185
+ return this.stopPromise;
186
+ }
187
+ }
@@ -1,6 +1,6 @@
1
1
  import { type L1ToL2MessageSource, type L2BlockSource } from '@aztec/circuit-types';
2
2
  import { type DataStoreConfig } from '@aztec/kv-store/config';
3
- import { type TelemetryClient } from '@aztec/telemetry-client';
3
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
4
4
 
5
5
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
6
6
  import { NativeWorldStateService } from '../native/native_world_state.js';
@@ -10,7 +10,7 @@ import { ServerWorldStateSynchronizer } from './server_world_state_synchronizer.
10
10
  export async function createWorldStateSynchronizer(
11
11
  config: WorldStateConfig & DataStoreConfig,
12
12
  l2BlockSource: L2BlockSource & L1ToL2MessageSource,
13
- client: TelemetryClient,
13
+ client: TelemetryClient = getTelemetryClient(),
14
14
  ) {
15
15
  const instrumentation = new WorldStateInstrumentation(client);
16
16
  const merkleTrees = await createWorldState(config, instrumentation);
@@ -23,8 +23,7 @@ import { createLogger } from '@aztec/foundation/log';
23
23
  import { promiseWithResolvers } from '@aztec/foundation/promise';
24
24
  import { elapsed } from '@aztec/foundation/timer';
25
25
  import { SHA256Trunc } from '@aztec/merkle-tree';
26
- import { TraceableL2BlockStream } from '@aztec/telemetry-client';
27
- import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
26
+ import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
28
27
 
29
28
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
30
29
  import { type WorldStateStatusFull } from '../native/message.js';
@@ -53,7 +52,7 @@ export class ServerWorldStateSynchronizer
53
52
  private readonly merkleTreeDb: MerkleTreeAdminDatabase,
54
53
  private readonly l2BlockSource: L2BlockSource & L1ToL2MessageSource,
55
54
  private readonly config: WorldStateConfig,
56
- private instrumentation = new WorldStateInstrumentation(new NoopTelemetryClient()),
55
+ private instrumentation = new WorldStateInstrumentation(getTelemetryClient()),
57
56
  private readonly log = createLogger('world_state'),
58
57
  ) {
59
58
  this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
package/src/test/utils.ts CHANGED
@@ -3,7 +3,6 @@ import {
3
3
  MerkleTreeId,
4
4
  type MerkleTreeReadOperations,
5
5
  type MerkleTreeWriteOperations,
6
- TxEffect,
7
6
  } from '@aztec/circuit-types';
8
7
  import {
9
8
  AppendOnlyTreeSnapshot,
@@ -21,15 +20,9 @@ export async function mockBlock(blockNum: number, size: number, fork: MerkleTree
21
20
  const l2Block = L2Block.random(blockNum, size);
22
21
  const l1ToL2Messages = Array(16).fill(0).map(Fr.random);
23
22
 
24
- const paddedTxEffects = padArrayEnd(
25
- l2Block.body.txEffects,
26
- TxEffect.empty(),
27
- l2Block.body.numberOfTxsIncludingPadded,
28
- );
29
-
30
23
  // Sync the append only trees
31
24
  {
32
- const noteHashesPadded = paddedTxEffects.flatMap(txEffect =>
25
+ const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
33
26
  padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
34
27
  );
35
28
  await fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
@@ -41,7 +34,7 @@ export async function mockBlock(blockNum: number, size: number, fork: MerkleTree
41
34
  // Sync the indexed trees
42
35
  {
43
36
  // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
44
- for (const txEffect of paddedTxEffects) {
37
+ for (const txEffect of l2Block.body.txEffects) {
45
38
  await fork.batchInsert(
46
39
  MerkleTreeId.PUBLIC_DATA_TREE,
47
40
  txEffect.publicDataWrites.map(write => write.toBuffer()),
@@ -1,4 +1,4 @@
1
- import { type L2Block, MerkleTreeId, type SiblingPath, TxEffect } from '@aztec/circuit-types';
1
+ import { type L2Block, MerkleTreeId, type SiblingPath } from '@aztec/circuit-types';
2
2
  import {
3
3
  type BatchInsertionResult,
4
4
  type IndexedTreeId,
@@ -47,8 +47,7 @@ import {
47
47
  loadTree,
48
48
  newTree,
49
49
  } from '@aztec/merkle-tree';
50
- import { type TelemetryClient } from '@aztec/telemetry-client';
51
- import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
50
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
52
51
  import { type Hasher } from '@aztec/types/interfaces';
53
52
 
54
53
  import {
@@ -123,7 +122,7 @@ export class MerkleTrees implements MerkleTreeAdminDatabase {
123
122
  */
124
123
  public static async new(
125
124
  store: AztecKVStore,
126
- client: TelemetryClient,
125
+ client: TelemetryClient = getTelemetryClient(),
127
126
  log = createLogger('world-state:merkle_trees'),
128
127
  ) {
129
128
  const merkleTrees = new MerkleTrees(store, client, log);
@@ -136,7 +135,7 @@ export class MerkleTrees implements MerkleTreeAdminDatabase {
136
135
  */
137
136
  public static tmp() {
138
137
  const store = openTmpStore();
139
- return MerkleTrees.new(store, new NoopTelemetryClient());
138
+ return MerkleTrees.new(store, getTelemetryClient());
140
139
  }
141
140
 
142
141
  /**
@@ -647,17 +646,11 @@ export class MerkleTrees implements MerkleTreeAdminDatabase {
647
646
  this.log.verbose(`Block ${l2Block.number} is not ours, rolling back world state and committing state from chain`);
648
647
  await this.#rollback();
649
648
 
650
- // We have to pad both the tx effects and the values within tx effects because that's how the trees are built
651
- // by circuits.
652
- const paddedTxEffects = padArrayEnd(
653
- l2Block.body.txEffects,
654
- TxEffect.empty(),
655
- l2Block.body.numberOfTxsIncludingPadded,
656
- );
649
+ // We have to pad the values within tx effects because that's how the trees are built by circuits.
657
650
 
658
651
  // Sync the append only trees
659
652
  {
660
- const noteHashesPadded = paddedTxEffects.flatMap(txEffect =>
653
+ const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
661
654
  padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
662
655
  );
663
656
  await this.#appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
@@ -668,7 +661,7 @@ export class MerkleTrees implements MerkleTreeAdminDatabase {
668
661
 
669
662
  // Sync the indexed trees
670
663
  {
671
- const nullifiersPadded = paddedTxEffects.flatMap(txEffect =>
664
+ const nullifiersPadded = l2Block.body.txEffects.flatMap(txEffect =>
672
665
  padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX),
673
666
  );
674
667
  await (this.trees[MerkleTreeId.NULLIFIER_TREE] as StandardIndexedTree).batchInsert(
@@ -679,7 +672,7 @@ export class MerkleTrees implements MerkleTreeAdminDatabase {
679
672
  const publicDataTree = this.trees[MerkleTreeId.PUBLIC_DATA_TREE] as StandardIndexedTree;
680
673
 
681
674
  // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
682
- for (const txEffect of paddedTxEffects) {
675
+ for (const txEffect of l2Block.body.txEffects) {
683
676
  const publicDataWrites = padArrayEnd(
684
677
  txEffect.publicDataWrites,
685
678
  PublicDataWrite.empty(),