Skip to content

Commit eba2f06

Browse files
committed
Merge pull request #6061
eb83719 Consensus: Refactor: Separate Consensus::CheckTxInputs and GetSpendHeight in CheckInputs (Jorge Timón)
2 parents 1fea667 + eb83719 commit eba2f06

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
@@ -1395,22 +1395,21 @@ bool CScriptCheck::operator()() {
13951395
return true;
13961396
}
13971397

1398-
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, std::vector<CScriptCheck> *pvChecks)
1398+
int GetSpendHeight(const CCoinsViewCache& inputs)
13991399
{
1400-
if (!tx.IsCoinBase())
1401-
{
1402-
if (pvChecks)
1403-
pvChecks->reserve(tx.vin.size());
1400+
LOCK(cs_main);
1401+
CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1402+
return pindexPrev->nHeight + 1;
1403+
}
14041404

1405+
namespace Consensus {
1406+
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
1407+
{
14051408
// This doesn't trigger the DoS code on purpose; if it did, it would make it easier
14061409
// for an attacker to attempt to split the network.
14071410
if (!inputs.HaveInputs(tx))
14081411
return state.Invalid(error("CheckInputs(): %s inputs unavailable", tx.GetHash().ToString()));
14091412

1410-
// While checking, GetBestBlock() refers to the parent block.
1411-
// This is also true for mempool checks.
1412-
CBlockIndex *pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1413-
int nSpendHeight = pindexPrev->nHeight + 1;
14141413
CAmount nValueIn = 0;
14151414
CAmount nFees = 0;
14161415
for (unsigned int i = 0; i < tx.vin.size(); i++)
@@ -1449,6 +1448,19 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
14491448
if (!MoneyRange(nFees))
14501449
return state.DoS(100, error("CheckInputs(): nFees out of range"),
14511450
REJECT_INVALID, "bad-txns-fee-outofrange");
1451+
return true;
1452+
}
1453+
}// namespace Consensus
1454+
1455+
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, std::vector<CScriptCheck> *pvChecks)
1456+
{
1457+
if (!tx.IsCoinBase())
1458+
{
1459+
if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
1460+
return false;
1461+
1462+
if (pvChecks)
1463+
pvChecks->reserve(tx.vin.size());
14521464

14531465
// The first loop above does all the inexpensive checks.
14541466
// 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
@@ -487,4 +487,11 @@ extern CCoinsViewCache *pcoinsTip;
487487
/** Global variable that points to the active block tree (protected by cs_main) */
488488
extern CBlockTreeDB *pblocktree;
489489

490+
/**
491+
* Return the spend height, which is one more than the inputs.GetBestBlock().
492+
* While checking, GetBestBlock() refers to the parent block. (protected by cs_main)
493+
* This is also true for mempool checks.
494+
*/
495+
int GetSpendHeight(const CCoinsViewCache& inputs);
496+
490497
#endif // BITCOIN_MAIN_H

0 commit comments

Comments
 (0)