Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions msg_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ import (
"github.com/davecgh/go-spew/spew"
)

// clearBlockTxCaches clears cached hashes on all transactions in a block
// so that reflect.DeepEqual comparisons are not affected by cache state.
func clearBlockTxCaches(b *MsgBlock) {
for _, tx := range b.Transactions {
tx.cachedHash = nil
}
}

// TestBlock tests the MsgBlock API.
func TestBlock(t *testing.T) {
pver := ProtocolVersion
Expand Down Expand Up @@ -65,8 +57,6 @@ func TestBlock(t *testing.T) {
tx := blockOne.Transactions[0].Copy()
_ = msg.AddTransaction(tx)

clearBlockTxCaches(msg)
clearBlockTxCaches(&blockOne)
if !reflect.DeepEqual(msg.Transactions, blockOne.Transactions) {
t.Errorf("AddTransaction: wrong transactions - got %v, want %v",
spew.Sdump(msg.Transactions),
Expand Down Expand Up @@ -217,8 +207,6 @@ func TestBlockWire(t *testing.T) {
continue
}

clearBlockTxCaches(&msg)
clearBlockTxCaches(test.out)
if !reflect.DeepEqual(&msg, test.out) {
t.Errorf("Bsvdecode #%d\n got: %s want: %s", i,
spew.Sdump(&msg), spew.Sdump(test.out))
Expand Down Expand Up @@ -334,8 +322,6 @@ func TestBlockSerialize(t *testing.T) {
continue
}

clearBlockTxCaches(&block)
clearBlockTxCaches(test.out)
if !reflect.DeepEqual(&block, test.out) {
t.Errorf("Deserialize #%d\n got: %s want: %s", i,
spew.Sdump(&block), spew.Sdump(test.out))
Expand All @@ -354,8 +340,6 @@ func TestBlockSerialize(t *testing.T) {
continue
}

clearBlockTxCaches(&txLocBlock)
clearBlockTxCaches(test.out)
if !reflect.DeepEqual(&txLocBlock, test.out) {
t.Errorf("DeserializeTxLoc #%d\n got: %s want: %s", i,
spew.Sdump(&txLocBlock), spew.Sdump(test.out))
Expand Down
23 changes: 7 additions & 16 deletions msg_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,41 +249,32 @@ func NewTxOut(value int64, pkScript []byte) *TxOut {
// Use the AddTxIn and AddTxOut functions to build up the list of transaction
// inputs and outputs.
type MsgTx struct {
Version int32
TxIn []*TxIn
TxOut []*TxOut
LockTime uint32
cachedHash *chainhash.Hash // lazily computed and cached tx hash
Version int32
TxIn []*TxIn
TxOut []*TxOut
LockTime uint32
}

// AddTxIn adds a transaction input to the message.
func (msg *MsgTx) AddTxIn(ti *TxIn) {
msg.TxIn = append(msg.TxIn, ti)
msg.cachedHash = nil
}

// AddTxOut adds a transaction output to the message.
func (msg *MsgTx) AddTxOut(to *TxOut) {
msg.TxOut = append(msg.TxOut, to)
msg.cachedHash = nil
}

// TxHash generates the Hash for the transaction. The result is cached so that
// subsequent calls do not re-serialize the transaction.
// TxHash generates the Hash for the transaction by serializing directly into
// a SHA-256 hasher, avoiding intermediate buffer allocations.
func (msg *MsgTx) TxHash() chainhash.Hash {
if msg.cachedHash != nil {
return *msg.cachedHash
}

// Serialize directly into a SHA-256 hasher to avoid allocating an
// intermediate buffer. Double SHA-256 is computed as sha256(sha256(tx)).
h := sha256.New()
_ = msg.Serialize(h)
var first [32]byte
h.Sum(first[:0])
hash := chainhash.Hash(sha256.Sum256(first[:]))
msg.cachedHash = &hash
return hash
return chainhash.Hash(sha256.Sum256(first[:]))
}

// Copy creates a deep copy of a transaction so that the original does not get
Expand Down
6 changes: 0 additions & 6 deletions msg_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ func TestTx(t *testing.T) {

// Ensure the copy produced an identical transaction message.
newMsg := msg.Copy()
msg.cachedHash = nil // Clear cache for comparison; Copy() doesn't copy it
newMsg.cachedHash = nil
if !reflect.DeepEqual(newMsg, msg) {
t.Errorf("Copy: mismatched tx messages - got %v, want %v",
spew.Sdump(newMsg), spew.Sdump(msg))
Expand Down Expand Up @@ -376,8 +374,6 @@ func TestTxWire(t *testing.T) {
continue
}

msg.cachedHash = nil
test.out.cachedHash = nil
if !reflect.DeepEqual(&msg, test.out) {
t.Errorf("Bsvdecode #%d\n got: %s want: %s", i,
spew.Sdump(&msg), spew.Sdump(test.out))
Expand Down Expand Up @@ -519,8 +515,6 @@ func TestTxSerialize(t *testing.T) {
continue
}

tx.cachedHash = nil
test.out.cachedHash = nil
if !reflect.DeepEqual(&tx, test.out) {
t.Errorf("Deserialize #%d\n got: %s want: %s", i,
spew.Sdump(&tx), spew.Sdump(test.out))
Expand Down