Skip to content

fix(db): reject forks below the solidified block - #7124

Merged
lvs0075 merged 2 commits into
tronprotocol:release_v4.8.3from
xxo1shine:fix/reject-forks-below-solidified-block
Nov 23, 2026
Merged

lvs0075 merged 2 commits into
tronprotocol:release_v4.8.3from
xxo1shine:fix/reject-forks-below-solidified-block

Conversation

@xxo1shine

Copy link
Copy Markdown
Collaborator

What does this PR do?
Reject branches whose common ancestor is below the current solidified height before erasing the canonical chain. Remove the rejected candidate path from Khaos and restore its canonical head.

Why are these changes required?

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details

Reject branches whose common ancestor is below the current solidified height before erasing the canonical chain. Remove the rejected candidate path from Khaos and restore its canonical head.
Keep the common parent at the solidified boundary in single-witness fork scenarios. Allow execution to reach permission revalidation and successful-fork side effects under the solidified ancestor guard.

@halibobo1205 halibobo1205 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[DISCUSS] Non-blocking follow-up. The guard reads LatestSolidifiedBlockNum from DynamicPropertiesStore, which eraseBlock() rolls back together with the snapshots, and DposService.updateSolidBlock() only compares against that rolled-back value. After an allowed switch, the solidified height can therefore end up lower than before, if the new branch's confirmations of the common prefix are spread differently (for example, a node on the smaller side of a partition rejoining). The next switch is then checked against the lower floor. That regression predates this PR. If the intent is "never reorg past a height this node has already reported as solidified", the guard could use a monotonic floor (for example, the highest solidified height seen after a committed block), plus a test that does an allowed switch which lowers the solidified height and then checks that a fork below the old height is still rejected.

long ancestorNum = binaryTree.getValue().peekLast().getNum() - 1;
long solidifiedNum = getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
if (ancestorNum < solidifiedNum) {
Metrics.counterInc(MetricKeys.Counter.BLOCK_FORK, 1, MetricLabels.FAIL);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[NIT] The BLOCK_FORK/FAIL counter plus the BLOCKCHAIN_FAIL_FORK_COUNT meter now appear three times in switchFork. A small private helper would keep them in sync.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The two metric updates are straightforward at each of the three failure sites. I'd prefer to keep this fix focused and retain the current structure for now. We can consider extracting a helper if more failure paths are added.


if (solidifiedOffset > 0) {
int snapshots = dbManager.getRevokingStore().size();
assertThrows(BadBlockException.class, () -> dbManager.pushBlock(newHead));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[NIT] No test covers khaosDb.setHead(binaryTree.getValue().peekFirst()) yet. With that line removed, all switchForkShould* tests still pass. Once the branch is removed, the highest block left in Khaos is oldHead anyway, so removeBlk's own head recompute already lands on it. If it's cheap to build, a case where Khaos still holds a non-canonical block at or above the old head's height when the guard fires would pin this down.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, the current fixture does not independently verify the explicit head restoration. The tests primarily cover rejection at the solidified boundary, preservation of the original chain, and continued processing of subsequent blocks. The additional cached-branch scenario you suggested would strengthen coverage; I'd prefer to keep the existing tests for this fix and consider adding that case separately.

@lvs0075
lvs0075 merged commit 7035fbd into tronprotocol:release_v4.8.3 Nov 23, 2026
10 checks passed
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.

3 participants