Skip to content

Commit

Permalink
Merge #2373: [Refactor] Combine scriptPubKey and amount as CTxOut in …
Browse files Browse the repository at this point in the history
…CScriptCheck

2af6794 Rename out to m_tx_out in CScriptCheck (Johnson Lau)
5f6cd00 [Refactor] Combine scriptPubKey and amount as CTxOut in CScriptCheck (Johnson Lau)

Pull request description:

  Straightforward. Coming from bitcoin#10953

  > This simplifies CScriptCheck by combining scriptPubKey and amount

ACKs for top commit:
  furszy:
    ACK 2af6794

Tree-SHA512: dbc2358315d2bbcd6313dfd41eb28c1a364e9b0b88f6b22c4b36ef3784849ccd2239fd035a784846f1ecbf90d5511d908f1df384c351c1d22d86bbb7da5d28ad
  • Loading branch information
furszy committed May 26, 2021
2 parents 6e98b23 + 2af6794 commit 2f0d2d0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/test/script_P2SH_tests.cpp
Expand Up @@ -113,8 +113,7 @@ BOOST_AUTO_TEST_CASE(sign)
for (int j = 0; j < 8; j++) {
CScript sigSave = txTo[i].vin[0].scriptSig;
txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
const CTxOut& output = txFrom.vout[txTo[i].vin[0].prevout.n];
bool sigOK = CScriptCheck(output.scriptPubKey, output.nValue, txTo[i], 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC,
bool sigOK = CScriptCheck(txFrom.vout[txTo[i].vin[0].prevout.n], txTo[i], 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC,
false, &precomTxData)();
if (i == j)
BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
Expand Down
3 changes: 1 addition & 2 deletions src/test/transaction_tests.cpp
Expand Up @@ -390,8 +390,7 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction) {

for(uint32_t i = 0; i < mtx.vin.size(); i++) {
std::vector<CScriptCheck> vChecks;
const CTxOut& output = coins[tx.vin[i].prevout.n].out;
CScriptCheck check(output.scriptPubKey, output.nValue, tx, i, SCRIPT_VERIFY_P2SH, false, &precomTxData);
CScriptCheck check(coins[tx.vin[i].prevout.n].out, tx, i, SCRIPT_VERIFY_P2SH, false, &precomTxData);
vChecks.emplace_back();
check.swap(vChecks.back());
control.Add(vChecks);
Expand Down
8 changes: 3 additions & 5 deletions src/validation.cpp
Expand Up @@ -1070,7 +1070,7 @@ void UpdateCoins(const CTransaction& tx, CCoinsViewCache &inputs, int nHeight, b
bool CScriptCheck::operator()()
{
const CScript& scriptSig = ptxTo->vin[nIn].scriptSig;
return VerifyScript(scriptSig, scriptPubKey, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, amount, cacheStore, *precomTxData), ptxTo->GetRequiredSigVersion(), &error);
return VerifyScript(scriptSig, m_tx_out.scriptPubKey, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, m_tx_out.nValue, cacheStore, *precomTxData), ptxTo->GetRequiredSigVersion(), &error);
}

int GetSpendHeight(const CCoinsViewCache& inputs)
Expand Down Expand Up @@ -1161,11 +1161,9 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
// a sanity check that our caching is not introducing consensus
// failures through additional data in, eg, the coins being
// spent being checked as a part of CScriptCheck.
const CScript& scriptPubKey = coin.out.scriptPubKey;
const CAmount amount = coin.out.nValue;

// Verify signature
CScriptCheck check(scriptPubKey, amount, tx, i, flags, cacheStore, &precomTxData);
CScriptCheck check(coin.out, tx, i, flags, cacheStore, &precomTxData);
if (pvChecks) {
pvChecks->emplace_back();
check.swap(pvChecks->back());
Expand All @@ -1177,7 +1175,7 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
// arguments; if so, don't trigger DoS protection to
// avoid splitting the network between upgraded and
// non-upgraded nodes.
CScriptCheck check2(scriptPubKey, amount, tx, i,
CScriptCheck check2(coin.out, tx, i,
flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheStore, &precomTxData);
if (check2())
return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
Expand Down
13 changes: 5 additions & 8 deletions src/validation.h
Expand Up @@ -281,8 +281,7 @@ Optional<int> GetUTXOHeight(const COutPoint& outpoint);
class CScriptCheck
{
private:
CScript scriptPubKey;
CAmount amount;
CTxOut m_tx_out;
const CTransaction* ptxTo;
unsigned int nIn;
unsigned int nFlags;
Expand All @@ -291,10 +290,9 @@ class CScriptCheck
PrecomputedTransactionData *precomTxData;

public:
CScriptCheck() : amount(0), ptxTo(0), nIn(0), nFlags(0), cacheStore(false), error(SCRIPT_ERR_UNKNOWN_ERROR), precomTxData(nullptr) {}
CScriptCheck(const CScript& scriptPubKeyIn, const CAmount amountIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn, bool cacheIn, PrecomputedTransactionData* cachedHashesIn) :
scriptPubKey(scriptPubKeyIn),
amount(amountIn),
CScriptCheck() : ptxTo(0), nIn(0), nFlags(0), cacheStore(false), error(SCRIPT_ERR_UNKNOWN_ERROR), precomTxData(nullptr) {}
CScriptCheck(const CTxOut& outIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn, bool cacheIn, PrecomputedTransactionData* cachedHashesIn) :
m_tx_out(outIn),
ptxTo(&txToIn),
nIn(nInIn),
nFlags(nFlagsIn),
Expand All @@ -306,9 +304,8 @@ class CScriptCheck

void swap(CScriptCheck& check)
{
scriptPubKey.swap(check.scriptPubKey);
std::swap(ptxTo, check.ptxTo);
std::swap(amount, check.amount);
std::swap(m_tx_out, check.m_tx_out);
std::swap(nIn, check.nIn);
std::swap(nFlags, check.nFlags);
std::swap(cacheStore, check.cacheStore);
Expand Down

0 comments on commit 2f0d2d0

Please sign in to comment.