-
-
Notifications
You must be signed in to change notification settings - Fork 433
refactor: generalize state repositories #8732
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
base: unstable
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @twoeths, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on generalizing state repositories to work with raw byte arrays instead of specific BeaconState types. This change facilitates future migrations to a more generic state view and improves error detection by enforcing repository method usage at compile time. The updates involve creating a Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request effectively generalizes the state repositories (StateArchiveRepository and CheckpointStateRepository) by introducing a BinaryRepository base class. This decouples them from BeaconStateAllForks and allows them to work with raw Uint8Array data, which is a great improvement for flexibility and compile-time type safety. The refactoring in abstractRepository.ts is clean and separates concerns well. The consumer code has been updated accordingly, for instance by removing initStateFromDb and adjusting how states are retrieved. I have one suggestion to improve the type safety and readability of the decodeKey implementation in StateArchiveRepository.
| /** | ||
| * Restore the latest beacon state from db | ||
| */ | ||
| export async function initStateFromDb( |
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.
remove unused function
| // returning 1 per 100s of states that are persisted in the archive state is not useful enough | ||
| // and it causes consumers having to loadState and createCachedBeaconState again | ||
| // if state is finalized, consumers need to use getHistoricalStateBySlot() api instead | ||
| return null; |
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.
this is to simplify code, same to https://github.com/ChainSafe/lodestar/pull/8728/changes#diff-29e6daed96db2b885b1c4f516eb55bf7e6916e298d50f78bd2f8c57811481d2fR590
|
|
||
| const state = states[0]; | ||
| const stateBytes = stateBytesArr[0]; | ||
| const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes); |
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.
this code make it ready for us to migrate to BeaconStateView later
Performance Report✔️ no performance regression detected Full benchmark results
|
| import {getRootIndex, getRootIndexKey, storeRootIndex} from "./stateArchiveIndex.js"; | ||
|
|
||
| export class StateArchiveRepository extends Repository<Slot, BeaconStateAllForks> { | ||
| export type BeaconStateArchive = { |
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.
generic type to work with the current CachedBeaconStateAllForks and the future BeaconStateView
| * By default, SSZ-encoded values, | ||
| * indexed by root | ||
| */ | ||
| export abstract class Repository<I extends Id, T> extends BinaryRepository<I> { |
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.
nothing change here, just a refactor to move methods without value (T) to BinaryRepository
no need to change anything at the consumer side
| // returning 1 per 100s of states that are persisted in the archive state is not useful enough | ||
| // and it causes consumers having to loadState and createCachedBeaconState again | ||
| // if state is finalized, consumers need to use getHistoricalStateBySlot() api instead | ||
| return null; |
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.
same concern as raised here #8728 (comment)
| optsBuff.lt = this.maxKey; | ||
| } | ||
|
|
||
| // Set at least on max key |
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.
| // Set at least on max key | |
| // Set at least one max key |
Motivation
BeaconStateAllForkswhich make it hard when we migrate to a generic BeaconStateView (to be come later, see Decouple beacon-node and state-transition #8650)Description
BinaryRepositoryStateArchiveRepository + CheckpointStateRepositoryto be extended fromBinaryRepositoryBinaryRepository, it's a compile time check, vs calling methods inRepositoryand throw runtime error which make it harder to detect errorsCloses #8729