diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 4411b802de..7f22fa1164 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -177,6 +177,7 @@ public: fDefaultConsistencyChecks = false; fRequireStandard = true; m_is_test_chain = false; + m_is_mockable_chain = false; checkpointData = { { @@ -287,7 +288,7 @@ public: fDefaultConsistencyChecks = false; fRequireStandard = false; m_is_test_chain = true; - + m_is_mockable_chain = false; checkpointData = { { @@ -372,6 +373,7 @@ public: fDefaultConsistencyChecks = true; fRequireStandard = true; m_is_test_chain = true; + m_is_mockable_chain = true; checkpointData = { { diff --git a/src/chainparams.h b/src/chainparams.h index 43f195b70e..5d30f098f7 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -72,6 +72,8 @@ public: bool RequireStandard() const { return fRequireStandard; } /** If this chain is exclusively used for testing */ bool IsTestChain() const { return m_is_test_chain; } + /** If this chain allows time to be mocked */ + bool IsMockableChain() const { return m_is_mockable_chain; } uint64_t PruneAfterHeight() const { return nPruneAfterHeight; } /** Minimum free space (in GB) needed for data directory */ uint64_t AssumedBlockchainSize() const { return m_assumed_blockchain_size; } @@ -118,6 +120,7 @@ protected: bool fDefaultConsistencyChecks; bool fRequireStandard; bool m_is_test_chain; + bool m_is_mockable_chain; CCheckpointData checkpointData; ChainTxData chainTxData; // ELEMENTS extra fields: diff --git a/src/init.cpp b/src/init.cpp index 8b49c48b68..92c73cb22e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -160,8 +160,6 @@ NODISCARD static bool CreatePidFile() static std::unique_ptr globalVerifyHandle; static boost::thread_group threadGroup; -static CScheduler scheduler; -static CScheduler reverification_scheduler; void Interrupt(NodeContext& node) { @@ -299,6 +297,8 @@ void Shutdown(NodeContext& node) globalVerifyHandle.reset(); ECC_Stop(); if (node.mempool) node.mempool = nullptr; + node.scheduler.reset(); + node.reverification_scheduler.reset(); LogPrintf("%s: done\n", __func__); } @@ -1321,16 +1321,21 @@ bool AppInitMain(NodeContext& node) } } + assert(!node.scheduler); + node.scheduler = MakeUnique(); + assert(!node.reverification_scheduler); + node.reverification_scheduler = MakeUnique(); + // Start the lightweight task scheduler thread - CScheduler::Function serviceLoop = std::bind(&CScheduler::serviceQueue, &scheduler); + CScheduler::Function serviceLoop = [&node]{ node.scheduler->serviceQueue(); }; threadGroup.create_thread(std::bind(&TraceThread, "scheduler", serviceLoop)); // Gather some entropy once per minute. - scheduler.scheduleEvery([]{ + node.scheduler->scheduleEvery([]{ RandAddPeriodic(); }, 60000); - GetMainSignals().RegisterBackgroundSignalScheduler(scheduler); + GetMainSignals().RegisterBackgroundSignalScheduler(*node.scheduler); // Create client interfaces for wallets that are supposed to be loaded // according to -wallet and -disablewallet options. This only constructs @@ -1383,7 +1388,7 @@ bool AppInitMain(NodeContext& node) assert(!node.connman); node.connman = std::unique_ptr(new CConnman(GetRand(std::numeric_limits::max()), GetRand(std::numeric_limits::max()))); - node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), scheduler)); + node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), *node.scheduler)); RegisterValidationInterface(node.peer_logic.get()); // sanitize comments per BIP-0014, format user agent and check total size @@ -1875,7 +1880,7 @@ bool AppInitMain(NodeContext& node) connOptions.m_specified_outgoing = connect; } } - if (!node.connman->Start(scheduler, connOptions)) { + if (!node.connman->Start(*node.scheduler, connOptions)) { return false; } @@ -1926,23 +1931,23 @@ bool AppInitMain(NodeContext& node) } // Start the lightweight block re-evaluation scheduler thread - CScheduler::Function reevaluationLoop = std::bind(&CScheduler::serviceQueue, &reverification_scheduler); + CScheduler::Function reevaluationLoop = [&node]{ node.reverification_scheduler->serviceQueue(); }; threadGroup.create_thread(std::bind(&TraceThread, "reevaluation_scheduler", reevaluationLoop)); CScheduler::Function f2 = boost::bind(&MainchainRPCCheck, false); unsigned int check_rpc_every = gArgs.GetArg("-recheckpeginblockinterval", 120) * 1000; if (check_rpc_every) { - reverification_scheduler.scheduleEvery(f2, check_rpc_every); + node.reverification_scheduler->scheduleEvery(f2, check_rpc_every); } uiInterface.InitMessage(_("Done loading").translated); for (const auto& client : node.chain_clients) { - client->start(scheduler); + client->start(*node.scheduler); } BanMan* banman = node.banman.get(); - scheduler.scheduleEvery([banman]{ + node.scheduler->scheduleEvery([banman]{ banman->DumpBanlist(); }, DUMP_BANS_INTERVAL * 1000); diff --git a/src/node/context.cpp b/src/node/context.cpp index 26a01420c8..5b19a41bd4 100644 --- a/src/node/context.cpp +++ b/src/node/context.cpp @@ -8,6 +8,7 @@ #include #include #include +#include NodeContext::NodeContext() {} NodeContext::~NodeContext() {} diff --git a/src/node/context.h b/src/node/context.h index dab5b5d048..8a08801940 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -10,6 +10,7 @@ class BanMan; class CConnman; +class CScheduler; class CTxMemPool; class PeerLogicValidation; namespace interfaces { @@ -34,6 +35,8 @@ struct NodeContext { std::unique_ptr banman; std::unique_ptr chain; std::vector> chain_clients; + std::unique_ptr scheduler; + std::unique_ptr reverification_scheduler; //! Declare default constructor and destructor that are not inline, so code //! instantiating the NodeContext struct doesn't need to #include class diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 6f212aa9fe..ef5f915122 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -27,6 +27,7 @@ public: static const CRPCConvertParam vRPCConvertParams[] = { { "setmocktime", 0, "timestamp" }, + { "mockscheduler", 0, "delta_time" }, { "utxoupdatepsbt", 1, "descriptors" }, { "generatetoaddress", 0, "nblocks" }, { "generatetoaddress", 2, "maxtries" }, diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 78b45dc90f..d80370fd78 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -7,11 +7,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include