Page MenuHomePhabricator

D7988.diff
No OneTemporary

D7988.diff

diff --git a/src/bench/bench.h b/src/bench/bench.h
--- a/src/bench/bench.h
+++ b/src/bench/bench.h
@@ -14,6 +14,10 @@
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>
+struct RegTestingSetup;
+//! A pointer to the current testing setup
+extern const RegTestingSetup *g_testing_setup;
+
// Simple micro-benchmarking framework; API mostly matches a subset of the
// Google Benchmark framework (see https://github.com/google/benchmark)
// Why not use the Google Benchmark framework? Because adding Yet Another
diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp
--- a/src/bench/bench.cpp
+++ b/src/bench/bench.cpp
@@ -16,6 +16,8 @@
#include <numeric>
#include <regex>
+const RegTestingSetup *g_testing_setup = nullptr;
+
void benchmark::ConsolePrinter::header() {
std::cout << "# Benchmark, evals, iterations, total, min, max, median"
<< std::endl;
@@ -149,6 +151,8 @@
for (const auto &p : benchmarks()) {
RegTestingSetup test{};
+ assert(g_testing_setup == nullptr);
+ g_testing_setup = &test;
assert(::ChainActive().Height() == 0);
if (!std::regex_match(p.first, baseMatch, reFilter)) {
@@ -165,6 +169,7 @@
p.second.func(state);
}
printer.result(state);
+ g_testing_setup = nullptr;
}
printer.footer();
diff --git a/src/bench/block_assemble.cpp b/src/bench/block_assemble.cpp
--- a/src/bench/block_assemble.cpp
+++ b/src/bench/block_assemble.cpp
@@ -7,6 +7,7 @@
#include <consensus/validation.h>
#include <script/standard.h>
#include <test/util/mining.h>
+#include <test/util/setup_common.h>
#include <test/util/wallet.h>
#include <txmempool.h>
#include <validation.h>
@@ -30,7 +31,8 @@
std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
for (size_t b = 0; b < NUM_BLOCKS; ++b) {
CMutableTransaction tx;
- tx.vin.push_back(MineBlock(config, SCRIPT_PUB));
+ tx.vin.push_back(
+ MineBlock(config, g_testing_setup->m_node, SCRIPT_PUB));
tx.vin.back().scriptSig = scriptSig;
tx.vout.emplace_back(1337 * SATOSHI, SCRIPT_PUB);
if (NUM_BLOCKS - b >= COINBASE_MATURITY) {
@@ -52,7 +54,7 @@
}
while (state.KeepRunning()) {
- PrepareBlock(config, SCRIPT_PUB);
+ PrepareBlock(config, g_testing_setup->m_node, SCRIPT_PUB);
}
}
diff --git a/src/bench/wallet_balance.cpp b/src/bench/wallet_balance.cpp
--- a/src/bench/wallet_balance.cpp
+++ b/src/bench/wallet_balance.cpp
@@ -8,6 +8,7 @@
#include <node/context.h>
#include <optional.h>
#include <test/util/mining.h>
+#include <test/util/setup_common.h>
#include <test/util/wallet.h>
#include <validationinterface.h>
#include <wallet/wallet.h>
@@ -41,8 +42,9 @@
}
for (int i = 0; i < 100; ++i) {
- generatetoaddress(config, address_mine.get_value_or(ADDRESS_WATCHONLY));
- generatetoaddress(config, ADDRESS_WATCHONLY);
+ generatetoaddress(config, g_testing_setup->m_node,
+ address_mine.get_value_or(ADDRESS_WATCHONLY));
+ generatetoaddress(config, g_testing_setup->m_node, ADDRESS_WATCHONLY);
}
SyncWithValidationInterfaceQueue();
diff --git a/src/miner.h b/src/miner.h
--- a/src/miner.h
+++ b/src/miner.h
@@ -155,7 +155,7 @@
int64_t nMedianTimePast;
const CChainParams &chainParams;
- const CTxMemPool *mempool;
+ const CTxMemPool &m_mempool;
public:
struct Options {
@@ -165,8 +165,8 @@
CFeeRate blockMinFeeRate;
};
- BlockAssembler(const Config &config, const CTxMemPool &_mempool);
- BlockAssembler(const CChainParams &params, const CTxMemPool &_mempool,
+ BlockAssembler(const Config &config, const CTxMemPool &mempool);
+ BlockAssembler(const CChainParams &params, const CTxMemPool &mempool,
const Options &options);
/** Construct a new block template with coinbase to scriptPubKeyIn */
@@ -192,7 +192,7 @@
* statistics from the package selection (for logging statistics).
*/
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
- EXCLUSIVE_LOCKS_REQUIRED(mempool->cs);
+ EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
// helper functions for addPackageTxs()
/** Remove confirmed (inBlock) entries from given set */
@@ -213,7 +213,7 @@
bool SkipMapTxEntry(CTxMemPool::txiter it,
indexed_modified_transaction_set &mapModifiedTx,
CTxMemPool::setEntries &failedTx)
- EXCLUSIVE_LOCKS_REQUIRED(mempool->cs);
+ EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
/** Sort the package in an order that is valid to appear in a block */
void SortForBlock(const CTxMemPool::setEntries &package,
std::vector<CTxMemPool::txiter> &sortedEntries);
@@ -224,7 +224,7 @@
*/
int UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded,
indexed_modified_transaction_set &mapModifiedTx)
- EXCLUSIVE_LOCKS_REQUIRED(mempool->cs);
+ EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
};
/** Modify the extranonce in a block */
diff --git a/src/miner.cpp b/src/miner.cpp
--- a/src/miner.cpp
+++ b/src/miner.cpp
@@ -59,9 +59,9 @@
blockMinFeeRate(DEFAULT_BLOCK_MIN_TX_FEE_PER_KB) {}
BlockAssembler::BlockAssembler(const CChainParams &params,
- const CTxMemPool &_mempool,
+ const CTxMemPool &mempool,
const Options &options)
- : chainParams(params), mempool(&_mempool) {
+ : chainParams(params), m_mempool(mempool) {
blockMinFeeRate = options.blockMinFeeRate;
// Limit size to between 1K and options.nExcessiveBlockSize -1K for sanity:
nMaxGeneratedBlockSize = std::max<uint64_t>(
@@ -100,9 +100,9 @@
return options;
}
-BlockAssembler::BlockAssembler(const Config &config, const CTxMemPool &_mempool)
- : BlockAssembler(config.GetChainParams(), _mempool,
- DefaultOptions(config)) {}
+BlockAssembler::BlockAssembler(const Config &config, const CTxMemPool &mempool)
+ : BlockAssembler(config.GetChainParams(), mempool, DefaultOptions(config)) {
+}
void BlockAssembler::resetBlock() {
inBlock.clear();
@@ -136,7 +136,7 @@
// Add dummy coinbase tx as first transaction. It is updated at the end.
pblocktemplate->entries.emplace_back(CTransactionRef(), -SATOSHI, -1);
- LOCK2(cs_main, mempool->cs);
+ LOCK2(cs_main, m_mempool.cs);
CBlockIndex *pindexPrev = ::ChainActive().Tip();
assert(pindexPrev != nullptr);
nHeight = pindexPrev->nHeight + 1;
@@ -322,7 +322,7 @@
int nDescendantsUpdated = 0;
for (CTxMemPool::txiter it : alreadyAdded) {
CTxMemPool::setEntries descendants;
- mempool->CalculateDescendants(it, descendants);
+ m_mempool.CalculateDescendants(it, descendants);
// Insert all descendants (not yet in block) into the modified set.
for (CTxMemPool::txiter desc : descendants) {
if (alreadyAdded.count(desc)) {
@@ -357,7 +357,7 @@
bool BlockAssembler::SkipMapTxEntry(
CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx,
CTxMemPool::setEntries &failedTx) {
- assert(it != mempool->mapTx.end());
+ assert(it != m_mempool.mapTx.end());
return mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it);
}
@@ -404,7 +404,7 @@
UpdatePackagesForAdded(inBlock, mapModifiedTx);
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator
- mi = mempool->mapTx.get<ancestor_score>().begin();
+ mi = m_mempool.mapTx.get<ancestor_score>().begin();
CTxMemPool::txiter iter;
// Limit the number of attempts to add transactions to the block when it is
@@ -413,11 +413,11 @@
const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
int64_t nConsecutiveFailed = 0;
- while (mi != mempool->mapTx.get<ancestor_score>().end() ||
+ while (mi != m_mempool.mapTx.get<ancestor_score>().end() ||
!mapModifiedTx.empty()) {
// First try to find a new transaction in mapTx to evaluate.
- if (mi != mempool->mapTx.get<ancestor_score>().end() &&
- SkipMapTxEntry(mempool->mapTx.project<0>(mi), mapModifiedTx,
+ if (mi != m_mempool.mapTx.get<ancestor_score>().end() &&
+ SkipMapTxEntry(m_mempool.mapTx.project<0>(mi), mapModifiedTx,
failedTx)) {
++mi;
continue;
@@ -428,13 +428,13 @@
bool fUsingModified = false;
modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
- if (mi == mempool->mapTx.get<ancestor_score>().end()) {
+ if (mi == m_mempool.mapTx.get<ancestor_score>().end()) {
// We're out of entries in mapTx; use the entry from mapModifiedTx
iter = modit->iter;
fUsingModified = true;
} else {
// Try to compare the mapTx entry to the mapModifiedTx entry.
- iter = mempool->mapTx.project<0>(mi);
+ iter = m_mempool.mapTx.project<0>(mi);
if (modit != mapModifiedTx.get<ancestor_score>().end() &&
CompareTxMemPoolEntryByAncestorFee()(
*modit, CTxMemPoolModifiedEntry(iter))) {
@@ -503,8 +503,9 @@
CTxMemPool::setEntries ancestors;
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
std::string dummy;
- mempool->CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit,
- nNoLimit, nNoLimit, dummy, false);
+ m_mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit,
+ nNoLimit, nNoLimit, nNoLimit, dummy,
+ false);
onlyUnconfirmed(ancestors);
ancestors.insert(iter);
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -117,7 +117,7 @@
!request.params[1].isNull() ? request.params[1].get_int() : -1);
}
-static UniValue generateBlocks(const Config &config,
+static UniValue generateBlocks(const Config &config, const CTxMemPool &mempool,
const CScript &coinbase_script, int nGenerate,
uint64_t nMaxTries) {
int nHeightEnd = 0;
@@ -132,8 +132,6 @@
const uint64_t nExcessiveBlockSize = config.GetMaxBlockSize();
- const CTxMemPool &mempool = EnsureMemPool();
-
unsigned int nExtraNonce = 0;
UniValue blockHashes(UniValue::VARR);
while (nHeight < nHeightEnd && !ShutdownRequested()) {
@@ -227,9 +225,12 @@
strprintf("Cannot derive script without private keys"));
}
+ const CTxMemPool &mempool = EnsureMemPool();
+
CHECK_NONFATAL(coinbase_script.size() == 1);
- return generateBlocks(config, coinbase_script.at(0), num_blocks, max_tries);
+ return generateBlocks(config, mempool, coinbase_script.at(0), num_blocks,
+ max_tries);
}
static UniValue generatetoaddress(const Config &config,
@@ -269,9 +270,12 @@
"Error: Invalid address");
}
+ const CTxMemPool &mempool = EnsureMemPool();
+
CScript coinbase_script = GetScriptForDestination(destination);
- return generateBlocks(config, coinbase_script, nGenerate, nMaxTries);
+ return generateBlocks(config, mempool, coinbase_script, nGenerate,
+ nMaxTries);
}
static UniValue getmininginfo(const Config &config,
diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp
--- a/src/test/blockfilter_index_tests.cpp
+++ b/src/test/blockfilter_index_tests.cpp
@@ -18,6 +18,15 @@
BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
+struct BuildChainTestingSetup : public TestChain100Setup {
+ CBlock CreateBlock(const CBlockIndex *prev,
+ const std::vector<CMutableTransaction> &txns,
+ const CScript &scriptPubKey);
+ bool BuildChain(const CBlockIndex *pindex,
+ const CScript &coinbase_script_pub_key, size_t length,
+ std::vector<std::shared_ptr<CBlock>> &chain);
+};
+
static bool CheckFilterLookups(BlockFilterIndex &filter_index,
const CBlockIndex *block_index,
uint256 &last_header) {
@@ -55,13 +64,12 @@
return true;
}
-static CBlock CreateBlock(const CBlockIndex *prev,
- const std::vector<CMutableTransaction> &txns,
- const CScript &scriptPubKey,
- const CTxMemPool &mempool) {
+CBlock BuildChainTestingSetup::CreateBlock(
+ const CBlockIndex *prev, const std::vector<CMutableTransaction> &txns,
+ const CScript &scriptPubKey) {
const Config &config = GetConfig();
std::unique_ptr<CBlockTemplate> pblocktemplate =
- BlockAssembler(config, mempool).CreateNewBlock(scriptPubKey);
+ BlockAssembler(config, *m_node.mempool).CreateNewBlock(scriptPubKey);
CBlock &block = pblocktemplate->block;
block.hashPrevBlock = prev->GetBlockHash();
block.nTime = prev->nTime + 1;
@@ -83,16 +91,15 @@
return block;
}
-static bool BuildChain(const CBlockIndex *pindex,
- const CScript &coinbase_script_pub_key, size_t length,
- std::vector<std::shared_ptr<CBlock>> &chain,
- const CTxMemPool &mempool) {
+bool BuildChainTestingSetup::BuildChain(
+ const CBlockIndex *pindex, const CScript &coinbase_script_pub_key,
+ size_t length, std::vector<std::shared_ptr<CBlock>> &chain) {
std::vector<CMutableTransaction> no_txns;
chain.resize(length);
for (auto &block : chain) {
block = std::make_shared<CBlock>(
- CreateBlock(pindex, no_txns, coinbase_script_pub_key, mempool));
+ CreateBlock(pindex, no_txns, coinbase_script_pub_key));
CBlockHeader header = block->GetBlockHeader();
BlockValidationState state;
@@ -104,7 +111,8 @@
return true;
}
-BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup) {
+BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync,
+ BuildChainTestingSetup) {
BlockFilterIndex filter_index(BlockFilterType::BASIC, 1 << 20, true);
uint256 last_header;
@@ -174,10 +182,8 @@
CScript coinbase_script_pub_key_B =
GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
std::vector<std::shared_ptr<CBlock>> chainA, chainB;
- BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA,
- *m_node.mempool));
- BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB,
- *m_node.mempool));
+ BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
+ BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
// Check that new blocks on chain A get indexed.
uint256 chainA_last_header = last_header;
diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp
--- a/src/test/miner_tests.cpp
+++ b/src/test/miner_tests.cpp
@@ -38,6 +38,7 @@
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs) {
return CheckSequenceLocks(*m_node.mempool, tx, flags);
}
+ BlockAssembler AssemblerForTest(const CChainParams &params);
};
} // namespace miner_tests
@@ -57,14 +58,13 @@
static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE_PER_KB);
-static BlockAssembler AssemblerForTest(const CChainParams &params,
- const CTxMemPool &mempool) {
+BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams &params) {
BlockAssembler::Options options;
options.blockMinFeeRate = blockMinFeeRate;
- return BlockAssembler(params, mempool, options);
+ return BlockAssembler(params, *m_node.mempool, options);
}
-static struct {
+constexpr static struct {
uint8_t extranonce;
uint32_t nonce;
} blockinfo[] = {
@@ -151,8 +151,7 @@
.FromTx(tx));
std::unique_ptr<CBlockTemplate> pblocktemplate =
- AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey);
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
BOOST_CHECK(pblocktemplate->block.vtx[1]->GetId() == parentTxId);
BOOST_CHECK(pblocktemplate->block.vtx[2]->GetId() == highFeeTxId);
BOOST_CHECK(pblocktemplate->block.vtx[3]->GetId() == mediumFeeTxId);
@@ -174,8 +173,7 @@
int64_t(5000000000LL - 1000 - 50000) * SATOSHI - feeToUse;
TxId lowFeeTxId = tx.GetId();
m_node.mempool->addUnchecked(entry.Fee(feeToUse).FromTx(tx));
- pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey);
+ pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
// Verify that the free tx and the low fee tx didn't get selected.
for (const auto &txn : pblocktemplate->block.vtx) {
BOOST_CHECK(txn->GetId() != freeTxId);
@@ -191,8 +189,7 @@
tx.vout[0].nValue -= 2 * SATOSHI;
lowFeeTxId = tx.GetId();
m_node.mempool->addUnchecked(entry.Fee(feeToUse + 2 * SATOSHI).FromTx(tx));
- pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey);
+ pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
BOOST_CHECK(pblocktemplate->block.vtx[4]->GetId() == freeTxId);
BOOST_CHECK(pblocktemplate->block.vtx[5]->GetId() == lowFeeTxId);
@@ -216,8 +213,7 @@
TxId lowFeeTxId2 = tx.GetId();
m_node.mempool->addUnchecked(
entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
- pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey);
+ pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
// Verify that this tx isn't selected.
for (const auto &txn : pblocktemplate->block.vtx) {
@@ -231,8 +227,7 @@
// 10k satoshi fee.
tx.vout[0].nValue = (100000000 - 10000) * SATOSHI;
m_node.mempool->addUnchecked(entry.Fee(10000 * SATOSHI).FromTx(tx));
- pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey);
+ pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
BOOST_CHECK(pblocktemplate->block.vtx[8]->GetId() == lowFeeTxId2);
}
@@ -292,11 +287,13 @@
fCheckpointsEnabled = false;
// Simple block creation, nothing special yet:
- BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey));
+ BOOST_CHECK(pblocktemplate =
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
// We can't make transactions until we have inputs.
- // Therefore, load 100 blocks :)
+ // Therefore, load 110 blocks :)
+ static_assert(sizeof(blockinfo) / sizeof(*blockinfo) == 110,
+ "Should have 110 blocks to import");
int baseheight = 0;
std::vector<CTransactionRef> txFirst;
for (size_t i = 0; i < sizeof(blockinfo) / sizeof(*blockinfo); ++i) {
@@ -333,8 +330,8 @@
LOCK(m_node.mempool->cs);
// Just to make sure we can still make simple blocks.
- BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey));
+ BOOST_CHECK(pblocktemplate =
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
const Amount BLOCKSUBSIDY = 50 * COIN;
const Amount LOWFEE = CENT;
@@ -366,16 +363,15 @@
tx.vin[0].prevout = COutPoint(txid, 0);
}
- BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey));
+ BOOST_CHECK(pblocktemplate =
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
m_node.mempool->clear();
// Orphan in mempool, template creation fails.
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx));
- BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey),
- std::runtime_error,
- HasReason("bad-txns-inputs-missingorspent"));
+ BOOST_CHECK_EXCEPTION(
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey),
+ std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
m_node.mempool->clear();
// Child with higher priority than parent.
@@ -394,8 +390,8 @@
txid = tx.GetId();
m_node.mempool->addUnchecked(
entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
- BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey));
+ BOOST_CHECK(pblocktemplate =
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
m_node.mempool->clear();
// Coinbase in mempool, template creation fails.
@@ -408,9 +404,9 @@
m_node.mempool->addUnchecked(
entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
// Should throw bad-tx-coinbase
- BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey),
- std::runtime_error, HasReason("bad-tx-coinbase"));
+ BOOST_CHECK_EXCEPTION(
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey),
+ std::runtime_error, HasReason("bad-tx-coinbase"));
m_node.mempool->clear();
// Double spend txn pair in mempool, template creation fails.
@@ -425,10 +421,9 @@
txid = tx.GetId();
m_node.mempool->addUnchecked(
entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
- BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey),
- std::runtime_error,
- HasReason("bad-txns-inputs-missingorspent"));
+ BOOST_CHECK_EXCEPTION(
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey),
+ std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
m_node.mempool->clear();
// Subsidy changing.
@@ -444,8 +439,8 @@
next->BuildSkip();
::ChainActive().SetTip(next);
}
- BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey));
+ BOOST_CHECK(pblocktemplate =
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
// Extend to a 210000-long block chain.
while (::ChainActive().Tip()->nHeight < 210000) {
CBlockIndex *prev = ::ChainActive().Tip();
@@ -458,8 +453,8 @@
::ChainActive().SetTip(next);
}
- BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey));
+ BOOST_CHECK(pblocktemplate =
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
// Invalid p2sh txn in mempool, template creation fails
tx.vin[0].prevout = COutPoint(txFirst[0]->GetId(), 0);
@@ -478,9 +473,9 @@
m_node.mempool->addUnchecked(
entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
// Should throw blk-bad-inputs
- BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey),
- std::runtime_error, HasReason("blk-bad-inputs"));
+ BOOST_CHECK_EXCEPTION(
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey),
+ std::runtime_error, HasReason("blk-bad-inputs"));
m_node.mempool->clear();
// Delete the dummy blocks again.
@@ -656,8 +651,7 @@
// Sequence locks fail.
BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags));
- pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey);
+ pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
BOOST_CHECK(pblocktemplate);
// None of the of the absolute height/time locked tx should have made it
@@ -677,8 +671,8 @@
::ChainActive().Tip()->nHeight++;
SetMockTime(::ChainActive().Tip()->GetMedianTimePast() + 1);
- BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams, *m_node.mempool)
- .CreateNewBlock(scriptPubKey));
+ BOOST_CHECK(pblocktemplate =
+ AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5UL);
::ChainActive().Tip()->nHeight--;
diff --git a/src/test/util/mining.h b/src/test/util/mining.h
--- a/src/test/util/mining.h
+++ b/src/test/util/mining.h
@@ -12,15 +12,18 @@
class Config;
class CScript;
class CTxIn;
+struct NodeContext;
/** Returns the generated coin */
-CTxIn MineBlock(const Config &config, const CScript &coinbase_scriptPubKey);
+CTxIn MineBlock(const Config &config, const NodeContext &,
+ const CScript &coinbase_scriptPubKey);
/** Prepare a block to be mined */
-std::shared_ptr<CBlock> PrepareBlock(const Config &config,
+std::shared_ptr<CBlock> PrepareBlock(const Config &config, const NodeContext &,
const CScript &coinbase_scriptPubKey);
/** RPC-like helper function, returns the generated coin */
-CTxIn generatetoaddress(const Config &config, const std::string &address);
+CTxIn generatetoaddress(const Config &config, const NodeContext &,
+ const std::string &address);
#endif // BITCOIN_TEST_UTIL_MINING_H
diff --git a/src/test/util/mining.cpp b/src/test/util/mining.cpp
--- a/src/test/util/mining.cpp
+++ b/src/test/util/mining.cpp
@@ -9,6 +9,7 @@
#include <consensus/merkle.h>
#include <key_io.h>
#include <miner.h>
+#include <node/context.h>
#include <pow/pow.h>
#include <script/standard.h>
#include <validation.h>
@@ -16,16 +17,18 @@
const std::string ADDRESS_BCHREG_UNSPENDABLE =
"bchreg:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqha9s37tt";
-CTxIn generatetoaddress(const Config &config, const std::string &address) {
+CTxIn generatetoaddress(const Config &config, const NodeContext &node,
+ const std::string &address) {
const auto dest = DecodeDestination(address, config.GetChainParams());
assert(IsValidDestination(dest));
const auto coinbase_script = GetScriptForDestination(dest);
- return MineBlock(config, coinbase_script);
+ return MineBlock(config, node, coinbase_script);
}
-CTxIn MineBlock(const Config &config, const CScript &coinbase_scriptPubKey) {
- auto block = PrepareBlock(config, coinbase_scriptPubKey);
+CTxIn MineBlock(const Config &config, const NodeContext &node,
+ const CScript &coinbase_scriptPubKey) {
+ auto block = PrepareBlock(config, node, coinbase_scriptPubKey);
while (!CheckProofOfWork(block->GetHash(), block->nBits,
config.GetChainParams().GetConsensus())) {
@@ -40,9 +43,11 @@
}
std::shared_ptr<CBlock> PrepareBlock(const Config &config,
+ const NodeContext &node,
const CScript &coinbase_scriptPubKey) {
+ assert(node.mempool);
auto block =
- std::make_shared<CBlock>(BlockAssembler{config, ::g_mempool}
+ std::make_shared<CBlock>(BlockAssembler{config, *node.mempool}
.CreateNewBlock(coinbase_scriptPubKey)
->block);
diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp
--- a/src/test/validation_block_tests.cpp
+++ b/src/test/validation_block_tests.cpp
@@ -20,7 +20,24 @@
#include <thread>
-BOOST_FIXTURE_TEST_SUITE(validation_block_tests, RegTestingSetup)
+namespace validation_block_tests {
+struct MinerTestingSetup : public RegTestingSetup {
+ std::shared_ptr<CBlock> Block(const Config &config,
+ const BlockHash &prev_hash);
+ std::shared_ptr<const CBlock> GoodBlock(const Config &config,
+ const BlockHash &prev_hash);
+ std::shared_ptr<const CBlock> BadBlock(const Config &config,
+ const BlockHash &prev_hash);
+ std::shared_ptr<CBlock> FinalizeBlock(const Consensus::Params &params,
+ std::shared_ptr<CBlock> pblock);
+ void BuildChain(const Config &config, const BlockHash &root, int height,
+ const unsigned int invalid_rate,
+ const unsigned int branch_rate, const unsigned int max_size,
+ std::vector<std::shared_ptr<const CBlock>> &blocks);
+};
+} // namespace validation_block_tests
+
+BOOST_FIXTURE_TEST_SUITE(validation_block_tests, MinerTestingSetup)
struct TestSubscriber : public CValidationInterface {
uint256 m_expected_tip;
@@ -52,15 +69,16 @@
}
};
-std::shared_ptr<CBlock> Block(const Config &config, const CTxMemPool &mempool,
- const BlockHash &prev_hash) {
+std::shared_ptr<CBlock> MinerTestingSetup::Block(const Config &config,
+ const BlockHash &prev_hash) {
static int i = 0;
static uint64_t time = config.GetChainParams().GenesisBlock().nTime;
CScript pubKey;
pubKey << i++ << OP_TRUE;
- auto ptemplate = BlockAssembler(config, mempool).CreateNewBlock(pubKey);
+ auto ptemplate =
+ BlockAssembler(config, *m_node.mempool).CreateNewBlock(pubKey);
auto pblock = std::make_shared<CBlock>(ptemplate->block);
pblock->hashPrevBlock = prev_hash;
pblock->nTime = ++time;
@@ -85,8 +103,9 @@
return pblock;
}
-std::shared_ptr<CBlock> FinalizeBlock(const Consensus::Params &params,
- std::shared_ptr<CBlock> pblock) {
+std::shared_ptr<CBlock>
+MinerTestingSetup::FinalizeBlock(const Consensus::Params &params,
+ std::shared_ptr<CBlock> pblock) {
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, params)) {
@@ -97,18 +116,16 @@
}
// construct a valid block
-std::shared_ptr<const CBlock> GoodBlock(const Config &config,
- const CTxMemPool &mempool,
- const BlockHash &prev_hash) {
+std::shared_ptr<const CBlock>
+MinerTestingSetup::GoodBlock(const Config &config, const BlockHash &prev_hash) {
return FinalizeBlock(config.GetChainParams().GetConsensus(),
- Block(config, mempool, prev_hash));
+ Block(config, prev_hash));
}
// construct an invalid block (but with a valid header)
-std::shared_ptr<const CBlock> BadBlock(const Config &config,
- const CTxMemPool &mempool,
- const BlockHash &prev_hash) {
- auto pblock = Block(config, mempool, prev_hash);
+std::shared_ptr<const CBlock>
+MinerTestingSetup::BadBlock(const Config &config, const BlockHash &prev_hash) {
+ auto pblock = Block(config, prev_hash);
CMutableTransaction coinbase_spend;
coinbase_spend.vin.push_back(
@@ -122,11 +139,11 @@
return ret;
}
-void BuildChain(const Config &config, const CTxMemPool &mempool,
- const BlockHash &root, int height,
- const unsigned int invalid_rate, const unsigned int branch_rate,
- const unsigned int max_size,
- std::vector<std::shared_ptr<const CBlock>> &blocks) {
+void MinerTestingSetup::BuildChain(
+ const Config &config, const BlockHash &root, int height,
+ const unsigned int invalid_rate, const unsigned int branch_rate,
+ const unsigned int max_size,
+ std::vector<std::shared_ptr<const CBlock>> &blocks) {
if (height <= 0 || blocks.size() >= max_size) {
return;
}
@@ -135,18 +152,17 @@
bool gen_fork = InsecureRandRange(100) < branch_rate;
const std::shared_ptr<const CBlock> pblock =
- gen_invalid ? BadBlock(config, mempool, root)
- : GoodBlock(config, mempool, root);
+ gen_invalid ? BadBlock(config, root) : GoodBlock(config, root);
blocks.push_back(pblock);
if (!gen_invalid) {
- BuildChain(config, mempool, pblock->GetHash(), height - 1, invalid_rate,
+ BuildChain(config, pblock->GetHash(), height - 1, invalid_rate,
branch_rate, max_size, blocks);
}
if (gen_fork) {
- blocks.push_back(GoodBlock(config, mempool, root));
- BuildChain(config, mempool, blocks.back()->GetHash(), height - 1,
- invalid_rate, branch_rate, max_size, blocks);
+ blocks.push_back(GoodBlock(config, root));
+ BuildChain(config, blocks.back()->GetHash(), height - 1, invalid_rate,
+ branch_rate, max_size, blocks);
}
}
@@ -158,9 +174,8 @@
std::vector<std::shared_ptr<const CBlock>> blocks;
while (blocks.size() < 50) {
blocks.clear();
- BuildChain(config, *m_node.mempool,
- chainParams.GenesisBlock().GetHash(), 100, 15, 10, 500,
- blocks);
+ BuildChain(config, chainParams.GenesisBlock().GetHash(), 100, 15, 10,
+ 500, blocks);
}
bool ignored;
@@ -260,8 +275,7 @@
// Process all mined blocks
BOOST_REQUIRE(
ProcessBlock(std::make_shared<CBlock>(chainParams.GenesisBlock())));
- auto last_mined = GoodBlock(config, *m_node.mempool,
- chainParams.GenesisBlock().GetHash());
+ auto last_mined = GoodBlock(config, chainParams.GenesisBlock().GetHash());
BOOST_REQUIRE(ProcessBlock(last_mined));
// Run the test multiple times
@@ -289,15 +303,13 @@
<< OP_EQUALVERIFY << OP_CHECKSIG));
}
txs.push_back(MakeTransactionRef(mtx));
- last_mined =
- GoodBlock(config, *m_node.mempool, last_mined->GetHash());
+ last_mined = GoodBlock(config, last_mined->GetHash());
BOOST_REQUIRE(ProcessBlock(last_mined));
}
// Mature the inputs of the txs
for (int j = COINBASE_MATURITY; j > 0; --j) {
- last_mined =
- GoodBlock(config, *m_node.mempool, last_mined->GetHash());
+ last_mined = GoodBlock(config, last_mined->GetHash());
BOOST_REQUIRE(ProcessBlock(last_mined));
}
@@ -305,11 +317,10 @@
const BlockHash tip_init{last_mined->GetHash()};
std::vector<std::shared_ptr<const CBlock>> reorg;
- last_mined = GoodBlock(config, *m_node.mempool, split_hash);
+ last_mined = GoodBlock(config, split_hash);
reorg.push_back(last_mined);
for (size_t j = COINBASE_MATURITY + txs.size() + 1; j > 0; --j) {
- last_mined =
- GoodBlock(config, *m_node.mempool, last_mined->GetHash());
+ last_mined = GoodBlock(config, last_mined->GetHash());
reorg.push_back(last_mined);
}

File Metadata

Mime Type
text/plain
Expires
Sat, Mar 1, 11:04 (15 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5187473
Default Alt Text
D7988.diff (35 KB)

Event Timeline