Skip to content

fix(db): build MerkleTree per-instance to fix concurrent race#6816

Merged
lvs0075 merged 1 commit into
tronprotocol:release_v4.8.2from
xxo1shine:fix/merkle-tree-singleton-race
Jun 3, 2026
Merged

fix(db): build MerkleTree per-instance to fix concurrent race#6816
lvs0075 merged 1 commit into
tronprotocol:release_v4.8.2from
xxo1shine:fix/merkle-tree-singleton-race

Conversation

@xxo1shine
Copy link
Copy Markdown
Collaborator

What does this PR do?

MerkleTree was a shared volatile singleton holding per-build mutable state (leaves/hashList/root). Build MerkleTree per-instance to fix concurrent race.

  • MerkleTree: replace the getInstance() singleton with a static build() factory returning a thread-confined instance; drop the now-inaccurate @NotThreadSafe.
  • BlockCapsule.calcMerkleRoot: use MerkleTree.build(ids); keeps the config-driven hash engine, so no consensus behavior change.
  • MerkleTreeTest: switch call sites to build(); un-ignore testConcurrent and turn it into a regression test asserting 1000 concurrent builds succeed.

Why are these changes required?

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details

MerkleTree was a shared volatile singleton holding per-build mutable state (leaves/hashList/root). When merkle validation runs concurrently (e.g. pre-broadcast validation on the P2P handler threads alongside the apply path), concurrent calls mutated the shared leaves list and threw ArrayIndexOutOfBoundsException (surfaced on an ARM SR due to its weaker memory model), which escaped the BadBlockException catch and dropped peers.

- MerkleTree: replace the getInstance() singleton with a static build() factory returning a thread-confined instance; drop the now-inaccurate @NotThreadSafe.
- BlockCapsule.calcMerkleRoot: use MerkleTree.build(ids); keeps the config-driven hash engine, so no consensus behavior change.
- MerkleTreeTest: switch call sites to build(); un-ignore testConcurrent and turn it into a regression test asserting 1000 concurrent builds succeed.
@github-actions github-actions Bot requested a review from halibobo1205 June 3, 2026 06:43
@lvs0075 lvs0075 requested a review from waynercheung June 3, 2026 06:46
@halibobo1205 halibobo1205 added the topic:DB Database label Jun 3, 2026
@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.8.2 milestone Jun 3, 2026
Copy link
Copy Markdown
Collaborator

@waynercheung waynercheung left a comment

Choose a reason for hiding this comment

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

LGTM

@lvs0075 lvs0075 merged commit ba5b012 into tronprotocol:release_v4.8.2 Jun 3, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:DB Database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants