add cached version of HasWitness

This commit is contained in:
Olaoluwa Osuntokun 2017-07-01 17:39:31 -07:00
parent 9c01665307
commit 501929d3d0
No known key found for this signature in database
GPG key ID: 9CC5B105D03521A2

15
tx.go
View file

@ -25,6 +25,7 @@ type Tx struct {
msgTx *wire.MsgTx // Underlying MsgTx msgTx *wire.MsgTx // Underlying MsgTx
txHash *chainhash.Hash // Cached transaction hash txHash *chainhash.Hash // Cached transaction hash
txHashWitness *chainhash.Hash // Cached transaction witness hash txHashWitness *chainhash.Hash // Cached transaction witness hash
txHasWitness *bool // If the transaction has witness data
txIndex int // Position within a block or TxIndexUnknown txIndex int // Position within a block or TxIndexUnknown
} }
@ -64,6 +65,20 @@ func (t *Tx) WitnessHash() *chainhash.Hash {
return &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 // Index returns the saved index of the transaction within a block. This value
// will be TxIndexUnknown if it hasn't already explicitly been set. // will be TxIndexUnknown if it hasn't already explicitly been set.
func (t *Tx) Index() int { func (t *Tx) Index() int {