mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-15 00:59:38 +02:00
Merge #19601: Refactoring CHashWriter & Get{Prevouts,Sequence,Outputs}Hash to SHA256 (Alternative to #18071)
9ab4cafabdRefactor Get{Prevout,Sequence,Outputs}Hash to Get{Prevouts,Sequences,Outputs}SHA256. (Jeremy Rubin)6510d0ff41Add SHA256Uint256 helper functions (Jeremy Rubin)b475d7d0faAdd single sha256 call to CHashWriter (Jeremy Rubin) Pull request description: Opened as an alternative to #18071 to be more similar to #17977. I'm fine with either, deferring to others. cc jnewbery Sjors ACKs for top commit: jnewbery: Code review ACK9ab4cafabdjonatack: Tested ACK9ab4caffjahr: tested ACK9ab4cafabdinstagibbs: reACK9ab4cafabdTree-SHA512: 93a7a47697f1657f027b18407bdcce16963f6b23d12372e7ac8fd4ee96769b3e2639369f9956fee669cc881b6338641cddfeeef1516c7104cb50ef4b880bb0a7
This commit is contained in:
@@ -1258,34 +1258,37 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/** Compute the (single) SHA256 of the concatenation of all prevouts of a tx. */
|
||||
template <class T>
|
||||
uint256 GetPrevoutHash(const T& txTo)
|
||||
uint256 GetPrevoutsSHA256(const T& txTo)
|
||||
{
|
||||
CHashWriter ss(SER_GETHASH, 0);
|
||||
for (const auto& txin : txTo.vin) {
|
||||
ss << txin.prevout;
|
||||
}
|
||||
return ss.GetHash();
|
||||
return ss.GetSHA256();
|
||||
}
|
||||
|
||||
/** Compute the (single) SHA256 of the concatenation of all nSequences of a tx. */
|
||||
template <class T>
|
||||
uint256 GetSequenceHash(const T& txTo)
|
||||
uint256 GetSequencesSHA256(const T& txTo)
|
||||
{
|
||||
CHashWriter ss(SER_GETHASH, 0);
|
||||
for (const auto& txin : txTo.vin) {
|
||||
ss << txin.nSequence;
|
||||
}
|
||||
return ss.GetHash();
|
||||
return ss.GetSHA256();
|
||||
}
|
||||
|
||||
/** Compute the (single) SHA256 of the concatenation of all txouts of a tx. */
|
||||
template <class T>
|
||||
uint256 GetOutputsHash(const T& txTo)
|
||||
uint256 GetOutputsSHA256(const T& txTo)
|
||||
{
|
||||
CHashWriter ss(SER_GETHASH, 0);
|
||||
for (const auto& txout : txTo.vout) {
|
||||
ss << txout;
|
||||
}
|
||||
return ss.GetHash();
|
||||
return ss.GetSHA256();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -1297,9 +1300,9 @@ void PrecomputedTransactionData::Init(const T& txTo)
|
||||
|
||||
// Cache is calculated only for transactions with witness
|
||||
if (txTo.HasWitness()) {
|
||||
hashPrevouts = GetPrevoutHash(txTo);
|
||||
hashSequence = GetSequenceHash(txTo);
|
||||
hashOutputs = GetOutputsHash(txTo);
|
||||
hashPrevouts = SHA256Uint256(GetPrevoutsSHA256(txTo));
|
||||
hashSequence = SHA256Uint256(GetSequencesSHA256(txTo));
|
||||
hashOutputs = SHA256Uint256(GetOutputsSHA256(txTo));
|
||||
}
|
||||
|
||||
m_ready = true;
|
||||
@@ -1329,16 +1332,16 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
|
||||
const bool cacheready = cache && cache->m_ready;
|
||||
|
||||
if (!(nHashType & SIGHASH_ANYONECANPAY)) {
|
||||
hashPrevouts = cacheready ? cache->hashPrevouts : GetPrevoutHash(txTo);
|
||||
hashPrevouts = cacheready ? cache->hashPrevouts : SHA256Uint256(GetPrevoutsSHA256(txTo));
|
||||
}
|
||||
|
||||
if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
|
||||
hashSequence = cacheready ? cache->hashSequence : GetSequenceHash(txTo);
|
||||
hashSequence = cacheready ? cache->hashSequence : SHA256Uint256(GetSequencesSHA256(txTo));
|
||||
}
|
||||
|
||||
|
||||
if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
|
||||
hashOutputs = cacheready ? cache->hashOutputs : GetOutputsHash(txTo);
|
||||
hashOutputs = cacheready ? cache->hashOutputs : SHA256Uint256(GetOutputsSHA256(txTo));
|
||||
} else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) {
|
||||
CHashWriter ss(SER_GETHASH, 0);
|
||||
ss << txTo.vout[nIn];
|
||||
|
||||
Reference in New Issue
Block a user