Repository navigation
fix(net): hash inventory items with a per-process key #7107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kuny0707
merged 3 commits into
tronprotocol:release_v4.8.3
from
Little-Peony:fix/inventory-item-keyed-hash
Nov 13, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
framework/src/test/java/org/tron/core/net/peer/ItemTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package org.tron.core.net.peer; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.tron.common.utils.Sha256Hash; | ||
| import org.tron.core.capsule.BlockCapsule.BlockId; | ||
| import org.tron.protos.Protocol.Inventory.InventoryType; | ||
|
|
||
| public class ItemTest { | ||
|
|
||
| private static Sha256Hash idEndingInZeros(int i) { | ||
| byte[] bytes = new byte[Sha256Hash.LENGTH]; | ||
| bytes[0] = (byte) (i >>> 24); | ||
| bytes[1] = (byte) (i >>> 16); | ||
| bytes[2] = (byte) (i >>> 8); | ||
| bytes[3] = (byte) i; | ||
| return Sha256Hash.wrap(bytes); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEqualItemsShareHashCode() { | ||
| Sha256Hash id = idEndingInZeros(7); | ||
| Item item = new Item(id, InventoryType.TRX); | ||
| Item copy = new Item(Sha256Hash.wrap(id.getBytes().clone()), InventoryType.TRX); | ||
| Assert.assertEquals(item, copy); | ||
| Assert.assertEquals(item.hashCode(), copy.hashCode()); | ||
| Assert.assertEquals(item.hashCode(), item.hashCode()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBlockIdAndPlainHashWithSameBytesShareHashCode() { | ||
| BlockId blockId = new BlockId(Sha256Hash.of(new byte[] {1}), 100L); | ||
| Item fromBlockId = new Item(blockId, InventoryType.BLOCK); | ||
| Item fromPlainHash = new Item(Sha256Hash.wrap(blockId.getBytes()), InventoryType.BLOCK); | ||
| Assert.assertEquals(fromBlockId, fromPlainHash); | ||
| Assert.assertEquals(fromPlainHash, fromBlockId); | ||
| Assert.assertEquals(fromBlockId.hashCode(), fromPlainHash.hashCode()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIdsWithEqualLastFourBytesGetDistinctHashCodes() { | ||
| int count = 10_000; | ||
| Set<Integer> hashCodes = new HashSet<>(); | ||
| for (int i = 0; i < count; i++) { | ||
| Sha256Hash id = idEndingInZeros(i); | ||
| Assert.assertEquals(0, id.hashCode()); | ||
| hashCodes.add(new Item(id, InventoryType.TRX).hashCode()); | ||
| } | ||
| // A keyed 32-bit hash gives about 0.01 expected collisions among 10,000 ids. | ||
| Assert.assertTrue("distinct hash codes: " + hashCodes.size(), hashCodes.size() >= count - 10); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you share the benchmark code and clarify whether the timings include Item construction? SipHash is computed in the constructor, so cache operations on prebuilt items would miss that added cost.
Please also compare construction plus cache lookup/insertion, including allocation and GC overhead, using the same workload and JVM warmup before and after the change. Results on both JDK 8/x86_64 and JDK 17/aarch64 would help assess the performance impact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The timings in the description include construction:
new Item(...)is inside the timed loop, and only id generation is outside. They were single cold runs, though, so I reran a warmed before/after comparison (code below).Base
37ea60a8bcvs this PR, each in its own JVM; 5 × 1 s warmup and 5 × 1 s measurement; JDK 17.0.18 / aarch64,-XX:+UseZGC -Xms2g -Xmx2g. Allocation is the bytes allocated by the measuring thread per item.new Item(...)¹ 695 B in one of the five JVMs.
Construction costs about 24 ns and 96 B more per item, because Guava's
hashBytesallocates a hasher, a buffer and theHashCode. With the cache operations included, allocation is 21% higher and the time difference is within run-to-run variation. On that path ZGC ran 8 times per 5 s measurement before and 4–8 times with this PR.I don't have a JDK 8 / x86_64 machine at hand, so there are no numbers for it yet; the harness runs there unchanged.