@aztec/foundation 0.23.0 → 0.26.1
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/abi/abi.d.ts +1 -1
- package/dest/abi/abi.d.ts.map +1 -1
- package/dest/abi/event_selector.d.ts +45 -0
- package/dest/abi/event_selector.d.ts.map +1 -0
- package/dest/abi/event_selector.js +60 -0
- package/dest/abi/function_selector.d.ts +69 -0
- package/dest/abi/function_selector.d.ts.map +1 -0
- package/dest/abi/function_selector.js +86 -0
- package/dest/abi/index.d.ts +2 -1
- package/dest/abi/index.d.ts.map +1 -1
- package/dest/abi/index.js +3 -2
- package/dest/abi/selector.d.ts +4 -106
- package/dest/abi/selector.d.ts.map +1 -1
- package/dest/abi/selector.js +8 -143
- package/dest/abi/utils.js +3 -3
- package/dest/array/array.d.ts +8 -0
- package/dest/array/array.d.ts.map +1 -1
- package/dest/array/array.js +26 -1
- package/dest/aztec-address/index.d.ts +8 -1
- package/dest/aztec-address/index.d.ts.map +1 -1
- package/dest/aztec-address/index.js +16 -2
- package/dest/crypto/index.d.ts +1 -0
- package/dest/crypto/index.d.ts.map +1 -1
- package/dest/crypto/index.js +2 -1
- package/dest/crypto/pedersen/pedersen.wasm.d.ts +2 -1
- package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.js +7 -6
- package/dest/crypto/poseidon/index.d.ts +8 -0
- package/dest/crypto/poseidon/index.d.ts.map +1 -0
- package/dest/crypto/poseidon/index.js +12 -0
- package/dest/eth-address/index.d.ts +3 -0
- package/dest/eth-address/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +5 -2
- package/dest/fields/fields.d.ts +19 -0
- package/dest/fields/fields.d.ts.map +1 -1
- package/dest/fields/fields.js +11 -4
- package/dest/json-rpc/client/json_rpc_client.js +2 -2
- package/dest/serialize/buffer_reader.d.ts +15 -0
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +18 -1
- package/dest/serialize/serialize.d.ts +3 -5
- package/dest/serialize/serialize.d.ts.map +1 -1
- package/dest/serialize/serialize.js +17 -8
- package/package.json +2 -2
- package/src/abi/abi.ts +337 -0
- package/src/abi/buffer.ts +36 -0
- package/src/abi/decoder.ts +176 -0
- package/src/abi/encoder.ts +143 -0
- package/src/abi/event_selector.ts +73 -0
- package/src/abi/function_selector.ts +122 -0
- package/src/abi/index.ts +7 -0
- package/src/abi/selector.ts +63 -0
- package/src/abi/utils.ts +50 -0
- package/src/array/array.ts +119 -0
- package/src/array/index.ts +1 -0
- package/src/async-map/index.ts +18 -0
- package/src/aztec-address/index.ts +56 -0
- package/src/bigint-buffer/index.ts +87 -0
- package/src/collection/array.ts +64 -0
- package/src/collection/index.ts +1 -0
- package/src/committable/committable.ts +46 -0
- package/src/committable/index.ts +1 -0
- package/src/crypto/index.ts +17 -0
- package/src/crypto/keccak/index.ts +33 -0
- package/src/crypto/pedersen/index.ts +1 -0
- package/src/crypto/pedersen/pedersen.elliptic.ts +584 -0
- package/src/crypto/pedersen/pedersen.noble.ts +573 -0
- package/src/crypto/pedersen/pedersen.wasm.ts +46 -0
- package/src/crypto/poseidon/index.ts +17 -0
- package/src/crypto/random/index.ts +42 -0
- package/src/crypto/sha256/index.ts +3 -0
- package/src/errors/index.ts +6 -0
- package/src/eth-address/index.ts +239 -0
- package/src/fields/coordinate.ts +104 -0
- package/src/fields/fields.ts +338 -0
- package/src/fields/index.ts +3 -0
- package/src/fields/point.ts +145 -0
- package/src/fifo/bounded_serial_queue.ts +100 -0
- package/src/fifo/index.ts +4 -0
- package/src/fifo/memory_fifo.ts +118 -0
- package/src/fifo/semaphore.ts +33 -0
- package/src/fifo/serial_queue.ts +81 -0
- package/src/index.ts +29 -0
- package/src/json-rpc/README.md +55 -0
- package/src/json-rpc/class_converter.ts +213 -0
- package/src/json-rpc/client/index.ts +1 -0
- package/src/json-rpc/client/json_rpc_client.ts +147 -0
- package/src/json-rpc/convert.ts +163 -0
- package/src/json-rpc/fixtures/class_a.ts +15 -0
- package/src/json-rpc/fixtures/class_b.ts +15 -0
- package/src/json-rpc/fixtures/test_state.ts +59 -0
- package/src/json-rpc/index.ts +8 -0
- package/src/json-rpc/js_utils.ts +20 -0
- package/src/json-rpc/server/index.ts +2 -0
- package/src/json-rpc/server/json_proxy.ts +60 -0
- package/src/json-rpc/server/json_rpc_server.ts +269 -0
- package/src/log/console.ts +39 -0
- package/src/log/debug.ts +83 -0
- package/src/log/index.ts +5 -0
- package/src/log/log_fn.ts +5 -0
- package/src/log/log_history.ts +44 -0
- package/src/log/logger.ts +137 -0
- package/src/mutex/index.ts +83 -0
- package/src/mutex/mutex_database.ts +12 -0
- package/src/noir/index.ts +1 -0
- package/src/noir/noir_package_config.ts +54 -0
- package/src/retry/index.ts +99 -0
- package/src/running-promise/index.ts +60 -0
- package/src/serialize/buffer_reader.ts +309 -0
- package/src/serialize/field_reader.ts +143 -0
- package/src/serialize/free_funcs.ts +147 -0
- package/src/serialize/index.ts +5 -0
- package/src/serialize/serialize.ts +310 -0
- package/src/serialize/types.ts +40 -0
- package/src/sleep/index.ts +71 -0
- package/src/testing/index.ts +1 -0
- package/src/testing/test_data.ts +36 -0
- package/src/timer/elapsed.ts +23 -0
- package/src/timer/index.ts +3 -0
- package/src/timer/timeout.ts +64 -0
- package/src/timer/timer.ts +48 -0
- package/src/transport/browser/index.ts +4 -0
- package/src/transport/browser/message_port_socket.ts +48 -0
- package/src/transport/browser/shared_worker_connector.ts +21 -0
- package/src/transport/browser/shared_worker_listener.ts +53 -0
- package/src/transport/browser/worker_connector.ts +30 -0
- package/src/transport/browser/worker_listener.ts +54 -0
- package/src/transport/dispatch/create_dispatch_fn.ts +35 -0
- package/src/transport/dispatch/create_dispatch_proxy.ts +141 -0
- package/src/transport/dispatch/messages.ts +58 -0
- package/src/transport/index.ts +11 -0
- package/src/transport/interface/connector.ts +9 -0
- package/src/transport/interface/listener.ts +16 -0
- package/src/transport/interface/socket.ts +15 -0
- package/src/transport/interface/transferable.ts +125 -0
- package/src/transport/node/index.ts +2 -0
- package/src/transport/node/node_connector.ts +30 -0
- package/src/transport/node/node_connector_socket.ts +52 -0
- package/src/transport/node/node_listener.ts +34 -0
- package/src/transport/node/node_listener_socket.ts +48 -0
- package/src/transport/transport_client.ts +131 -0
- package/src/transport/transport_server.ts +108 -0
- package/src/trees/index.ts +54 -0
- package/src/types/index.ts +8 -0
- package/src/url/index.ts +73 -0
- package/src/wasm/README.md +6 -0
- package/src/wasm/empty_wasi_sdk.ts +166 -0
- package/src/wasm/fixtures/gcd.wasm +0 -0
- package/src/wasm/fixtures/gcd.wat +27 -0
- package/src/wasm/index.ts +1 -0
- package/src/wasm/wasm_module.ts +260 -0
- package/src/worker/browser/index.ts +2 -0
- package/src/worker/browser/start_web_module.ts +23 -0
- package/src/worker/browser/web_data_store.ts +38 -0
- package/src/worker/browser/web_worker.ts +24 -0
- package/src/worker/data_store.ts +19 -0
- package/src/worker/index.ts +2 -0
- package/src/worker/node/index.ts +2 -0
- package/src/worker/node/node_data_store.ts +27 -0
- package/src/worker/node/node_worker.ts +22 -0
- package/src/worker/node/start_node_module.ts +29 -0
- package/src/worker/wasm_worker.ts +7 -0
- package/src/worker/worker_pool.ts +73 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { BufferReader, FieldReader } from '../serialize/index.js';
|
|
2
|
+
import { Fr } from './fields.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a Point on an elliptic curve with x and y coordinates.
|
|
6
|
+
* The Point class provides methods for creating instances from different input types,
|
|
7
|
+
* converting instances to various output formats, and checking the equality of points.
|
|
8
|
+
*/
|
|
9
|
+
export class Point {
|
|
10
|
+
static ZERO = new Point(Fr.ZERO, Fr.ZERO);
|
|
11
|
+
static SIZE_IN_BYTES = Fr.SIZE_IN_BYTES * 2;
|
|
12
|
+
|
|
13
|
+
/** Used to differentiate this class from AztecAddress */
|
|
14
|
+
public readonly kind = 'point';
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
/**
|
|
18
|
+
* The point's x coordinate
|
|
19
|
+
*/
|
|
20
|
+
public readonly x: Fr,
|
|
21
|
+
/**
|
|
22
|
+
* The point's y coordinate
|
|
23
|
+
*/
|
|
24
|
+
public readonly y: Fr,
|
|
25
|
+
) {}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Generate a random Point instance.
|
|
29
|
+
*
|
|
30
|
+
* @returns A randomly generated Point instance.
|
|
31
|
+
*/
|
|
32
|
+
static random() {
|
|
33
|
+
// TODO is this a random point on the curve?
|
|
34
|
+
return new Point(Fr.random(), Fr.random());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create a Point instance from a given buffer or BufferReader.
|
|
39
|
+
* The input 'buffer' should have exactly 64 bytes representing the x and y coordinates.
|
|
40
|
+
*
|
|
41
|
+
* @param buffer - The buffer or BufferReader containing the x and y coordinates of the point.
|
|
42
|
+
* @returns A Point instance.
|
|
43
|
+
*/
|
|
44
|
+
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
45
|
+
const reader = BufferReader.asReader(buffer);
|
|
46
|
+
return new this(Fr.fromBuffer(reader.readBytes(32)), Fr.fromBuffer(reader.readBytes(32)));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Create a Point instance from a hex-encoded string.
|
|
51
|
+
* The input 'address' should be prefixed with '0x' or not, and have exactly 128 hex characters representing the x and y coordinates.
|
|
52
|
+
* Throws an error if the input length is invalid or coordinate values are out of range.
|
|
53
|
+
*
|
|
54
|
+
* @param address - The hex-encoded string representing the Point coordinates.
|
|
55
|
+
* @returns A Point instance.
|
|
56
|
+
*/
|
|
57
|
+
static fromString(address: string) {
|
|
58
|
+
return this.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Returns the contents of the point as an array of 2 fields.
|
|
63
|
+
* @returns The point as an array of 2 fields
|
|
64
|
+
*/
|
|
65
|
+
toFields() {
|
|
66
|
+
return [this.x, this.y];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static fromFields(fields: Fr[] | FieldReader) {
|
|
70
|
+
const reader = FieldReader.asReader(fields);
|
|
71
|
+
return new this(reader.readField(), reader.readField());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Returns the contents of the point as BigInts.
|
|
76
|
+
* @returns The point as BigInts
|
|
77
|
+
*/
|
|
78
|
+
toBigInts() {
|
|
79
|
+
return {
|
|
80
|
+
x: this.x.toBigInt(),
|
|
81
|
+
y: this.y.toBigInt(),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Converts the Point instance to a Buffer representation of the coordinates.
|
|
87
|
+
* The outputs buffer length will be 64, the length of both coordinates not represented as fields.
|
|
88
|
+
* @returns A Buffer representation of the Point instance.
|
|
89
|
+
*/
|
|
90
|
+
toBuffer() {
|
|
91
|
+
return Buffer.concat([this.x.toBuffer(), this.y.toBuffer()]);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Convert the Point instance to a hexadecimal string representation.
|
|
96
|
+
* The output string is prefixed with '0x' and consists of exactly 128 hex characters,
|
|
97
|
+
* representing the concatenated x and y coordinates of the point.
|
|
98
|
+
*
|
|
99
|
+
* @returns A hex-encoded string representing the Point instance.
|
|
100
|
+
*/
|
|
101
|
+
toString() {
|
|
102
|
+
return '0x' + this.toBuffer().toString('hex');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Generate a short string representation of the Point instance.
|
|
107
|
+
* The returned string includes the first 10 and last 4 characters of the full string representation,
|
|
108
|
+
* with '...' in between to indicate truncation. This is useful for displaying or logging purposes
|
|
109
|
+
* when the full string representation may be too long.
|
|
110
|
+
*
|
|
111
|
+
* @returns A truncated string representation of the Point instance.
|
|
112
|
+
*/
|
|
113
|
+
toShortString() {
|
|
114
|
+
const str = this.toString();
|
|
115
|
+
return `${str.slice(0, 10)}...${str.slice(-4)}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Check if two Point instances are equal by comparing their buffer values.
|
|
120
|
+
* Returns true if the buffer values are the same, and false otherwise.
|
|
121
|
+
*
|
|
122
|
+
* @param rhs - The Point instance to compare with the current instance.
|
|
123
|
+
* @returns A boolean indicating whether the two Point instances are equal.
|
|
124
|
+
*/
|
|
125
|
+
equals(rhs: Point) {
|
|
126
|
+
return this.x.equals(rhs.x) && this.y.equals(rhs.y);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
isZero() {
|
|
130
|
+
return this.x.isZero() && this.y.isZero();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Does this object look like a point?
|
|
136
|
+
* @param obj - Object to test if it is a point.
|
|
137
|
+
* @returns Whether it looks like a point.
|
|
138
|
+
*/
|
|
139
|
+
export function isPoint(obj: object): obj is Point {
|
|
140
|
+
if (!obj) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
const point = obj as Point;
|
|
144
|
+
return point.kind === 'point' && point.x !== undefined && point.y !== undefined;
|
|
145
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { createDebugLogger } from '../log/index.js';
|
|
2
|
+
import { Semaphore } from './semaphore.js';
|
|
3
|
+
import { SerialQueue } from './serial_queue.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Leverages the unbounded SerialQueue and Semaphore to create a SerialQueue that will block when putting an item
|
|
7
|
+
* if the queue size = maxQueueSize.
|
|
8
|
+
*/
|
|
9
|
+
export class BoundedSerialQueue {
|
|
10
|
+
private readonly queue = new SerialQueue();
|
|
11
|
+
private semaphore: Semaphore;
|
|
12
|
+
|
|
13
|
+
constructor(maxQueueSize: number, private log = createDebugLogger('aztec:foundation:bounded_serial_queue')) {
|
|
14
|
+
this.semaphore = new Semaphore(maxQueueSize);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Initializes the underlying SerialQueue instance, allowing items to be processed from the queue.
|
|
19
|
+
* The start method should be called before using the BoundedSerialQueue to ensure proper functionality.
|
|
20
|
+
*/
|
|
21
|
+
public start() {
|
|
22
|
+
this.queue.start();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns the current number of items in the queue.
|
|
27
|
+
* This is useful for monitoring the size of BoundedSerialQueue and understanding its utilization.
|
|
28
|
+
*
|
|
29
|
+
* @returns The length of the queue as an integer value.
|
|
30
|
+
*/
|
|
31
|
+
public length() {
|
|
32
|
+
return this.queue.length();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Cancels the current operation in the SerialQueue, if any, and clears the queue.
|
|
37
|
+
* Any pending tasks in the queue will not be executed, and the queue will be emptied.
|
|
38
|
+
* This method is useful for cleaning up resources and stopping ongoing processes when they are no longer needed.
|
|
39
|
+
* @returns A promise, resolved once cancelled.
|
|
40
|
+
*/
|
|
41
|
+
public cancel() {
|
|
42
|
+
return this.queue.cancel();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Ends the queue processing gracefully, preventing new items from being added.
|
|
47
|
+
* The currently executing item, if any, will complete and remaining queued items
|
|
48
|
+
* will be processed in order. Once all items have been processed, the queue becomes
|
|
49
|
+
* permanently unusable.
|
|
50
|
+
*
|
|
51
|
+
* @returns A promise that resolves when all items in the queue have been processed.
|
|
52
|
+
*/
|
|
53
|
+
public end() {
|
|
54
|
+
return this.queue.end();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The caller will block until fn is successfully enqueued.
|
|
59
|
+
* The fn itself is execute asynchronously and its result discarded.
|
|
60
|
+
* TODO(AD) do we need this if we have exec()?
|
|
61
|
+
* @param fn - The function to call once unblocked.
|
|
62
|
+
*/
|
|
63
|
+
public async put(fn: () => Promise<void>): Promise<void> {
|
|
64
|
+
await this.semaphore.acquire();
|
|
65
|
+
this.queue
|
|
66
|
+
.put(async () => {
|
|
67
|
+
try {
|
|
68
|
+
await fn();
|
|
69
|
+
} finally {
|
|
70
|
+
this.semaphore.release();
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
.catch(err => {
|
|
74
|
+
this.log.error('BoundedSerialQueue handler exception:', err);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The caller will block until fn is successfully executed, and it's result returned.
|
|
80
|
+
* @param fn - The function.
|
|
81
|
+
* @returns A promise that resolves with the result once executed.
|
|
82
|
+
*/
|
|
83
|
+
public async exec<T>(fn: () => Promise<T>): Promise<T> {
|
|
84
|
+
await this.semaphore.acquire();
|
|
85
|
+
return this.queue.put(async () => {
|
|
86
|
+
try {
|
|
87
|
+
return await fn();
|
|
88
|
+
} finally {
|
|
89
|
+
this.semaphore.release();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Awaiting this ensures the queue is empty before resuming.
|
|
96
|
+
*/
|
|
97
|
+
public async syncPoint() {
|
|
98
|
+
await this.queue.syncPoint();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { createDebugLogger } from '../log/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A simple fifo queue. It can grow unbounded. It can have multiple producers and consumers.
|
|
5
|
+
* Putting an item onto the queue always succeeds, unless either end() or cancel() has been called in which case
|
|
6
|
+
* the item being pushed is simply discarded.
|
|
7
|
+
*/
|
|
8
|
+
export class MemoryFifo<T> {
|
|
9
|
+
private waiting: ((item: T | null) => void)[] = [];
|
|
10
|
+
private items: T[] = [];
|
|
11
|
+
private flushing = false;
|
|
12
|
+
|
|
13
|
+
constructor(private log = createDebugLogger('aztec:foundation:memory_fifo')) {}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns the current number of items in the queue.
|
|
17
|
+
* The length represents the size of the queue at the time of invocation and may change as new items are added or consumed.
|
|
18
|
+
*
|
|
19
|
+
* @returns The number of items in the queue.
|
|
20
|
+
*/
|
|
21
|
+
public length() {
|
|
22
|
+
return this.items.length;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns next item within the queue, or blocks until and item has been put into the queue.
|
|
27
|
+
* If given a timeout, the promise will reject if no item is received after `timeout` seconds.
|
|
28
|
+
* If the queue is flushing, `null` is returned.
|
|
29
|
+
* @param timeout - The timeout in seconds.
|
|
30
|
+
* @returns A result promise.
|
|
31
|
+
*/
|
|
32
|
+
public get(timeout?: number): Promise<T | null> {
|
|
33
|
+
if (this.items.length) {
|
|
34
|
+
return Promise.resolve(this.items.shift()!);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (this.items.length === 0 && this.flushing) {
|
|
38
|
+
return Promise.resolve(null);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return new Promise<T | null>((resolve, reject) => {
|
|
42
|
+
this.waiting.push(resolve);
|
|
43
|
+
|
|
44
|
+
if (timeout) {
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
const index = this.waiting.findIndex(r => r === resolve);
|
|
47
|
+
if (index > -1) {
|
|
48
|
+
this.waiting.splice(index, 1);
|
|
49
|
+
const err = new Error('Timeout getting item from queue.');
|
|
50
|
+
reject(err);
|
|
51
|
+
}
|
|
52
|
+
}, timeout * 1000);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Put an item onto back of the queue.
|
|
59
|
+
* @param item - The item to enqueue.
|
|
60
|
+
* @returns A boolean indicating whether the item was successfully added to the queue.
|
|
61
|
+
*/
|
|
62
|
+
public put(item: T): boolean {
|
|
63
|
+
if (this.flushing) {
|
|
64
|
+
this.log.warn('Discarding item because queue is flushing');
|
|
65
|
+
return false;
|
|
66
|
+
} else if (this.waiting.length) {
|
|
67
|
+
this.waiting.shift()!(item);
|
|
68
|
+
return true;
|
|
69
|
+
} else {
|
|
70
|
+
this.items.push(item);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Once ended, no further items are added to queue. Consumers will consume remaining items within the queue.
|
|
77
|
+
* The queue is not reusable after calling `end()`.
|
|
78
|
+
* Any consumers waiting for an item receive null.
|
|
79
|
+
*/
|
|
80
|
+
public end() {
|
|
81
|
+
this.flushing = true;
|
|
82
|
+
this.waiting.forEach(resolve => resolve(null));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Once cancelled, all items are discarded from the queue, and no further items are added to the queue.
|
|
87
|
+
* The queue is not reusable after calling `cancel()`.
|
|
88
|
+
* Any consumers waiting for an item receive null.
|
|
89
|
+
*/
|
|
90
|
+
public cancel() {
|
|
91
|
+
this.flushing = true;
|
|
92
|
+
this.items = [];
|
|
93
|
+
this.waiting.forEach(resolve => resolve(null));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Process items from the queue using a provided handler function.
|
|
98
|
+
* The function iterates over items in the queue, invoking the handler for each item until the queue is empty and flushing.
|
|
99
|
+
* If the handler throws an error, it will be caught and logged as 'Queue handler exception:', but the iteration will continue.
|
|
100
|
+
* The process function returns a promise that resolves when there are no more items in the queue and the queue is flushing.
|
|
101
|
+
*
|
|
102
|
+
* @param handler - A function that takes an item of type T and returns a Promise<void> after processing the item.
|
|
103
|
+
* @returns A Promise<void> that resolves when the queue is finished processing.
|
|
104
|
+
*/
|
|
105
|
+
public async process(handler: (item: T) => Promise<void>) {
|
|
106
|
+
try {
|
|
107
|
+
while (true) {
|
|
108
|
+
const item = await this.get();
|
|
109
|
+
if (item === null) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
await handler(item);
|
|
113
|
+
}
|
|
114
|
+
} catch (err) {
|
|
115
|
+
this.log.error('Queue handler exception', err);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { MemoryFifo } from './memory_fifo.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Allows the acquiring of up to `size` tokens before calls to acquire block, waiting for a call to release().
|
|
5
|
+
*/
|
|
6
|
+
export class Semaphore {
|
|
7
|
+
private readonly queue = new MemoryFifo<boolean>();
|
|
8
|
+
|
|
9
|
+
constructor(size: number) {
|
|
10
|
+
new Array(size).fill(true).map(() => this.queue.put(true));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Acquires a token from the Semaphore, allowing access to a limited resource.
|
|
15
|
+
* If no tokens are available, the call will block and wait until a token is released.
|
|
16
|
+
* Use in conjunction with the release() method to manage access to resources with limited capacity.
|
|
17
|
+
*
|
|
18
|
+
* @returns A Promise that resolves when a token is acquired.
|
|
19
|
+
*/
|
|
20
|
+
public async acquire() {
|
|
21
|
+
await this.queue.get();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Releases a token back into the semaphore, allowing another acquire call to proceed.
|
|
26
|
+
* If there are any pending calls to acquire(), one of them will be unblocked and allowed to proceed.
|
|
27
|
+
* This method should only be called by the holder of the acquired token to ensure proper functionality
|
|
28
|
+
* and avoid unexpected behavior.
|
|
29
|
+
*/
|
|
30
|
+
public release() {
|
|
31
|
+
this.queue.put(true);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { MemoryFifo } from './memory_fifo.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A more specialized fifo queue that enqueues functions to execute. Enqueued functions are executed in serial.
|
|
5
|
+
*/
|
|
6
|
+
export class SerialQueue {
|
|
7
|
+
private readonly queue = new MemoryFifo<() => Promise<void>>();
|
|
8
|
+
private runningPromise!: Promise<void>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Initializes the execution of enqueued functions in the serial queue.
|
|
12
|
+
* Functions are executed in the order they were added to the queue, with each function
|
|
13
|
+
* waiting for the completion of the previous one before starting its execution.
|
|
14
|
+
* This method should be called once to start processing the queue.
|
|
15
|
+
*/
|
|
16
|
+
public start() {
|
|
17
|
+
this.runningPromise = this.queue.process(fn => fn());
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Returns the current number of enqueued functions in the serial queue.
|
|
22
|
+
* This provides a way to check the size of the queue and monitor its progress.
|
|
23
|
+
*
|
|
24
|
+
* @returns The length of the serial queue as a number.
|
|
25
|
+
*/
|
|
26
|
+
public length() {
|
|
27
|
+
return this.queue.length();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Cancels the processing of the remaining functions in the serial queue and resolves the running promise.
|
|
32
|
+
* Any enqueued functions that have not yet been executed will be discarded. The queue can still accept new
|
|
33
|
+
* functions after cancellation, but the previously enqueued functions will not be re-processed.
|
|
34
|
+
*
|
|
35
|
+
* @returns The running promise which resolves when the current executing function (if any) completes.
|
|
36
|
+
*/
|
|
37
|
+
public cancel() {
|
|
38
|
+
this.queue.cancel();
|
|
39
|
+
return this.runningPromise;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Signals the SerialQueue that it should finish processing its current task and stop accepting new tasks.
|
|
44
|
+
* The returned Promise resolves when all enqueued tasks have completed execution.
|
|
45
|
+
*
|
|
46
|
+
* @returns A Promise that resolves when the queue is completely emptied and no new tasks are allowed.
|
|
47
|
+
*/
|
|
48
|
+
public end() {
|
|
49
|
+
this.queue.end();
|
|
50
|
+
return this.runningPromise;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Enqueues fn for execution on the serial queue.
|
|
55
|
+
* Returns the result of the function after execution.
|
|
56
|
+
* @param fn - The function to enqueue.
|
|
57
|
+
* @returns A resolution promise. Rejects if the function does, or if the function could not be enqueued.
|
|
58
|
+
*/
|
|
59
|
+
public put<T>(fn: () => Promise<T>): Promise<T> {
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
const accepted = this.queue.put(async () => {
|
|
62
|
+
try {
|
|
63
|
+
const res = await fn();
|
|
64
|
+
resolve(res);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
reject(e);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
if (!accepted) {
|
|
70
|
+
reject(new Error('Could not enqueue function'));
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Awaiting this ensures the queue is empty before resuming.
|
|
77
|
+
*/
|
|
78
|
+
public async syncPoint() {
|
|
79
|
+
await this.put(async () => {});
|
|
80
|
+
}
|
|
81
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Reexport all folders at the root for packages targeting CommonJS
|
|
2
|
+
export * as abi from './abi/index.js';
|
|
3
|
+
export * as asyncMap from './async-map/index.js';
|
|
4
|
+
export * as aztecAddress from './aztec-address/index.js';
|
|
5
|
+
export * as bigintBuffer from './bigint-buffer/index.js';
|
|
6
|
+
export * as collection from './collection/index.js';
|
|
7
|
+
export * as committable from './committable/index.js';
|
|
8
|
+
export * as crypto from './crypto/index.js';
|
|
9
|
+
export * as errors from './errors/index.js';
|
|
10
|
+
export * as ethAddress from './eth-address/index.js';
|
|
11
|
+
export * as fields from './fields/index.js';
|
|
12
|
+
export * as fifo from './fifo/index.js';
|
|
13
|
+
export * as jsonRpc from './json-rpc/index.js';
|
|
14
|
+
export * as jsonRpcClient from './json-rpc/client/index.js';
|
|
15
|
+
export * as jsonRpcServer from './json-rpc/server/index.js';
|
|
16
|
+
export * as log from './log/index.js';
|
|
17
|
+
export * as mutex from './mutex/index.js';
|
|
18
|
+
export * as retry from './retry/index.js';
|
|
19
|
+
export * as runningPromise from './running-promise/index.js';
|
|
20
|
+
export * as serialize from './serialize/index.js';
|
|
21
|
+
export * as sleep from './sleep/index.js';
|
|
22
|
+
export * as timer from './timer/index.js';
|
|
23
|
+
export * as transport from './transport/index.js';
|
|
24
|
+
export * as trees from './trees/index.js';
|
|
25
|
+
export * as types from './types/index.js';
|
|
26
|
+
export * as url from './url/index.js';
|
|
27
|
+
export * as wasm from './wasm/index.js';
|
|
28
|
+
export * as worker from './worker/index.js';
|
|
29
|
+
export * as testing from './testing/index.js';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# json-rpc
|
|
2
|
+
|
|
3
|
+
json-rpc
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
-- src
|
|
7
|
+
-- client
|
|
8
|
+
Code to use by a client wishing to use a json-rpc server
|
|
9
|
+
Includes syntax sugar for making requests with normal method syntax
|
|
10
|
+
-- server
|
|
11
|
+
Code for easily turning a class into an exposed RPC with one endpoint per method
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Each createJsonRpcClient and JsonRpcServer call needs a map of classes that will be translated in input and output values.
|
|
15
|
+
By default, Buffer is handled, but other user-made classes need to define toString() and static fromString() like so:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
class PublicKey {
|
|
19
|
+
toString() {
|
|
20
|
+
return '...';
|
|
21
|
+
}
|
|
22
|
+
static fromString(str) {
|
|
23
|
+
return new PublicKey(...);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
In Dapp:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
const wallet = createJsonRpcClient<WalletImplementation>('wallet-server.com', /*register classes*/ {PublicKey, TxRequest});
|
|
34
|
+
const response = await wallet.signTxRequest(accountPubKey, txRequest);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The client will send `[{ name: 'PublicKey', value: accountPubKey.toString() }, { name: 'TxRequest', txRequest.toString() }]` to the server.
|
|
38
|
+
|
|
39
|
+
In wallet:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
const publicClient = createJsonRpcClient<PublicClient>('public-client.com', /*register classes*/ ...);
|
|
43
|
+
const keyStore = createJsonRpcClient<KeyStore>('key-store.com', /*register classes*/ ...);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Different clients for different services.
|
|
47
|
+
|
|
48
|
+
-- server
|
|
49
|
+
Running a wallet server:
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
const wallet = new WalletImplementation();
|
|
53
|
+
const server = new JsonRpcServer(wallet, /*register classes*/ ...);
|
|
54
|
+
server.start(8080);
|
|
55
|
+
```
|