Skip to content

Commit

Permalink
add cached version of HasWitness
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Jul 26, 2017
1 parent 9c01665 commit 501929d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Tx struct {
msgTx *wire.MsgTx // Underlying MsgTx
txHash *chainhash.Hash // Cached transaction hash
txHashWitness *chainhash.Hash // Cached transaction witness hash
txHasWitness *bool // If the transaction has witness data
txIndex int // Position within a block or TxIndexUnknown
}

Expand Down Expand Up @@ -64,6 +65,20 @@ func (t *Tx) WitnessHash() *chainhash.Hash {
return &hash
}

// HasWitness returns false if none of the inputs within the transaction
// contain witness data, true false otherwise. This equivalent to calling
// HasWitness on the underlying wire.MsgTx, however it caches the result so
// subsequent calls are more efficient.
func (t *Tx) HasWitness() bool {
if t.txHashWitness != nil {
return *t.txHasWitness
}

hasWitness := t.msgTx.HasWitness()
t.txHasWitness = &hasWitness
return hasWitness
}

// Index returns the saved index of the transaction within a block. This value
// will be TxIndexUnknown if it hasn't already explicitly been set.
func (t *Tx) Index() int {
Expand Down

0 comments on commit 501929d

Please sign in to comment.