Line data Source code
1 : // Copyright (c) 2011-2021 The Bitcoin Core developers
2 : // Distributed under the MIT software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include <test/util/setup_common.h>
6 :
7 : #include <addrman.h>
8 : #include <banman.h>
9 : #include <chainlock/chainlock.h>
10 : #include <chainlock/handler.h>
11 : #include <chainparams.h>
12 : #include <common/url.h>
13 : #include <consensus/consensus.h>
14 : #include <consensus/merkle.h>
15 : #include <consensus/params.h>
16 : #include <consensus/validation.h>
17 : #include <deploymentstatus.h>
18 : #include <crypto/sha256.h>
19 : #include <crypto/x11/dispatch.h>
20 : #include <index/txindex.h>
21 : #include <init.h>
22 : #include <interfaces/chain.h>
23 : #include <net.h>
24 : #include <net_processing.h>
25 : #include <noui.h>
26 : #include <node/blockstorage.h>
27 : #include <node/chainstate.h>
28 : #include <node/miner.h>
29 : #include <node/sync_manager.h>
30 : #include <policy/fees.h>
31 : #include <policy/settings.h>
32 : #include <pow.h>
33 : #include <rpc/blockchain.h>
34 : #include <rpc/register.h>
35 : #include <rpc/server.h>
36 : #include <scheduler.h>
37 : #include <script/sigcache.h>
38 : #include <shutdown.h>
39 : #include <streams.h>
40 : #include <test/util/index.h>
41 : #include <test/util/net.h>
42 : #include <test/util/txmempool.h>
43 : #include <txdb.h>
44 : #include <util/strencodings.h>
45 : #include <util/string.h>
46 : #include <util/thread.h>
47 : #include <util/threadnames.h>
48 : #include <util/time.h>
49 : #include <util/translation.h>
50 : #include <util/vector.h>
51 : #include <validation.h>
52 : #include <validationinterface.h>
53 : #include <walletinitinterface.h>
54 :
55 : #include <bls/bls.h>
56 : #include <coinjoin/coinjoin.h>
57 : #include <coinjoin/walletman.h>
58 : #include <evo/cbtx.h>
59 : #include <evo/chainhelper.h>
60 : #include <evo/deterministicmns.h>
61 : #include <evo/evodb.h>
62 : #include <evo/simplifiedmns.h>
63 : #include <evo/specialtx.h>
64 : #include <evo/specialtxman.h>
65 : #include <flat-database.h>
66 : #include <governance/governance.h>
67 : #include <llmq/context.h>
68 : #include <llmq/signing.h>
69 : #include <masternode/meta.h>
70 : #include <masternode/sync.h>
71 : #include <netfulfilledman.h>
72 : #include <spork.h>
73 : #include <stats/client.h>
74 :
75 : #ifdef ENABLE_WALLET
76 : #include <interfaces/coinjoin.h>
77 : #include <interfaces/wallet.h>
78 : #endif // ENABLE_WALLET
79 :
80 : #include <algorithm>
81 : #include <memory>
82 : #include <stdexcept>
83 :
84 : using node::BlockAssembler;
85 : using node::CalculateCacheSizes;
86 : using node::DashChainstateSetup;
87 : using node::DashChainstateSetupClose;
88 : using node::LoadChainstate;
89 : using node::NodeContext;
90 : using node::VerifyLoadedChainstate;
91 : using node::fPruneMode;
92 : using node::fReindex;
93 :
94 146 : const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
95 : UrlDecodeFn* const URL_DECODE = nullptr;
96 :
97 146 : FastRandomContext g_insecure_rand_ctx;
98 : /** Random context to get unique temp data dirs. Separate from g_insecure_rand_ctx, which can be seeded from a const env var */
99 146 : static FastRandomContext g_insecure_rand_ctx_temp_path;
100 :
101 : /** Return the unsigned from the environment var if available, otherwise 0 */
102 116 : static uint256 GetUintFromEnv(const std::string& env_name)
103 : {
104 116 : const char* num = std::getenv(env_name.c_str());
105 116 : if (!num) return {};
106 0 : return uint256S(num);
107 116 : }
108 :
109 691 : void Seed(FastRandomContext& ctx)
110 : {
111 : // Should be enough to get the seed once for the process
112 : static uint256 seed{};
113 : static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
114 691 : if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
115 691 : if (seed.IsNull()) seed = GetRandHash();
116 691 : LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
117 691 : ctx = FastRandomContext(seed);
118 691 : }
119 :
120 0 : std::ostream& operator<<(std::ostream& os, const uint256& num)
121 : {
122 0 : os << num.ToString();
123 0 : return os;
124 0 : }
125 :
126 183 : std::unique_ptr<PeerManager> MakePeerManager(CConnman& connman,
127 : NodeContext& node,
128 : BanMan* banman,
129 : const CChainParams& chainparams,
130 : bool ignore_incoming_txs)
131 : {
132 366 : return PeerManager::make(chainparams, connman, *node.addrman, banman, *node.dstxman, *node.chainman, *node.mempool, *node.mn_metaman,
133 183 : *node.mn_sync, *node.sporkman, *node.chainlocks, *node.clhandler, /*nodeman=*/nullptr, node.dmnman, node.cj_walletman,
134 183 : node.llmq_ctx, ignore_incoming_txs);
135 : }
136 :
137 2 : void DashChainstateSetup(ChainstateManager& chainman,
138 : NodeContext& node,
139 : bool llmq_dbs_in_memory,
140 : bool llmq_dbs_wipe,
141 : const Consensus::Params& consensus_params)
142 : {
143 4 : DashChainstateSetup(chainman, *Assert(node.mn_metaman.get()),
144 2 : *Assert(node.sporkman.get()), *Assert(node.chainlocks), *Assert(node.mn_sync), node.chain_helper, node.dmnman, *node.evodb,
145 2 : node.llmq_ctx, Assert(node.mempool.get()), node.args->GetDataDirNet(), llmq_dbs_in_memory, llmq_dbs_wipe,
146 2 : llmq::DEFAULT_BLSCHECK_THREADS, llmq::DEFAULT_WORKER_COUNT, llmq::DEFAULT_MAX_RECOVERED_SIGS_AGE,
147 2 : consensus_params);
148 2 : }
149 :
150 180 : void DashChainstateSetupClose(NodeContext& node)
151 : {
152 360 : DashChainstateSetupClose(node.chain_helper, node.dmnman, node.llmq_ctx,
153 180 : Assert(node.mempool.get()));
154 180 : }
155 :
156 627 : BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::vector<const char*>& extra_args)
157 627 : : m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / g_insecure_rand_ctx_temp_path.rand256().ToString()},
158 627 : m_args{}
159 0 : {
160 627 : m_node.args = &gArgs;
161 627 : std::vector<const char*> arguments = Cat(
162 627 : {
163 : "dummy",
164 : "-printtoconsole=0",
165 : "-logsourcelocations",
166 : "-logtimemicros",
167 : "-logthreadnames",
168 : "-loglevel=trace",
169 : "-debug",
170 : "-debugexclude=libevent",
171 : "-debugexclude=leveldb",
172 : },
173 627 : extra_args);
174 627 : if (G_TEST_COMMAND_LINE_ARGUMENTS) {
175 627 : arguments = Cat(arguments, G_TEST_COMMAND_LINE_ARGUMENTS());
176 627 : }
177 627 : util::ThreadRename("test");
178 627 : fs::create_directories(m_path_root);
179 627 : m_args.ForceSetArg("-datadir", fs::PathToString(m_path_root));
180 627 : gArgs.ForceSetArg("-datadir", fs::PathToString(m_path_root));
181 627 : gArgs.ClearPathCache();
182 : {
183 627 : SetupServerArgs(*m_node.args);
184 627 : std::string error;
185 627 : if (!m_node.args->ParseParameters(arguments.size(), arguments.data(), error)) {
186 0 : m_node.args->ClearArgs();
187 0 : throw std::runtime_error{error};
188 : }
189 627 : }
190 627 : SelectParams(chainName);
191 627 : SeedInsecureRand();
192 627 : if (G_TEST_LOG_FUN) LogInstance().PushBackCallback(G_TEST_LOG_FUN);
193 627 : InitLogging(*m_node.args);
194 627 : AppInitParameterInteraction(*m_node.args);
195 627 : LogInstance().StartLogging();
196 627 : SapphireAutoDetect();
197 627 : SHA256AutoDetect();
198 627 : ECC_Start();
199 627 : BLSInit();
200 627 : SetupEnvironment();
201 627 : SetupNetworking();
202 627 : InitSignatureCache();
203 627 : InitScriptExecutionCache();
204 :
205 627 : m_node.chain = interfaces::MakeChain(m_node);
206 627 : m_node.netgroupman = std::make_unique<NetGroupManager>(/*asmap=*/std::vector<bool>());
207 1254 : m_node.addrman = std::make_unique<AddrMan>(*m_node.netgroupman,
208 627 : /*deterministic=*/false,
209 627 : m_node.args->GetIntArg("-checkaddrman", 0));
210 :
211 627 : std::string sem_str = m_args.GetArg("-socketevents", DEFAULT_SOCKETEVENTS);
212 627 : ::g_socket_events_mode = SEMFromString(sem_str);
213 627 : if (::g_socket_events_mode == SocketEventsMode::Unknown) {
214 0 : throw std::runtime_error(
215 0 : strprintf("Invalid -socketevents ('%s') specified. Only these modes are supported: %s",
216 0 : sem_str, GetSupportedSocketEventsStr()));
217 : }
218 :
219 : {
220 627 : auto stats_client = StatsdClient::make(*m_node.args);
221 627 : if (!stats_client) {
222 0 : throw std::runtime_error{strprintf("Cannot init Statsd client (%s)", util::ErrorString(stats_client).original)};
223 : }
224 627 : ::g_stats_client = std::move(*stats_client);
225 627 : }
226 :
227 627 : m_node.connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman); // Deterministic randomness for tests.
228 :
229 627 : fCheckBlockIndex = true;
230 :
231 627 : m_node.mn_metaman = std::make_unique<CMasternodeMetaMan>();
232 627 : m_node.netfulfilledman = std::make_unique<CNetFulfilledRequestManager>();
233 627 : m_node.sporkman = std::make_unique<CSporkManager>();
234 627 : m_node.chainlocks = std::make_unique<chainlock::Chainlocks>(*m_node.sporkman);
235 627 : m_node.evodb = std::make_unique<CEvoDB>(util::DbWrapperParams{.path = m_node.args->GetDataDirNet(), .memory = true, .wipe = true});
236 :
237 : static bool noui_connected = false;
238 : if (!noui_connected) {
239 : noui_connect();
240 : noui_connected = true;
241 : }
242 :
243 : bls::bls_legacy_scheme.store(true);
244 0 : }
245 :
246 626 : BasicTestingSetup::~BasicTestingSetup()
247 0 : {
248 626 : SetMockTime(0s); // Reset mocktime for following tests
249 626 : LogInstance().DisconnectTestLogger();
250 626 : fs::remove_all(m_path_root);
251 626 : gArgs.ClearArgs();
252 :
253 626 : m_node.evodb.reset();
254 626 : m_node.sporkman.reset();
255 626 : m_node.netfulfilledman.reset();
256 626 : m_node.mn_metaman.reset();
257 626 : m_node.connman.reset();
258 626 : ::g_stats_client.reset();
259 626 : ::g_socket_events_mode = SocketEventsMode::Unknown;
260 626 : m_node.addrman.reset();
261 626 : m_node.netgroupman.reset();
262 626 : m_node.args = nullptr;
263 :
264 626 : ECC_Stop();
265 626 : }
266 :
267 184 : ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::vector<const char*>& extra_args)
268 184 : : BasicTestingSetup(chainName, extra_args)
269 0 : {
270 : const CChainParams& chainparams = Params();
271 :
272 : // We have to run a scheduler thread to prevent ActivateBestChain
273 : // from blocking due to queue overrun.
274 : m_node.scheduler = std::make_unique<CScheduler>();
275 184 : m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); });
276 : GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
277 :
278 : m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
279 : m_node.mempool = std::make_unique<CTxMemPool>(m_node.fee_estimator.get(), m_node.args->GetIntArg("-checkmempool", 1));
280 :
281 : m_cache_sizes = CalculateCacheSizes(m_args);
282 :
283 : m_node.chainman = std::make_unique<ChainstateManager>(chainparams);
284 : m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);
285 :
286 : m_node.mn_sync = std::make_unique<CMasternodeSync>(std::make_unique<NodeSyncNotifierImpl>(*m_node.connman, *m_node.netfulfilledman));
287 :
288 : m_node.clhandler = std::make_unique<chainlock::ChainlockHandler>(*m_node.chainlocks, *m_node.chainman, *m_node.mempool, *m_node.mn_sync);
289 :
290 : // Start script-checking threads. Set g_parallel_script_checks to true so they are used.
291 : constexpr int script_check_threads = 2;
292 : StartScriptCheckWorkerThreads(script_check_threads);
293 : g_parallel_script_checks = true;
294 0 : }
295 :
296 184 : ChainTestingSetup::~ChainTestingSetup()
297 0 : {
298 184 : m_node.scheduler->stop();
299 184 : StopScriptCheckWorkerThreads();
300 184 : GetMainSignals().FlushBackgroundCallbacks();
301 184 : GetMainSignals().UnregisterBackgroundSignalScheduler();
302 184 : m_node.mn_sync.reset();
303 184 : m_node.chainman.reset();
304 184 : m_node.mempool.reset();
305 184 : m_node.fee_estimator.reset();
306 184 : m_node.scheduler.reset();
307 184 : }
308 :
309 178 : void ChainTestingSetup::LoadVerifyActivateChainstate()
310 : {
311 178 : const CChainParams& chainparams = Params();
312 178 : auto& chainman{*Assert(m_node.chainman)};
313 356 : auto maybe_load_error = LoadChainstate(fReindex.load(),
314 178 : chainman,
315 178 : *Assert(m_node.mn_metaman.get()),
316 178 : *Assert(m_node.sporkman.get()),
317 178 : *Assert(m_node.chainlocks.get()),
318 178 : *Assert(m_node.mn_sync.get()),
319 178 : m_node.chain_helper,
320 178 : m_node.dmnman,
321 178 : m_node.evodb,
322 178 : m_node.llmq_ctx,
323 178 : Assert(m_node.mempool.get()),
324 178 : Assert(m_node.args)->GetDataDirNet(),
325 178 : fPruneMode,
326 178 : chainparams.GetConsensus(),
327 178 : m_args.GetBoolArg("-reindex-chainstate", false),
328 178 : m_cache_sizes.block_tree_db,
329 178 : m_cache_sizes.coins_db,
330 178 : m_cache_sizes.coins,
331 178 : m_block_tree_db_in_memory,
332 178 : m_coins_db_in_memory,
333 : /*dash_dbs_in_memory=*/true,
334 : llmq::DEFAULT_BLSCHECK_THREADS,
335 178 : llmq::DEFAULT_WORKER_COUNT,
336 : llmq::DEFAULT_MAX_RECOVERED_SIGS_AGE);
337 178 : assert(!maybe_load_error.has_value());
338 :
339 178 : m_node.govman = std::make_unique<CGovernanceManager>(*m_node.mn_metaman, *m_node.chainman, *m_node.chain_helper->superblocks, *m_node.dmnman, *m_node.mn_sync);
340 :
341 178 : auto maybe_verify_error = VerifyLoadedChainstate(
342 178 : chainman,
343 178 : *Assert(m_node.evodb.get()),
344 178 : fReindex.load(),
345 178 : m_args.GetBoolArg("-reindex-chainstate", false),
346 178 : chainparams.GetConsensus(),
347 178 : m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS),
348 178 : m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
349 178 : /*get_unix_time_seconds=*/static_cast<int64_t(*)()>(GetTime),
350 178 : [](bool bls_state) {
351 0 : LogPrintf("%s: bls_legacy_scheme=%d\n", __func__, bls_state);
352 0 : });
353 178 : assert(!maybe_verify_error.has_value());
354 :
355 178 : BlockValidationState state;
356 178 : if (!chainman.ActiveChainstate().ActivateBestChain(state)) {
357 0 : throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString()));
358 : }
359 178 : }
360 :
361 178 : TestingSetup::TestingSetup(
362 : const std::string& chainName,
363 : const std::vector<const char*>& extra_args,
364 : const bool coins_db_in_memory,
365 : const bool block_tree_db_in_memory)
366 178 : : ChainTestingSetup(chainName, extra_args)
367 0 : {
368 178 : m_coins_db_in_memory = coins_db_in_memory;
369 178 : m_block_tree_db_in_memory = block_tree_db_in_memory;
370 178 : const CChainParams& chainparams = Params();
371 : // Ideally we'd move all the RPC tests to the functional testing framework
372 : // instead of unit tests, but for now we need these here.
373 178 : RegisterAllCoreRPCCommands(tableRPC);
374 178 : LoadVerifyActivateChainstate();
375 :
376 178 : m_node.dstxman = std::make_unique<CDSTXManager>(*Assert(m_node.chainlocks));
377 : #ifdef ENABLE_WALLET
378 : // The test suite doesn't use masternode mode, so we may initialize it
379 356 : m_node.cj_walletman = CJWalletManager::make(*m_node.chainman, *m_node.dmnman, *m_node.mn_metaman, *m_node.mempool,
380 178 : *m_node.mn_sync, *m_node.llmq_ctx->isman, /*relay_txes=*/true);
381 178 : assert(m_node.cj_walletman);
382 :
383 : // WalletInit::Construct()-like logic needed for wallet tests that run on
384 : // TestingSetup and its children (e.g. TestChain100Setup) instead of
385 : // WalletTestingSetup
386 178 : m_node.coinjoin_loader = interfaces::MakeCoinJoinLoader(m_node);
387 :
388 178 : auto wallet_loader = interfaces::MakeWalletLoader(*m_node.chain, *m_node.args, m_node, *m_node.coinjoin_loader);
389 178 : m_node.wallet_loader = wallet_loader.get();
390 178 : m_node.chain_clients.emplace_back(std::move(wallet_loader));
391 : #endif // ENABLE_WALLET
392 :
393 178 : m_node.banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
394 178 : m_node.peerman = MakePeerManager(*m_node.connman, m_node, m_node.banman.get(), chainparams,
395 : /*ignore_incoming_txs=*/false);
396 : {
397 178 : CConnman::Options options;
398 178 : options.m_msgproc = m_node.peerman.get();
399 178 : options.socketEventsMode = ::g_socket_events_mode;
400 178 : m_node.connman->Init(options);
401 178 : }
402 :
403 178 : }
404 :
405 178 : TestingSetup::~TestingSetup()
406 0 : {
407 178 : m_node.peerman.reset();
408 178 : m_node.banman.reset();
409 : #ifdef ENABLE_WALLET
410 356 : for (auto& client : m_node.chain_clients) {
411 178 : client.reset();
412 : }
413 178 : m_node.wallet_loader = nullptr;
414 178 : m_node.coinjoin_loader.reset();
415 178 : m_node.cj_walletman.reset();
416 : #endif // ENABLE_WALLET
417 178 : m_node.dstxman.reset();
418 :
419 : // Interrupt() and PrepareShutdown() routines
420 178 : if (m_node.clhandler) {
421 178 : m_node.clhandler->Stop();
422 178 : }
423 178 : if (m_node.connman) {
424 178 : m_node.connman->Stop();
425 178 : }
426 :
427 : // govman holds a reference to chain_helper->superblocks, so it must be reset
428 : // before DashChainstateSetupClose() destroys chain_helper (matches PrepareShutdown
429 : // ordering in init.cpp).
430 178 : m_node.govman.reset();
431 :
432 : // DashChainstateSetup() is called by LoadChainstate() internally but
433 : // winding them down is our responsibility
434 178 : DashChainstateSetupClose(m_node);
435 178 : }
436 :
437 44 : TestChain100Setup::TestChain100Setup(const std::string& chain_name, const std::vector<const char*>& extra_args)
438 44 : : TestChainSetup{100, chain_name, extra_args}
439 0 : {
440 44 : }
441 :
442 136 : TestChainSetup::TestChainSetup(int num_blocks, const std::string& chain_name, const std::vector<const char*>& extra_args)
443 68 : : TestingSetup{chain_name, extra_args}
444 0 : {
445 68 : SetMockTime(1598887952);
446 68 : constexpr std::array<unsigned char, 32> vchKey = {
447 : {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}};
448 68 : coinbaseKey.Set(vchKey.begin(), vchKey.end(), true);
449 :
450 : // Generate a num_blocks length chain:
451 68 : this->mineBlocks(num_blocks);
452 :
453 : // Initialize transaction index *after* chain has been constructed
454 68 : g_txindex = std::make_unique<TxIndex>(1 << 20, true);
455 68 : assert(!g_txindex->BlockUntilSyncedToCurrentChain());
456 68 : if (!g_txindex->Start(m_node.chainman->ActiveChainstate())) {
457 0 : throw std::runtime_error("TxIndex::Start() failed.");
458 : }
459 68 : IndexWaitSynced(*g_txindex);
460 :
461 68 : CCheckpointData checkpoints{
462 68 : {
463 : /*TestChainDATSetup=*/
464 68 : { 98, uint256S("0x150e127929d578d8129b77a6cb7e2e343a1379aa3feaaa9cce59e0a645756a81") },
465 : /*TestChain100Setup=*/
466 68 : { 100, uint256S("0x6ffb83129c19ebdf1ae3771be6a67fe34b35f4c956326b9ba152fac1649f65ae") },
467 : /*TestChainDIP3BeforeActivationSetup=*/
468 68 : { 430, uint256S("0x0bcefaa33fec56cd84d05d0e76cd6a78badcc20f627d91903646de6a07930a14") },
469 : /*TestChainBRRBeforeActivationSetup=*/
470 68 : { 497, uint256S("0x0857a9b5db51835b1c828f019f4c664b5fe6c28ac44a6d868436930f832d31e5") },
471 : /*TestChainV19BeforeActivationSetup=*/
472 68 : { 494, uint256S("0x44ee5c8a5e5cbd4437d63c54ddc1d40329be811b25c492fa901e11cdf408f905") },
473 : }
474 : };
475 :
476 : {
477 68 : LOCK(::cs_main);
478 68 : auto hash = checkpoints.mapCheckpoints.find(num_blocks);
479 136 : assert(
480 : hash != checkpoints.mapCheckpoints.end() &&
481 : m_node.chainman->ActiveChain().Tip()->GetBlockHash() == hash->second);
482 68 : }
483 68 : }
484 :
485 73 : void TestChainSetup::mineBlocks(int num_blocks)
486 : {
487 73 : CScript scriptPubKey = GetScriptForRawPubKey(coinbaseKey.GetPubKey());
488 11322 : for (int i = 0; i < num_blocks; i++) {
489 11249 : std::vector<CMutableTransaction> noTxns;
490 11249 : CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
491 11249 : SetMockTime(GetTime() + 1);
492 11249 : m_coinbase_txns.push_back(b.vtx[0]);
493 11249 : }
494 :
495 : // Allow tx index to catch up with the block index.
496 73 : if (g_txindex) {
497 5 : IndexWaitSynced(*g_txindex);
498 5 : }
499 73 : }
500 :
501 23579 : CBlock TestChainSetup::CreateAndProcessBlock(
502 : const std::vector<CMutableTransaction>& txns,
503 : const CScript& scriptPubKey,
504 : CChainState* chainstate)
505 : {
506 23579 : if (!chainstate) {
507 23579 : chainstate = &Assert(m_node.chainman)->ActiveChainstate();
508 23579 : }
509 :
510 23579 : CBlock block = this->CreateBlock(txns, scriptPubKey, *chainstate);
511 23579 : std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
512 23579 : Assert(m_node.chainman)->ProcessNewBlock(shared_pblock, true, nullptr);
513 :
514 23579 : return block;
515 23579 : }
516 :
517 23591 : CBlock TestChainSetup::CreateBlock(
518 : const std::vector<CMutableTransaction>& txns,
519 : const CScript& scriptPubKey,
520 : CChainState& chainstate)
521 : {
522 23591 : const CChainParams& chainparams = Params();
523 23591 : CTxMemPool empty_pool;
524 23591 : CBlock block = BlockAssembler(chainstate, m_node, &empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
525 :
526 23591 : std::vector<CTransactionRef> llmqCommitments;
527 61647 : for (const auto& tx : block.vtx) {
528 38056 : if (tx->IsSpecialTxVersion() && tx->nType == TRANSACTION_QUORUM_COMMITMENT) {
529 14465 : llmqCommitments.emplace_back(tx);
530 14465 : }
531 : }
532 :
533 : // Replace mempool-selected txns with just coinbase plus passed-in txns:
534 23591 : block.vtx.resize(1);
535 23591 : Assert(block.vtx.size() == 1);
536 :
537 : // Re-add quorum commitments
538 23591 : block.vtx.insert(block.vtx.end(), llmqCommitments.begin(), llmqCommitments.end());
539 23668 : for (const CMutableTransaction& tx : txns) {
540 77 : block.vtx.push_back(MakeTransactionRef(tx));
541 : }
542 :
543 : // Manually update CbTx as we modified the block here
544 23591 : if (block.vtx[0]->nType == TRANSACTION_COINBASE) {
545 9081 : LOCK(cs_main);
546 9081 : auto cbTx = GetTxPayload<CCbTx>(*block.vtx[0]);
547 9081 : Assert(cbTx.has_value());
548 9081 : BlockValidationState state;
549 9081 : CDeterministicMNList mn_list;
550 9081 : if (!chainstate.ChainHelper().special_tx->BuildNewListFromBlock(block, chainstate.m_chain.Tip(), chainstate.CoinsTip(), true, state, mn_list)) {
551 0 : Assert(false);
552 0 : }
553 9081 : if (!CalcCbTxMerkleRootMNList(cbTx->merkleRootMNList, mn_list.to_sml(), state)) {
554 0 : Assert(false);
555 0 : }
556 9081 : if (!CalcCbTxMerkleRootQuorums(block, chainstate.m_chain.Tip(), *m_node.llmq_ctx->quorum_block_processor, cbTx->merkleRootQuorums, state)) {
557 0 : Assert(false);
558 0 : }
559 9081 : CMutableTransaction tmpTx{*block.vtx[0]};
560 9081 : SetTxPayload(tmpTx, *cbTx);
561 9081 : block.vtx[0] = MakeTransactionRef(tmpTx);
562 9081 : }
563 :
564 : // Create a valid coinbase and merkleRoot
565 : {
566 23591 : LOCK(::cs_main);
567 23591 : block.hashPrevBlock = chainstate.m_chain.Tip()->GetBlockHash();
568 23591 : CMutableTransaction tx_coinbase{*block.vtx[0]};
569 23591 : tx_coinbase.vin[0].scriptSig = CScript{} << (chainstate.m_chain.Height() + 1) << CScriptNum{1};
570 23591 : block.vtx[0] = MakeTransactionRef(std::move(tx_coinbase));
571 23591 : block.hashMerkleRoot = BlockMerkleRoot(block);
572 23591 : }
573 :
574 47703 : while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
575 :
576 23591 : CBlock result = block;
577 23591 : return result;
578 23591 : }
579 :
580 47 : CMutableTransaction TestChainSetup::CreateValidMempoolTransaction(CTransactionRef input_transaction,
581 : int input_vout,
582 : int input_height,
583 : CKey input_signing_key,
584 : CScript output_destination,
585 : CAmount output_amount,
586 : bool submit)
587 : {
588 : // Transaction we will submit to the mempool
589 47 : CMutableTransaction mempool_txn;
590 :
591 : // Create an input
592 47 : COutPoint outpoint_to_spend(input_transaction->GetHash(), input_vout);
593 47 : CTxIn input(outpoint_to_spend);
594 47 : mempool_txn.vin.push_back(input);
595 :
596 : // Create an output
597 47 : CTxOut output(output_amount, output_destination);
598 47 : mempool_txn.vout.push_back(output);
599 :
600 : // Sign the transaction
601 : // - Add the signing key to a keystore
602 47 : FillableSigningProvider keystore;
603 47 : keystore.AddKey(input_signing_key);
604 : // - Populate a CoinsViewCache with the unspent output
605 47 : CCoinsView coins_view;
606 47 : CCoinsViewCache coins_cache(&coins_view);
607 47 : AddCoins(coins_cache, *input_transaction.get(), input_height);
608 : // - Use GetCoin to properly populate utxo_to_spend,
609 47 : Coin utxo_to_spend;
610 47 : assert(coins_cache.GetCoin(outpoint_to_spend, utxo_to_spend));
611 : // - Then add it to a map to pass in to SignTransaction
612 47 : std::map<COutPoint, Coin> input_coins;
613 47 : input_coins.insert({outpoint_to_spend, utxo_to_spend});
614 : // - Default signature hashing type
615 47 : int nHashType = SIGHASH_ALL;
616 47 : std::map<int, bilingual_str> input_errors;
617 47 : assert(SignTransaction(mempool_txn, &keystore, input_coins, nHashType, input_errors));
618 :
619 : // If submit=true, add transaction to the mempool.
620 47 : if (submit) {
621 0 : LOCK(cs_main);
622 0 : const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(mempool_txn));
623 0 : assert(result.m_result_type == MempoolAcceptResult::ResultType::VALID);
624 0 : }
625 :
626 47 : return mempool_txn;
627 47 : }
628 :
629 68 : TestChainSetup::~TestChainSetup()
630 0 : {
631 : // Allow tx index to catch up with the block index cause otherwise
632 : // we might be destroying it while scheduler still has some work for it
633 : // e.g. via BlockConnected signal
634 68 : IndexWaitSynced(*g_txindex);
635 68 : g_txindex->Interrupt();
636 68 : g_txindex->Stop();
637 68 : SyncWithValidationInterfaceQueue();
638 68 : g_txindex.reset();
639 68 : }
640 :
641 0 : std::vector<CTransactionRef> TestChainSetup::PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit)
642 : {
643 0 : std::vector<CTransactionRef> mempool_transactions;
644 0 : std::deque<std::pair<COutPoint, CAmount>> unspent_prevouts;
645 0 : std::transform(m_coinbase_txns.begin(), m_coinbase_txns.end(), std::back_inserter(unspent_prevouts),
646 0 : [](const auto& tx){ return std::make_pair(COutPoint(tx->GetHash(), 0), tx->vout[0].nValue); });
647 0 : while (num_transactions > 0 && !unspent_prevouts.empty()) {
648 : // The number of inputs and outputs are random, between 1 and 24.
649 0 : CMutableTransaction mtx = CMutableTransaction();
650 0 : const size_t num_inputs = det_rand.randrange(24) + 1;
651 0 : CAmount total_in{0};
652 0 : for (size_t n{0}; n < num_inputs; ++n) {
653 0 : if (unspent_prevouts.empty()) break;
654 0 : const auto& [prevout, amount] = unspent_prevouts.front();
655 0 : mtx.vin.emplace_back(prevout, CScript());
656 0 : total_in += amount;
657 0 : unspent_prevouts.pop_front();
658 0 : }
659 0 : const size_t num_outputs = det_rand.randrange(24) + 1;
660 : // Approximately 1000sat "fee," equal output amounts.
661 0 : const CAmount amount_per_output = (total_in - 1000) / num_outputs;
662 0 : for (size_t n{0}; n < num_outputs; ++n) {
663 0 : CScript spk = CScript() << CScriptNum(num_transactions + n);
664 0 : mtx.vout.emplace_back(amount_per_output, spk);
665 0 : }
666 0 : CTransactionRef ptx = MakeTransactionRef(mtx);
667 0 : mempool_transactions.push_back(ptx);
668 0 : if (amount_per_output > 2000) {
669 : // If the value is high enough to fund another transaction + fees, keep track of it so
670 : // it can be used to build a more complex transaction graph. Insert randomly into
671 : // unspent_prevouts for extra randomness in the resulting structures.
672 0 : for (size_t n{0}; n < num_outputs; ++n) {
673 0 : unspent_prevouts.emplace_back(COutPoint(ptx->GetHash(), n), amount_per_output);
674 0 : std::swap(unspent_prevouts.back(), unspent_prevouts[det_rand.randrange(unspent_prevouts.size())]);
675 0 : }
676 0 : }
677 0 : if (submit) {
678 0 : LOCK2(m_node.mempool->cs, cs_main);
679 0 : LockPoints lp;
680 0 : m_node.mempool->addUnchecked(CTxMemPoolEntry(ptx, 1000, 0, 1, false, 4, lp));
681 0 : }
682 0 : --num_transactions;
683 0 : }
684 0 : return mempool_transactions;
685 0 : }
686 :
687 1 : void TestChainSetup::MockMempoolMinFee(const CFeeRate& target_feerate)
688 : {
689 1 : LOCK2(cs_main, m_node.mempool->cs);
690 : // Transactions in the mempool will affect the new minimum feerate.
691 1 : assert(m_node.mempool->size() == 0);
692 : // The target feerate cannot be too low...
693 : // ...otherwise the transaction's feerate will need to be negative.
694 1 : assert(target_feerate > ::incrementalRelayFee);
695 : // ...otherwise this is not meaningful. The feerate policy uses the maximum of both feerates.
696 1 : assert(target_feerate > ::minRelayTxFee);
697 :
698 : // Manually create an invalid transaction. Manually set the fee in the CTxMemPoolEntry to
699 : // achieve the exact target feerate.
700 1 : CMutableTransaction mtx = CMutableTransaction();
701 1 : mtx.vin.emplace_back(COutPoint{g_insecure_rand_ctx.rand256(), 0});
702 1 : mtx.vout.emplace_back(1 * COIN, GetScriptForDestination(ScriptHash(CScript() << OP_TRUE)));
703 1 : const auto tx{MakeTransactionRef(mtx)};
704 1 : LockPoints lp;
705 : // The new mempool min feerate is equal to the removed package's feerate + incremental feerate.
706 2 : const auto tx_fee = target_feerate.GetFee(GetVirtualTransactionSize(*tx)) -
707 1 : ::incrementalRelayFee.GetFee(GetVirtualTransactionSize(*tx));
708 2 : m_node.mempool->addUnchecked(CTxMemPoolEntry(tx, /*fee=*/tx_fee,
709 : /*time=*/0, /*entry_height=*/1,
710 1 : /*spends_coinbase=*/true, /*sigops_count=*/1, lp));
711 1 : m_node.mempool->TrimToSize(0);
712 1 : assert(m_node.mempool->GetMinFee(0) == target_feerate);
713 1 : }
714 : /**
715 : * @returns a real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af)
716 : * with 9 txs.
717 : */
718 3 : CBlock getBlock13b8a()
719 : {
720 3 : CBlock block;
721 3 : CDataStream stream(ParseHex("0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf930901000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0146ffffffff0100f2052a01000000434104e18f7afbe4721580e81e8414fc8c24d7cfacf254bb5c7b949450c3e997c2dc1242487a8169507b631eb3771f2b425483fb13102c4eb5d858eef260fe70fbfae0ac00000000010000000196608ccbafa16abada902780da4dc35dafd7af05fa0da08cf833575f8cf9e836000000004a493046022100dab24889213caf43ae6adc41cf1c9396c08240c199f5225acf45416330fd7dbd022100fe37900e0644bf574493a07fc5edba06dbc07c311b947520c2d514bc5725dcb401ffffffff0100f2052a010000001976a914f15d1921f52e4007b146dfa60f369ed2fc393ce288ac000000000100000001fb766c1288458c2bafcfec81e48b24d98ec706de6b8af7c4e3c29419bfacb56d000000008c493046022100f268ba165ce0ad2e6d93f089cfcd3785de5c963bb5ea6b8c1b23f1ce3e517b9f022100da7c0f21adc6c401887f2bfd1922f11d76159cbc597fbd756a23dcbb00f4d7290141042b4e8625a96127826915a5b109852636ad0da753c9e1d5606a50480cd0c40f1f8b8d898235e571fe9357d9ec842bc4bba1827daaf4de06d71844d0057707966affffffff0280969800000000001976a9146963907531db72d0ed1a0cfb471ccb63923446f388ac80d6e34c000000001976a914f0688ba1c0d1ce182c7af6741e02658c7d4dfcd388ac000000000100000002c40297f730dd7b5a99567eb8d27b78758f607507c52292d02d4031895b52f2ff010000008b483045022100f7edfd4b0aac404e5bab4fd3889e0c6c41aa8d0e6fa122316f68eddd0a65013902205b09cc8b2d56e1cd1f7f2fafd60a129ed94504c4ac7bdc67b56fe67512658b3e014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffffca5065ff9617cbcba45eb23726df6498a9b9cafed4f54cbab9d227b0035ddefb000000008a473044022068010362a13c7f9919fa832b2dee4e788f61f6f5d344a7c2a0da6ae740605658022006d1af525b9a14a35c003b78b72bd59738cd676f845d1ff3fc25049e01003614014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffff01001ec4110200000043410469ab4181eceb28985b9b4e895c13fa5e68d85761b7eee311db5addef76fa8621865134a221bd01f28ec9999ee3e021e60766e9d1f3458c115fb28650605f11c9ac000000000100000001cdaf2f758e91c514655e2dc50633d1e4c84989f8aa90a0dbc883f0d23ed5c2fa010000008b48304502207ab51be6f12a1962ba0aaaf24a20e0b69b27a94fac5adf45aa7d2d18ffd9236102210086ae728b370e5329eead9accd880d0cb070aea0c96255fae6c4f1ddcce1fd56e014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff02404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac002d3101000000001976a9141befba0cdc1ad56529371864d9f6cb042faa06b588ac000000000100000001b4a47603e71b61bc3326efd90111bf02d2f549b067f4c4a8fa183b57a0f800cb010000008a4730440220177c37f9a505c3f1a1f0ce2da777c339bd8339ffa02c7cb41f0a5804f473c9230220585b25a2ee80eb59292e52b987dad92acb0c64eced92ed9ee105ad153cdb12d001410443bd44f683467e549dae7d20d1d79cbdb6df985c6e9c029c8d0c6cb46cc1a4d3cf7923c5021b27f7a0b562ada113bc85d5fda5a1b41e87fe6e8802817cf69996ffffffff0280651406000000001976a9145505614859643ab7b547cd7f1f5e7e2a12322d3788ac00aa0271000000001976a914ea4720a7a52fc166c55ff2298e07baf70ae67e1b88ac00000000010000000586c62cd602d219bb60edb14a3e204de0705176f9022fe49a538054fb14abb49e010000008c493046022100f2bc2aba2534becbdf062eb993853a42bbbc282083d0daf9b4b585bd401aa8c9022100b1d7fd7ee0b95600db8535bbf331b19eed8d961f7a8e54159c53675d5f69df8c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff03ad0e58ccdac3df9dc28a218bcf6f1997b0a93306faaa4b3a28ae83447b2179010000008b483045022100be12b2937179da88599e27bb31c3525097a07cdb52422d165b3ca2f2020ffcf702200971b51f853a53d644ebae9ec8f3512e442b1bcb6c315a5b491d119d10624c83014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff2acfcab629bbc8685792603762c921580030ba144af553d271716a95089e107b010000008b483045022100fa579a840ac258871365dd48cd7552f96c8eea69bd00d84f05b283a0dab311e102207e3c0ee9234814cfbb1b659b83671618f45abc1326b9edcc77d552a4f2a805c0014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffdcdc6023bbc9944a658ddc588e61eacb737ddf0a3cd24f113b5a8634c517fcd2000000008b4830450221008d6df731df5d32267954bd7d2dda2302b74c6c2a6aa5c0ca64ecbabc1af03c75022010e55c571d65da7701ae2da1956c442df81bbf076cdbac25133f99d98a9ed34c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffe15557cd5ce258f479dfd6dc6514edf6d7ed5b21fcfa4a038fd69f06b83ac76e010000008b483045022023b3e0ab071eb11de2eb1cc3a67261b866f86bf6867d4558165f7c8c8aca2d86022100dc6e1f53a91de3efe8f63512850811f26284b62f850c70ca73ed5de8771fb451014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff01404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000010000000166d7577163c932b4f9690ca6a80b6e4eb001f0a2fa9023df5595602aae96ed8d000000008a4730440220262b42546302dfb654a229cefc86432b89628ff259dc87edd1154535b16a67e102207b4634c020a97c3e7bbd0d4d19da6aa2269ad9dded4026e896b213d73ca4b63f014104979b82d02226b3a4597523845754d44f13639e3bf2df5e82c6aab2bdc79687368b01b1ab8b19875ae3c90d661a3d0a33161dab29934edeb36aa01976be3baf8affffffff02404b4c00000000001976a9144854e695a02af0aeacb823ccbc272134561e0a1688ac40420f00000000001976a914abee93376d6b37b5c2940655a6fcaf1c8e74237988ac0000000001000000014e3f8ef2e91349a9059cb4f01e54ab2597c1387161d3da89919f7ea6acdbb371010000008c49304602210081f3183471a5ca22307c0800226f3ef9c353069e0773ac76bb580654d56aa523022100d4c56465bdc069060846f4fbf2f6b20520b2a80b08b168b31e66ddb9c694e240014104976c79848e18251612f8940875b2b08d06e6dc73b9840e8860c066b7e87432c477e9a59a453e71e6d76d5fe34058b800a098fc1740ce3012e8fc8a00c96af966ffffffff02c0e1e400000000001976a9144134e75a6fcb6042034aab5e18570cf1f844f54788ac404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000"), SER_NETWORK, PROTOCOL_VERSION);
722 3 : stream >> block;
723 3 : return block;
724 3 : }
|