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

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 (80) hide show
  1. package/dest/index.d.ts +2 -1
  2. package/dest/index.d.ts.map +1 -1
  3. package/dest/index.js +1 -0
  4. package/dest/instrumentation/instrumentation.d.ts +4 -3
  5. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  6. package/dest/instrumentation/instrumentation.js +21 -50
  7. package/dest/native/bench_metrics.d.ts +1 -1
  8. package/dest/native/bench_metrics.d.ts.map +1 -1
  9. package/dest/native/fork_checkpoint.d.ts +7 -1
  10. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  11. package/dest/native/fork_checkpoint.js +15 -3
  12. package/dest/native/index.d.ts +2 -1
  13. package/dest/native/index.d.ts.map +1 -1
  14. package/dest/native/index.js +1 -0
  15. package/dest/native/ipc_world_state_instance.d.ts +89 -0
  16. package/dest/native/ipc_world_state_instance.d.ts.map +1 -0
  17. package/dest/native/ipc_world_state_instance.js +661 -0
  18. package/dest/native/merkle_trees_facade.d.ts +18 -8
  19. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  20. package/dest/native/merkle_trees_facade.js +90 -111
  21. package/dest/native/message.d.ts +22 -228
  22. package/dest/native/message.d.ts.map +1 -1
  23. package/dest/native/message.js +14 -55
  24. package/dest/native/native_world_state.d.ts +30 -15
  25. package/dest/native/native_world_state.d.ts.map +1 -1
  26. package/dest/native/native_world_state.js +133 -66
  27. package/dest/native/native_world_state_instance.d.ts +56 -33
  28. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  29. package/dest/native/native_world_state_instance.js +1 -182
  30. package/dest/native/world_state_operation.d.ts +7 -0
  31. package/dest/native/world_state_operation.d.ts.map +1 -0
  32. package/dest/native/world_state_operation.js +5 -0
  33. package/dest/synchronizer/config.d.ts +3 -5
  34. package/dest/synchronizer/config.d.ts.map +1 -1
  35. package/dest/synchronizer/config.js +15 -18
  36. package/dest/synchronizer/errors.d.ts +8 -1
  37. package/dest/synchronizer/errors.d.ts.map +1 -1
  38. package/dest/synchronizer/errors.js +8 -1
  39. package/dest/synchronizer/factory.d.ts +6 -5
  40. package/dest/synchronizer/factory.d.ts.map +1 -1
  41. package/dest/synchronizer/factory.js +7 -6
  42. package/dest/synchronizer/index.d.ts +2 -1
  43. package/dest/synchronizer/index.d.ts.map +1 -1
  44. package/dest/synchronizer/index.js +1 -0
  45. package/dest/synchronizer/server_world_state_synchronizer.d.ts +20 -32
  46. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  47. package/dest/synchronizer/server_world_state_synchronizer.js +181 -85
  48. package/dest/test/index.d.ts +1 -1
  49. package/dest/test/utils.d.ts +12 -5
  50. package/dest/test/utils.d.ts.map +1 -1
  51. package/dest/test/utils.js +59 -50
  52. package/dest/testing.d.ts +5 -4
  53. package/dest/testing.d.ts.map +1 -1
  54. package/dest/testing.js +15 -8
  55. package/dest/world-state-db/index.d.ts +1 -1
  56. package/dest/world-state-db/merkle_tree_db.d.ts +9 -19
  57. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  58. package/package.json +16 -13
  59. package/src/index.ts +1 -0
  60. package/src/instrumentation/instrumentation.ts +23 -59
  61. package/src/native/fork_checkpoint.ts +19 -3
  62. package/src/native/index.ts +1 -0
  63. package/src/native/ipc_world_state_instance.ts +834 -0
  64. package/src/native/merkle_trees_facade.ts +118 -104
  65. package/src/native/message.ts +33 -305
  66. package/src/native/native_world_state.ts +179 -108
  67. package/src/native/native_world_state_instance.ts +94 -251
  68. package/src/native/world_state_operation.ts +33 -0
  69. package/src/synchronizer/config.ts +16 -23
  70. package/src/synchronizer/errors.ts +8 -0
  71. package/src/synchronizer/factory.ts +18 -11
  72. package/src/synchronizer/index.ts +1 -0
  73. package/src/synchronizer/server_world_state_synchronizer.ts +219 -111
  74. package/src/test/utils.ts +96 -92
  75. package/src/testing.ts +13 -11
  76. package/src/world-state-db/merkle_tree_db.ts +12 -19
  77. package/dest/native/world_state_ops_queue.d.ts +0 -19
  78. package/dest/native/world_state_ops_queue.d.ts.map +0 -1
  79. package/dest/native/world_state_ops_queue.js +0 -146
  80. package/src/native/world_state_ops_queue.ts +0 -190
