Skip to content

feat(consensus): eliminate block ties#673

Merged
msbrogli merged 1 commit intomasterfrom
feat/consensus-change
Dec 11, 2025
Merged

feat(consensus): eliminate block ties#673
msbrogli merged 1 commit intomasterfrom
feat/consensus-change

Conversation

@msbrogli
Copy link
Member

@msbrogli msbrogli commented Jun 16, 2023

Motivation

We've always supported "block ties", meaning that blocks that have the same score will tie and both void each other, while at the same time having to remain as "best block tips" (which always had to be a list). This pushes some unnecessary complexity throughout the code. There are no disadvantages to using the block hash as a tie-breaker and simplifying this complexity.

In practice there's a slight difference in the consensus of certain situations, the difference is that before some tied block chains would be voided all the way back until they weren't tied anymore, now one of them would be picked as best chain. This situation does not cause any incompatibility in how nodes sync and any new block with higher score will cause the same state on both chains.

This PR is the first step, that only focuses on removing ties at the consensus level. Subsequent PRs will focus on refactoring out the complexity of supporting multiple tips.

Acceptance criteria

  • update block consensus' update_voided_info so when testing if a new block might be a winner, and its score is neither higher or lower than the current winner (that is, a tie), the hash is used as a tie breaker
  • update block consensus' update_voided_info so when a new block is not a winner, only the tip with highest score and lowest hash is stored as "best block tips" cache
  • update transaction storage's get_best_block_tips so it only ever returns a list of size 1, when the block tips index returns more than one tip with the highest score, the one if lowest hash is selected
  • update test_split_brain_only_blocks_different_height so it checks the node will correctly not void everything back to the genesis and tie the two blocks (when before it used to)
  • update test_single_fork_not_best similarly, so it checks that the best chain is considered

@msbrogli msbrogli requested a review from jansegre as a code owner June 16, 2023 07:14
@msbrogli msbrogli self-assigned this Jun 16, 2023
@msbrogli msbrogli force-pushed the feat/consensus-change branch 3 times, most recently from 5598803 to 8abec23 Compare June 16, 2023 08:51
@msbrogli msbrogli force-pushed the feat/consensus-change branch from 8abec23 to 5026f34 Compare July 12, 2023 12:07
@msbrogli msbrogli force-pushed the feat/consensus-change branch from 5026f34 to d4dc39e Compare October 17, 2023 20:16
@jansegre jansegre assigned jansegre and unassigned msbrogli Jul 10, 2024
@jansegre jansegre force-pushed the feat/consensus-change branch from d4dc39e to 807d778 Compare July 10, 2024 16:37
@jansegre jansegre force-pushed the feat/consensus-change branch 5 times, most recently from 7e4b8b8 to e516014 Compare August 1, 2024 16:36
@jansegre jansegre changed the base branch from master to feat/score-acc-weight-types August 1, 2024 16:36
@codecov
Copy link

codecov bot commented Aug 1, 2024

Codecov Report

❌ Patch coverage is 92.30769% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.19%. Comparing base (623856d) to head (5e3d848).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
hathor/transaction/storage/transaction_storage.py 0.00% 1 Missing and 1 partial ⚠️
hathor/consensus/block_consensus.py 97.22% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #673      +/-   ##
==========================================
- Coverage   86.23%   86.19%   -0.05%     
==========================================
  Files         440      440              
  Lines       34068    34056      -12     
  Branches     5330     5327       -3     
==========================================
- Hits        29378    29353      -25     
- Misses       3668     3677       +9     
- Partials     1022     1026       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

best_tip_blocks = [block_hash]

# XXX: if there's more than one we filter it so it's the smallest hash
if len(best_tip_blocks) > 1:
Copy link
Member Author

Choose a reason for hiding this comment

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

Should we create an issue to refactor this method and simply store the current best block in the storage?

Copy link
Member

Choose a reason for hiding this comment

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

#540 sort of does this by using the height-index, but because it's not using this PR as a base it does this incorrectly and hides other tips, after this PR that change would be correct

Copy link
Member

Choose a reason for hiding this comment

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

