-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement db persistent option
- Loading branch information
Showing
16 changed files
with
187 additions
and
64 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
38 changes: 38 additions & 0 deletions
38
packages/beacon-node/src/chain/stateCache/persistent/db.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,38 @@ | ||
import {fromHexString, toHexString} from "@chainsafe/ssz"; | ||
import {CachedBeaconStateAllForks} from "@lodestar/state-transition"; | ||
import {IBeaconDb} from "../../../db/interface.js"; | ||
import {CPStatePersistentApis, PersistentKey} from "./types.js"; | ||
|
||
/** | ||
* Implementation of CPStatePersistentApis using db. | ||
*/ | ||
export class DbPersistentApis implements CPStatePersistentApis { | ||
constructor(private readonly db: IBeaconDb) { | ||
void cleanBucket(db); | ||
} | ||
async write(_: string, state: CachedBeaconStateAllForks): Promise<string> { | ||
const root = state.hashTreeRoot(); | ||
const stateBytes = state.serialize(); | ||
await this.db.checkpointState.putBinary(root, stateBytes); | ||
return toHexString(root); | ||
} | ||
|
||
async remove(persistentKey: PersistentKey): Promise<boolean> { | ||
await this.db.checkpointState.delete(fromHexString(persistentKey)); | ||
return true; | ||
} | ||
|
||
async read(persistentKey: string): Promise<Uint8Array | null> { | ||
return this.db.checkpointState.getBinary(fromHexString(persistentKey)); | ||
} | ||
} | ||
|
||
/** | ||
* Clean all checkpoint state in db at startup time. | ||
*/ | ||
async function cleanBucket(db: IBeaconDb): Promise<void> { | ||
const keys = await db.checkpointState.keys(); | ||
for (const key of keys) { | ||
await db.checkpointState.delete(key); | ||
} | ||
} |
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
5 changes: 3 additions & 2 deletions
5
packages/beacon-node/src/chain/stateCache/persistent/types.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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import {CachedBeaconStateAllForks} from "@lodestar/state-transition"; | ||
import {CheckpointKey} from "../types.js"; | ||
|
||
// With fs implementation, persistentKey is ${CHECKPOINT_STATES_FOLDER/rootHex_epoch} | ||
export type PersistentKey = string; | ||
|
||
// Make this generic to support testing | ||
export interface CPStatePersistentApis { | ||
write: (cpKey: CheckpointKey, bytes: Uint8Array) => Promise<PersistentKey>; | ||
write: (cpKey: CheckpointKey, state: CachedBeaconStateAllForks) => Promise<PersistentKey>; | ||
remove: (persistentKey: PersistentKey) => Promise<boolean>; | ||
read: (persistentKey: PersistentKey) => Promise<Uint8Array>; | ||
read: (persistentKey: PersistentKey) => Promise<Uint8Array | null>; | ||
} |
Oops, something went wrong.