Skip to content

Commit

Permalink
Use a new local scheduler instead of the global one
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone committed Sep 14, 2023
1 parent 79b09bb commit edddada
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,7 @@ bool AppInitMain()
// The on-disk coinsdb is now in a good state, create the cache
pcoinsTip.reset(new CCoinsViewCache(pcoinscatcher.get()));

InitTierTwoPostCoinsCacheLoad(&scheduler);
InitTierTwoPostCoinsCacheLoad();

bool is_coinsview_empty = fReset || fReindexChainState || pcoinsTip->GetBestBlock().IsNull();
if (!is_coinsview_empty) {
Expand Down
11 changes: 9 additions & 2 deletions src/llmq/quorums_chainlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ std::string CChainLockSig::ToString() const
return strprintf("CChainLockSig(nHeight=%d, blockHash=%s)", nHeight, blockHash.ToString());
}

CChainLocksHandler::CChainLocksHandler(CScheduler* _scheduler) :
scheduler(_scheduler)
CChainLocksHandler::CChainLocksHandler()
{
scheduler = new CScheduler();
CScheduler::Function serviceLoop = boost::bind(&CScheduler::serviceQueue, scheduler);
scheduler_thread = new boost::thread(boost::bind(&TraceThread<CScheduler::Function>, "chainlock-scheduler", serviceLoop));
}

CChainLocksHandler::~CChainLocksHandler()
{
scheduler_thread->interrupt();
scheduler_thread->join();
delete scheduler_thread;
delete scheduler;
}

void CChainLocksHandler::Start()
Expand All @@ -47,6 +53,7 @@ void CChainLocksHandler::Start()

void CChainLocksHandler::Stop()
{
scheduler->stop();
quorumSigningManager->UnregisterRecoveredSigsListener(this);
}

Expand Down
5 changes: 3 additions & 2 deletions src/llmq/quorums_chainlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

#include "net.h"
#include "chainparams.h"
#include "threadsafety.h"

#include <atomic>
#include <boost/thread.hpp>

class CBlockIndex;
class CScheduler;
Expand Down Expand Up @@ -45,6 +45,7 @@ class CChainLocksHandler : public CRecoveredSigsListener

private:
CScheduler* scheduler;
boost::thread* scheduler_thread;
RecursiveMutex cs;
bool tryLockChainTipScheduled GUARDED_BY(cs) {false};

Expand All @@ -63,7 +64,7 @@ class CChainLocksHandler : public CRecoveredSigsListener
int64_t lastCleanupTime{0};

public:
CChainLocksHandler(CScheduler* _scheduler);
CChainLocksHandler();
~CChainLocksHandler();

void Start();
Expand Down
4 changes: 2 additions & 2 deletions src/llmq/quorums_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace llmq

CBLSWorker* blsWorker;

void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests)
void InitLLMQSystem(CEvoDB& evoDb, bool unitTests)
{
blsWorker = new CBLSWorker();

Expand All @@ -29,7 +29,7 @@ void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests)
quorumManager.reset(new CQuorumManager(evoDb, *blsWorker, *quorumDKGSessionManager));
quorumSigSharesManager = new CSigSharesManager();
quorumSigningManager = new CSigningManager(unitTests);
chainLocksHandler = new CChainLocksHandler(scheduler);
chainLocksHandler = new CChainLocksHandler();
}

void DestroyLLMQSystem()
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/quorums_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace llmq
{

// Init/destroy LLMQ globals
void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests);
void InitLLMQSystem(CEvoDB& evoDb, bool unitTests);
void DestroyLLMQSystem();

// Manage scheduled tasks, threads, listeners etc.
Expand Down
3 changes: 2 additions & 1 deletion src/test/test_pivx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
llmq::InitLLMQSystem(*evoDb, &scheduler, true);
llmq::InitLLMQSystem(*evoDb, true);
if (!LoadGenesisBlock()) {
throw std::runtime_error("Error initializing block database");
}
Expand All @@ -141,6 +141,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
TestingSetup::~TestingSetup()
{
scheduler.stop();
llmq::StopLLMQSystem();
threadGroup.interrupt_all();
threadGroup.join_all();
GetMainSignals().FlushBackgroundCallbacks();
Expand Down
4 changes: 2 additions & 2 deletions src/tiertwo/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ void InitTierTwoPreChainLoad(bool fReindex)
deterministicMNManager.reset(new CDeterministicMNManager(*evoDb));
}

void InitTierTwoPostCoinsCacheLoad(CScheduler* scheduler)
void InitTierTwoPostCoinsCacheLoad()
{
// Initialize LLMQ system
llmq::InitLLMQSystem(*evoDb, scheduler, false);
llmq::InitLLMQSystem(*evoDb, false);
}

void InitTierTwoChainTip()
Expand Down
2 changes: 1 addition & 1 deletion src/tiertwo/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void ResetTierTwoInterfaces();
void InitTierTwoPreChainLoad(bool fReindex);

/** Inits the tier two global objects that require access to the coins tip cache */
void InitTierTwoPostCoinsCacheLoad(CScheduler* scheduler);
void InitTierTwoPostCoinsCacheLoad();

/** Initialize chain tip */
void InitTierTwoChainTip();
Expand Down
6 changes: 3 additions & 3 deletions test/functional/tiertwo_chainlocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ def run_test(self):

# Keep node connected and let it try to reorg the chain
good_tip = self.nodes[0].getbestblockhash()
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
# Restart it so that it forgets all the chainlocks from the past
self.stop_node(0)
self.start_node(0, extra_args=self.extra_args[0])
connect_nodes(self.nodes[0], 1)
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
# Now try to reorg the chain
self.nodes[0].generate(2)
time.sleep(2)
time.sleep(6)
assert self.nodes[1].getbestblockhash() == good_tip
self.nodes[0].generate(2)
time.sleep(2)
time.sleep(6)
assert self.nodes[1].getbestblockhash() == good_tip

# Now let the node which is on the wrong chain reorg back to the locked chain
Expand Down

0 comments on commit edddada

Please sign in to comment.