@@ -1,190 +0,0 @@
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.FINALIZE_BLOCKS,
39
- WorldStateMessageType.UNWIND_BLOCKS,
40
- WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
41
- WorldStateMessageType.CREATE_CHECKPOINT,
42
- WorldStateMessageType.COMMIT_CHECKPOINT,
43
- WorldStateMessageType.REVERT_CHECKPOINT,
44
- ]);
45
-
46
- // This class implements the per-fork operation queue
47
- export class WorldStateOpsQueue {
48
- private requests: WorldStateOp[] = [];
49
- private inFlightMutatingCount = 0;
50
- private inFlightCount = 0;
51
- private stopPromise?: Promise<void>;
52
- private stopResolve?: () => void;
53
- private requestId = 0;
54
- private ops: Map<number, WorldStateOp> = new Map();
55
-
56
- // The primary public api, this is where an operation is queued
57
- // We return a promise that will ultimately be resolved/rejected with the response/error generated by the 'request' argument
58
- public execute(request: () => Promise<any>, messageType: WorldStateMessageType, committedOnly: boolean) {
59
- if (this.stopResolve !== undefined) {
60
- throw new Error('Unable to send request to world state, queue already stopped');
61
- }
62
-
63
- const op: WorldStateOp = {
64
- requestId: this.requestId++,
65
- mutating: MUTATING_MSG_TYPES.has(messageType),
66
- request,
67
- promise: promiseWithResolvers(),
68
- };
69
- this.ops.set(op.requestId, op);
70
-
71
- // Perform the appropriate action based upon the queueing rules
72
- if (op.mutating) {
73
- this.executeMutating(op);
74
- } else if (committedOnly === false) {
75
- this.executeNonMutatingUncommitted(op);
76
- } else {
77
- this.executeNonMutatingCommitted(op);
78
- }
79
- return op.promise.promise;
80
- }
81
-
82
- // Mutating requests need exclusive access
83
- private executeMutating(op: WorldStateOp) {
84
- // If nothing is in flight then we send the request immediately
85
- // Otherwise add to the queue
86
- if (this.inFlightCount === 0) {
87
- this.sendEnqueuedRequest(op);
88
- } else {
89
- this.requests.push(op);
90
- }
91
- }
92
-
93
- // Non mutating requests including uncommitted state
94
- private executeNonMutatingUncommitted(op: WorldStateOp) {
95
- // If there are no mutating requests in flight and there is nothing queued
96
- // then send the request immediately
97
- // If a mutating request is in flight then we must wait
98
- // If a mutating request is not in flight but something is queued then it must be a mutating request
99
- if (this.inFlightMutatingCount == 0 && this.requests.length == 0) {
100
- this.sendEnqueuedRequest(op);
101
- } else {
102
- this.requests.push(op);
103
- }
104
- }
105
-
106
- private executeNonMutatingCommitted(op: WorldStateOp) {
107
- // This is a non-mutating request for committed data
108
- // It can always be sent
109
- op.request()
110
- .then(op.promise.resolve, op.promise.reject)
111
- .finally(() => {
112
- this.ops.delete(op.requestId);
113
- });
114
- }
115
-
116
- private checkAndEnqueue(completedOp: WorldStateOp) {
117
- // As request has completed
118
- // First we decrements the relevant in flight counters
119
- if (completedOp.mutating) {
120
- --this.inFlightMutatingCount;
121
- }
122
- --this.inFlightCount;
123
-
124
- // If there are still requests in flight then do nothing further
125
- if (this.inFlightCount != 0) {
126
- return;
127
- }
128
-
129
- // No requests in flight, send next queued requests
130
- // We loop and send:
131
- // 1 mutating request if it is next in the queue
132
- // As many non-mutating requests as we encounter until
133
- // we exhaust the queue or we reach a mutating request
134
- while (this.requests.length > 0) {
135
- const next = this.requests[0];
136
- if (next.mutating) {
137
- if (this.inFlightCount == 0) {
138
- // send the mutating request
139
- this.requests.shift();
140
- this.sendEnqueuedRequest(next);
141
- }
142
- // this request is mutating, we need to stop here
143
- break;
144
- } else {
145
- // not mutating, send and go round again
146
- this.requests.shift();
147
- this.sendEnqueuedRequest(next);
148
- }
149
- }
150
-
151
- // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
152
- if (this.inFlightCount == 0 && this.stopResolve !== undefined) {
153
- this.stopResolve();
154
- }
155
- }
156
-
157
- private sendEnqueuedRequest(op: WorldStateOp) {
158
- // Here we increment the in flight counts before sending
159
- ++this.inFlightCount;
160
- if (op.mutating) {
161
- ++this.inFlightMutatingCount;
162
- }
163
-
164
- // Make the request and pass the response/error through to the stored promise
165
- op.request()
166
- .then(op.promise.resolve, op.promise.reject)
167
- .finally(() => {
168
- this.checkAndEnqueue(op);
169
- this.ops.delete(op.requestId);
170
- });
171
- }
172
-
173
- public stop() {
174
- // If there is already a stop promise then return it
175
- if (this.stopPromise) {
176
- return this.stopPromise;
177
- }
178
-
179
- // Otherwise create a new one and capture the resolve method
180
- this.stopPromise = new Promise(resolve => {
181
- this.stopResolve = resolve;
182
- });
183
-
184
- // If no outstanding requests then immediately resolve the promise
185
- if (this.requests.length == 0 && this.inFlightCount == 0 && this.stopResolve !== undefined) {
186
- this.stopResolve();
187
- }
188
- return this.stopPromise;
189
- }
190
- }