Skip to content

Conversation

nazarhussain
Copy link
Contributor

Motivation

Use smaller and pure functions for complex logic.

Description

  • Split the operation into plan, fetch and apply stages

Steps to test or reproduce

Run all tests

@nazarhussain nazarhussain requested a review from a team as a code owner October 8, 2025 11:39
Copy link
Contributor

Summary of Changes

Hello @nazarhussain, 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 PR refactors the state regeneration logic by breaking it down into three distinct phases: planning the regeneration steps, fetching the necessary data (snapshots and differentials), and applying these artifacts to construct the final state. This change improves code organization and readability by replacing a single large operation file with several smaller, focused modules and a new orchestration layer.

Highlights

  • Intent: This pull request refactors the state regeneration operation to improve modularity and maintainability. The primary goal is to decompose the complex logic into smaller, pure functions, specifically by splitting the operation into distinct 'plan', 'fetch', and 'apply' stages.
  • Key Changes: The core change involves replacing the monolithic differentialOperation.ts file with a new, structured approach across several new files:
  • plan.ts: Introduces StateRegenPlan and buildStateRegenPlan to determine the necessary steps (snapshot slot, differential slots, and optional block replay range) required to regenerate a state for a given target slot.
  • fetch.ts: Defines StateRegenArtifacts and fetchStateRegenArtifacts to retrieve the required BeaconStateSnapshot and BeaconStateDifferentials from the database based on the generated plan.
  • apply.ts: Introduces StateRegenContext and applyStateRegenPlan to execute the regeneration. This function takes the plan and fetched artifacts, applies state differentials, and performs block replay if specified.
  • execute.ts: Provides a high-level entry point, regenerateState, which orchestrates the plan, fetch, and apply stages to regenerate a BeaconState.

Accompanying these functional changes, the pull request also includes:

  • Removal of the old differentialOperation.test.ts and addition of plan.test.ts to reflect the new structure.
  • Minor non-functional updates to comments in replayBlocks.ts, reordering of exports in index.ts, and slight wording adjustments in test fixture descriptions in hierarchicalLayers.ts.
  • Reviewer Notes: No specific reviewer activity or comments were provided in the context.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This is a great refactoring that improves the structure and clarity of the state regeneration logic by splitting it into plan, fetch, and apply stages. I've found a critical issue and a few areas for improvement regarding performance and code clarity. Please see my comments below.

Comment on lines +79 to +88
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,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There's a critical bug here in determining the fromSlot for block replay. plan.blockReplay.fromSlot is calculated based on the assumption that all planned diffs are available. However, if some diffs are missing (as warned on line 37), stateWithDiffApplied will be at an earlier slot. Starting the block replay from the planned fromSlot will cause some blocks to be skipped, leading to an incorrect final state.

The block replay must start from the slot of the state after applying all available diffs, which is stateWithDiffApplied.slot.

  const fromSlot = stateWithDiffApplied.slot;
  const toSlot = plan.blockReplay.tillSlot;

  ctx.logger?.verbose("Replaying blocks", {
    fromSlot,
    tillSlot: toSlot,
  });

  const replayed = await replayBlocks(ctx, {
    stateBytes,
    fromSlot,
    toSlot,
  });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a check orderedDiffs.at(-1)?.slot !== plan.blockReplay.fromSlot - 1 that will make sure the last diff we have is adjacent to block replay.

Comment on lines +44 to +54
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(",")}`);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for ordering diffs and checking for missing ones can be made more efficient and concise.

  1. Inefficient Loop: The current implementation uses a for loop with artifacts.diffs.find() inside, which results in a time complexity of roughly O(M*N), where M is plan.diffSlots.length and N is artifacts.diffs.length. This can be optimized to O(M+N) by using a Map for faster lookups.
  2. Redundant Check: The if condition on line 52 is redundant. The fetchStateRegenArtifacts function ensures that artifacts.diffs.length + artifacts.missingDiffs.length is always equal to plan.diffSlots.length. Since orderedDiffs.length will equal artifacts.diffs.length, this condition will never be met and can be removed.

Here is a suggested refactoring that addresses both points:

  const diffsMap = new Map(artifacts.diffs.map((d) => [d.slot, d]));
  const orderedDiffs = plan.diffSlots.flatMap((slot) => {
    const diff = diffsMap.get(slot);
    return diff ? [diff] : [];
  });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer to use for over map.

Comment on lines +28 to +31
for (const edge of plan.diffSlots) {
const diff = await getStateDifferential({db}, {slot: edge});
diff ? diffs.push(diff) : missingDiffs.push(edge);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation fetches state differentials from the database sequentially within a for...of loop. This can be inefficient if there are multiple diffs to retrieve. To improve performance, you can fetch them in parallel using Promise.all.

Suggested change
for (const edge of plan.diffSlots) {
const diff = await getStateDifferential({db}, {slot: edge});
diff ? diffs.push(diff) : missingDiffs.push(edge);
}
await Promise.all(
plan.diffSlots.map(async (edge) => {
const diff = await getStateDifferential({db}, {slot: edge});
diff ? diffs.push(diff) : missingDiffs.push(edge);
})
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer to use for over map and there will be maximum of 4 diffs in worst case anyway so not a big problem.

Copy link

codecov bot commented Oct 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (feature/differential-archive@01b3fce). Learn more about missing BASE report.

Additional details and impacted files
@@                       Coverage Diff                       @@
##             feature/differential-archive    #8509   +/-   ##
===============================================================
  Coverage                                ?   52.29%           
===============================================================
  Files                                   ?      859           
  Lines                                   ?    65291           
  Branches                                ?     4804           
===============================================================
  Hits                                    ?    34142           
  Misses                                  ?    31080           
  Partials                                ?       69           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant