Skip to content

fix(common): use keyed hash codes and honor transaction cache flag - #7146

Open
317787106 wants to merge 2 commits into
tronprotocol:release_v4.8.3from
317787106:fix/sha256hash_hashcode
Open

317787106 wants to merge 2 commits into
tronprotocol:release_v4.8.3from
317787106:fix/sha256hash_hashcode

Conversation

@317787106

@317787106 317787106 commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Harden in-memory ID hashing while preserving the wire protocol, chain identifiers, persistent data formats, and transaction/block validation rules. No fork activation, database migration, or resynchronization is required.

  1. Compute Sha256Hash.hashCode() from all 32 ID bytes using SipHash-1-3 from the existing Bouncy Castle dependency, then use the low 32 bits as the Java hash code. Generate a random 128-bit key once per process and create an independent SipHash(1, 3) instance for each calculation. BlockId and wrappers that delegate to it automatically use the same implementation.
  2. Reuse this hash when Item precomputes its hash code, removing its separate SipHash implementation while retaining its final fields and cached result in fix(net): hash inventory items with a per-process key #7107.
  3. Skip block-side transactionIdCache writes when the existing trxCacheEnable option is disabled. Keep the existing cache behavior when it is enabled.

Why are these changes required?

The previous Sha256Hash.hashCode() used only the last four bytes, allowing predictable collision sets to concentrate entries in hash buckets. A keyed hash over the complete ID protects the relevant in-memory collections from these precomputed sets.

The broadcast API reads transactionIdCache only when trxCacheEnable is enabled, but block processing previously populated it unconditionally under the Manager lock. Honor the same option on the write path.

This PR has been tested by:

  • Unit tests on local JDK 17 / aarch64.

Follow up

None.

Extra details

Compatibility:

Area Result and evidence
Network protocol and chain identity Cryptographic SHA-256 operations, protobuf fields, signing inputs, and the underlying 32-byte IDs are unchanged. Fixture comparisons confirm identical txID, block ID, Merkle root, and block encoding. Existing and updated nodes use the same wire data.
Existing databases Persistent keys and values continue to use ID bytes and their existing encodings. Object hashCode() values are not substituted for database keys. No database conversion or resync is needed.
Java API and key equality No public signature changes. equals(), compareTo(), getBytes(), and toString() are unchanged. Equal plain hashes and BlockId instances have equal hash codes and support mixed-type map lookups.
Java serialization No instance field or serialization-format change. The new hash function is private and static. Existing serialVersionUID values remain 699713841498311882 for Sha256Hash and -8103005124700041973 for BlockId; bidirectional serialized-map/set checks pass.
Cache configuration The existing option and its default are unchanged. With the option disabled, block processing stops maintaining an unused API cache; enabled configurations retain their existing read/write behavior. Transaction queries and consensus duplicate checks use their existing stores/caches.

Numeric Java hash codes and hash-container traversal order intentionally vary between processes. They are not transaction/block identifiers; transaction order inside a block and validation/execution rules are unchanged. Synchronization and fork-recovery regressions pass. The existing requirement not to modify key bytes while they are stored in a hash collection still applies.

Reuse the existing Bouncy Castle dependency; no dependency changes.

Hash all Sha256Hash bytes with a per-process SipHash key to prevent
precomputed collision sets from degrading ID-keyed hash collections.
Reuse this hash when Item precomputes its hash code.

Skip block-side transaction ID cache writes when trxCacheEnable is
disabled. Cover collision distribution, equality with BlockId, Java
serialization compatibility, and both cache settings in existing tests.
Use the existing Bouncy Castle implementation with one compression
round and three finalization rounds. Retain a random 128-bit key per
process and independent state for every hash calculation.

Add the 64 reference vectors and a concurrent hash-code consistency
test. Preserve ID encodings and Java serialization without changing
dependencies.
* Returns a process-local SipHash-1-3 hash code of all bytes with a random key.
*/
@Override
public int hashCode() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Make sure this new change does not effect all caller logic.

@SeriousCoding789
SeriousCoding789 self-requested a review December 5, 2026 03:15
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.

2 participants