Skip to content

Commit

Permalink
blockchain: wipe internal tx cache after mutating txns for fullblockt…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
Roasbeef committed Dec 5, 2023
1 parent 0924825 commit e2a7cd1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions blockchain/fullblocktests/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ func additionalCoinbase(amount btcutil.Amount) func(*wire.MsgBlock) {
// Increase the first proof-of-work coinbase subsidy by the
// provided amount.
b.Transactions[0].TxOut[0].Value += int64(amount)

b.Transactions[0].WipeCache()
}
}

Expand All @@ -402,6 +404,8 @@ func additionalSpendFee(fee btcutil.Amount) func(*wire.MsgBlock) {
fee))
}
b.Transactions[1].TxOut[0].Value -= int64(fee)

b.Transactions[1].WipeCache()
}
}

Expand All @@ -410,6 +414,8 @@ func additionalSpendFee(fee btcutil.Amount) func(*wire.MsgBlock) {
func replaceSpendScript(pkScript []byte) func(*wire.MsgBlock) {
return func(b *wire.MsgBlock) {
b.Transactions[1].TxOut[0].PkScript = pkScript

b.Transactions[1].WipeCache()
}
}

Expand All @@ -418,6 +424,8 @@ func replaceSpendScript(pkScript []byte) func(*wire.MsgBlock) {
func replaceCoinbaseSigScript(script []byte) func(*wire.MsgBlock) {
return func(b *wire.MsgBlock) {
b.Transactions[0].TxIn[0].SignatureScript = script

b.Transactions[0].WipeCache()
}
}

Expand Down
7 changes: 7 additions & 0 deletions wire/msgtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ func (msg *MsgTx) TxHash() chainhash.Hash {
return chainhash.DoubleHashH(msg.cachedSeralizedNoWitness)
}

// WipeCache removes the cached serialized bytes of the transaction. This is
// useful to be able to get the correct txid after mutating a transaction's
// state.
func (msg *MsgTx) WipeCache() {
msg.cachedSeralizedNoWitness = nil
}

// WitnessHash generates the hash of the transaction serialized according to
// the new witness serialization defined in BIP0141 and BIP0144. The final
// output is used within the Segregated Witness commitment of all the witnesses
Expand Down

0 comments on commit e2a7cd1

Please sign in to comment.