@aztec/foundation 0.23.0 → 0.24.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.
- package/dest/fields/fields.js +3 -3
- 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/index.ts +6 -0
- package/src/abi/selector.ts +243 -0
- package/src/abi/utils.ts +50 -0
- package/src/array/array.ts +86 -0
- package/src/array/index.ts +1 -0
- package/src/async-map/index.ts +18 -0
- package/src/aztec-address/index.ts +36 -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 +16 -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 +42 -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 +234 -0
- package/src/fields/coordinate.ts +104 -0
- package/src/fields/fields.ts +328 -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 +286 -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 +303 -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,24 @@
|
|
|
1
|
+
import { DispatchMsg, TransportClient, WorkerConnector, createDispatchProxy } from '../../transport/index.js';
|
|
2
|
+
import { WasmModule } from '../../wasm/index.js';
|
|
3
|
+
import { WasmWorker } from '../wasm_worker.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Instantiate a web worker.
|
|
7
|
+
* @param url - The URL.
|
|
8
|
+
* @param initialMem - Initial memory pages.
|
|
9
|
+
* @param maxMem - Maximum memory pages.
|
|
10
|
+
* @returns The worker.
|
|
11
|
+
*/
|
|
12
|
+
export async function createWebWorker(url: string, initialMem?: number, maxMem?: number): Promise<WasmWorker> {
|
|
13
|
+
const worker = new Worker(url);
|
|
14
|
+
const transportConnect = new WorkerConnector(worker);
|
|
15
|
+
const transportClient = new TransportClient<DispatchMsg>(transportConnect);
|
|
16
|
+
await transportClient.open();
|
|
17
|
+
const remoteModule = createDispatchProxy(WasmModule, transportClient) as WasmWorker;
|
|
18
|
+
remoteModule.destroyWorker = async () => {
|
|
19
|
+
await transportClient.request({ fn: '__destroyWorker__', args: [] });
|
|
20
|
+
transportClient.close();
|
|
21
|
+
};
|
|
22
|
+
await remoteModule.init(initialMem, maxMem);
|
|
23
|
+
return remoteModule;
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple read/write interface for wasm modules.
|
|
3
|
+
*/
|
|
4
|
+
export interface DataStore {
|
|
5
|
+
/**
|
|
6
|
+
* Get a value from our DB.
|
|
7
|
+
* @param key - The key to look up.
|
|
8
|
+
* @returns The value.
|
|
9
|
+
*/
|
|
10
|
+
get(key: string): Promise<Buffer | undefined>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Set a value in our DB.
|
|
14
|
+
* @param key - The key to update.
|
|
15
|
+
* @param value - The value to set.
|
|
16
|
+
* @returns Nothing.
|
|
17
|
+
*/
|
|
18
|
+
set(key: string, value: Buffer): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import leveldown from 'leveldown';
|
|
2
|
+
import levelup, { LevelUp } from 'levelup';
|
|
3
|
+
import memdown from 'memdown';
|
|
4
|
+
|
|
5
|
+
import { DataStore } from '../data_store.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Cache for data used by wasm module.
|
|
9
|
+
*/
|
|
10
|
+
export class NodeDataStore implements DataStore {
|
|
11
|
+
private db: LevelUp;
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line
|
|
14
|
+
constructor(path?: string) {
|
|
15
|
+
// Hack: Cast as any to work around packages "broken" with node16 resolution
|
|
16
|
+
// See https://github.com/microsoft/TypeScript/issues/49160
|
|
17
|
+
this.db = levelup(path ? (leveldown as any)(path) : (memdown as any)());
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async get(key: string): Promise<Buffer | undefined> {
|
|
21
|
+
return await this.db.get(key).catch(() => {});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async set(key: string, value: Buffer): Promise<void> {
|
|
25
|
+
await this.db.put(key, value);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Worker } from 'worker_threads';
|
|
2
|
+
|
|
3
|
+
import { DispatchMsg, NodeConnector, TransportClient, createDispatchProxy } from '../../transport/index.js';
|
|
4
|
+
import { WasmModule } from '../../wasm/wasm_module.js';
|
|
5
|
+
import { WasmWorker } from '../wasm_worker.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a node worker.
|
|
9
|
+
*/
|
|
10
|
+
export async function createNodeWorker(filepath: string, initialMem?: number, maxMem?: number): Promise<WasmWorker> {
|
|
11
|
+
const worker = new Worker(filepath);
|
|
12
|
+
const transportConnect = new NodeConnector(worker);
|
|
13
|
+
const transportClient = new TransportClient<DispatchMsg>(transportConnect);
|
|
14
|
+
await transportClient.open();
|
|
15
|
+
const remoteModule = createDispatchProxy(WasmModule, transportClient) as WasmWorker;
|
|
16
|
+
remoteModule.destroyWorker = async () => {
|
|
17
|
+
await transportClient.request({ fn: '__destroyWorker__', args: [] });
|
|
18
|
+
transportClient.close();
|
|
19
|
+
};
|
|
20
|
+
await remoteModule.init(initialMem, maxMem);
|
|
21
|
+
return remoteModule;
|
|
22
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { parentPort } from 'worker_threads';
|
|
2
|
+
|
|
3
|
+
import { DispatchMsg, NodeListener, TransportServer } from '../../transport/index.js';
|
|
4
|
+
import { WasmModule } from '../../wasm/wasm_module.js';
|
|
5
|
+
|
|
6
|
+
if (!parentPort) {
|
|
7
|
+
throw new Error('InvalidWorker');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Start the transport server corresponding to this module.
|
|
12
|
+
* @param module - The WasmModule to host.
|
|
13
|
+
*/
|
|
14
|
+
export function startNodeModule(module: WasmModule) {
|
|
15
|
+
const dispatch = async ({ fn, args }: DispatchMsg) => {
|
|
16
|
+
if (fn === '__destroyWorker__') {
|
|
17
|
+
transportServer.stop();
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (!(module as any)[fn]) {
|
|
21
|
+
throw new Error(`dispatch error, function not found: ${fn}`);
|
|
22
|
+
}
|
|
23
|
+
return await (module as any)[fn](...args);
|
|
24
|
+
};
|
|
25
|
+
const transportListener = new NodeListener();
|
|
26
|
+
const transportServer = new TransportServer<DispatchMsg>(transportListener, dispatch);
|
|
27
|
+
module.addLogger((...args: any[]) => transportServer.broadcast({ fn: 'emit', args: ['log', ...args] }));
|
|
28
|
+
transportServer.start();
|
|
29
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { createDebugLogger } from '../log/index.js';
|
|
2
|
+
import { WasmWorker } from './wasm_worker.js';
|
|
3
|
+
|
|
4
|
+
const debug = createDebugLogger('bb:worker_pool');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Type of a worker factory.
|
|
8
|
+
* Used to customize WorkerPool worker construction.
|
|
9
|
+
*/
|
|
10
|
+
export type CreateWorker = (name: string, minMem: number, maxMem: number) => WasmWorker;
|
|
11
|
+
/**
|
|
12
|
+
* Allocates a pool of WasmWorker's.
|
|
13
|
+
* Worker 0 is allocated MAX_PAGES memory pages. This is because worker 0 will need to hold the proving key
|
|
14
|
+
* (i.e. Has state), whereas the others are pure compute (they hold a little crs state).
|
|
15
|
+
*/
|
|
16
|
+
export class WorkerPool {
|
|
17
|
+
// TODO(AD): Revisit what this means in aztec 3 context
|
|
18
|
+
// --
|
|
19
|
+
// Introduction of low mem prover work (polynomial cache) may actually increase mem usage when the backing store isn't
|
|
20
|
+
// enabled. We were seeing intermittent failings related to memory in production for some users when limiting to
|
|
21
|
+
// 6660 (416MB). It would be nice to understand why this is (the non determinism and/or the increased mem usage).
|
|
22
|
+
// For now, increasing mem usage to 512MB. This maybe preferable to backing out the low mem work, but
|
|
23
|
+
// ironically may break the chance of us using it in mobile.
|
|
24
|
+
// We *could* enable the low memory backing store, but this needs a little bit of work to actually
|
|
25
|
+
// read/write from indexeddb, performance testing, and actual further memory load testing.
|
|
26
|
+
// At this point it's hard to know what our memory savings would be relative to just fully reverting the LMP.
|
|
27
|
+
// public static MAX_PAGES = 6660;
|
|
28
|
+
/**
|
|
29
|
+
* The maximum number of memory pages to be used by the webassembly.
|
|
30
|
+
*/
|
|
31
|
+
public static MAX_PAGES = 8192;
|
|
32
|
+
/**
|
|
33
|
+
* The workers in the pool.
|
|
34
|
+
*/
|
|
35
|
+
private workers: WasmWorker[] = [];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create an instance and initialize the workers.
|
|
39
|
+
* @param createWorker - Worker factory.
|
|
40
|
+
* @param poolSize - Pool size.
|
|
41
|
+
* @returns An initialized WorkerPool.
|
|
42
|
+
*/
|
|
43
|
+
static async new(createWorker: CreateWorker, poolSize: number) {
|
|
44
|
+
const pool = new WorkerPool();
|
|
45
|
+
await pool.init(createWorker, poolSize);
|
|
46
|
+
return pool;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Initialize the workers.
|
|
51
|
+
* @param createWorker - Worker factory().
|
|
52
|
+
* @param poolSize - Pool size.
|
|
53
|
+
* @param maxMem - Max memory pages.
|
|
54
|
+
*/
|
|
55
|
+
public async init(createWorker: CreateWorker, poolSize: number, maxMem = WorkerPool.MAX_PAGES) {
|
|
56
|
+
debug(`creating ${poolSize} workers...`);
|
|
57
|
+
const start = new Date().getTime();
|
|
58
|
+
this.workers = await Promise.all(
|
|
59
|
+
Array(poolSize)
|
|
60
|
+
.fill(0)
|
|
61
|
+
.map((_, i) => createWorker(`${i}`, i === 0 ? Math.min(WorkerPool.MAX_PAGES, maxMem) : 768, maxMem)),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
debug(`created workers: ${new Date().getTime() - start}ms`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Tell all workers in the pool to stop processing.
|
|
69
|
+
*/
|
|
70
|
+
public async destroy() {
|
|
71
|
+
await Promise.all(this.workers.map(w => w.destroyWorker()));
|
|
72
|
+
}
|
|
73
|
+
}
|