Skip to content

Commit eb83719

Browse files
committed
Consensus: Refactor: Separate Consensus::CheckTxInputs and GetSpendHeight in CheckInputs
1 parent b6ea3bc commit eb83719

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/main.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,22 +1379,21 @@ bool CScriptCheck::operator()() {
13791379
return true;
13801380
}
13811381

1382-
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, std::vector<CScriptCheck> *pvChecks)
1382+
int GetSpendHeight(const CCoinsViewCache& inputs)
13831383
{
1384-
if (!tx.IsCoinBase())
1385-
{
1386-
if (pvChecks)
1387-
pvChecks->reserve(tx.vin.size());
1384+
LOCK(cs_main);
1385+
CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1386+
return pindexPrev->nHeight + 1;
1387+
}
13881388

1389+
namespace Consensus {
1390+
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
1391+
{
13891392
// This doesn't trigger the DoS code on purpose; if it did, it would make it easier
13901393
// for an attacker to attempt to split the network.
13911394
if (!inputs.HaveInputs(tx))
13921395
return state.Invalid(error("CheckInputs(): %s inputs unavailable", tx.GetHash().ToString()));
13931396

1394-
// While checking, GetBestBlock() refers to the parent block.
1395-
// This is also true for mempool checks.
1396-
CBlockIndex *pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1397-
int nSpendHeight = pindexPrev->nHeight + 1;
13981397
CAmount nValueIn = 0;
13991398
CAmount nFees = 0;
14001399
for (unsigned int i = 0; i < tx.vin.size(); i++)
@@ -1433,6 +1432,19 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
14331432
if (!MoneyRange(nFees))
14341433
return state.DoS(100, error("CheckInputs(): nFees out of range"),
14351434
REJECT_INVALID, "bad-txns-fee-outofrange");
1435+
return true;
1436+
}
1437+
}// namespace Consensus
1438+
1439+
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, std::vector<CScriptCheck> *pvChecks)
1440+
{
1441+
if (!tx.IsCoinBase())
1442+
{
1443+
if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
1444+
return false;
1445+
1446+
if (pvChecks)
1447+
pvChecks->reserve(tx.vin.size());
14361448

14371449
// The first loop above does all the inexpensive checks.
14381450
// Only if ALL inputs pass do we perform expensive ECDSA signature checks.

src/main.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,4 +507,11 @@ extern CCoinsViewCache *pcoinsTip;
507507
/** Global variable that points to the active block tree (protected by cs_main) */
508508
extern CBlockTreeDB *pblocktree;
509509

510+
/**
511+
* Return the spend height, which is one more than the inputs.GetBestBlock().
512+
* While checking, GetBestBlock() refers to the parent block. (protected by cs_main)
513+
* This is also true for mempool checks.
514+
*/
515+
int GetSpendHeight(const CCoinsViewCache& inputs);
516+
510517
#endif // BITCOIN_MAIN_H

0 commit comments

Comments
 (0)