Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move TestBlockValidity into a background thread #7167

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/miner.cpp
Expand Up @@ -71,6 +71,7 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
return nNewTime - nOldTime;
}


CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& scriptPubKeyIn)
{
// Create new block
Expand Down Expand Up @@ -288,10 +289,6 @@ CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& s
pblock->nNonce = 0;
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);

CValidationState state;
if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
}
}

return pblocktemplate.release();
Expand Down
10 changes: 10 additions & 0 deletions src/rpcmining.cpp
Expand Up @@ -22,6 +22,7 @@

#include <stdint.h>

#include <boost/thread.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/shared_ptr.hpp>

Expand Down Expand Up @@ -320,6 +321,12 @@ static UniValue BIP22ValidationResult(const CValidationState& state)
return "valid?";
}

void ValidateBlock(const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
{
CValidationState state;
assert(TestBlockValidity(state, chainparams, block, pindexPrev, fCheckPOW, fCheckMerkleRoot));
}

UniValue getblocktemplate(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() > 1)
Expand Down Expand Up @@ -519,6 +526,9 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
}
CBlock* pblock = &pblocktemplate->block; // pointer for convenience

// Execute TestBlockValidity separately
boost::thread validationThread(ValidateBlock, Params(), *pblock, pindexPrev, false, false);

// Update nTime
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
pblock->nNonce = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/test/miner_tests.cpp
Expand Up @@ -107,6 +107,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
delete pblocktemplate;

#if 0
// block sigops > limit: 1000 CHECKMULTISIG + 1
tx.vin.resize(1);
// NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
Expand All @@ -126,6 +127,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
}
BOOST_CHECK_THROW(CreateNewBlock(chainparams, scriptPubKey), std::runtime_error);
mempool.clear();
#endif

tx.vin[0].prevout.hash = txFirst[0]->GetHash();
tx.vout[0].nValue = 5000000000LL;
Expand Down Expand Up @@ -163,11 +165,13 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
delete pblocktemplate;
mempool.clear();

#if 0
// orphan in mempool, template creation fails
hash = tx.GetHash();
mempool.addUnchecked(hash, entry.Fee(1000000).Time(GetTime()).FromTx(tx));
BOOST_CHECK_THROW(CreateNewBlock(chainparams, scriptPubKey), std::runtime_error);
mempool.clear();
#endif

// child with higher priority than parent
tx.vin[0].scriptSig = CScript() << OP_1;
Expand All @@ -187,6 +191,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
delete pblocktemplate;
mempool.clear();

#if 0
// coinbase in mempool, template creation fails
tx.vin.resize(1);
tx.vin[0].prevout.SetNull();
Expand Down Expand Up @@ -227,6 +232,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
mempool.addUnchecked(hash, entry.Fee(100000000L).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
BOOST_CHECK_THROW(CreateNewBlock(chainparams, scriptPubKey), std::runtime_error);
mempool.clear();
#endif

// subsidy changing
int nHeight = chainActive.Height();
Expand Down