Skip to content

Commit 0da49b5

Browse files
committed
Skip precompute sighash for transactions without witness
1 parent 6696b46 commit 0da49b5

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/script/interpreter.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,13 @@ uint256 GetOutputsHash(const CTransaction& txTo) {
11701170

11711171
PrecomputedTransactionData::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

11781182
uint256 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];

src/script/interpreter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned i
113113
struct PrecomputedTransactionData
114114
{
115115
uint256 hashPrevouts, hashSequence, hashOutputs;
116+
bool ready = false;
116117

117118
PrecomputedTransactionData(const CTransaction& tx);
118119
};

0 commit comments

Comments
 (0)