diff --git a/src/avalanche.h b/src/avalanche.h --- a/src/avalanche.h +++ b/src/avalanche.h @@ -351,4 +351,9 @@ friend struct AvalancheTest; }; +/** + * Global avalanche instance. + */ +extern std::unique_ptr g_avalanche; + #endif // BITCOIN_AVALANCHE_H diff --git a/src/avalanche.cpp b/src/avalanche.cpp --- a/src/avalanche.cpp +++ b/src/avalanche.cpp @@ -23,6 +23,10 @@ */ static const size_t AVALANCHE_MAX_ELEMENT_POLL = 4096; +// Unfortunately, the bitcoind codebase is full of global and we are kinda +// forced into it here. +std::unique_ptr g_avalanche; + bool VoteRecord::registerVote(NodeId nodeid, uint32_t error) { // We just got a new vote, so there is one less inflight request. clearInflightRequest(); diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -153,6 +154,11 @@ InterruptREST(); InterruptTorControl(); InterruptMapPort(); + if (g_avalanche) { + // Avalanche needs to be stopped before we interrupt the thread group as + // the scheduler will stop working then. + g_avalanche->stopEventLoop(); + } if (g_connman) { g_connman->Interrupt(); } @@ -185,6 +191,16 @@ } StopMapPort(); + // Because avalanche and the network depend on each other, it is important + // to shut them down in this order: + // 1. Stop avalanche event loop. + // 2. Shutdown network processing. + // 3. Destroy AvalancheProcessor. + // 4. Destroy CConnman + if (g_avalanche) { + g_avalanche->stopEventLoop(); + } + // Because these depend on each-other, we make sure that neither can be // using the other before destroying them. if (peerLogic) { @@ -207,6 +223,9 @@ // After the threads that potentially access these pointers have been // stopped, destruct and reset all to nullptr. peerLogic.reset(); + + // Destroy various global instances + g_avalanche.reset(); g_connman.reset(); g_banman.reset(); g_txindex.reset(); @@ -1953,9 +1972,9 @@ config, GetRand(std::numeric_limits::max()), GetRand(std::numeric_limits::max())); - peerLogic.reset(new PeerLogicValidation( + peerLogic = std::make_unique( g_connman.get(), g_banman.get(), scheduler, - gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61))); + gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61)); RegisterValidationInterface(peerLogic.get()); // sanitize comments per BIP-0014, format user agent and check total size @@ -2084,6 +2103,9 @@ 1024; } + // Step 6.5 (I guess ?): Initialize Avalanche. + g_avalanche = std::make_unique(g_connman.get()); + // Step 7: load block chain fReindex = gArgs.GetBoolArg("-reindex", false); @@ -2499,5 +2521,8 @@ }, DUMP_BANS_INTERVAL * 1000); + // Start Avalanche's event loop. + g_avalanche->startEventLoop(scheduler); + return true; }