@aztec/foundation 0.0.1-commit.c7c42ec → 0.0.1-commit.f295ac2
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/branded-types/block_number.d.ts +4 -1
- package/dest/branded-types/block_number.d.ts.map +1 -1
- package/dest/branded-types/block_number.js +3 -0
- package/dest/branded-types/checkpoint_number.d.ts +10 -2
- package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
- package/dest/branded-types/checkpoint_number.js +17 -8
- package/dest/branded-types/index.d.ts +3 -2
- package/dest/branded-types/index.d.ts.map +1 -1
- package/dest/branded-types/index.js +2 -1
- package/dest/branded-types/index_within_checkpoint.d.ts +42 -0
- package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -0
- package/dest/branded-types/index_within_checkpoint.js +59 -0
- package/dest/buffer/buffer16.d.ts +4 -1
- package/dest/buffer/buffer16.d.ts.map +1 -1
- package/dest/buffer/buffer16.js +3 -1
- package/dest/buffer/buffer32.d.ts +4 -1
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/buffer32.js +3 -1
- package/dest/config/env_var.d.ts +2 -2
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/index.d.ts +6 -3
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +16 -7
- package/dest/config/parse-env.d.ts +3 -0
- package/dest/config/parse-env.d.ts.map +1 -0
- package/dest/config/parse-env.js +7 -0
- package/dest/config/secret_value.js +3 -1
- package/dest/crypto/ecdsa/signature.d.ts +10 -1
- package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
- package/dest/crypto/random/randomness_singleton.d.ts +2 -2
- package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
- package/dest/crypto/random/randomness_singleton.js +3 -3
- package/dest/curves/bls12/field.js +6 -3
- package/dest/curves/bls12/point.d.ts +10 -1
- package/dest/curves/bls12/point.d.ts.map +1 -1
- package/dest/curves/bls12/point.js +3 -1
- package/dest/curves/bn254/field.js +5 -2
- package/dest/curves/grumpkin/point.d.ts +11 -2
- package/dest/curves/grumpkin/point.d.ts.map +1 -1
- package/dest/error/index.d.ts +4 -4
- package/dest/error/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +4 -2
- package/dest/eth-signature/eth_signature.d.ts +4 -1
- package/dest/eth-signature/eth_signature.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/class_a.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/class_b.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
- package/dest/log/pino-logger.d.ts +1 -1
- package/dest/log/pino-logger.d.ts.map +1 -1
- package/dest/log/pino-logger.js +1 -1
- package/dest/retry/index.d.ts +11 -1
- package/dest/retry/index.d.ts.map +1 -1
- package/dest/retry/index.js +11 -0
- package/dest/serialize/buffer_reader.d.ts +7 -4
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +13 -4
- package/dest/trees/{balanced_merkle_tree.d.ts → balanced_merkle_tree_root.d.ts} +1 -5
- package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
- package/dest/trees/{balanced_merkle_tree.js → balanced_merkle_tree_root.js} +1 -15
- package/dest/trees/hasher.d.ts +4 -1
- package/dest/trees/hasher.d.ts.map +1 -1
- package/dest/trees/hasher.js +15 -5
- package/dest/trees/index.d.ts +4 -4
- package/dest/trees/index.d.ts.map +1 -1
- package/dest/trees/index.js +3 -3
- package/dest/trees/membership_witness.d.ts +7 -1
- package/dest/trees/membership_witness.d.ts.map +1 -1
- package/dest/trees/sibling_path.d.ts +2 -1
- package/dest/trees/sibling_path.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +2 -3
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.js +1 -5
- package/dest/trees/{unbalanced_merkle_tree.d.ts → unbalanced_merkle_tree_root.d.ts} +3 -5
- package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
- package/dest/trees/{unbalanced_merkle_tree.js → unbalanced_merkle_tree_root.js} +11 -49
- package/dest/trees/unbalanced_tree_store.d.ts +5 -1
- package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
- package/dest/trees/unbalanced_tree_store.js +49 -1
- package/dest/types/index.d.ts +1 -1
- package/package.json +4 -4
- package/src/branded-types/block_number.ts +5 -0
- package/src/branded-types/checkpoint_number.ts +22 -7
- package/src/branded-types/index.ts +2 -1
- package/src/branded-types/index_within_checkpoint.ts +88 -0
- package/src/config/env_var.ts +15 -6
- package/src/config/index.ts +19 -4
- package/src/config/parse-env.ts +4 -0
- package/src/crypto/random/randomness_singleton.ts +2 -4
- package/src/eth-address/index.ts +1 -1
- package/src/log/pino-logger.ts +2 -1
- package/src/retry/index.ts +18 -0
- package/src/serialize/buffer_reader.ts +21 -9
- package/src/trees/{balanced_merkle_tree.ts → balanced_merkle_tree_root.ts} +1 -14
- package/src/trees/hasher.ts +13 -0
- package/src/trees/index.ts +3 -3
- package/src/trees/unbalanced_merkle_tree_calculator.ts +1 -12
- package/src/trees/{unbalanced_merkle_tree.ts → unbalanced_merkle_tree_root.ts} +17 -61
- package/src/trees/unbalanced_tree_store.ts +57 -2
- package/src/types/index.ts +1 -1
- package/dest/json-rpc/server/telemetry.d.ts +0 -2
- package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
- package/dest/json-rpc/server/telemetry.js +0 -0
- package/dest/trees/balanced_merkle_tree.d.ts.map +0 -1
- package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
- package/src/json-rpc/server/telemetry.ts +0 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import type { Branded } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A branded type representing the index of a block within a checkpoint.
|
|
7
|
+
* The first block in a checkpoint has index 0, the second has index 1, etc.
|
|
8
|
+
*
|
|
9
|
+
* This is a nominal type backed by a number, but TypeScript will treat it as
|
|
10
|
+
* incompatible with plain numbers and other branded numeric types.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const index = IndexWithinCheckpoint(0);
|
|
15
|
+
* const plainNumber: number = index; // OK - IndexWithinCheckpoint is assignable to number
|
|
16
|
+
* const indexFromNumber: IndexWithinCheckpoint = 0; // ERROR - number is not assignable to IndexWithinCheckpoint
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export type IndexWithinCheckpoint = Branded<number, 'IndexWithinCheckpoint'>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates an IndexWithinCheckpoint from a number.
|
|
23
|
+
* @param value - The index (must be a non-negative integer)
|
|
24
|
+
* @returns The branded IndexWithinCheckpoint value
|
|
25
|
+
* @throws If the value is negative or not an integer
|
|
26
|
+
*/
|
|
27
|
+
export function IndexWithinCheckpoint(value: number): IndexWithinCheckpoint {
|
|
28
|
+
if (!Number.isInteger(value)) {
|
|
29
|
+
throw new Error(`IndexWithinCheckpoint must be an integer, got ${value}`);
|
|
30
|
+
}
|
|
31
|
+
if (value < 0) {
|
|
32
|
+
throw new Error(`IndexWithinCheckpoint must be non-negative, got ${value}`);
|
|
33
|
+
}
|
|
34
|
+
return value as IndexWithinCheckpoint;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates an IndexWithinCheckpoint from a bigint.
|
|
39
|
+
* @param value - The index as bigint (must be a non-negative integer within safe integer range)
|
|
40
|
+
* @returns The branded IndexWithinCheckpoint value
|
|
41
|
+
* @throws If the value is negative or exceeds Number.MAX_SAFE_INTEGER
|
|
42
|
+
*/
|
|
43
|
+
IndexWithinCheckpoint.fromBigInt = function (value: bigint): IndexWithinCheckpoint {
|
|
44
|
+
if (value < 0n) {
|
|
45
|
+
throw new Error(`IndexWithinCheckpoint must be non-negative, got ${value}`);
|
|
46
|
+
}
|
|
47
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
48
|
+
throw new Error(`IndexWithinCheckpoint ${value} exceeds MAX_SAFE_INTEGER`);
|
|
49
|
+
}
|
|
50
|
+
return Number(value) as IndexWithinCheckpoint;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Creates an IndexWithinCheckpoint from a string.
|
|
55
|
+
* @param value - The index as a string
|
|
56
|
+
* @returns The branded IndexWithinCheckpoint value
|
|
57
|
+
* @throws If the string cannot be parsed as a valid index
|
|
58
|
+
*/
|
|
59
|
+
IndexWithinCheckpoint.fromString = function (value: string): IndexWithinCheckpoint {
|
|
60
|
+
const parsed = parseInt(value, 10);
|
|
61
|
+
if (isNaN(parsed)) {
|
|
62
|
+
throw new Error(`Cannot parse IndexWithinCheckpoint from string: ${value}`);
|
|
63
|
+
}
|
|
64
|
+
return IndexWithinCheckpoint(parsed);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Type guard to check if a value is a valid IndexWithinCheckpoint.
|
|
69
|
+
* Note: At runtime, an IndexWithinCheckpoint is just a number, so this checks if the value
|
|
70
|
+
* is a non-negative integer.
|
|
71
|
+
*/
|
|
72
|
+
IndexWithinCheckpoint.isValid = function (value: unknown): value is IndexWithinCheckpoint {
|
|
73
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The zero index value (first block in checkpoint).
|
|
78
|
+
*/
|
|
79
|
+
IndexWithinCheckpoint.ZERO = IndexWithinCheckpoint(0);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Zod schema for parsing and validating IndexWithinCheckpoint values.
|
|
83
|
+
* Accepts numbers, bigints, or strings and coerces them to IndexWithinCheckpoint.
|
|
84
|
+
*/
|
|
85
|
+
export const IndexWithinCheckpointSchema = z
|
|
86
|
+
.union([z.number(), z.bigint(), z.string()])
|
|
87
|
+
.pipe(z.coerce.number().int().min(0))
|
|
88
|
+
.transform(value => IndexWithinCheckpoint(value));
|
package/src/config/env_var.ts
CHANGED
|
@@ -21,15 +21,13 @@ export type EnvVar =
|
|
|
21
21
|
| 'BB_NUM_IVC_VERIFIERS'
|
|
22
22
|
| 'BB_IVC_CONCURRENCY'
|
|
23
23
|
| 'BOOTSTRAP_NODES'
|
|
24
|
-
| '
|
|
25
|
-
| 'BLOB_SINK_PORT'
|
|
26
|
-
| 'BLOB_SINK_URL'
|
|
24
|
+
| 'BLOB_ARCHIVE_API_URL'
|
|
27
25
|
| 'BLOB_FILE_STORE_URLS'
|
|
28
26
|
| 'BLOB_FILE_STORE_UPLOAD_URL'
|
|
29
27
|
| 'BLOB_HEALTHCHECK_UPLOAD_INTERVAL_MINUTES'
|
|
30
28
|
| 'BOT_DA_GAS_LIMIT'
|
|
31
29
|
| 'BOT_FEE_PAYMENT_METHOD'
|
|
32
|
-
| '
|
|
30
|
+
| 'BOT_MIN_FEE_PADDING'
|
|
33
31
|
| 'BOT_FLUSH_SETUP_TRANSACTIONS'
|
|
34
32
|
| 'BOT_FOLLOW_CHAIN'
|
|
35
33
|
| 'BOT_L2_GAS_LIMIT'
|
|
@@ -279,6 +277,7 @@ export type EnvVar =
|
|
|
279
277
|
| 'AZTEC_SLASHER_FLAVOR'
|
|
280
278
|
| 'AZTEC_GOVERNANCE_PROPOSER_QUORUM'
|
|
281
279
|
| 'AZTEC_GOVERNANCE_PROPOSER_ROUND_SIZE'
|
|
280
|
+
| 'AZTEC_GOVERNANCE_VOTING_DURATION'
|
|
282
281
|
| 'AZTEC_EXIT_DELAY_SECONDS'
|
|
283
282
|
| 'L1_GAS_LIMIT_BUFFER_PERCENTAGE'
|
|
284
283
|
| 'L1_GAS_PRICE_MAX'
|
|
@@ -305,7 +304,6 @@ export type EnvVar =
|
|
|
305
304
|
| 'K8S_POD_UID'
|
|
306
305
|
| 'K8S_NAMESPACE_NAME'
|
|
307
306
|
| 'VALIDATOR_REEXECUTE_DEADLINE_MS'
|
|
308
|
-
| 'ALWAYS_REEXECUTE_BLOCK_PROPOSALS'
|
|
309
307
|
| 'AUTO_UPDATE'
|
|
310
308
|
| 'AUTO_UPDATE_URL'
|
|
311
309
|
| 'WEB3_SIGNER_URL'
|
|
@@ -314,4 +312,15 @@ export type EnvVar =
|
|
|
314
312
|
| 'FISHERMAN_MODE'
|
|
315
313
|
| 'MAX_ALLOWED_ETH_CLIENT_DRIFT_SECONDS'
|
|
316
314
|
| 'LEGACY_BLS_CLI'
|
|
317
|
-
| 'DEBUG_FORCE_TX_PROOF_VERIFICATION'
|
|
315
|
+
| 'DEBUG_FORCE_TX_PROOF_VERIFICATION'
|
|
316
|
+
| 'VALIDATOR_HA_SIGNING_ENABLED'
|
|
317
|
+
| 'VALIDATOR_HA_NODE_ID'
|
|
318
|
+
| 'VALIDATOR_HA_POLLING_INTERVAL_MS'
|
|
319
|
+
| 'VALIDATOR_HA_SIGNING_TIMEOUT_MS'
|
|
320
|
+
| 'VALIDATOR_HA_MAX_STUCK_DUTIES_AGE_MS'
|
|
321
|
+
| 'VALIDATOR_HA_DATABASE_URL'
|
|
322
|
+
| 'VALIDATOR_HA_RUN_MIGRATIONS'
|
|
323
|
+
| 'VALIDATOR_HA_POOL_MAX'
|
|
324
|
+
| 'VALIDATOR_HA_POOL_MIN'
|
|
325
|
+
| 'VALIDATOR_HA_POOL_IDLE_TIMEOUT_MS'
|
|
326
|
+
| 'VALIDATOR_HA_POOL_CONNECTION_TIMEOUT_MS';
|
package/src/config/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Fq, Fr } from '../curves/bn254/field.js';
|
|
|
2
2
|
import { createConsoleLogger } from '../log/console.js';
|
|
3
3
|
import type { EnvVar } from './env_var.js';
|
|
4
4
|
import { type NetworkNames, getActiveNetworkName } from './network_name.js';
|
|
5
|
+
import { parseBooleanEnv } from './parse-env.js';
|
|
5
6
|
import { SecretValue } from './secret_value.js';
|
|
6
7
|
|
|
7
8
|
export { SecretValue, getActiveNetworkName };
|
|
@@ -148,6 +149,23 @@ export function floatConfigHelper(
|
|
|
148
149
|
};
|
|
149
150
|
}
|
|
150
151
|
|
|
152
|
+
/**
|
|
153
|
+
* Parses an environment variable to a 0-1 percentage value
|
|
154
|
+
*/
|
|
155
|
+
export function percentageConfigHelper(defaultVal: number): Pick<ConfigMapping, 'parseEnv' | 'defaultValue'> {
|
|
156
|
+
return {
|
|
157
|
+
parseEnv: (val: string): number => {
|
|
158
|
+
const parsed = safeParseFloat(val, defaultVal);
|
|
159
|
+
if (parsed < 0 || parsed > 1) {
|
|
160
|
+
throw new TypeError(`Invalid percentage value: ${parsed} should be between 0 and 1`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return parsed;
|
|
164
|
+
},
|
|
165
|
+
defaultValue: defaultVal,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
151
169
|
/**
|
|
152
170
|
* Generates parseEnv and default values for a numerical config value.
|
|
153
171
|
* @param defaultVal - The default numerical value to use if the environment variable is not set or is invalid
|
|
@@ -231,10 +249,7 @@ export function secretValueConfigHelper<T>(parse: (val: string | undefined) => T
|
|
|
231
249
|
};
|
|
232
250
|
}
|
|
233
251
|
|
|
234
|
-
|
|
235
|
-
export function parseBooleanEnv(val: string | undefined): boolean {
|
|
236
|
-
return val !== undefined && ['1', 'true', 'TRUE'].includes(val);
|
|
237
|
-
}
|
|
252
|
+
export { parseBooleanEnv } from './parse-env.js';
|
|
238
253
|
|
|
239
254
|
export function secretStringConfigHelper(): Required<
|
|
240
255
|
Pick<ConfigMapping, 'parseEnv' | 'defaultValue' | 'isBoolean'> & {
|
|
@@ -12,11 +12,9 @@ export class RandomnessSingleton {
|
|
|
12
12
|
private static instance: RandomnessSingleton;
|
|
13
13
|
|
|
14
14
|
private counter = 0;
|
|
15
|
+
private readonly log = createLogger('foundation:randomness_singleton');
|
|
15
16
|
|
|
16
|
-
private constructor(
|
|
17
|
-
private readonly seed?: number,
|
|
18
|
-
private readonly log = createLogger('foundation:randomness_singleton'),
|
|
19
|
-
) {
|
|
17
|
+
private constructor(private readonly seed?: number) {
|
|
20
18
|
if (seed !== undefined) {
|
|
21
19
|
this.log.debug(`Using pseudo-randomness with seed: ${seed}`);
|
|
22
20
|
this.counter = seed;
|
package/src/eth-address/index.ts
CHANGED
|
@@ -249,7 +249,7 @@ export class EthAddress {
|
|
|
249
249
|
/** Converts a number into an address. Useful for testing. */
|
|
250
250
|
static fromNumber(num: bigint | number): EthAddress {
|
|
251
251
|
const buffer = Buffer.alloc(EthAddress.SIZE_IN_BYTES);
|
|
252
|
-
buffer.writeBigUInt64BE(BigInt(num),
|
|
252
|
+
buffer.writeBigUInt64BE(BigInt(num), EthAddress.SIZE_IN_BYTES - 8);
|
|
253
253
|
return new EthAddress(buffer);
|
|
254
254
|
}
|
|
255
255
|
|
package/src/log/pino-logger.ts
CHANGED
|
@@ -5,7 +5,8 @@ import type { Writable } from 'stream';
|
|
|
5
5
|
import { inspect } from 'util';
|
|
6
6
|
|
|
7
7
|
import { compactArray } from '../collection/array.js';
|
|
8
|
-
import {
|
|
8
|
+
import type { EnvVar } from '../config/index.js';
|
|
9
|
+
import { parseBooleanEnv } from '../config/parse-env.js';
|
|
9
10
|
import { GoogleCloudLoggerConfig } from './gcloud-logger-config.js';
|
|
10
11
|
import { getLogLevelFromFilters, parseEnv } from './log-filters.js';
|
|
11
12
|
import type { LogLevel } from './log-levels.js';
|
package/src/retry/index.ts
CHANGED
|
@@ -103,3 +103,21 @@ export async function retryUntil<T>(
|
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Convenience wrapper around retryUntil with fast polling for tests.
|
|
109
|
+
* Uses 10s timeout and 100ms polling interval by default.
|
|
110
|
+
*
|
|
111
|
+
* @param fn - The function to retry until it returns a truthy value.
|
|
112
|
+
* @param name - Description of what we're waiting for (for error messages).
|
|
113
|
+
* @param timeout - Optional timeout in seconds. Defaults to 10s.
|
|
114
|
+
* @param interval - Optional interval in seconds. Defaults to 0.1s (100ms).
|
|
115
|
+
*/
|
|
116
|
+
export function retryFastUntil<T>(
|
|
117
|
+
fn: () => (T | undefined) | Promise<T | undefined>,
|
|
118
|
+
name = '',
|
|
119
|
+
timeout = 10,
|
|
120
|
+
interval = 0.1,
|
|
121
|
+
) {
|
|
122
|
+
return retryUntil(fn, name, timeout, interval);
|
|
123
|
+
}
|
|
@@ -224,15 +224,22 @@ export class BufferReader {
|
|
|
224
224
|
* deserializing each one using the 'fromBuffer' method of 'itemDeserializer'.
|
|
225
225
|
*
|
|
226
226
|
* @param itemDeserializer - Object with 'fromBuffer' method to deserialize vector elements.
|
|
227
|
+
* @param maxSize - Optional maximum allowed size for the vector. If the size exceeds this, an error is thrown.
|
|
227
228
|
* @returns An array of deserialized elements of type T.
|
|
228
229
|
*/
|
|
229
|
-
public readVector<T>(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
230
|
+
public readVector<T>(
|
|
231
|
+
itemDeserializer: {
|
|
232
|
+
/**
|
|
233
|
+
* A method to deserialize data from a buffer.
|
|
234
|
+
*/
|
|
235
|
+
fromBuffer: (reader: BufferReader) => T;
|
|
236
|
+
},
|
|
237
|
+
maxSize?: number,
|
|
238
|
+
): T[] {
|
|
235
239
|
const size = this.readNumber();
|
|
240
|
+
if (maxSize !== undefined && size > maxSize) {
|
|
241
|
+
throw new Error(`Vector size ${size} exceeds maximum allowed ${maxSize}`);
|
|
242
|
+
}
|
|
236
243
|
const result = new Array<T>(size);
|
|
237
244
|
for (let i = 0; i < size; i++) {
|
|
238
245
|
result[i] = itemDeserializer.fromBuffer(this);
|
|
@@ -344,10 +351,11 @@ export class BufferReader {
|
|
|
344
351
|
* The method first reads the size of the string, then reads the corresponding
|
|
345
352
|
* number of bytes from the buffer and converts them to a string.
|
|
346
353
|
*
|
|
354
|
+
* @param maxSize - Optional maximum allowed size for the string buffer. If the size exceeds this, an error is thrown.
|
|
347
355
|
* @returns The read string from the buffer.
|
|
348
356
|
*/
|
|
349
|
-
public readString(): string {
|
|
350
|
-
return this.readBuffer().toString();
|
|
357
|
+
public readString(maxSize?: number): string {
|
|
358
|
+
return this.readBuffer(maxSize).toString();
|
|
351
359
|
}
|
|
352
360
|
|
|
353
361
|
/**
|
|
@@ -356,10 +364,14 @@ export class BufferReader {
|
|
|
356
364
|
* a Buffer with that size containing the bytes. Useful for reading variable-length
|
|
357
365
|
* binary data encoded as (size, data) format.
|
|
358
366
|
*
|
|
367
|
+
* @param maxSize - Optional maximum allowed size for the buffer. If the size exceeds this, an error is thrown.
|
|
359
368
|
* @returns A Buffer containing the read bytes.
|
|
360
369
|
*/
|
|
361
|
-
public readBuffer(): Buffer {
|
|
370
|
+
public readBuffer(maxSize?: number): Buffer {
|
|
362
371
|
const size = this.readNumber();
|
|
372
|
+
if (maxSize !== undefined && size > maxSize) {
|
|
373
|
+
throw new Error(`Buffer size ${size} exceeds maximum allowed ${maxSize}`);
|
|
374
|
+
}
|
|
363
375
|
this.#rangeCheck(size);
|
|
364
376
|
return this.readBytes(size);
|
|
365
377
|
}
|
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
3
|
-
import { sha256Trunc } from '@aztec/foundation/crypto/sha256';
|
|
4
|
-
|
|
5
|
-
import type { AsyncHasher, Hasher } from './hasher.js';
|
|
6
|
-
|
|
7
|
-
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
|
|
8
|
-
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
|
|
9
|
-
|
|
10
|
-
export const pedersenMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
11
|
-
(await pedersenHashArray([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
12
|
-
|
|
13
|
-
export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
14
|
-
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
1
|
+
import { pedersenMerkleHash, poseidonMerkleHash, shaMerkleHash } from './hasher.js';
|
|
15
2
|
|
|
16
3
|
export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);
|
|
17
4
|
|
package/src/trees/hasher.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { pedersenHash as pedersenHashArray } from '../crypto/pedersen/index.js';
|
|
2
|
+
import { poseidon2Hash } from '../crypto/poseidon/index.js';
|
|
3
|
+
import { sha256Trunc } from '../crypto/sha256/index.js';
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Defines hasher interface used by Merkle trees.
|
|
3
7
|
*/
|
|
@@ -37,3 +41,12 @@ export interface AsyncHasher {
|
|
|
37
41
|
*/
|
|
38
42
|
hashInputs(inputs: Buffer[]): Promise<Buffer<ArrayBuffer>>;
|
|
39
43
|
}
|
|
44
|
+
|
|
45
|
+
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
|
|
46
|
+
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
|
|
47
|
+
|
|
48
|
+
export const pedersenMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
49
|
+
(await pedersenHashArray([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
50
|
+
|
|
51
|
+
export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
52
|
+
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
package/src/trees/index.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './unbalanced_merkle_tree.js';
|
|
3
|
-
export * from './unbalanced_tree_store.js';
|
|
1
|
+
export * from './balanced_merkle_tree_root.js';
|
|
4
2
|
export * from './merkle_tree_calculator.js';
|
|
5
3
|
export * from './merkle_tree.js';
|
|
6
4
|
export * from './indexed_merkle_tree_calculator.js';
|
|
@@ -10,3 +8,5 @@ export * from './membership_witness.js';
|
|
|
10
8
|
export * from './hasher.js';
|
|
11
9
|
export * from './indexed_tree_leaf.js';
|
|
12
10
|
export * from './unbalanced_merkle_tree_calculator.js';
|
|
11
|
+
export * from './unbalanced_merkle_tree_root.js';
|
|
12
|
+
export * from './unbalanced_tree_store.js';
|
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
import { shaMerkleHash } from './
|
|
2
|
-
import type { Hasher } from './hasher.js';
|
|
1
|
+
import { type Hasher, shaMerkleHash } from './hasher.js';
|
|
3
2
|
import { SiblingPath } from './sibling_path.js';
|
|
4
3
|
import { type TreeNodeLocation, UnbalancedTreeStore } from './unbalanced_tree_store.js';
|
|
5
4
|
|
|
6
|
-
export function computeCompressedUnbalancedMerkleTreeRoot(
|
|
7
|
-
leaves: Buffer[],
|
|
8
|
-
valueToCompress = Buffer.alloc(32),
|
|
9
|
-
emptyRoot = Buffer.alloc(32),
|
|
10
|
-
hasher?: Hasher['hash'],
|
|
11
|
-
): Buffer {
|
|
12
|
-
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, emptyRoot, hasher);
|
|
13
|
-
return calculator.getRoot();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
5
|
interface TreeNode {
|
|
17
6
|
value: Buffer;
|
|
18
7
|
leafIndex?: number;
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
poseidonMerkleHash,
|
|
5
|
-
shaMerkleHash,
|
|
6
|
-
} from './balanced_merkle_tree.js';
|
|
1
|
+
import { computeBalancedMerkleTreeRoot, computeBalancedMerkleTreeRootAsync } from './balanced_merkle_tree_root.js';
|
|
2
|
+
import { poseidonMerkleHash, shaMerkleHash } from './hasher.js';
|
|
3
|
+
import { UnbalancedMerkleTreeCalculator } from './unbalanced_merkle_tree_calculator.js';
|
|
7
4
|
|
|
8
5
|
export const computeUnbalancedShaRoot = (leaves: Buffer[]) => computeUnbalancedMerkleTreeRoot(leaves, shaMerkleHash);
|
|
9
6
|
|
|
10
7
|
export const computeUnbalancedPoseidonRoot = async (leaves: Buffer[]) =>
|
|
11
8
|
await computeUnbalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
|
12
9
|
|
|
10
|
+
export const computeWonkyShaRoot = (leaves: Buffer[]) => computeWonkyMerkleTreeRoot(leaves);
|
|
11
|
+
|
|
13
12
|
/**
|
|
14
13
|
* Computes the Merkle root of an unbalanced tree.
|
|
15
14
|
*
|
|
@@ -101,59 +100,16 @@ export async function computeUnbalancedMerkleTreeRootAsync(
|
|
|
101
100
|
return root!;
|
|
102
101
|
}
|
|
103
102
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function findPosition(
|
|
119
|
-
rootLevel: number,
|
|
120
|
-
leafLevel: number,
|
|
121
|
-
numLeaves: number,
|
|
122
|
-
indexOffset: number,
|
|
123
|
-
targetIndex: number,
|
|
124
|
-
): { level: number; indexAtLevel: number } {
|
|
125
|
-
if (numLeaves <= 1) {
|
|
126
|
-
// Single leaf.
|
|
127
|
-
return { level: rootLevel, indexAtLevel: indexOffset };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// The largest balanced tree that can be created with the given number of leaves.
|
|
131
|
-
const maxBalancedTreeDepth = getMaxBalancedSubtreeDepth(numLeaves);
|
|
132
|
-
const numBalancedLeaves = 2 ** maxBalancedTreeDepth;
|
|
133
|
-
const numRemainingLeaves = numLeaves - numBalancedLeaves;
|
|
134
|
-
|
|
135
|
-
if (targetIndex < numBalancedLeaves) {
|
|
136
|
-
// Target is in the balanced tree.
|
|
137
|
-
|
|
138
|
-
// - If numRemainingLeaves is 0: this balanced tree is grown from the current root.
|
|
139
|
-
// - If numRemainingLeaves is not 0: the remaining leaves will form another tree, which will become the right child of the root.
|
|
140
|
-
// And the balanced tree will be the left child of the root.
|
|
141
|
-
// There will be an extra level between the root of the balanced tree and the current root.
|
|
142
|
-
const extraLevel = numRemainingLeaves ? 1 : 0;
|
|
143
|
-
|
|
144
|
-
return { level: rootLevel + maxBalancedTreeDepth + extraLevel, indexAtLevel: indexOffset + targetIndex };
|
|
145
|
-
} else {
|
|
146
|
-
// Target is in the right branch.
|
|
147
|
-
const rightBranchMaxLevel = getMaxUnbalancedTreeDepth(numRemainingLeaves);
|
|
148
|
-
const shiftedUp = leafLevel - rootLevel - rightBranchMaxLevel - 1;
|
|
149
|
-
const nextLeafLevel = leafLevel - shiftedUp;
|
|
150
|
-
const newIndexOffset = (indexOffset + numBalancedLeaves) >> shiftedUp;
|
|
151
|
-
const shiftedTargetIndex = targetIndex - numBalancedLeaves;
|
|
152
|
-
return findPosition(rootLevel + 1, nextLeafLevel, numRemainingLeaves, newIndexOffset, shiftedTargetIndex);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function findLeafLevelAndIndex(numLeaves: number, leafIndex: number) {
|
|
157
|
-
const maxLevel = getMaxUnbalancedTreeDepth(numLeaves);
|
|
158
|
-
return findPosition(0, maxLevel, numLeaves, 0, leafIndex);
|
|
103
|
+
// A **wonky** tree is a "compressed" unbalanced Merkle tree.
|
|
104
|
+
// It is constructed in the same way as an unbalanced tree: by first creating the largest possible left subtree, with
|
|
105
|
+
// the remaining leaves forming a right subtree that follows the same process recursively.
|
|
106
|
+
// During construction, leaves equal to `valueToCompress` are skipped (compressed) and do not contribute to the tree.
|
|
107
|
+
export function computeWonkyMerkleTreeRoot(
|
|
108
|
+
leaves: Buffer[],
|
|
109
|
+
valueToCompress = Buffer.alloc(32),
|
|
110
|
+
emptyRoot = Buffer.alloc(32),
|
|
111
|
+
hasher = shaMerkleHash,
|
|
112
|
+
): Buffer {
|
|
113
|
+
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, emptyRoot, hasher);
|
|
114
|
+
return calculator.getRoot();
|
|
159
115
|
}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { findLeafLevelAndIndex } from './unbalanced_merkle_tree.js';
|
|
2
|
-
|
|
3
1
|
export interface TreeNodeLocation {
|
|
4
2
|
level: number;
|
|
5
3
|
index: number;
|
|
@@ -104,3 +102,60 @@ export class UnbalancedTreeStore<T> {
|
|
|
104
102
|
return `${location.level}-${location.index}`;
|
|
105
103
|
}
|
|
106
104
|
}
|
|
105
|
+
|
|
106
|
+
/// Get the depth of the maximum balanced tree that can be created with the given number of leaves. The subtree will be
|
|
107
|
+
/// the left most subtree of the unbalanced tree with a total of `numLeaves` leaves.
|
|
108
|
+
///
|
|
109
|
+
/// Note: All the leaves may not be used to form the tree. For example, if there are 5 leaves, the maximum depth is 2,
|
|
110
|
+
/// only 4 leaves are used to form a balanced tree.
|
|
111
|
+
function getMaxBalancedSubtreeDepth(numLeaves: number) {
|
|
112
|
+
return Math.floor(Math.log2(numLeaves));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// Get the maximum depth of an unbalanced tree that can be created with the given number of leaves.
|
|
116
|
+
function getMaxUnbalancedTreeDepth(numLeaves: number) {
|
|
117
|
+
return Math.ceil(Math.log2(numLeaves));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function findPosition(
|
|
121
|
+
rootLevel: number,
|
|
122
|
+
leafLevel: number,
|
|
123
|
+
numLeaves: number,
|
|
124
|
+
indexOffset: number,
|
|
125
|
+
targetIndex: number,
|
|
126
|
+
): { level: number; indexAtLevel: number } {
|
|
127
|
+
if (numLeaves <= 1) {
|
|
128
|
+
// Single leaf.
|
|
129
|
+
return { level: rootLevel, indexAtLevel: indexOffset };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// The largest balanced tree that can be created with the given number of leaves.
|
|
133
|
+
const maxBalancedTreeDepth = getMaxBalancedSubtreeDepth(numLeaves);
|
|
134
|
+
const numBalancedLeaves = 2 ** maxBalancedTreeDepth;
|
|
135
|
+
const numRemainingLeaves = numLeaves - numBalancedLeaves;
|
|
136
|
+
|
|
137
|
+
if (targetIndex < numBalancedLeaves) {
|
|
138
|
+
// Target is in the balanced tree.
|
|
139
|
+
|
|
140
|
+
// - If numRemainingLeaves is 0: this balanced tree is grown from the current root.
|
|
141
|
+
// - If numRemainingLeaves is not 0: the remaining leaves will form another tree, which will become the right child of the root.
|
|
142
|
+
// And the balanced tree will be the left child of the root.
|
|
143
|
+
// There will be an extra level between the root of the balanced tree and the current root.
|
|
144
|
+
const extraLevel = numRemainingLeaves ? 1 : 0;
|
|
145
|
+
|
|
146
|
+
return { level: rootLevel + maxBalancedTreeDepth + extraLevel, indexAtLevel: indexOffset + targetIndex };
|
|
147
|
+
} else {
|
|
148
|
+
// Target is in the right branch.
|
|
149
|
+
const rightBranchMaxLevel = getMaxUnbalancedTreeDepth(numRemainingLeaves);
|
|
150
|
+
const shiftedUp = leafLevel - rootLevel - rightBranchMaxLevel - 1;
|
|
151
|
+
const nextLeafLevel = leafLevel - shiftedUp;
|
|
152
|
+
const newIndexOffset = (indexOffset + numBalancedLeaves) >> shiftedUp;
|
|
153
|
+
const shiftedTargetIndex = targetIndex - numBalancedLeaves;
|
|
154
|
+
return findPosition(rootLevel + 1, nextLeafLevel, numRemainingLeaves, newIndexOffset, shiftedTargetIndex);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function findLeafLevelAndIndex(numLeaves: number, leafIndex: number) {
|
|
159
|
+
const maxLevel = getMaxUnbalancedTreeDepth(numLeaves);
|
|
160
|
+
return findPosition(0, maxLevel, numLeaves, 0, leafIndex);
|
|
161
|
+
}
|
package/src/types/index.ts
CHANGED
|
@@ -33,7 +33,7 @@ export type Prettify<T> = {
|
|
|
33
33
|
* Type-safe Event Emitter type
|
|
34
34
|
* @example
|
|
35
35
|
* export type ArchiverEmitter = TypedEventEmitter<{
|
|
36
|
-
* [L2BlockSourceEvents.
|
|
36
|
+
* [L2BlockSourceEvents.L2PruneUnproven]: (args: L2BlockSourceEvent) => void;
|
|
37
37
|
* [L2BlockSourceEvents.L2BlockProven]: (args: L2BlockSourceEvent) => void;
|
|
38
38
|
* }>;
|
|
39
39
|
* class Archiver extends (EventEmitter as new () => ArchiverEmitter) {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../../src/json-rpc/server/telemetry.ts"],"names":[],"mappings":""}
|
|
File without changes
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"balanced_merkle_tree.d.ts","sourceRoot":"","sources":["../../src/trees/balanced_merkle_tree.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAEvD,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,CACyB,CAAC;AAEnE,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CACyB,CAAC;AAE7E,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CACqB,CAAC;AAEzE,eAAO,MAAM,sBAAsB,gEAA8D,CAAC;AAElG,eAAO,MAAM,2BAA2B,yEAC8B,CAAC;AAEvE,eAAO,MAAM,2BAA2B,yEAC8B,CAAC;AAEvE;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,8FAAgB,GAAG,MAAM,CAe9F;AAED;;;;;GAKG;AACH,wBAAsB,kCAAkC,CACtD,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,uGAAqB,GAC1B,OAAO,CAAC,MAAM,CAAC,CAejB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unbalanced_merkle_tree.d.ts","sourceRoot":"","sources":["../../src/trees/unbalanced_merkle_tree.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,wBAAwB,gEAA+E,CAAC;AAErH,eAAO,MAAM,6BAA6B,yEAC8B,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,8FAAgB,EACtB,SAAS,sBAAmB,GAC3B,MAAM,CA6BR;AAED,wBAAsB,oCAAoC,CACxD,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,uGAAqB,EAC3B,SAAS,sBAAmB,GAC3B,OAAO,CAAC,MAAM,CAAC,CA6BjB;AAsDD,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;;;EAGzE"}
|
|
File without changes
|