Skip to content
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

core/types,node/types: add cached tx hash to ktypes.Transaction, create node/types.Tx #1422

Merged
merged 2 commits into from
Feb 27, 2025

Conversation

jchappelow
Copy link
Member

@jchappelow jchappelow commented Feb 26, 2025

This updates core/types.Transaction a.k.a. ktypes.Transation with an unexported cachedHash field, which is only populated or referenced via the new HashCache method, rather than modifying the Hash method to do this automatically. A few reasons are outlined in the docs, but they include concurrency concerns, needless allocs in many cases, the tx mutability gotchas particularly in client side applications.

This also removes the node/types.NamedTx which was used in a handful of places to pair the hash with the core/types.Transaction, replacing NamedTx with node/types.Tx for brevity. This new types adds a constructor NewTx that computes the has on construction. The Hash() method of this type shadows the embedded types.Transaction's method to return this stored hash. This should be less likely to become an issue since it is "internal" to the node/... packages, and the hash being computed in the constructor avoids any concurrency concerns. Plus it is documented explicitly as an immutable transaction type.

The node/types.Tx with it's construction-time hash storage is primarily used in mempool and the CE.
The HashCache method is used in other places that deal with a core/types.Block, which is necessarily defined in terms of a core/types.Transaction.

@@ -34,7 +34,7 @@ func (ce *ConsensusEngine) validateBlock(blk *ktypes.Block) error {
}

// Verify the merkle root of the block transactions
merkleRoot := blk.MerkleRoot()
merkleRoot := blk.CalcMerkleRoot() // NOTE: this expects CalcMerkleRoot to use tx.HashCache() to prepare the Transaction's internal hash cache
Copy link
Member Author

Choose a reason for hiding this comment

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

validateBlock is a key method that is one of the first points at which a node will begin processing a block it has received, e.g. from p2p, block store, etc. Ensuring that this method warms the internal tx hash cached in core/types.Transaction benefits many other spots downstream.

Comment on lines -126 to -127
txHash := tx.Hash()
have, rejected := ce.mempool.Store(txHash, tx)
Copy link
Member Author

Choose a reason for hiding this comment

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

This silly optimization of having mempool.Store take a hash and the transaction separately was silly and now obsolete.

@jchappelow jchappelow linked an issue Feb 27, 2025 that may be closed by this pull request
@jchappelow jchappelow merged commit 510ede1 into kwilteam:main Feb 27, 2025
2 checks passed
@jchappelow jchappelow deleted the txhashcache branch February 27, 2025 16:14
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.

tx hash caching in transaction struct
2 participants