-
-
Notifications
You must be signed in to change notification settings - Fork 410
refactor: update the structure of state regen operation #8509
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
Open
nazarhussain
wants to merge
2
commits into
feature/differential-archive
Choose a base branch
from
nh/restructure-diff-op
base: feature/differential-archive
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
95 changes: 95 additions & 0 deletions
95
packages/beacon-node/src/chain/archiveStore/differentialState/apply.ts
This file contains hidden or 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,95 @@ | ||
| import {PubkeyIndexMap} from "@chainsafe/pubkey-index-map"; | ||
| import {BeaconConfig} from "@lodestar/config"; | ||
| import {Logger} from "@lodestar/logger"; | ||
| import {IBeaconDb} from "../../../db/index.ts"; | ||
| import {IStateDiffCodec} from "../interface.ts"; | ||
| import {replayBlocks} from "../utils/replayBlocks.ts"; | ||
| import {StateRegenArtifacts} from "./fetch.ts"; | ||
| import {StateRegenPlan} from "./plan.ts"; | ||
| import {BeaconStateSnapshot} from "./ssz.ts"; | ||
| import {replayStateDifferentials} from "./stateDifferential.ts"; | ||
| import {beaconStateBytesToSnapshot, snapshotToBeaconStateBytes} from "./stateSnapshot.ts"; | ||
|
|
||
| export type StateRegenContext = { | ||
| codec: IStateDiffCodec; | ||
| config: BeaconConfig; | ||
| logger?: Logger; | ||
| pubkey2index: PubkeyIndexMap; | ||
| db: IBeaconDb; | ||
| }; | ||
|
|
||
| export async function applyStateRegenPlan( | ||
| ctx: StateRegenContext, | ||
| plan: StateRegenPlan, | ||
| artifacts: StateRegenArtifacts | ||
| ): Promise<BeaconStateSnapshot> { | ||
| // When we start a node from a certain checkpoint which is usually | ||
| // not the snapshot epoch but we fetch it because of the fallback settings | ||
| if (plan.snapshotSlot !== artifacts.snapshot.slot) { | ||
| ctx.logger?.warn("Expected snapshot not found", { | ||
| expectedSnapshotSlot: plan.snapshotSlot, | ||
| availableSnapshotSlot: artifacts.snapshot.slot, | ||
| }); | ||
| } | ||
|
|
||
| // TODO: Need to do further thinking if we fail here with fatal error | ||
| if (artifacts.missingDiffs.length) { | ||
| ctx.logger?.warn("Missing some diff states", { | ||
| snapshotSlot: plan.snapshotSlot, | ||
| diffPath: plan.diffSlots.join(","), | ||
| missingDiffs: artifacts.missingDiffs.join(","), | ||
| }); | ||
| } | ||
|
|
||
| const orderedDiffs = []; | ||
| for (const diffSlot of plan.diffSlots) { | ||
| const diff = artifacts.diffs.find((d) => d.slot === diffSlot); | ||
| if (diff) { | ||
| orderedDiffs.push(diff); | ||
| } | ||
| } | ||
|
|
||
| if (orderedDiffs.length + artifacts.missingDiffs.length !== plan.diffSlots.length) { | ||
| throw new Error(`Can not find required state diffs ${plan.diffSlots.join(",")}`); | ||
| } | ||
ensi321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (plan.blockReplay && orderedDiffs.at(-1)?.slot !== plan.blockReplay.fromSlot - 1) { | ||
| throw new Error(`Can not replay blocks due to missing state diffs ${artifacts.missingDiffs.join(",")}`); | ||
| } | ||
|
|
||
| ctx.logger?.verbose("Replaying state diffs", { | ||
| snapshotSlot: plan.snapshotSlot, | ||
| diffPath: plan.diffSlots.join(","), | ||
| availableDiffs: orderedDiffs.map((d) => d.slot).join(","), | ||
| }); | ||
|
|
||
| const stateWithDiffApplied = await replayStateDifferentials( | ||
| {codec: ctx.codec, logger: ctx.logger}, | ||
| {stateDifferentials: orderedDiffs, stateSnapshot: artifacts.snapshot} | ||
| ); | ||
|
|
||
| if (stateWithDiffApplied.stateBytes.byteLength === 0 || stateWithDiffApplied.balancesBytes.byteLength === 0) { | ||
| throw new Error( | ||
| `Invalid state after applying diffs: | ||
| stateBytesSize=${stateWithDiffApplied.stateBytes.byteLength}, | ||
| balancesBytesSize=${stateWithDiffApplied.balancesBytes.byteLength}` | ||
| ); | ||
| } | ||
|
|
||
| if (!plan.blockReplay) return stateWithDiffApplied; | ||
|
|
||
| const stateBytes = snapshotToBeaconStateBytes({config: ctx.config}, stateWithDiffApplied); | ||
|
|
||
| ctx.logger?.verbose("Replaying blocks", { | ||
| fromSlot: plan.blockReplay.fromSlot, | ||
| tillSlot: plan.blockReplay.tillSlot, | ||
| }); | ||
|
|
||
| const replayed = await replayBlocks(ctx, { | ||
| stateBytes, | ||
| fromSlot: plan.blockReplay.fromSlot, | ||
| toSlot: plan.blockReplay.tillSlot, | ||
| }); | ||
ensi321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return beaconStateBytesToSnapshot({config: ctx.config}, plan.blockReplay.tillSlot, replayed); | ||
| } | ||
143 changes: 0 additions & 143 deletions
143
packages/beacon-node/src/chain/archiveStore/differentialState/differentialOperation.ts
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
packages/beacon-node/src/chain/archiveStore/differentialState/execute.ts
This file contains hidden or 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,22 @@ | ||
| import {BeaconState, Slot} from "@lodestar/types"; | ||
| import {StateRegenContext, applyStateRegenPlan} from "./apply.ts"; | ||
| import {fetchStateRegenArtifacts} from "./fetch.ts"; | ||
| import {HierarchicalLayers} from "./hierarchicalLayers.ts"; | ||
| import {buildStateRegenPlan} from "./plan.ts"; | ||
| import {snapshotToBeaconState} from "./stateSnapshot.ts"; | ||
|
|
||
| export async function regenerateState( | ||
| ctx: StateRegenContext & {layers: HierarchicalLayers}, | ||
| target: Slot, | ||
| opts?: {fallbackSnapshot?: boolean} | ||
| ): Promise<BeaconState | null> { | ||
| ctx.logger?.verbose("Regenerating state via state differential", { | ||
| slot: target, | ||
| }); | ||
|
|
||
| const plan = buildStateRegenPlan(ctx.layers, target); | ||
| const artifacts = await fetchStateRegenArtifacts(ctx.db, plan, opts); | ||
| const finalState = await applyStateRegenPlan(ctx, plan, artifacts); | ||
|
|
||
| return snapshotToBeaconState(ctx, finalState); | ||
| } |
34 changes: 34 additions & 0 deletions
34
packages/beacon-node/src/chain/archiveStore/differentialState/fetch.ts
This file contains hidden or 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,34 @@ | ||
| import {Slot} from "@lodestar/types"; | ||
| import {IBeaconDb} from "../../../db/index.ts"; | ||
| import {StateRegenPlan} from "./plan.ts"; | ||
| import {BeaconStateDifferential, BeaconStateSnapshot} from "./ssz.ts"; | ||
| import {getStateDifferential} from "./stateDifferential.ts"; | ||
| import {getStateSnapshot} from "./stateSnapshot.ts"; | ||
|
|
||
| export type StateRegenArtifacts = { | ||
| snapshot: BeaconStateSnapshot; | ||
| diffs: BeaconStateDifferential[]; | ||
| missingDiffs: Slot[]; | ||
| }; | ||
|
|
||
| export async function fetchStateRegenArtifacts( | ||
| db: IBeaconDb, | ||
| plan: StateRegenPlan, | ||
| opts: {fallbackSnapshot?: boolean} = {} | ||
| ): Promise<StateRegenArtifacts> { | ||
| const snapshot = await getStateSnapshot({db}, {slot: plan.snapshotSlot, fallback: opts.fallbackSnapshot ?? false}); | ||
|
|
||
| if (!snapshot) { | ||
| throw new Error(`Can not find state snapshot for slot=${plan.snapshotSlot}`); | ||
| } | ||
|
|
||
| const diffs: BeaconStateDifferential[] = []; | ||
| const missingDiffs: Slot[] = []; | ||
|
|
||
| for (const edge of plan.diffSlots) { | ||
| const diff = await getStateDifferential({db}, {slot: edge}); | ||
| diff ? diffs.push(diff) : missingDiffs.push(edge); | ||
| } | ||
ensi321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return {snapshot, diffs, missingDiffs}; | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
packages/beacon-node/src/chain/archiveStore/differentialState/plan.ts
This file contains hidden or 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,34 @@ | ||
| import {Slot} from "@lodestar/types"; | ||
| import {HierarchicalLayers} from "./hierarchicalLayers.ts"; | ||
|
|
||
| export type StateRegenPlan = { | ||
| targetSlot: Slot; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of |
||
| snapshotSlot: Slot; | ||
| diffSlots: Slot[]; | ||
| blockReplay?: {fromSlot: Slot; tillSlot: Slot}; | ||
| }; | ||
|
|
||
| export function buildStateRegenPlan(layers: HierarchicalLayers, target: Slot): StateRegenPlan { | ||
| const path = layers.computeSlotPath(target); | ||
| const [snapshotSlot, ...diffSlots] = path; | ||
| const lastDiffSlot = diffSlots.at(-1); | ||
|
|
||
| if (target === lastDiffSlot || target === snapshotSlot) { | ||
| return { | ||
| snapshotSlot, | ||
| diffSlots, | ||
| blockReplay: undefined, | ||
| targetSlot: target, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| snapshotSlot, | ||
| diffSlots, | ||
| blockReplay: { | ||
| fromSlot: lastDiffSlot ? lastDiffSlot + 1 : snapshotSlot + 1, | ||
| tillSlot: target, | ||
| }, | ||
| targetSlot: target, | ||
| }; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
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.
I think we can make an assumption that
plan.diffSlotsis ordered, and thereforeartifacts.diffsis ordered. I think we can just useartifacts.diffsinstead oforderedDiffsfor any subsequent code from this point.