Skip to content

Commit

Permalink
Merge bitcoin#11107: Fix races in AppInitMain and others with lock an…
Browse files Browse the repository at this point in the history
…d 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#11106

  Also makes fReindex atomic as suggested in @TheBlueMatt comment below, and makes fUseCrypto atomic as suggested in 10916

  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
  • Loading branch information
MarcoFalke authored and PastaPastaPasta committed Jan 29, 2020
1 parent 8e9dcba commit d44ea69
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/init.cpp
Expand Up @@ -2189,9 +2189,15 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)

// ********************************************************* Step 12: start node

int chain_active_height;

//// debug print
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
LogPrintf("chainActive.Height() = %d\n", chainActive.Height());
{
LOCK(cs_main);
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
chain_active_height = chainActive.Height();
}
LogPrintf("chainActive.Height() = %d\n", chain_active_height);
if (gArgs.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
StartTorControl(threadGroup, scheduler);

Expand All @@ -2206,7 +2212,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
connOptions.nMaxOutbound = std::min(MAX_OUTBOUND_CONNECTIONS, connOptions.nMaxConnections);
connOptions.nMaxAddnode = MAX_ADDNODE_CONNECTIONS;
connOptions.nMaxFeeler = 1;
connOptions.nBestHeight = chainActive.Height();
connOptions.nBestHeight = chain_active_height;
connOptions.uiInterface = &uiInterface;
connOptions.m_msgproc = peerLogic.get();
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
Expand Down
8 changes: 4 additions & 4 deletions src/txdb.h
Expand Up @@ -117,13 +117,13 @@ class CBlockTreeDB : public CDBWrapper
CBlockTreeDB& operator=(const CBlockTreeDB&) = delete;

bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
bool ReadLastBlockFile(int &nFile);
bool WriteReindexing(bool fReindex);
bool ReadReindexing(bool &fReindex);
bool WriteReindexing(bool fReindexing);
bool ReadReindexing(bool &fReindexing);
bool HasTxIndex(const uint256 &txid);
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &vect);
bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value);
bool UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect);
bool UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect);
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Expand Up @@ -81,7 +81,7 @@ CWaitableCriticalSection csBestBlock;
CConditionVariable cvBlockChange;
int nScriptCheckThreads = 0;
std::atomic_bool fImporting(false);
bool fReindex = false;
std::atomic_bool fReindex(false);
bool fTxIndex = true;
bool fAddressIndex = false;
bool fTimestampIndex = false;
Expand Down Expand Up @@ -3968,7 +3968,7 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams)
// Check whether we need to continue reindexing
bool fReindexing = false;
pblocktree->ReadReindexing(fReindexing);
fReindex |= fReindexing;
if(fReindexing) fReindex = true;

// Check whether we have a transaction index
pblocktree->ReadFlag("txindex", fTxIndex);
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Expand Up @@ -170,7 +170,7 @@ extern const std::string strMessageMagic;
extern CWaitableCriticalSection csBestBlock;
extern CConditionVariable cvBlockChange;
extern std::atomic_bool fImporting;
extern bool fReindex;
extern std::atomic_bool fReindex;
extern int nScriptCheckThreads;
extern bool fTxIndex;
extern bool fAddressIndex;
Expand Down
4 changes: 3 additions & 1 deletion src/wallet/crypter.h
Expand Up @@ -9,6 +9,8 @@
#include "serialize.h"
#include "support/allocators/secure.h"

#include <atomic>

const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
Expand Down Expand Up @@ -124,7 +126,7 @@ class CCryptoKeyStore : public CBasicKeyStore

//! if fUseCrypto is true, mapKeys must be empty
//! if fUseCrypto is false, vMasterKey must be empty
bool fUseCrypto;
std::atomic<bool> fUseCrypto;

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

0 comments on commit d44ea69

Please sign in to comment.