@@ -1170,9 +1170,13 @@ uint256 GetOutputsHash(const CTransaction& txTo) {
11701170
11711171PrecomputedTransactionData::PrecomputedTransactionData (const CTransaction& txTo)
11721172{
1173- hashPrevouts = GetPrevoutHash (txTo);
1174- hashSequence = GetSequenceHash (txTo);
1175- hashOutputs = GetOutputsHash (txTo);
1173+ // Cache is calculated only for transactions with witness
1174+ if (txTo.HasWitness ()) {
1175+ hashPrevouts = GetPrevoutHash (txTo);
1176+ hashSequence = GetSequenceHash (txTo);
1177+ hashOutputs = GetOutputsHash (txTo);
1178+ ready = true ;
1179+ }
11761180}
11771181
11781182uint256 SignatureHash (const CScript& scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache)
@@ -1181,18 +1185,19 @@ uint256 SignatureHash(const CScript& scriptCode, const CTransaction& txTo, unsig
11811185 uint256 hashPrevouts;
11821186 uint256 hashSequence;
11831187 uint256 hashOutputs;
1188+ const bool cacheready = cache && cache->ready ;
11841189
11851190 if (!(nHashType & SIGHASH_ANYONECANPAY)) {
1186- hashPrevouts = cache ? cache->hashPrevouts : GetPrevoutHash (txTo);
1191+ hashPrevouts = cacheready ? cache->hashPrevouts : GetPrevoutHash (txTo);
11871192 }
11881193
11891194 if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f ) != SIGHASH_SINGLE && (nHashType & 0x1f ) != SIGHASH_NONE) {
1190- hashSequence = cache ? cache->hashSequence : GetSequenceHash (txTo);
1195+ hashSequence = cacheready ? cache->hashSequence : GetSequenceHash (txTo);
11911196 }
11921197
11931198
11941199 if ((nHashType & 0x1f ) != SIGHASH_SINGLE && (nHashType & 0x1f ) != SIGHASH_NONE) {
1195- hashOutputs = cache ? cache->hashOutputs : GetOutputsHash (txTo);
1200+ hashOutputs = cacheready ? cache->hashOutputs : GetOutputsHash (txTo);
11961201 } else if ((nHashType & 0x1f ) == SIGHASH_SINGLE && nIn < txTo.vout .size ()) {
11971202 CHashWriter ss (SER_GETHASH, 0 );
11981203 ss << txTo.vout [nIn];
0 commit comments