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

cleanup: replace crypto.HashObj(Transaction) with Transaction.ID() #3958

Merged
merged 3 commits into from
May 16, 2022

Conversation

brianolson
Copy link
Contributor

Summary

For clarity, always use Transaction.ID() when generating the txid hash of a Transaction. crypto.HashObj(txn) is equivalent but less commonly used and can lead one on a wild goose chase to determine, "wait, is that equivalent?"

Test Plan

No functional change should be in this change. No new features. Should just pass existing tests.

@brianolson brianolson self-assigned this May 6, 2022
@brianolson brianolson added Team Carbon-11 tech debt Things that need re-work for simplification / sanitization to reduce implementation overhead labels May 6, 2022
@@ -161,6 +161,7 @@ type TxGroup struct {
// together, sequentially, in a block in order for the group to be
// valid. Each hash in the list is a hash of a transaction with
// the `Group` field omitted.
// These are all `Txid` which is equivalent to `crypto.Digest`
Copy link
Contributor

Choose a reason for hiding this comment

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

could you please add a test into transaction_test.go checking tx.ID() == crypto.HashObj(tx) ? In code I see they are the same but this is not enforced.

Copy link
Contributor

Choose a reason for hiding this comment

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

agreed, would be good to put that assertion in this PR

@brianolson brianolson changed the title replace crypto.HashObj(Transaction) with Transaction.ID() cleanup: replace crypto.HashObj(Transaction) with Transaction.ID() May 9, 2022
for i, txn := range txns {
txn.Group = crypto.Digest{}
txgroup.TxGroupHashes[i] = crypto.HashObj(txn.Txn())
stxns[i] = txn.SignedTxn()
Copy link
Contributor

@cce cce May 13, 2022

Choose a reason for hiding this comment

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

Why flip the order? (pulling txn.SignedTxn() up to happen before the ID stuff) I guess I don't understand this change

Copy link
Contributor

@jannotti jannotti May 13, 2022

Choose a reason for hiding this comment

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

I get the impression that the intent was to get things ready so that once the group was calculated it could be put into the txn objects and the stxn objects, saving a loop. I think that would work. But this change doesn't save a loop, because of the loop used to initialize stxns. I think that loop could be removed, and the initialization done in the final loop (at what would now be line 282.5). Then you would indeed have the benefit of having eliminated a loop.
(Though I should point out this is entirely test code - note sure it needs to be faster.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fewer copies. The local Txn object does a bunch of work inside .Txn() or .SignedTxn() to construct the data/transactions object. This ordering eliminates a call to .Txn() to build an object, and just uses thee data/transactions.SignedTxn to get the txid from.

@@ -752,7 +752,7 @@ func (c *Client) GroupID(txgroup []transactions.Transaction) (gid crypto.Digest,
return
}

group.TxGroupHashes = append(group.TxGroupHashes, crypto.HashObj(tx))
group.TxGroupHashes = append(group.TxGroupHashes, crypto.Digest((tx.ID())))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there an extra set of parens here?

@@ -161,6 +161,7 @@ type TxGroup struct {
// together, sequentially, in a block in order for the group to be
// valid. Each hash in the list is a hash of a transaction with
// the `Group` field omitted.
// These are all `Txid` which is equivalent to `crypto.Digest`
TxGroupHashes []crypto.Digest `codec:"txlist,allocbound=config.MaxTxGroupSize"`
Copy link
Contributor

Choose a reason for hiding this comment

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

would it make sense to make this []Txid?

@codecov
Copy link

codecov bot commented May 13, 2022

Codecov Report

Merging #3958 (7af2a68) into master (704e0b4) will increase coverage by 0.01%.
The diff coverage is 50.00%.

@@            Coverage Diff             @@
##           master    #3958      +/-   ##
==========================================
+ Coverage   49.81%   49.82%   +0.01%     
==========================================
  Files         409      409              
  Lines       68929    68929              
==========================================
+ Hits        34335    34347      +12     
+ Misses      30882    30879       -3     
+ Partials     3712     3703       -9     
Impacted Files Coverage Δ
cmd/goal/clerk.go 8.98% <0.00%> (ø)
data/transactions/transaction.go 36.15% <ø> (ø)
libgoal/transactions.go 0.00% <0.00%> (ø)
ledger/internal/eval.go 67.28% <100.00%> (ø)
node/node.go 23.19% <0.00%> (-1.85%) ⬇️
ledger/acctupdates.go 68.77% <0.00%> (-0.80%) ⬇️
catchup/service.go 68.88% <0.00%> (-0.50%) ⬇️
data/abi/abi_type.go 88.62% <0.00%> (+0.94%) ⬆️
ledger/tracker.go 73.16% <0.00%> (+3.89%) ⬆️
network/wsPeer.go 71.66% <0.00%> (+5.83%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 704e0b4...7af2a68. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Team Carbon-11 tech debt Things that need re-work for simplification / sanitization to reduce implementation overhead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants