-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: restore world state from database
Fix #2357
- Loading branch information
Showing
10 changed files
with
222 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { LevelDown, default as leveldown } from 'leveldown'; | ||
import { LevelUp, default as levelup } from 'levelup'; | ||
import { MemDown, default as memdown } from 'memdown'; | ||
import { join } from 'node:path'; | ||
|
||
import { AztecNodeConfig } from './config.js'; | ||
|
||
export const createMemDown = () => (memdown as any)() as MemDown<any, any>; | ||
export const createLevelDown = (path: string) => (leveldown as any)(path) as LevelDown; | ||
|
||
const DB_SUBDIR = 'aztec-node'; | ||
const NODE_METADATA_KEY = '@@aztec_node_metadata'; | ||
|
||
/** | ||
* The metadata for an aztec node. | ||
*/ | ||
type NodeMetadata = { | ||
/** | ||
* The address of the rollup contract on L1 | ||
*/ | ||
rollupContractAddress: string; | ||
}; | ||
|
||
/** | ||
* Opens the database for the aztec node. | ||
* @param config - The configuration to be used by the aztec node. | ||
* @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()); | ||
const prevNodeMetadata = await getNodeMetadata(db); | ||
|
||
// if the rollup addresses are different, wipe the local database and start over | ||
if (nodeMetadata.rollupContractAddress !== prevNodeMetadata.rollupContractAddress) { | ||
await db.clear(); | ||
} | ||
|
||
await db.put(NODE_METADATA_KEY, JSON.stringify(nodeMetadata)); | ||
return db; | ||
} | ||
|
||
/** | ||
* Gets the metadata for the aztec node. | ||
* @param db - The database for the aztec node. | ||
* @returns Node metadata. | ||
*/ | ||
async function getNodeMetadata(db: LevelUp): Promise<NodeMetadata> { | ||
try { | ||
const value: Buffer = await db.get(NODE_METADATA_KEY); | ||
return JSON.parse(value.toString('utf-8')); | ||
} catch { | ||
return { | ||
rollupContractAddress: '', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.