From 15a14f3d4022d15dd4f55e7c772260b20b943bab Mon Sep 17 00:00:00 2001 From: GrapeS Date: Fri, 13 Nov 2026 11:23:40 +0800 Subject: [PATCH 1/2] fix(net): hash inventory items with a per-process key Inventory ids come from peers, so Item no longer derives its hash code from the id bytes directly. It computes SipHash-2-4 over the 32 id bytes once in the constructor, keyed by two SecureRandom values drawn when the class loads. equals() and Sha256Hash.hashCode() are unchanged. --- .../java/org/tron/core/net/peer/Item.java | 18 ++++++- .../java/org/tron/core/net/peer/ItemTest.java | 54 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 framework/src/test/java/org/tron/core/net/peer/ItemTest.java diff --git a/framework/src/main/java/org/tron/core/net/peer/Item.java b/framework/src/main/java/org/tron/core/net/peer/Item.java index 1bdd381d85d..c05b073878c 100644 --- a/framework/src/main/java/org/tron/core/net/peer/Item.java +++ b/framework/src/main/java/org/tron/core/net/peer/Item.java @@ -1,5 +1,9 @@ package org.tron.core.net.peer; +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; +import java.security.SecureRandom; +import lombok.AccessLevel; import lombok.Getter; import org.tron.common.utils.Sha256Hash; import org.tron.protos.Protocol.Inventory.InventoryType; @@ -7,18 +11,30 @@ @Getter public class Item { + // Ids are chosen by the announcing peer. A per-process random key keeps a peer from + // predicting which ids share a bucket in the inventory caches. + private static final HashFunction ID_HASH = newIdHash(); + @Getter private Sha256Hash hash; @Getter private InventoryType type; @Getter private long time; + @Getter(AccessLevel.NONE) + private final int idHash; public Item(Sha256Hash hash, InventoryType type) { this.hash = hash; this.type = type; this.time = System.currentTimeMillis(); + this.idHash = ID_HASH.hashBytes(hash.getBytes()).asInt(); + } + + private static HashFunction newIdHash() { + SecureRandom random = new SecureRandom(); + return Hashing.sipHash24(random.nextLong(), random.nextLong()); } @Override @@ -36,7 +52,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return hash.hashCode(); + return idHash; } @Override diff --git a/framework/src/test/java/org/tron/core/net/peer/ItemTest.java b/framework/src/test/java/org/tron/core/net/peer/ItemTest.java new file mode 100644 index 00000000000..252d78e6827 --- /dev/null +++ b/framework/src/test/java/org/tron/core/net/peer/ItemTest.java @@ -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 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); + } +} From 02b22dc5c4ee1ba723d549c1fb2d89677bc4fa9a Mon Sep 17 00:00:00 2001 From: GrapeS Date: Fri, 13 Nov 2026 11:23:40 +0800 Subject: [PATCH 2/2] fix(net): hash inventory items with a per-process key Inventory ids come from peers, so Item no longer derives its hash code from the id bytes directly. It computes SipHash-2-4 over the 32 id bytes once in the constructor, keyed by two SecureRandom values drawn when the class loads. equals() and Sha256Hash.hashCode() are unchanged. --- .../java/org/tron/core/net/peer/Item.java | 16 +++++- .../java/org/tron/core/net/peer/ItemTest.java | 54 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 framework/src/test/java/org/tron/core/net/peer/ItemTest.java diff --git a/framework/src/main/java/org/tron/core/net/peer/Item.java b/framework/src/main/java/org/tron/core/net/peer/Item.java index 1bdd381d85d..4a924aeff47 100644 --- a/framework/src/main/java/org/tron/core/net/peer/Item.java +++ b/framework/src/main/java/org/tron/core/net/peer/Item.java @@ -1,5 +1,9 @@ package org.tron.core.net.peer; +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; +import java.security.SecureRandom; +import lombok.AccessLevel; import lombok.Getter; import org.tron.common.utils.Sha256Hash; import org.tron.protos.Protocol.Inventory.InventoryType; @@ -7,18 +11,28 @@ @Getter public class Item { + private static final HashFunction ID_HASH = newIdHash(); + @Getter private Sha256Hash hash; @Getter private InventoryType type; @Getter private long time; + @Getter(AccessLevel.NONE) + private final int idHash; public Item(Sha256Hash hash, InventoryType type) { this.hash = hash; this.type = type; this.time = System.currentTimeMillis(); + this.idHash = ID_HASH.hashBytes(hash.getBytes()).asInt(); + } + + private static HashFunction newIdHash() { + SecureRandom random = new SecureRandom(); + return Hashing.sipHash24(random.nextLong(), random.nextLong()); } @Override @@ -36,7 +50,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return hash.hashCode(); + return idHash; } @Override diff --git a/framework/src/test/java/org/tron/core/net/peer/ItemTest.java b/framework/src/test/java/org/tron/core/net/peer/ItemTest.java new file mode 100644 index 00000000000..252d78e6827 --- /dev/null +++ b/framework/src/test/java/org/tron/core/net/peer/ItemTest.java @@ -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 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); + } +}