Skip to content

Commit 17f2ace

Browse files
committed
Merge #9572: Skip witness sighash cache for non-segwit transactions
0da49b5 Skip precompute sighash for transactions without witness (Johnson Lau) Pull request description: This saves unnecessary hash caching for non-segwit transactions, but I am not sure if the difference is noticeable. Tree-SHA512: 5cd733a729a52a45781510b3572b26e76837a94155caa14311c6d23a27a12e9613ff278dfc2592e21f640202782f22c5ad00fca85c4de5efacaa617c48ccb08d
2 parents 9e8ef9d + 0da49b5 commit 17f2ace

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
@@ -1168,9 +1168,13 @@ uint256 GetOutputsHash(const CTransaction& txTo) {
11681168

11691169
PrecomputedTransactionData::PrecomputedTransactionData(const CTransaction& txTo)
11701170
{
1171-
hashPrevouts = GetPrevoutHash(txTo);
1172-
hashSequence = GetSequenceHash(txTo);
1173-
hashOutputs = GetOutputsHash(txTo);
1171+
// Cache is calculated only for transactions with witness
1172+
if (txTo.HasWitness()) {
1173+
hashPrevouts = GetPrevoutHash(txTo);
1174+
hashSequence = GetSequenceHash(txTo);
1175+
hashOutputs = GetOutputsHash(txTo);
1176+
ready = true;
1177+
}
11741178
}
11751179

11761180
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
explicit PrecomputedTransactionData(const CTransaction& tx);
118119
};

0 commit comments

Comments
 (0)