#540 was superseded by #1200 which just removes sync-v1 indexes instead of making them optional, because sync-v1 has already been removed.

Copy link
Member

Choose a reason for hiding this comment

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

#1200 uses the block-height index to get the best block now, which will now always match with the best-block-tip since they can't be voided because of a tie

@jansegre jansegre changed the title feat(consensus): Change feat(consensus): eliminate block ties Aug 12, 2024
@jansegre jansegre force-pushed the feat/score-acc-weight-types branch from 17c1ffc to 93d9f89 Compare August 21, 2024 13:18
@jansegre jansegre force-pushed the feat/consensus-change branch from e516014 to 1b93f4d Compare August 21, 2024 13:25
@jansegre jansegre requested a review from glevco August 22, 2024 00:36
@jansegre jansegre force-pushed the feat/score-acc-weight-types branch from 93d9f89 to da4ac0d Compare August 22, 2024 21:45
@jansegre jansegre force-pushed the feat/score-acc-weight-types branch from da4ac0d to b74fa0d Compare September 3, 2024 15:44
@jansegre jansegre force-pushed the feat/consensus-change branch from 1b93f4d to b9b2390 Compare September 3, 2024 16:34
@jansegre jansegre force-pushed the feat/score-acc-weight-types branch from b74fa0d to 45884e3 Compare September 10, 2024 16:05
@jansegre jansegre force-pushed the feat/consensus-change branch from b9b2390 to 2dddd20 Compare September 10, 2024 16:20
@jansegre jansegre force-pushed the feat/score-acc-weight-types branch from 45884e3 to b21d67c Compare September 24, 2024 14:30
@jansegre jansegre force-pushed the feat/score-acc-weight-types branch 2 times, most recently from 2f2d7a8 to 1a3e1d0 Compare October 3, 2024 21:51
@msbrogli msbrogli force-pushed the feat/consensus-change branch from 299608f to dd0b9fe Compare September 30, 2025 16:54
@msbrogli msbrogli moved this from In Progress (Done) to In Progress (WIP) in Hathor Network Sep 30, 2025
@msbrogli msbrogli force-pushed the feat/consensus-change branch 4 times, most recently from dda0697 to 4c5e2c1 Compare December 9, 2025 12:43

self.assert_response_equal(responses, expected)

@pytest.mark.skip(reason='broken')
Copy link
Member Author

Choose a reason for hiding this comment

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

This test needs to be fixed.


self.assert_response_equal(responses, expected)

@pytest.mark.skip(reason='broken')
Copy link
Member Author

Choose a reason for hiding this comment

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

This test needs to be fixed.

@msbrogli msbrogli force-pushed the feat/consensus-change branch 2 times, most recently from bd3fc3e to e789495 Compare December 9, 2025 13:49
jansegre
jansegre previously approved these changes Dec 9, 2025
Copy link
Member

@jansegre jansegre left a comment

Choose a reason for hiding this comment

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

We still need to have the tests passing, but I'm OK with the implementation.

@msbrogli msbrogli force-pushed the feat/consensus-change branch 4 times, most recently from 0122823 to ebac414 Compare December 9, 2025 22:33
@msbrogli msbrogli moved this from In Progress (WIP) to In Progress (Done) in Hathor Network Dec 9, 2025
@msbrogli msbrogli force-pushed the feat/consensus-change branch from ebac414 to da8b8d0 Compare December 9, 2025 22:37
jansegre
jansegre previously approved these changes Dec 10, 2025
glevco
glevco previously approved these changes Dec 10, 2025
@github-project-automation github-project-automation bot moved this from In Progress (Done) to In Review (WIP) in Hathor Network Dec 10, 2025
@msbrogli msbrogli force-pushed the feat/consensus-change branch from da8b8d0 to 248b99e Compare December 11, 2025 01:19
@msbrogli msbrogli dismissed stale reviews from glevco and jansegre via 08a48da December 11, 2025 04:04
Co-authored-by: Jan Segre <jan@hathor.network>
This was referenced Dec 11, 2025
@jansegre jansegre mentioned this pull request Jan 14, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants