-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: restore latest block number (#2474)
This PR updates the `WorldStateSynchroniser` to save and restore the last L2 block it has seen. Fix #2357 # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist). Co-authored-by: PhilWindle <[email protected]>
- Loading branch information
1 parent
47d0d37
commit 6dc2da7
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.