-
-
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 BufferPool for PersistentCPStateCache
- Loading branch information
Showing
16 changed files
with
590 additions
and
113 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
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
52 changes: 52 additions & 0 deletions
52
packages/beacon-node/src/chain/stateCache/datastore/file.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,52 @@ | ||
import path from "node:path"; | ||
import {toHexString, fromHexString} from "@chainsafe/ssz"; | ||
import {phase0, ssz} from "@lodestar/types"; | ||
import {ensureDir, readFile, readFileNames, removeFile, writeIfNotExist} from "../../../util/file.js"; | ||
import {CPStateDatastore, DatastoreKey} from "./types.js"; | ||
|
||
const CHECKPOINT_STATES_FOLDER = "checkpoint_states"; | ||
const CHECKPOINT_FILE_NAME_LENGTH = 82; | ||
|
||
/** | ||
* Implementation of CPStatePersistentApis using file system, this is beneficial for debugging. | ||
*/ | ||
export class FileCPStateDatastore implements CPStateDatastore { | ||
private readonly folderPath: string; | ||
|
||
constructor(parentDir: string = ".") { | ||
// by default use the beacon folder `/beacon/checkpoint_states` | ||
this.folderPath = path.join(parentDir, CHECKPOINT_STATES_FOLDER); | ||
} | ||
|
||
async init(): Promise<void> { | ||
try { | ||
await ensureDir(this.folderPath); | ||
} catch (_) { | ||
// do nothing | ||
} | ||
} | ||
|
||
async write(cpKey: phase0.Checkpoint, stateBytes: Uint8Array): Promise<DatastoreKey> { | ||
const serializedCheckpoint = ssz.phase0.Checkpoint.serialize(cpKey); | ||
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint)); | ||
await writeIfNotExist(filePath, stateBytes); | ||
return serializedCheckpoint; | ||
} | ||
|
||
async remove(serializedCheckpoint: DatastoreKey): Promise<void> { | ||
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint)); | ||
await removeFile(filePath); | ||
} | ||
|
||
async read(serializedCheckpoint: DatastoreKey): Promise<Uint8Array | null> { | ||
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint)); | ||
return readFile(filePath); | ||
} | ||
|
||
async readKeys(): Promise<DatastoreKey[]> { | ||
const fileNames = await readFileNames(this.folderPath); | ||
return fileNames | ||
.filter((fileName) => fileName.startsWith("0x") && fileName.length === CHECKPOINT_FILE_NAME_LENGTH) | ||
.map((fileName) => fromHexString(fileName)); | ||
} | ||
} |
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,13 +1,13 @@ | ||
import {CachedBeaconStateAllForks} from "@lodestar/state-transition"; | ||
import {phase0} from "@lodestar/types"; | ||
|
||
// With db implementation, persistedKey is serialized data of a checkpoint | ||
export type DatastoreKey = Uint8Array; | ||
|
||
// Make this generic to support testing | ||
export interface CPStateDatastore { | ||
write: (cpKey: phase0.Checkpoint, state: CachedBeaconStateAllForks) => Promise<DatastoreKey>; | ||
write: (cpKey: phase0.Checkpoint, stateBytes: Uint8Array) => Promise<DatastoreKey>; | ||
remove: (key: DatastoreKey) => Promise<void>; | ||
read: (key: DatastoreKey) => Promise<Uint8Array | null>; | ||
readKeys: () => Promise<DatastoreKey[]>; | ||
init?: () => Promise<void>; | ||
} |
Oops, something went wrong.