core/types,node/types: add cached tx hash to ktypes.Transaction, create node/types.Tx #1422
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.
This updates
core/types.Transaction
a.k.a.ktypes.Transation
with an unexportedcachedHash
field, which is only populated or referenced via the newHashCache
method, rather than modifying theHash
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 thecore/types.Transaction
, replacingNamedTx
withnode/types.Tx
for brevity. This new types adds a constructorNewTx
that computes the has on construction. TheHash()
method of this type shadows the embeddedtypes.Transaction
's method to return this stored hash. This should be less likely to become an issue since it is "internal" to thenode/...
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 acore/types.Block
, which is necessarily defined in terms of acore/types.Transaction
.