-
-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement BufferPool for PersistentCPStateCache #6269
Merged
wemeetagain
merged 6 commits into
unstable
from
tuyen/persistent_cp_state_cache_buffer_pool
Jan 18, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
527c0a3
feat: implement BufferPool for PersistentCPStateCache
twoeths 32b8328
fix: alloc vs allocUnsafe for BufferPool
twoeths a97905b
chore: conform to style guide
twoeths 545599d
feat: use using with Disposable object
twoeths a79d35a
Add custom build target for beacon-node unit tests
nazarhussain 1fc6ed9
chore: address PR comments
twoeths File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 CPStateDatastore 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the current
CheckpointStateCache
implementation does nothing on this call