-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Track world state db versions and wipe the state upon version c…
…hange (#9946) Implements a very basic mechanism of tracking changes to the world state DB structure and deleting the world state when a change is detected.
- Loading branch information
1 parent
30ca68c
commit 209d484
Showing
3 changed files
with
103 additions
and
9 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
41 changes: 41 additions & 0 deletions
41
yarn-project/world-state/src/native/world_state_version.ts
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,41 @@ | ||
import { EthAddress } from '@aztec/circuits.js'; | ||
|
||
import { readFile, writeFile } from 'fs/promises'; | ||
|
||
export class WorldStateVersion { | ||
constructor(readonly version: number, readonly rollupAddress: EthAddress) {} | ||
|
||
static async readVersion(filename: string) { | ||
const versionData = await readFile(filename, 'utf-8').catch(() => undefined); | ||
if (versionData === undefined) { | ||
return undefined; | ||
} | ||
const versionJSON = JSON.parse(versionData); | ||
if (versionJSON.version === undefined || versionJSON.rollupAddress === undefined) { | ||
return undefined; | ||
} | ||
return WorldStateVersion.fromJSON(versionJSON); | ||
} | ||
|
||
public async writeVersionFile(filename: string) { | ||
const data = JSON.stringify(this.toJSON()); | ||
await writeFile(filename, data, 'utf-8'); | ||
} | ||
|
||
toJSON() { | ||
return { | ||
version: this.version, | ||
rollupAddress: this.rollupAddress.toChecksumString(), | ||
}; | ||
} | ||
|
||
static fromJSON(obj: any): WorldStateVersion { | ||
const version = obj.version; | ||
const rollupAddress = EthAddress.fromString(obj.rollupAddress); | ||
return new WorldStateVersion(version, rollupAddress); | ||
} | ||
|
||
static empty() { | ||
return new WorldStateVersion(0, EthAddress.ZERO); | ||
} | ||
} |