Browse Source

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
0.16
Wladimir J. van der Laan 7 years ago
parent
commit
17f2acedbe
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
  1. 17
      src/script/interpreter.cpp
  2. 1
      src/script/interpreter.h

17
src/script/interpreter.cpp

@ -1168,9 +1168,13 @@ uint256 GetOutputsHash(const CTransaction& txTo) {
PrecomputedTransactionData::PrecomputedTransactionData(const CTransaction& txTo) PrecomputedTransactionData::PrecomputedTransactionData(const CTransaction& txTo)
{ {
hashPrevouts = GetPrevoutHash(txTo); // Cache is calculated only for transactions with witness
hashSequence = GetSequenceHash(txTo); if (txTo.HasWitness()) {
hashOutputs = GetOutputsHash(txTo); hashPrevouts = GetPrevoutHash(txTo);
hashSequence = GetSequenceHash(txTo);
hashOutputs = GetOutputsHash(txTo);
ready = true;
}
} }
uint256 SignatureHash(const CScript& scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache) 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
uint256 hashPrevouts; uint256 hashPrevouts;
uint256 hashSequence; uint256 hashSequence;
uint256 hashOutputs; uint256 hashOutputs;
const bool cacheready = cache && cache->ready;
if (!(nHashType & SIGHASH_ANYONECANPAY)) { if (!(nHashType & SIGHASH_ANYONECANPAY)) {
hashPrevouts = cache ? cache->hashPrevouts : GetPrevoutHash(txTo); hashPrevouts = cacheready ? cache->hashPrevouts : GetPrevoutHash(txTo);
} }
if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) { if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
hashSequence = cache ? cache->hashSequence : GetSequenceHash(txTo); hashSequence = cacheready ? cache->hashSequence : GetSequenceHash(txTo);
} }
if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) { if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
hashOutputs = cache ? cache->hashOutputs : GetOutputsHash(txTo); hashOutputs = cacheready ? cache->hashOutputs : GetOutputsHash(txTo);
} else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) { } else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) {
CHashWriter ss(SER_GETHASH, 0); CHashWriter ss(SER_GETHASH, 0);
ss << txTo.vout[nIn]; ss << txTo.vout[nIn];

1
src/script/interpreter.h

@ -113,6 +113,7 @@ bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned i
struct PrecomputedTransactionData struct PrecomputedTransactionData
{ {
uint256 hashPrevouts, hashSequence, hashOutputs; uint256 hashPrevouts, hashSequence, hashOutputs;
bool ready = false;
explicit PrecomputedTransactionData(const CTransaction& tx); explicit PrecomputedTransactionData(const CTransaction& tx);
}; };

Loading…
Cancel
Save