Skip to content

Commit e93fff1

Browse files
author
MarcoFalke
committed
Merge #11107: Fix races in AppInitMain and others with lock and atomic bools
c626dcb Make fUseCrypto atomic (MeshCollider) 731065b Consistent parameter names in txdb.h (MeshCollider) 35aeabe Make fReindex atomic to avoid race (MeshCollider) 58d91af Fix race for mapBlockIndex in AppInitMain (MeshCollider) Pull request description: Fixes bitcoin/bitcoin#11106 Also makes fReindex atomic as suggested in @TheBlueMatt comment below, and makes fUseCrypto atomic as suggested in 10916 bitcoin/bitcoin@d291e76 just renames the parameters in the txdb header file to make them consistent with those used in the cpp file, noticed it when looking for uses of fReindex Tree-SHA512: b378aa7289fd505b76565cd4d48dcdc04ac5540283ea1c80442170b0f13cb6df771b1a94dd54b7fec3478a7b4668c224ec9d795f16937782724c5d020edd3a42
2 parents 167cef8 + c626dcb commit e93fff1

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

src/init.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,9 +1643,16 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16431643

16441644
// ********************************************************* Step 11: start node
16451645

1646+
int chain_active_height;
1647+
16461648
//// debug print
1647-
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
1648-
LogPrintf("nBestHeight = %d\n", chainActive.Height());
1649+
{
1650+
LOCK(cs_main);
1651+
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
1652+
chain_active_height = chainActive.Height();
1653+
}
1654+
LogPrintf("nBestHeight = %d\n", chain_active_height);
1655+
16491656
if (gArgs.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
16501657
StartTorControl(threadGroup, scheduler);
16511658

@@ -1661,7 +1668,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16611668
connOptions.nMaxOutbound = std::min(MAX_OUTBOUND_CONNECTIONS, connOptions.nMaxConnections);
16621669
connOptions.nMaxAddnode = MAX_ADDNODE_CONNECTIONS;
16631670
connOptions.nMaxFeeler = 1;
1664-
connOptions.nBestHeight = chainActive.Height();
1671+
connOptions.nBestHeight = chain_active_height;
16651672
connOptions.uiInterface = &uiInterface;
16661673
connOptions.m_msgproc = peerLogic.get();
16671674
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);

src/txdb.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ class CBlockTreeDB : public CDBWrapper
115115
CBlockTreeDB& operator=(const CBlockTreeDB&) = delete;
116116

117117
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
118-
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
118+
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
119119
bool ReadLastBlockFile(int &nFile);
120-
bool WriteReindexing(bool fReindex);
121-
bool ReadReindexing(bool &fReindex);
120+
bool WriteReindexing(bool fReindexing);
121+
bool ReadReindexing(bool &fReindexing);
122122
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
123-
bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
123+
bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &vect);
124124
bool WriteFlag(const std::string &name, bool fValue);
125125
bool ReadFlag(const std::string &name, bool &fValue);
126126
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ CWaitableCriticalSection csBestBlock;
6969
CConditionVariable cvBlockChange;
7070
int nScriptCheckThreads = 0;
7171
std::atomic_bool fImporting(false);
72-
bool fReindex = false;
72+
std::atomic_bool fReindex(false);
7373
bool fTxIndex = false;
7474
bool fHavePruned = false;
7575
bool fPruneMode = false;
@@ -3533,7 +3533,7 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams)
35333533
// Check whether we need to continue reindexing
35343534
bool fReindexing = false;
35353535
pblocktree->ReadReindexing(fReindexing);
3536-
fReindex |= fReindexing;
3536+
if(fReindexing) fReindex = true;
35373537

35383538
// Check whether we have a transaction index
35393539
pblocktree->ReadFlag("txindex", fTxIndex);

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ extern const std::string strMessageMagic;
166166
extern CWaitableCriticalSection csBestBlock;
167167
extern CConditionVariable cvBlockChange;
168168
extern std::atomic_bool fImporting;
169-
extern bool fReindex;
169+
extern std::atomic_bool fReindex;
170170
extern int nScriptCheckThreads;
171171
extern bool fTxIndex;
172172
extern bool fIsBareMultisigStd;

src/wallet/crypter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "serialize.h"
1010
#include "support/allocators/secure.h"
1111

12+
#include <atomic>
13+
1214
const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
1315
const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
1416
const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
@@ -118,7 +120,7 @@ class CCryptoKeyStore : public CBasicKeyStore
118120

119121
//! if fUseCrypto is true, mapKeys must be empty
120122
//! if fUseCrypto is false, vMasterKey must be empty
121-
bool fUseCrypto;
123+
std::atomic<bool> fUseCrypto;
122124

123125
//! keeps track of whether Unlock has run a thorough check before
124126
bool fDecryptionThoroughlyChecked;

0 commit comments

Comments
 (0)