Skip to content

Commit

Permalink
fix: Delete forks after proving job has finished (#7972)
Browse files Browse the repository at this point in the history
Adds a delete method to the db, which is called in a cleanup handler of
the proving job once it finishes. This is needed so we don't pile up
forks of the db as we keep proving blocks.
  • Loading branch information
spalladino authored Aug 14, 2024
1 parent 7cc47a6 commit 2b4a842
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ export interface MerkleTreeOperations {
* Rolls back pending changes.
*/
rollback(): Promise<void>;

/** Deletes this database. Useful for cleaning up forks. */
delete(): Promise<void>;
}

/** Return type for handleL2BlockAndMessages */
Expand Down
7 changes: 6 additions & 1 deletion yarn-project/kv-store/src/interfaces/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ export interface AztecKVStore {
transaction<T extends Exclude<any, Promise<any>>>(callback: () => T): Promise<T>;

/**
* Clears the store
* Clears all entries in the store
*/
clear(): Promise<void>;

/**
* Forks the store.
*/
fork(): Promise<AztecKVStore>;

/**
* Deletes the store
*/
delete(): Promise<void>;
}
3 changes: 3 additions & 0 deletions yarn-project/kv-store/src/lmdb/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe('AztecLmdbStore', () => {
expect(forkedSingleton.get()).toEqual('foo');
await forkedSingleton.set('bar');
expect(singleton.get()).toEqual('foo');
expect(forkedSingleton.get()).toEqual('bar');
await forkedSingleton.delete();
expect(singleton.get()).toEqual('foo');
};

it('forks a persistent store', async () => {
Expand Down
7 changes: 6 additions & 1 deletion yarn-project/kv-store/src/lmdb/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,14 @@ export class AztecLmdbStore implements AztecKVStore {
}

/**
* Clears the store
* Clears all entries in the store
*/
async clear() {
await this.#rootDb.clearAsync();
}

/** Deletes this store */
async delete() {
await this.#rootDb.drop();
}
}
3 changes: 3 additions & 0 deletions yarn-project/prover-node/src/job/block-proving-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class BlockProvingJob {
private l2BlockSource: L2BlockSource,
private l1ToL2MessageSource: L1ToL2MessageSource,
private txProvider: TxProvider,
private cleanUp: () => Promise<void> = () => Promise.resolve(),
) {}

public getState(): BlockProvingJobState {
Expand Down Expand Up @@ -105,6 +106,8 @@ export class BlockProvingJob {
} catch (err) {
this.log.error(`Error running block prover job: ${err}`);
this.state = 'failed';
} finally {
await this.cleanUp();
}
}

Expand Down
1 change: 1 addition & 0 deletions yarn-project/prover-node/src/prover-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class ProverNode {
this.l2BlockSource,
this.l1ToL2MessageSource,
this.txProvider,
() => db.delete(),
);
}
}
3 changes: 3 additions & 0 deletions yarn-project/world-state/src/world-state-db/merkle_tree_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ export type MerkleTreeDb = {
* Forks the database at its current state.
*/
fork(): Promise<MerkleTreeDb>;

/** Deletes this database. */
delete(): Promise<void>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,8 @@ export class MerkleTreeOperationsFacade implements MerkleTreeOperations {
): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>> {
return this.trees.batchInsert(treeId, leaves, subtreeHeight);
}

public delete(): Promise<void> {
return this.trees.delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ export class MerkleTreeSnapshotOperationsFacade implements MerkleTreeOperations
return Promise.reject(new Error('Tree snapshot operations are read-only'));
}

delete(): Promise<void> {
return Promise.reject(new Error('Tree snapshot operations are read-only'));
}

updateHistoricArchive(): Promise<void> {
return Promise.reject(new Error('Tree snapshot operations are read-only'));
}
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/world-state/src/world-state-db/merkle_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ export class MerkleTrees implements MerkleTreeDb {
return MerkleTrees.new(forked, this.log);
}

public async delete() {
await this.store.delete();
}

public getInitialHeader(): Header {
return Header.empty({ state: this.#loadInitialStateReference() });
}
Expand Down

0 comments on commit 2b4a842

Please sign in to comment.