From e2a7cd16099be1a082ed78ada29bf503c5864735 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 4 Dec 2023 18:44:39 -0800 Subject: [PATCH] blockchain: wipe internal tx cache after mutating txns for fullblocktests --- blockchain/fullblocktests/generate.go | 8 ++++++++ wire/msgtx.go | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/blockchain/fullblocktests/generate.go b/blockchain/fullblocktests/generate.go index 4c551c05e0..963926e781 100644 --- a/blockchain/fullblocktests/generate.go +++ b/blockchain/fullblocktests/generate.go @@ -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() } } @@ -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() } } @@ -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() } } @@ -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() } } diff --git a/wire/msgtx.go b/wire/msgtx.go index 21e191d37f..58668b7eca 100644 --- a/wire/msgtx.go +++ b/wire/msgtx.go @@ -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