Skip to content

Commit

Permalink
fix: delete temp lmdb stores + close db connection (#8778)
Browse files Browse the repository at this point in the history
  • Loading branch information
spypsy authored and Rumata888 committed Sep 27, 2024
1 parent 0025164 commit 05d99cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
17 changes: 9 additions & 8 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { AztecAddress } from '@aztec/foundation/aztec-address';
import { padArrayEnd } from '@aztec/foundation/collection';
import { createDebugLogger } from '@aztec/foundation/log';
import { Timer } from '@aztec/foundation/timer';
import { type AztecKVStore } from '@aztec/kv-store';
import { openTmpStore } from '@aztec/kv-store/utils';
import { SHA256Trunc, StandardTree, UnbalancedTree } from '@aztec/merkle-tree';
import {
Expand Down Expand Up @@ -508,18 +509,15 @@ export class AztecNodeService implements AztecNode {
throw new Error('The L2ToL1Message you are trying to prove inclusion of does not exist');
}

const tempStores: AztecKVStore[] = [];

// Construct message subtrees
const l2toL1Subtrees = await Promise.all(
l2ToL1Messages.map(async (msgs, i) => {
const store = openTmpStore(true);
tempStores.push(store);
const treeHeight = msgs.length <= 1 ? 1 : Math.ceil(Math.log2(msgs.length));
const tree = new StandardTree(
openTmpStore(true),
new SHA256Trunc(),
`temp_msgs_subtrees_${i}`,
treeHeight,
0n,
Fr,
);
const tree = new StandardTree(store, new SHA256Trunc(), `temp_msgs_subtrees_${i}`, treeHeight, 0n, Fr);
await tree.appendLeaves(msgs);
return tree;
}),
Expand Down Expand Up @@ -551,6 +549,9 @@ export class AztecNodeService implements AztecNode {
2,
);

// clear the tmp stores
await Promise.all(tempStores.map(store => store.delete()));

return [BigInt(mergedIndex), new SiblingPath(mergedPath.length, mergedPath)];
}

Expand Down
9 changes: 9 additions & 0 deletions yarn-project/kv-store/src/lmdb/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,18 @@ export class AztecLmdbStore implements AztecKVStore {
await this.#rootDb.clearAsync();
}

/**
* Close the database. Note, once this is closed we can no longer interact with the DB.
* Closing the root DB also closes child DBs.
*/
async close() {
await this.#rootDb.close();
}

/** Deletes this store and removes the database files from disk */
async delete() {
await this.#rootDb.drop();
await this.close();
if (this.path) {
await rm(this.path, { recursive: true, force: true });
this.#log.verbose(`Deleted database files at ${this.path}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,13 @@ export class ServerWorldStateSynchronizer implements WorldStateSynchronizer {
* @throws If the L1 to L2 messages do not hash to the block inHash.
*/
async #verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Buffer) {
const tree = new StandardTree(
openTmpStore(true),
new SHA256Trunc(),
'temp_in_hash_check',
L1_TO_L2_MSG_SUBTREE_HEIGHT,
0n,
Fr,
);
const store = openTmpStore(true);
const tree = new StandardTree(store, new SHA256Trunc(), 'temp_in_hash_check', L1_TO_L2_MSG_SUBTREE_HEIGHT, 0n, Fr);
await tree.appendLeaves(l1ToL2Messages);

if (!tree.getRoot(true).equals(inHash)) {
throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
}
await store.delete();
}
}

0 comments on commit 05d99cd

Please sign in to comment.