Skip to content

Commit

Permalink
Fix unlock data directory
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Jan 26, 2024
1 parent 04ee324 commit 3f8c1be
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ void Shutdown(NodeContext& node)
}
}
#endif
if(node.peerman) node.peerman->StopCleanBlockIndex();

if (node.mempool) node.mempool->AddTransactionsUpdated(1);

Expand Down Expand Up @@ -2226,5 +2227,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
void UnlockDataDirectory()
{
// Unlock
LockDataDirectory(true, false);
const fs::path& datadir = gArgs.GetDataDirNet();
if (DirIsWritable(datadir)) {
UnlockDirectory(datadir, ".lock");
}
}
20 changes: 15 additions & 5 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#include <validation.h>
#include <clientversion.h>
#include <consensus/merkle.h>
#include <shutdown.h>
#include <pos.h>

#include <algorithm>
Expand Down Expand Up @@ -629,6 +628,7 @@ class PeerManagerImpl final : public PeerManager
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
void InitCleanBlockIndex() override;
void StopCleanBlockIndex() override;

private:
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
Expand Down Expand Up @@ -832,6 +832,7 @@ class PeerManagerImpl final : public PeerManager
std::set<std::pair<COutPoint, unsigned int>> setStakeSeenOrphan GUARDED_BY(cs_main);
size_t nOrphanBlocksSize = 0;
std::thread threadCleanBlockIndex;
std::atomic<bool> m_stop_thread_clean_block_index = false;

const CChainParams& m_chainparams;
CConnman& m_connman;
Expand Down Expand Up @@ -2030,8 +2031,7 @@ void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
PeerManagerImpl::~PeerManagerImpl()
{
// Stop clean block index thread
if (threadCleanBlockIndex.joinable())
threadCleanBlockIndex.join();
StopCleanBlockIndex();
}

/**
Expand Down Expand Up @@ -6461,7 +6461,7 @@ void PeerManagerImpl::CleanBlockIndex()
unsigned int cleanTimeout = gArgs.GetIntArg("-cleanblockindextimeout", DEFAULT_CLEANBLOCKINDEXTIMEOUT);
if(cleanTimeout == 0) cleanTimeout = DEFAULT_CLEANBLOCKINDEXTIMEOUT;

while(!ShutdownRequested())
while(!m_stop_thread_clean_block_index)
{
if(!m_chainman.ActiveChainstate().IsInitialBlockDownload())
{
Expand Down Expand Up @@ -6518,17 +6518,27 @@ void PeerManagerImpl::CleanBlockIndex()
}
}

for(unsigned int i = 0; (i < cleanTimeout) && !ShutdownRequested(); i++)
for(unsigned int i = 0; (i < cleanTimeout) && !m_stop_thread_clean_block_index; i++)
UninterruptibleSleep(std::chrono::seconds{1});
}
}

void PeerManagerImpl::InitCleanBlockIndex()
{
m_stop_thread_clean_block_index = false;
if(gArgs.GetBoolArg("-cleanblockindex", DEFAULT_CLEANBLOCKINDEX))
threadCleanBlockIndex = std::thread(&util::TraceThread, "cleanindex", [this] { CleanBlockIndex(); });
}

void PeerManagerImpl::StopCleanBlockIndex()
{
if(!m_stop_thread_clean_block_index) {
m_stop_thread_clean_block_index = true;
if (threadCleanBlockIndex.joinable())
threadCleanBlockIndex.join();
}
}

unsigned int GefaultHeaderSpamFilterMaxSize()
{
return Params().GetConsensus().MaxCheckpointSpan();
Expand Down
3 changes: 3 additions & 0 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class PeerManager : public CValidationInterface, public NetEventsInterface

/** Initialize clean block index */
virtual void InitCleanBlockIndex() = 0;

/** Stop clean block index thread */
virtual void StopCleanBlockIndex() = 0;
};

/** Default for -headerspamfiltermaxsize, maximum size of the list of indexes in the header spam filter */
Expand Down
1 change: 0 additions & 1 deletion src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,6 @@ void BitcoinApplication::restart(const QString& program, const QStringList& argu
{
// Unlock the data folder
UnlockDataDirectory();
QThread::currentThread()->sleep(2);

// Create new process and start the wallet
QProcess::startDetached(program, arguments);
Expand Down
6 changes: 6 additions & 0 deletions src/wallet/stake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ void StartStake(CWallet& wallet)
wallet.m_enabled_staking = true;
}

wallet.m_is_staking_thread_stopped = false;
StakeQtums(wallet, true);
}

void StopStake(CWallet& wallet)
{
if(wallet.m_is_staking_thread_stopped)
return;

if(!wallet.stakeThread)
{
if(wallet.m_enabled_staking)
Expand All @@ -41,6 +45,8 @@ void StopStake(CWallet& wallet)
wallet.stakeThread = 0;
wallet.m_stop_staking_thread = false;
}

wallet.m_is_staking_thread_stopped = true;
}

bool CreateCoinStakeFromMine(CWallet& wallet, unsigned int nBits, const CAmount& nTotalFees, uint32_t nTimeBlock, CMutableTransaction& tx, PKHash& pkhash, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoins, std::vector<COutPoint>& setSelectedCoins, bool selectedOnly, bool sign, COutPoint& headerPrevout)
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet

interfaces::Chain& chain = wallet->chain();
std::string name = wallet->GetName();
wallet->StopStake();

// Unregister with the validation interface which also drops shared pointers.
wallet->m_chain_notifications_handler.reset();
Expand Down Expand Up @@ -5174,7 +5175,7 @@ void CWallet::StopStake()

bool CWallet::IsStakeClosing()
{
return chain().shutdownRequested() || m_stop_staking_thread;
return m_stop_staking_thread;
}

void CWallet::updateDelegationsStaker(const std::map<uint160, Delegation> &delegations_staker)
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
int32_t m_staker_max_utxo_script_cache{DEFAULT_STAKER_MAX_UTXO_SCRIPT_CACHE};
uint8_t m_staking_min_fee{DEFAULT_STAKING_MIN_FEE};
std::atomic<bool> m_stop_staking_thread{false};
std::atomic<bool> m_is_staking_thread_stopped{false};

size_t KeypoolCountExternalKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool TopUpKeyPool(unsigned int kpSize = 0);
Expand Down

0 comments on commit 3f8c1be

Please sign in to comment.