Skip to content

Commit

Permalink
types: Normalize proof in (V2Transaction).ID
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Oct 11, 2023
1 parent b58e9e8 commit 6ca0ac7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
4 changes: 2 additions & 2 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ func (s State) Commitment(txnsHash types.Hash256, minerAddr types.Address) types

// InputSigHash returns the hash that must be signed for each v2 transaction input.
func (s State) InputSigHash(txn types.V2Transaction) types.Hash256 {
// NOTE: This currently covers exactly the same fields as txn.ID(), and for
// similar reasons.
// NOTE: This currently covers exactly the same fields as txn.ID(), for the
// same reasons.
h := hasherPool.Get().(*types.Hasher)
defer hasherPool.Put(h)
h.Reset()
Expand Down
11 changes: 6 additions & 5 deletions types/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func HashBytes(b []byte) Hash256 {

// A Hasher streams objects into an instance of Sia's hash function.
type Hasher struct {
h hash.Hash
E *Encoder
h hash.Hash
sum Hash256 // prevent Sum from allocating
E *Encoder
}

// Reset resets the underlying hash and encoder state.
Expand All @@ -33,15 +34,15 @@ func (h *Hasher) WriteDistinguisher(p string) {
// Sum returns the digest of the objects written to the Hasher.
func (h *Hasher) Sum() (sum Hash256) {
_ = h.E.Flush() // no error possible
h.h.Sum(sum[:0])
return
h.h.Sum(h.sum[:0])
return h.sum
}

// NewHasher returns a new Hasher instance.
func NewHasher() *Hasher {
h := blake2b.New256()
e := NewEncoder(h)
return &Hasher{h, e}
return &Hasher{h: h, E: e}
}

// Pool for reducing heap allocations when hashing. This is only necessary
Expand Down
3 changes: 0 additions & 3 deletions types/multiproof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ func TestMultiproofEncoding(t *testing.T) {
t.Fatal(err)
}
if !reflect.DeepEqual(b, b2) {
t.Log(b.Transactions[0].SiacoinInputs[0].Parent.MerkleProof)
t.Log(b2.Transactions[0].SiacoinInputs[0].Parent.MerkleProof)
t.FailNow()
t.Fatalf("multiproof encoding of %v txns did not survive roundtrip: expected %v, got %v", n, b, b2)
}
}
Expand Down
15 changes: 6 additions & 9 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,15 +712,6 @@ type V2Transaction struct {
//
// To hash all of the data in a transaction, use the FullHash method.
func (txn *V2Transaction) ID() TransactionID {
// NOTE: In general, it is not possible to change a transaction's ID without
// causing it to become invalid, but an exception exists for non-standard
// spend policies. Consider a policy that may be satisfied by either a
// signature or a timelock. If a transaction is broadcast that signs the
// input, and the timelock has expired, then anyone may remove the signature
// from the input without invalidating the transaction. Of course, the net
// result will be the same, so arguably there's little reason to care. You
// only need to worry about this if you're hashing the full transaction data
// for some reason.
h := hasherPool.Get().(*Hasher)
defer hasherPool.Put(h)
h.Reset()
Expand Down Expand Up @@ -753,6 +744,12 @@ func (txn *V2Transaction) ID() TransactionID {
h.E.WritePrefix(len(txn.FileContractResolutions))
for _, fcr := range txn.FileContractResolutions {
fcr.Parent.ID.EncodeTo(h.E)
// normalize proof
if sp, ok := fcr.Resolution.(*V2StorageProof); ok {
c := *sp // don't modify original
c.ProofIndex.MerkleProof = nil
fcr.Resolution = &c
}
fcr.Resolution.(EncoderTo).EncodeTo(h.E)
}
h.E.WritePrefix(len(txn.Attestations))
Expand Down

0 comments on commit 6ca0ac7

Please sign in to comment.