From b047110fc950518e9b8a824a351aa6d33059078e Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Wed, 14 Aug 2024 10:41:37 -0300 Subject: [PATCH] fix: Use data dir for lmdb forks In case tmpdir() is not available, rely on the data dir for creating forks of the store. --- yarn-project/kv-store/src/lmdb/store.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/yarn-project/kv-store/src/lmdb/store.ts b/yarn-project/kv-store/src/lmdb/store.ts index cb6ee87d7d7..dfeca64fd00 100644 --- a/yarn-project/kv-store/src/lmdb/store.ts +++ b/yarn-project/kv-store/src/lmdb/store.ts @@ -3,7 +3,7 @@ import { createDebugLogger } from '@aztec/foundation/log'; import { mkdtemp } from 'fs/promises'; import { type Database, type Key, type RootDatabase, open } from 'lmdb'; import { tmpdir } from 'os'; -import { join } from 'path'; +import { dirname, join } from 'path'; import { type AztecArray } from '../interfaces/array.js'; import { type AztecCounter } from '../interfaces/counter.js'; @@ -25,7 +25,7 @@ export class AztecLmdbStore implements AztecKVStore { #data: Database; #multiMapData: Database; - constructor(rootDb: RootDatabase, public readonly isEphemeral: boolean) { + constructor(rootDb: RootDatabase, public readonly isEphemeral: boolean, private path?: string) { this.#rootDb = rootDb; // big bucket to store all the data @@ -61,7 +61,7 @@ export class AztecLmdbStore implements AztecKVStore { ): AztecLmdbStore { log.info(`Opening LMDB database at ${path || 'temporary location'}`); const rootDb = open({ path, noSync: ephemeral }); - return new AztecLmdbStore(rootDb, ephemeral); + return new AztecLmdbStore(rootDb, ephemeral, path); } /** @@ -69,10 +69,11 @@ export class AztecLmdbStore implements AztecKVStore { * @returns A new AztecLmdbStore. */ async fork() { - const forkPath = join(await mkdtemp(join(tmpdir(), 'aztec-store-fork-')), 'root.mdb'); + const baseDir = this.path ? dirname(this.path) : tmpdir(); + const forkPath = join(await mkdtemp(join(baseDir, 'aztec-store-fork-')), 'root.mdb'); await this.#rootDb.backup(forkPath, false); const forkDb = open(forkPath, { noSync: this.isEphemeral }); - return new AztecLmdbStore(forkDb, this.isEphemeral); + return new AztecLmdbStore(forkDb, this.isEphemeral, forkPath); } /**