Skip to content

[FEATURE]: Implement Lazy Hashing for Transactions #74

@sanaica

Description

@sanaica

Description

Currently, the Transaction.tx_id calculation logic re-computes the SHA-256 hash every time the identifier is requested. During high-load operations like block validation or full-node syncing, the CPU performs these expensive cryptographic operations thousands of times for the same static data.

By implementing a caching pattern, we store the result of the first hash calculation and reuse it for all subsequent calls, significantly reducing redundant CPU cycles.

Proposed Optimization

  1. Lazy Initialization: Add a _cached_tx_id attribute to the Transaction class (initialized as None in __init__).
  2. Memoization: Update the @property tx_id to check if _cached_tx_id exists. If not, it calculates the hash once via canonical_json_hash and stores it.
  3. Cache Invalidation: The sign() method modifies the transaction payload by adding a signature, will reset _cached_tx_id to None to ensure the ID always reflects a signed state.

Expected Impact

  • Performance: Dramatically reduces SHA-256 overhead during validation loops.
  • Minimality: Implemented with a code footprint, maintaining the project's "strictly minimal" philosophy.
  • Scalability: Complements the ongoing $O(1)$ mempool work by ensuring ID retrieval is as fast as the dictionary lookup.

Roadmap Alignment

This supports the "Core Perfection" phase to make it minimal, clean, and performant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions