Skip to content

Commit

Permalink
refactor: replace leveldb with lmdb for merkle trees (AztecProtocol#4119
Browse files Browse the repository at this point in the history
)

Replace LevelDB with LMDB (based on @aztec/kv-store) in the Merkle tree
implementations. This PR is stacked on top of AztecProtocol#3966

Close AztecProtocol#3362
  • Loading branch information
alexghr authored Jan 26, 2024
1 parent b4dc31d commit 84967b2
Show file tree
Hide file tree
Showing 69 changed files with 699 additions and 910 deletions.
1 change: 1 addition & 0 deletions yarn-project/acir-simulator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"tslib": "^2.4.0"
},
"devDependencies": {
"@aztec/kv-store": "workspace:^",
"@aztec/merkle-tree": "workspace:^",
"@aztec/noir-contracts": "workspace:^",
"@jest/globals": "^29.5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr, GrumpkinScalar } from '@aztec/foundation/fields';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { FieldsOf } from '@aztec/foundation/types';
import { AztecLmdbStore } from '@aztec/kv-store';
import { AppendOnlyTree, Pedersen, StandardTree, newTree } from '@aztec/merkle-tree';
import {
ChildContractArtifact,
Expand All @@ -50,8 +51,6 @@ import {

import { jest } from '@jest/globals';
import { MockProxy, mock } from 'jest-mock-extended';
import { default as levelup } from 'levelup';
import { type MemDown, default as memdown } from 'memdown';
import { getFunctionSelector } from 'viem';

import { KeyPair } from '../acvm/index.js';
Expand All @@ -62,8 +61,6 @@ import { AcirSimulator } from './simulator.js';

jest.setTimeout(60_000);

const createMemDown = () => (memdown as any)() as MemDown<any, any>;

describe('Private Execution test suite', () => {
let oracle: MockProxy<DBOracle>;
let acirSimulator: AcirSimulator;
Expand Down Expand Up @@ -136,7 +133,7 @@ describe('Private Execution test suite', () => {
throw new Error(`Unknown tree ${name}`);
}
if (!trees[name]) {
const db = levelup(createMemDown());
const db = await AztecLmdbStore.openTmp();
const pedersen = new Pedersen();
trees[name] = await newTree(StandardTree, db, pedersen, name, treeHeights[name]);
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/acir-simulator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
{
"path": "../foundation"
},
{
"path": "../kv-store"
},
{
"path": "../merkle-tree"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export class BlockStore {
#log = createDebugLogger('aztec:archiver:block_store');

constructor(private db: AztecKVStore) {
this.#blocks = db.createMap('archiver_blocks');
this.#blocks = db.openMap('archiver_blocks');

this.#txIndex = db.createMap('archiver_tx_index');
this.#contractIndex = db.createMap('archiver_contract_index');
this.#txIndex = db.openMap('archiver_tx_index');
this.#contractIndex = db.openMap('archiver_contract_index');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ContractClassStore {
#contractClasses: AztecMap<string, Buffer>;

constructor(db: AztecKVStore) {
this.#contractClasses = db.createMap('archiver_contract_classes');
this.#contractClasses = db.openMap('archiver_contract_classes');
}

addContractClass(contractClass: ContractClassWithId): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ContractInstanceStore {
#contractInstances: AztecMap<string, Buffer>;

constructor(db: AztecKVStore) {
this.#contractInstances = db.createMap('archiver_contract_instances');
this.#contractInstances = db.openMap('archiver_contract_instances');
}

addContractInstance(contractInstance: ContractInstanceWithAddress): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ContractStore {
#log = createDebugLogger('aztec:archiver:contract_store');

constructor(private db: AztecKVStore, blockStore: BlockStore) {
this.#extendedContractData = db.createMap('archiver_extended_contract_data');
this.#extendedContractData = db.openMap('archiver_extended_contract_data');
this.#blockStore = blockStore;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EthAddress } from '@aztec/circuits.js';
import { AztecLmdbStore } from '@aztec/kv-store';

import { describeArchiverDataStore } from '../archiver_store_test_suite.js';
Expand All @@ -8,7 +7,7 @@ describe('KVArchiverDataStore', () => {
let archiverStore: KVArchiverDataStore;

beforeEach(async () => {
archiverStore = new KVArchiverDataStore(await AztecLmdbStore.create(EthAddress.random()));
archiverStore = new KVArchiverDataStore(await AztecLmdbStore.openTmp());
});

describeArchiverDataStore('ArchiverStore', () => archiverStore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export class LogStore {
#log = createDebugLogger('aztec:archiver:log_store');

constructor(private db: AztecKVStore, private blockStore: BlockStore, logsMaxPageSize: number = 1000) {
this.#encryptedLogs = db.createMap('archiver_encrypted_logs');
this.#unencryptedLogs = db.createMap('archiver_unencrypted_logs');
this.#encryptedLogs = db.openMap('archiver_encrypted_logs');
this.#unencryptedLogs = db.openMap('archiver_unencrypted_logs');

this.#logsMaxPageSize = logsMaxPageSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export class MessageStore {
#log = createDebugLogger('aztec:archiver:message_store');

constructor(private db: AztecKVStore) {
this.#messages = db.createMap('archiver_l1_to_l2_messages');
this.#pendingMessagesByFee = db.createCounter('archiver_messages_by_fee');
this.#lastL1BlockAddingMessages = db.createSingleton('archiver_last_l1_block_adding_messages');
this.#lastL1BlockCancellingMessages = db.createSingleton('archiver_last_l1_block_cancelling_messages');
this.#messages = db.openMap('archiver_l1_to_l2_messages');
this.#pendingMessagesByFee = db.openCounter('archiver_messages_by_fee');
this.#lastL1BlockAddingMessages = db.openSingleton('archiver_last_l1_block_adding_messages');
this.#lastL1BlockCancellingMessages = db.openSingleton('archiver_last_l1_block_cancelling_messages');
}

/**
Expand Down
6 changes: 0 additions & 6 deletions yarn-project/aztec-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,11 @@
"@aztec/world-state": "workspace:^",
"koa": "^2.14.2",
"koa-router": "^12.0.0",
"levelup": "^5.1.1",
"lmdb": "^2.9.1",
"memdown": "^6.1.1",
"tslib": "^2.4.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@types/jest": "^29.5.0",
"@types/leveldown": "^4.0.4",
"@types/levelup": "^5.1.2",
"@types/memdown": "^3.0.0",
"@types/node": "^18.7.23",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
Expand Down
92 changes: 0 additions & 92 deletions yarn-project/aztec-node/src/aztec-node/db.ts

This file was deleted.

23 changes: 6 additions & 17 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { computeGlobalsHash, computePublicDataTreeLeafSlot } from '@aztec/circui
import { L1ContractAddresses, createEthereumChain } from '@aztec/ethereum';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { createDebugLogger } from '@aztec/foundation/log';
import { AztecLmdbStore } from '@aztec/kv-store';
import { AztecKVStore, AztecLmdbStore } from '@aztec/kv-store';
import { AztecKVTxPool, P2P, createP2PClient } from '@aztec/p2p';
import {
GlobalVariableBuilder,
Expand All @@ -56,10 +56,7 @@ import {
getConfigEnvVars as getWorldStateConfig,
} from '@aztec/world-state';

import { LevelUp } from 'levelup';

import { AztecNodeConfig } from './config.js';
import { openDb } from './db.js';

/**
* The aztec node.
Expand All @@ -78,7 +75,7 @@ export class AztecNodeService implements AztecNode {
protected readonly chainId: number,
protected readonly version: number,
protected readonly globalVariableBuilder: GlobalVariableBuilder,
protected readonly merkleTreesDb: LevelUp,
protected readonly merkleTreesDb: AztecKVStore,
private log = createDebugLogger('aztec:node'),
) {
const message =
Expand Down Expand Up @@ -106,8 +103,7 @@ export class AztecNodeService implements AztecNode {
}

const log = createDebugLogger('aztec:node');
const store = await AztecLmdbStore.create(config.l1Contracts.rollupAddress, config.dataDirectory);
const [_, worldStateDb] = await openDb(config, log);
const store = await AztecLmdbStore.open(config.l1Contracts.rollupAddress, config.dataDirectory);

// first create and sync the archiver
const archiverStore = new KVArchiverDataStore(store, config.maxLogs);
Expand All @@ -121,14 +117,9 @@ export class AztecNodeService implements AztecNode {
const p2pClient = await createP2PClient(store, config, new AztecKVTxPool(store), archiver);

// now create the merkle trees and the world state synchronizer
const merkleTrees = await MerkleTrees.new(worldStateDb);
const merkleTrees = await MerkleTrees.new(store);
const worldStateConfig: WorldStateConfig = getWorldStateConfig();
const worldStateSynchronizer = await ServerWorldStateSynchronizer.new(
worldStateDb,
merkleTrees,
archiver,
worldStateConfig,
);
const worldStateSynchronizer = new ServerWorldStateSynchronizer(store, merkleTrees, archiver, worldStateConfig);

// start both and wait for them to sync from the block source
await Promise.all([p2pClient.start(), worldStateSynchronizer.start()]);
Expand All @@ -151,7 +142,7 @@ export class AztecNodeService implements AztecNode {
ethereumChain.chainInfo.id,
config.version,
getGlobalVariableBuilder(config),
worldStateDb,
store,
log,
);
}
Expand Down Expand Up @@ -285,8 +276,6 @@ export class AztecNodeService implements AztecNode {
await this.p2pClient.stop();
await this.worldStateSynchronizer.stop();
await this.blockSource.stop();
this.log('Closing Merkle Trees');
await this.merkleTreesDb.close();
this.log.info(`Stopped`);
}

Expand Down
16 changes: 0 additions & 16 deletions yarn-project/aztec-node/src/declaration.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ import {
computeAuthWitMessageHash,
computeMessageSecretHash,
} from '@aztec/aztec.js';
import { AztecLmdbStore } from '@aztec/kv-store';
import { Pedersen, SparseTree, newTree } from '@aztec/merkle-tree';
import { SlowTreeContract, TokenBlacklistContract, TokenContract } from '@aztec/noir-contracts';

import { jest } from '@jest/globals';
import levelup from 'levelup';
import { type MemDown, default as memdown } from 'memdown';

import { setup } from './fixtures/utils.js';
import { TokenSimulator } from './simulators/token_simulator.js';

export const createMemDown = () => (memdown as any)() as MemDown<any, any>;

const TIMEOUT = 90_000;

describe('e2e_blacklist_token_contract', () => {
Expand All @@ -48,7 +45,7 @@ describe('e2e_blacklist_token_contract', () => {
const getMembershipProof = async (index: bigint, includeUncommitted: boolean) => {
return {
index,
value: Fr.fromBuffer((await slowUpdateTreeSimulator.getLeafValue(index, includeUncommitted))!),
value: Fr.fromBuffer(slowUpdateTreeSimulator.getLeafValue(index, includeUncommitted)!),
// eslint-disable-next-line camelcase
sibling_path: (await slowUpdateTreeSimulator.getSiblingPath(index, includeUncommitted)).toFieldArray(),
};
Expand Down Expand Up @@ -107,7 +104,7 @@ describe('e2e_blacklist_token_contract', () => {
slowTree = await SlowTreeContract.deploy(wallets[0]).send().deployed();

const depth = 254;
slowUpdateTreeSimulator = await newTree(SparseTree, levelup(createMemDown()), new Pedersen(), 'test', depth);
slowUpdateTreeSimulator = await newTree(SparseTree, await AztecLmdbStore.openTmp(), new Pedersen(), 'test', depth);

const deployTx = TokenBlacklistContract.deploy(wallets[0], accounts[0], slowTree.address).send({});
const receipt = await deployTx.wait();
Expand Down
Loading

0 comments on commit 84967b2

Please sign in to comment.