Skip to content

Commit

Permalink
Speedup Consensus::CheckTxInputs by removing a redundant coin lookup
Browse files Browse the repository at this point in the history
Co-authored-by: matricz <811338-matricz@users.noreply.gitlab.com>

We lookup the coins in HaveInputs, but then we look them up again in
the next for() loop.  Avoid looking up the map twice. This should speed
up checking blocks.
  • Loading branch information
cculianu committed Jun 24, 2021
1 parent aff593f commit 837452d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/consensus/tx_verify.cpp
Expand Up @@ -156,18 +156,19 @@ namespace Consensus {
bool CheckTxInputs(const CTransaction &tx, CValidationState &state,
const CCoinsViewCache &inputs, int nSpendHeight,
Amount &txfee) {
// are the actual inputs available?
if (!inputs.HaveInputs(tx)) {
return state.DoS(100, false, REJECT_INVALID,
"bad-txns-inputs-missingorspent", false,
strprintf("%s: inputs missing/spent", __func__));
}
assert(!tx.IsCoinBase()); // precondition of this function

Amount nValueIn = Amount::zero();
for (const auto &in : tx.vin) {
const COutPoint &prevout = in.prevout;
const Coin &coin = inputs.AccessCoin(prevout);
assert(!coin.IsSpent());

// Is the actual input available?
if (coin.IsSpent()) {
return state.DoS(100, false, REJECT_INVALID,
"bad-txns-inputs-missingorspent", false,
strprintf("%s: inputs missing/spent", __func__));
}

// If prev is coinbase, check that it's matured
if (coin.IsCoinBase() &&
Expand Down

0 comments on commit 837452d

Please sign in to comment.