Near-Bugfix: Optimization: Minimize the number of times it is checked that no money... #8498
Open
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cac4df7
Proper indentation for CheckTxInputs and other minor style fixes
jtimon 58dedef
Optimization: Minimize the number of times it is checked that no mone…
jtimon faad3e3
Introduce CheckInputsAndUpdateCoins wrapper
jtimon 736cf73
Near-Bugfix: Reestablish consensus check removed in 8d7849b
jtimon
Jump to file or symbol
Failed to load files and symbols.
| @@ -484,7 +484,6 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool | ||
| CCoinsView dummy; | ||
| CCoinsViewCache view(&dummy); | ||
| - CAmount nValueIn = 0; | ||
| LockPoints lp; | ||
| { | ||
| LOCK(pool.cs); | ||
| @@ -519,8 +518,6 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool | ||
| // Bring the best block into scope | ||
| view.GetBestBlock(); | ||
| - nValueIn = view.GetValueIn(tx); | ||
| - | ||
| // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool | ||
| view.SetBackend(dummy); | ||
| @@ -531,6 +528,12 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool | ||
| // CoinsViewCache instead of create its own | ||
| if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp)) | ||
| return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final"); | ||
| + | ||
| + } // end LOCK(pool.cs) | ||
| + | ||
| + CAmount nFees = 0; | ||
| + if (!Consensus::CheckTxInputs(tx, state, view, GetSpendHeight(view), nFees)) { | ||
| + return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state)); | ||
| } | ||
| // Check for non-standard pay-to-script-hash in inputs | ||
| @@ -543,8 +546,6 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool | ||
| int64_t nSigOpsCost = GetTransactionSigOpCost(tx, view, STANDARD_SCRIPT_VERIFY_FLAGS); | ||
| - CAmount nValueOut = tx.GetValueOut(); | ||
| - CAmount nFees = nValueIn-nValueOut; | ||
| // nModifiedFees includes any fee deltas from PrioritiseTransaction | ||
| CAmount nModifiedFees = nFees; | ||
| pool.ApplyDelta(hash, nModifiedFees); | ||
| @@ -1161,9 +1162,6 @@ static bool CheckInputs(const CTransaction& tx, CValidationState &state, const C | ||
| { | ||
| if (!tx.IsCoinBase()) | ||
| { | ||
| - if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs))) | ||
| - return false; | ||
| - | ||
| if (pvChecks) | ||
| pvChecks->reserve(tx.vin.size()); | ||
| @@ -1635,9 +1633,15 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd | ||
| if (!tx.IsCoinBase()) | ||
| { | ||
| - if (!view.HaveInputs(tx)) | ||
| - return state.DoS(100, error("ConnectBlock(): inputs missing/spent"), | ||
| - REJECT_INVALID, "bad-txns-inputs-missingorspent"); | ||
| + CAmount tx_fees = 0; | ||
| + if (!Consensus::CheckTxInputs(tx, state, view, pindex->nHeight, tx_fees)) { | ||
| + return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state)); | ||
| + } | ||
| + nFees += tx_fees; | ||
TheBlueMatt
Contributor
|
||
| + if (!MoneyRange(nFees)) { | ||
| + return state.DoS(100, error("%s: accumulated fee in the block out of range.", __func__), | ||
| + REJECT_INVALID, "bad-txns-accumulated-fee-outofrange"); | ||
| + } | ||
| // Check that transaction is BIP68 final | ||
| // BIP68 lock checks (as opposed to nLockTime checks) must | ||
| @@ -1665,8 +1669,6 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd | ||
| txdata.emplace_back(tx); | ||
| if (!tx.IsCoinBase()) | ||
| { | ||
| - nFees += view.GetValueIn(tx)-tx.GetValueOut(); | ||
| - | ||
| std::vector<CScriptCheck> vChecks; | ||
| bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */ | ||
| if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : NULL)) | ||
While you're at it, can you please re-add the MoneyRange checks here that Gavin removed years ago (and maybe update the PR title to indicate that you're fixing Gavin's near-bug while also decreasing the number of CCoinsView map lookups).