Skip to content

Conversation

@kamilsa
Copy link
Contributor

@kamilsa kamilsa commented Sep 26, 2025

This pull request introduces a new fork choice rule implementation and integrates foundational infrastructure for fork choice logic in the blockchain module. The main changes include the addition of the ForkChoiceStore and related APIs, a new FCBlockTree class, and updates to the state transition function. There are also improvements to the timeline logic and node initialization to support the new fork choice mechanism.

Known issues

  • No persistency
  • Genesis time is set as next multiple of 12 seconds

Copilot AI review requested due to automatic review settings September 26, 2025 06:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements a fork choice rule mechanism and integrates foundational infrastructure for fork choice logic in the blockchain module. The changes introduce the ForkChoiceStore class with vote processing, block production, and head selection capabilities, along with a new FCBlockTree implementation and updates to support interval-based timeline execution.

Key changes:

  • Implements ForkChoiceStore with comprehensive fork choice algorithms including vote processing, safe target computation, and block production
  • Adds FCBlockTree as a fork choice-aware block tree implementation
  • Updates timeline logic to support interval-based execution with slot subdivision
  • Integrates fork choice store into production and networking modules

Reviewed Changes

Copilot reviewed 38 out of 38 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/blockchain/fork_choice.hpp/cpp Core fork choice store implementation with vote processing and block production
src/blockchain/impl/fc_block_tree.hpp/cpp Fork choice-aware block tree implementation
src/modules/production/production.cpp Integration of fork choice for block and vote production
src/modules/networking/networking.cpp Integration of fork choice for attestation processing
src/app/impl/timeline_impl.cpp Enhanced timeline with interval-based slot execution
tests/unit/blockchain/fork_choice_test.cpp Comprehensive test suite for fork choice functionality

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

};
store.blocks_.emplace(anchor_root, std::move(anchor_block));
store.states_.emplace(anchor_root, std::move(anchor_state));
std::cout << "Genesis (anchor) root " << anchor_root.toHex() << "\n";
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

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

Using std::cout for output in production code violates logging conventions. This should use the proper logging system (SL_INFO, SL_DEBUG, etc.) or be removed entirely.

Copilot uses AI. Check for mistakes.
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
genesis_time += 12000 - (genesis_time % 12000); // 1758719218000
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

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

The magic number 12000 should be replaced with a named constant (e.g., GENESIS_TIME_INTERVAL_MS) to improve code readability and maintainability. The comment with the hardcoded timestamp should also be removed.

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings September 26, 2025 13:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 39 out of 39 changed files in this pull request and generated 6 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


private:
STF stf_;
std::shared_ptr<clock::SystemClock> clock_;
Copy link
Contributor

Choose a reason for hiding this comment

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

time_ counter tracks time observed and incremented by ForkChoiceStore state machine.
It's not expected to be real clock, and not mockable.

Suggested change
std::shared_ptr<clock::SystemClock> clock_;
Interval time_ = 0;

May add separate slotFromClock method

} else {
// forkchoice should be correctly ticked to current time before importing
// gossiped attestations
if (vote.slot > getCurrentSlot() + 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

getCurrentSlot() should return slot from time_, not clock.now()

Suggested change
if (vote.slot > getCurrentSlot() + 1) {
if (vote.slot > getCurrentSlot()) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Copilot AI review requested due to automatic review settings September 29, 2025 05:03
Co-authored-by: Copilot <[email protected]>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 39 out of 39 changed files in this pull request and generated 5 comments.

Comments suppressed due to low confidence (2)

src/modules/shared/prodution_types.tmp.hpp:1

  • The filename contains a typo: 'prodution_types.tmp.hpp' should be 'production_types.tmp.hpp' (missing 'c' in 'production').
/**

src/blockchain/fork_choice.cpp:1

  • Large block of commented debug code should be removed from production code. If debugging functionality is needed, consider using a proper logging mechanism with debug levels instead.
/**

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

blocks_.emplace(block_hash, final_block);
states_.emplace(block_hash, std::move(final_state));

// update head (not in spec)
Copy link

Copilot AI Sep 29, 2025

Choose a reason for hiding this comment

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

The comment '(not in spec)' indicates a deviation from specification. This should be properly documented with rationale for the deviation and potential implications.

Suggested change
// update head (not in spec)
/**
* Deviation from specification: update head_ to the newly produced block.
*
* Rationale:
* The specification does not require the fork choice head to be updated immediately
* upon block production. However, in this implementation, we update head_ here to
* ensure that subsequent queries to getHead() reflect the most recently produced block.
* This can simplify block production logic and improve responsiveness for clients
* querying the current head.
*
* Potential implications:
* - May cause temporary divergence from the canonical fork choice rule if other
* blocks are produced or imported concurrently.
* - Should be reviewed to ensure it does not affect consensus safety or liveness.
* - If strict spec compliance is required, consider removing or gating this update.
*/

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings September 29, 2025 09:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 39 out of 39 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

src/blockchain/fork_choice.cpp:1

  • Remove commented-out debug code. This clutters the codebase and should be replaced with proper logging if needed.
/**

src/blockchain/state_transition_function.cpp:1

  • Remove commented-out code. If this assignment is intentionally disabled, add a comment explaining why.
/**

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

} else {
// forkchoice should be correctly ticked to current time before importing
// gossiped attestations
if (vote.slot > getCurrentSlot() + 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on lines +167 to +172
auto block_hash = block.hash();
blocks_.emplace(block_hash, block);
states_.emplace(block_hash, std::move(state));

// update head (not in spec)
head_ = block_hash;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto block_hash = block.hash();
blocks_.emplace(block_hash, block);
states_.emplace(block_hash, std::move(state));
// update head (not in spec)
head_ = block_hash;
BOOST_OUTCOME_TRY(onBlock(block));

Comment on lines +126 to +127
AnchorState genesis_state = STF::generateGenesisState(*genesis_config);
AnchorBlock genesis_block = STF::genesisBlock(genesis_state);
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to refactor it to use only injections. AnchorState inherited on State and having ctor by genesis_config. And the same for AnchorBlock with ctor by AnchorState.

@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@kamilsa kamilsa enabled auto-merge (squash) September 30, 2025 07:39
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@qdrvm qdrvm deleted a comment from Copilot AI Sep 30, 2025
@xDimon xDimon self-requested a review October 1, 2025 06:31
@kamilsa kamilsa merged commit 6765a12 into master Oct 1, 2025
2 checks passed
@kamilsa kamilsa deleted the feature/fork_choice_timeline branch October 1, 2025 06:37
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.

4 participants