From 58d91af59e6417ad4dde0c5e1c9bfd18017d755d Mon Sep 17 00:00:00 2001
From: MeshCollider <dobsonsa68@gmail.com>
Date: Tue, 22 Aug 2017 21:02:25 +1200
Subject: [PATCH 1/4] Fix race for mapBlockIndex in AppInitMain

---
 src/init.cpp | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/init.cpp b/src/init.cpp
index 3f68ea102..af1d4a085 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1631,9 +1631,16 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
 
     // ********************************************************* Step 11: start node
 
+    int chain_active_height;
+
     //// debug print
-    LogPrintf("mapBlockIndex.size() = %u\n",   mapBlockIndex.size());
-    LogPrintf("nBestHeight = %d\n",                   chainActive.Height());
+    {
+        LOCK(cs_main);
+        LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
+        chain_active_height = chainActive.Height();
+    }
+    LogPrintf("nBestHeight = %d\n", chain_active_height);
+
     if (gArgs.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
         StartTorControl(threadGroup, scheduler);
 
@@ -1649,7 +1656,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.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
     connOptions.nReceiveFloodSize = 1000*gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);

From 35aeabec62cf10f3c0de297b1189f0a669b69d6e Mon Sep 17 00:00:00 2001
From: MeshCollider <dobsonsa68@gmail.com>
Date: Thu, 24 Aug 2017 13:45:26 +1200
Subject: [PATCH 2/4] Make fReindex atomic to avoid race

---
 src/validation.cpp | 4 ++--
 src/validation.h   | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/validation.cpp b/src/validation.cpp
index d1a8b8460..0d4fdf79a 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -66,7 +66,7 @@ CWaitableCriticalSection csBestBlock;
 CConditionVariable cvBlockChange;
 int nScriptCheckThreads = 0;
 std::atomic_bool fImporting(false);
-bool fReindex = false;
+std::atomic_bool fReindex(false);
 bool fTxIndex = false;
 bool fHavePruned = false;
 bool fPruneMode = false;
@@ -3523,7 +3523,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);
diff --git a/src/validation.h b/src/validation.h
index d0f6cdc13..10511fce3 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -167,7 +167,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 fIsBareMultisigStd;

From 731065b114452ff770320d09639448b3c9a74b0a Mon Sep 17 00:00:00 2001
From: MeshCollider <dobsonsa68@gmail.com>
Date: Thu, 24 Aug 2017 13:58:28 +1200
Subject: [PATCH 3/4] Consistent parameter names in txdb.h

---
 src/txdb.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/txdb.h b/src/txdb.h
index d1cd5a425..ab3a9f7e3 100644
--- a/src/txdb.h
+++ b/src/txdb.h
@@ -115,12 +115,12 @@ private:
     void operator=(const CBlockTreeDB&);
 public:
     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 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 WriteFlag(const std::string &name, bool fValue);
     bool ReadFlag(const std::string &name, bool &fValue);
     bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);

From c626dcb50eed496462fd4ac3e05bf79164749ebe Mon Sep 17 00:00:00 2001
From: MeshCollider <dobsonsa68@gmail.com>
Date: Sun, 10 Sep 2017 11:19:46 +1200
Subject: [PATCH 4/4] Make fUseCrypto atomic

---
 src/wallet/crypter.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h
index f1e8a2565..0948de42e 100644
--- a/src/wallet/crypter.h
+++ b/src/wallet/crypter.h
@@ -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;
@@ -118,7 +120,7 @@ private:
 
     //! 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;