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

fix: create data dir on node boot #2864

Merged
merged 1 commit into from
Oct 16, 2023
Merged
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
16 changes: 14 additions & 2 deletions yarn-project/aztec-node/src/aztec-node/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LevelDown, default as leveldown } from 'leveldown';
import { LevelUp, default as levelup } from 'levelup';
import { MemDown, default as memdown } from 'memdown';
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';

import { AztecNodeConfig } from './config.js';
Expand All @@ -22,16 +23,27 @@ type NodeMetadata = {
};

/**
* Opens the database for the aztec node.
* Opens the database for the aztec node. If a data directory is specified, then this attempts to create it.
* @param config - The configuration to be used by the aztec node.
* @throws If `config.dataDirectory` is set and the directory cannot be created.
* @returns The database for the aztec node.
*/
export async function openDb(config: AztecNodeConfig): Promise<LevelUp> {
const nodeMetadata: NodeMetadata = {
rollupContractAddress: config.l1Contracts.rollupAddress.toString(),
};

const db = levelup(config.dataDirectory ? createLevelDown(join(config.dataDirectory, DB_SUBDIR)) : createMemDown());
let db: LevelUp;

if (config.dataDirectory) {
const dbDir = join(config.dataDirectory, DB_SUBDIR);
// this throws if we don't have permissions to create the directory
await mkdir(dbDir, { recursive: true });
db = levelup(createLevelDown(dbDir));
} else {
db = levelup(createMemDown());
}

const prevNodeMetadata = await getNodeMetadata(db);

// if the rollup addresses are different, wipe the local database and start over
Expand Down