Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lighter primitives.wasm for merkle trees by default #238

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion yarn-project/aztec-node/src/aztec-node/aztec-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MerkleTreeId, MerkleTrees, ServerWorldStateSynchroniser, WorldStateSync
import { default as levelup } from 'levelup';
import { default as memdown } from 'memdown';
import { AztecNodeConfig } from './config.js';
import { CircuitsWasm } from '@aztec/circuits.js';

/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
Expand Down Expand Up @@ -41,7 +42,7 @@ export class AztecNode {
const p2pClient = new P2PClient(archiver);

// now create the merkle trees and the world state syncher
const merkleTreeDB = await MerkleTrees.new(levelup(createMemDown()));
const merkleTreeDB = await MerkleTrees.new(levelup(createMemDown()), await CircuitsWasm.get());
const worldStateSynchroniser = new ServerWorldStateSynchroniser(merkleTreeDB, archiver);

// start both and wait for them to sync from the block source
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/merkle-tree/src/pedersen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
pedersenGetHash,
pedersenGetHashTree,
} from '@aztec/barretenberg.js/crypto';
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm';

import { WasmWrapper } from '@aztec/foundation/wasm';
import { Hasher } from './hasher.js';

export class Pedersen implements Hasher {
constructor(private wasm: BarretenbergWasm) {}
constructor(private wasm: WasmWrapper) {}

public compress(lhs: Uint8Array, rhs: Uint8Array): Buffer {
return pedersenCompress(this.wasm, lhs, rhs);
Expand Down
7 changes: 4 additions & 3 deletions yarn-project/merkle-tree/src/test/test_suite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm';
import { PrimitivesWasm } from '@aztec/barretenberg.js/wasm';
import { WasmWrapper } from '@aztec/foundation/wasm';
import { default as levelup } from 'levelup';
import { default as memdown } from 'memdown';
import { Hasher, MerkleTree, Pedersen, SiblingPath } from '../index.js';
Expand Down Expand Up @@ -26,7 +27,7 @@ export const merkleTreeTestSuite = (
) => {
describe(testName, () => {
const values: Buffer[] = [];
let wasm: BarretenbergWasm;
let wasm: WasmWrapper;
let pedersen: Pedersen;

beforeAll(() => {
Expand All @@ -38,7 +39,7 @@ export const merkleTreeTestSuite = (
});

beforeEach(async () => {
wasm = await BarretenbergWasm.get();
wasm = await PrimitivesWasm.get();
pedersen = new Pedersen(wasm);
});

Expand Down
11 changes: 6 additions & 5 deletions yarn-project/world-state/src/world-state-db/merkle_trees.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { PrimitivesWasm } from '@aztec/barretenberg.js/wasm';
import {
CONTRACT_TREE_HEIGHT,
CONTRACT_TREE_ROOTS_TREE_HEIGHT,
NULLIFIER_TREE_HEIGHT,
PRIVATE_DATA_TREE_HEIGHT,
PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT,
} from '@aztec/circuits.js';
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm';
import { SerialQueue } from '@aztec/foundation';
import { WasmWrapper } from '@aztec/foundation/wasm';
import { IndexedTree, LeafData, MerkleTree, Pedersen, SiblingPath, StandardMerkleTree } from '@aztec/merkle-tree';
import { default as levelup } from 'levelup';
import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js';
Expand All @@ -31,8 +32,8 @@ export class MerkleTrees implements MerkleTreeDb {
/**
* Initialises the collection of Merkle Trees.
*/
public async init() {
const wasm = await BarretenbergWasm.get();
public async init(optionalWasm?: WasmWrapper) {
const wasm = optionalWasm ?? (await PrimitivesWasm.get());
const hasher = new Pedersen(wasm);
const contractTree = await StandardMerkleTree.new(
this.db,
Expand Down Expand Up @@ -74,9 +75,9 @@ export class MerkleTrees implements MerkleTreeDb {
* @param db - The db instance to use for data persistance.
* @returns - A fully initialised MerkleTrees instance.
*/
public static async new(db: levelup.LevelUp) {
public static async new(db: levelup.LevelUp, wasm?: WasmWrapper) {
const merkleTrees = new MerkleTrees(db);
await merkleTrees.init();
await merkleTrees.init(wasm);
return merkleTrees;
}

Expand Down