From de39c95c2468cc643a5d5bed9e5dd8cea6a40747 Mon Sep 17 00:00:00 2001
From: "Wladimir J. van der Laan" <laanwj@gmail.com>
Date: Mon, 18 Apr 2016 14:03:53 +0200
Subject: [PATCH 1/4] test: move accounting_tests and rpc_wallet_tests to
 wallet/test

Move the two other wallet tests to where they belong.
---
 src/Makefile.test.include                  | 4 ++--
 src/{ => wallet}/test/accounting_tests.cpp | 0
 src/{ => wallet}/test/rpc_wallet_tests.cpp | 0
 3 files changed, 2 insertions(+), 2 deletions(-)
 rename src/{ => wallet}/test/accounting_tests.cpp (100%)
 rename src/{ => wallet}/test/rpc_wallet_tests.cpp (100%)

diff --git a/src/Makefile.test.include b/src/Makefile.test.include
index f025b18c7..989718554 100644
--- a/src/Makefile.test.include
+++ b/src/Makefile.test.include
@@ -90,9 +90,9 @@ BITCOIN_TESTS =\
 
 if ENABLE_WALLET
 BITCOIN_TESTS += \
-  test/accounting_tests.cpp \
+  wallet/test/accounting_tests.cpp \
   wallet/test/wallet_tests.cpp \
-  test/rpc_wallet_tests.cpp
+  wallet/test/rpc_wallet_tests.cpp
 endif
 
 test_test_bitcoin_SOURCES = $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
diff --git a/src/test/accounting_tests.cpp b/src/wallet/test/accounting_tests.cpp
similarity index 100%
rename from src/test/accounting_tests.cpp
rename to src/wallet/test/accounting_tests.cpp
diff --git a/src/test/rpc_wallet_tests.cpp b/src/wallet/test/rpc_wallet_tests.cpp
similarity index 100%
rename from src/test/rpc_wallet_tests.cpp
rename to src/wallet/test/rpc_wallet_tests.cpp

From f4eae2d910d9edb3750efec4facbeab161cce593 Mon Sep 17 00:00:00 2001
From: "Wladimir J. van der Laan" <laanwj@gmail.com>
Date: Mon, 18 Apr 2016 14:54:57 +0200
Subject: [PATCH 2/4] test: Create test fixture for wallet

Removes all the `#ifdef ENABLE_WALLET` from `test_bitcoin` by
making the wallet tests use their own fixture.
---
 src/Makefile.test.include               |  2 ++
 src/test/test_bitcoin.cpp               | 23 --------------------
 src/test/test_bitcoin.h                 |  3 +--
 src/wallet/test/accounting_tests.cpp    |  4 ++--
 src/wallet/test/rpc_wallet_tests.cpp    |  4 ++--
 src/wallet/test/wallet_test_fixture.cpp | 28 +++++++++++++++++++++++++
 src/wallet/test/wallet_test_fixture.h   | 18 ++++++++++++++++
 src/wallet/test/wallet_tests.cpp        |  4 ++--
 8 files changed, 55 insertions(+), 31 deletions(-)
 create mode 100644 src/wallet/test/wallet_test_fixture.cpp
 create mode 100644 src/wallet/test/wallet_test_fixture.h

diff --git a/src/Makefile.test.include b/src/Makefile.test.include
index 989718554..08e2f6af4 100644
--- a/src/Makefile.test.include
+++ b/src/Makefile.test.include
@@ -90,6 +90,8 @@ BITCOIN_TESTS =\
 
 if ENABLE_WALLET
 BITCOIN_TESTS += \
+  wallet/test/wallet_test_fixture.cpp \
+  wallet/test/wallet_test_fixture.h \
   wallet/test/accounting_tests.cpp \
   wallet/test/wallet_tests.cpp \
   wallet/test/rpc_wallet_tests.cpp
diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp
index 97b999625..eb8ac01e8 100644
--- a/src/test/test_bitcoin.cpp
+++ b/src/test/test_bitcoin.cpp
@@ -19,10 +19,6 @@
 #include "ui_interface.h"
 #include "rpc/server.h"
 #include "rpc/register.h"
-#ifdef ENABLE_WALLET
-#include "wallet/db.h"
-#include "wallet/wallet.h"
-#endif
 
 #include "test/testutil.h"
 
@@ -57,10 +53,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
         // Ideally we'd move all the RPC tests to the functional testing framework
         // instead of unit tests, but for now we need these here.
         RegisterAllCoreRPCCommands(tableRPC);
-#ifdef ENABLE_WALLET
-        bitdb.MakeMock();
-        RegisterWalletRPCCommands(tableRPC);
-#endif
         ClearDatadirCache();
         pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
         boost::filesystem::create_directories(pathTemp);
@@ -69,12 +61,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
         pcoinsdbview = new CCoinsViewDB(1 << 23, true);
         pcoinsTip = new CCoinsViewCache(pcoinsdbview);
         InitBlockIndex(chainparams);
-#ifdef ENABLE_WALLET
-        bool fFirstRun;
-        pwalletMain = new CWallet("wallet.dat");
-        pwalletMain->LoadWallet(fFirstRun);
-        RegisterValidationInterface(pwalletMain);
-#endif
         nScriptCheckThreads = 3;
         for (int i=0; i < nScriptCheckThreads-1; i++)
             threadGroup.create_thread(&ThreadScriptCheck);
@@ -86,19 +72,10 @@ TestingSetup::~TestingSetup()
         UnregisterNodeSignals(GetNodeSignals());
         threadGroup.interrupt_all();
         threadGroup.join_all();
-#ifdef ENABLE_WALLET
-        UnregisterValidationInterface(pwalletMain);
-        delete pwalletMain;
-        pwalletMain = NULL;
-#endif
         UnloadBlockIndex();
         delete pcoinsTip;
         delete pcoinsdbview;
         delete pblocktree;
-#ifdef ENABLE_WALLET
-        bitdb.Flush(true);
-        bitdb.Reset();
-#endif
         boost::filesystem::remove_all(pathTemp);
 }
 
diff --git a/src/test/test_bitcoin.h b/src/test/test_bitcoin.h
index 769ae5a13..57f66f6c6 100644
--- a/src/test/test_bitcoin.h
+++ b/src/test/test_bitcoin.h
@@ -25,8 +25,7 @@ struct BasicTestingSetup {
 };
 
 /** Testing setup that configures a complete environment.
- * Included are data directory, coins database, script check threads
- * and wallet (if enabled) setup.
+ * Included are data directory, coins database, script check threads setup.
  */
 struct TestingSetup: public BasicTestingSetup {
     CCoinsViewDB *pcoinsdbview;
diff --git a/src/wallet/test/accounting_tests.cpp b/src/wallet/test/accounting_tests.cpp
index dad191c68..d075b2b64 100644
--- a/src/wallet/test/accounting_tests.cpp
+++ b/src/wallet/test/accounting_tests.cpp
@@ -5,7 +5,7 @@
 #include "wallet/wallet.h"
 #include "wallet/walletdb.h"
 
-#include "test/test_bitcoin.h"
+#include "wallet/test/wallet_test_fixture.h"
 
 #include <stdint.h>
 
@@ -14,7 +14,7 @@
 
 extern CWallet* pwalletMain;
 
-BOOST_FIXTURE_TEST_SUITE(accounting_tests, TestingSetup)
+BOOST_FIXTURE_TEST_SUITE(accounting_tests, WalletTestingSetup)
 
 static void
 GetResults(CWalletDB& walletdb, std::map<CAmount, CAccountingEntry>& results)
diff --git a/src/wallet/test/rpc_wallet_tests.cpp b/src/wallet/test/rpc_wallet_tests.cpp
index 3443be209..4e7d177f5 100644
--- a/src/wallet/test/rpc_wallet_tests.cpp
+++ b/src/wallet/test/rpc_wallet_tests.cpp
@@ -9,7 +9,7 @@
 #include "main.h"
 #include "wallet/wallet.h"
 
-#include "test/test_bitcoin.h"
+#include "wallet/test/wallet_test_fixture.h"
 
 #include <boost/algorithm/string.hpp>
 #include <boost/test/unit_test.hpp>
@@ -23,7 +23,7 @@ extern UniValue CallRPC(string args);
 
 extern CWallet* pwalletMain;
 
-BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
+BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, WalletTestingSetup)
 
 BOOST_AUTO_TEST_CASE(rpc_addmultisig)
 {
diff --git a/src/wallet/test/wallet_test_fixture.cpp b/src/wallet/test/wallet_test_fixture.cpp
new file mode 100644
index 000000000..837030096
--- /dev/null
+++ b/src/wallet/test/wallet_test_fixture.cpp
@@ -0,0 +1,28 @@
+#include "wallet/test/wallet_test_fixture.h"
+
+#include "rpc/server.h"
+#include "wallet/db.h"
+#include "wallet/wallet.h"
+
+WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
+    TestingSetup(chainName)
+{
+    bitdb.MakeMock();
+
+    bool fFirstRun;
+    pwalletMain = new CWallet("wallet.dat");
+    pwalletMain->LoadWallet(fFirstRun);
+    RegisterValidationInterface(pwalletMain);
+
+    RegisterWalletRPCCommands(tableRPC);
+}
+
+WalletTestingSetup::~WalletTestingSetup()
+{
+    UnregisterValidationInterface(pwalletMain);
+    delete pwalletMain;
+    pwalletMain = NULL;
+
+    bitdb.Flush(true);
+    bitdb.Reset();
+}
diff --git a/src/wallet/test/wallet_test_fixture.h b/src/wallet/test/wallet_test_fixture.h
new file mode 100644
index 000000000..97a6d9839
--- /dev/null
+++ b/src/wallet/test/wallet_test_fixture.h
@@ -0,0 +1,18 @@
+// Copyright (c) 2016 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_WALLET_TEST_FIXTURE_H
+#define BITCOIN_WALLET_TEST_FIXTURE_H
+
+#include "test/test_bitcoin.h"
+
+/** Testing setup and teardown for wallet.
+ */
+struct WalletTestingSetup: public TestingSetup {
+    WalletTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
+    ~WalletTestingSetup();
+};
+
+#endif
+
diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
index e84d58802..b759a6b2e 100644
--- a/src/wallet/test/wallet_tests.cpp
+++ b/src/wallet/test/wallet_tests.cpp
@@ -9,7 +9,7 @@
 #include <utility>
 #include <vector>
 
-#include "test/test_bitcoin.h"
+#include "wallet/test/wallet_test_fixture.h"
 
 #include <boost/foreach.hpp>
 #include <boost/test/unit_test.hpp>
@@ -25,7 +25,7 @@ using namespace std;
 
 typedef set<pair<const CWalletTx*,unsigned int> > CoinSet;
 
-BOOST_FIXTURE_TEST_SUITE(wallet_tests, TestingSetup)
+BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
 
 static CWallet wallet;
 static vector<COutput> vCoins;

From a25a4f5b04c3e045557e9e7e807b2af74ad75128 Mon Sep 17 00:00:00 2001
From: "Wladimir J. van der Laan" <laanwj@gmail.com>
Date: Mon, 18 Apr 2016 15:12:46 +0200
Subject: [PATCH 3/4] =?UTF-8?q?wallet=5Fismine.h=20=E2=86=92=20script/ismi?=
 =?UTF-8?q?ne.h?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Removes conditional dependency of `src/test` on wallet.

Makes multisig and P2SH tests complete without wallet built-in.
---
 src/Makefile.am                                     |  4 ++--
 src/{wallet/wallet_ismine.cpp => script/ismine.cpp} |  2 +-
 src/{wallet/wallet_ismine.h => script/ismine.h}     |  6 +++---
 src/test/multisig_tests.cpp                         | 12 +-----------
 src/test/script_P2SH_tests.cpp                      |  9 +--------
 src/wallet/wallet.h                                 |  2 +-
 6 files changed, 9 insertions(+), 26 deletions(-)
 rename src/{wallet/wallet_ismine.cpp => script/ismine.cpp} (99%)
 rename src/{wallet/wallet_ismine.h => script/ismine.h} (90%)

diff --git a/src/Makefile.am b/src/Makefile.am
index d91e959cf..24744835f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -135,6 +135,7 @@ BITCOIN_CORE_H = \
   script/sigcache.h \
   script/sign.h \
   script/standard.h \
+  script/ismine.h \
   streams.h \
   support/allocators/secure.h \
   support/allocators/zeroafterfree.h \
@@ -157,7 +158,6 @@ BITCOIN_CORE_H = \
   wallet/db.h \
   wallet/rpcwallet.h \
   wallet/wallet.h \
-  wallet/wallet_ismine.h \
   wallet/walletdb.h \
   zmq/zmqabstractnotifier.h \
   zmq/zmqconfig.h\
@@ -199,6 +199,7 @@ libbitcoin_server_a_SOURCES = \
   rpc/rawtransaction.cpp \
   rpc/server.cpp \
   script/sigcache.cpp \
+  script/ismine.cpp \
   timedata.cpp \
   torcontrol.cpp \
   txdb.cpp \
@@ -229,7 +230,6 @@ libbitcoin_wallet_a_SOURCES = \
   wallet/rpcdump.cpp \
   wallet/rpcwallet.cpp \
   wallet/wallet.cpp \
-  wallet/wallet_ismine.cpp \
   wallet/walletdb.cpp \
   policy/rbf.cpp \
   $(BITCOIN_CORE_H)
diff --git a/src/wallet/wallet_ismine.cpp b/src/script/ismine.cpp
similarity index 99%
rename from src/wallet/wallet_ismine.cpp
rename to src/script/ismine.cpp
index ebda5cc53..535c56b57 100644
--- a/src/wallet/wallet_ismine.cpp
+++ b/src/script/ismine.cpp
@@ -3,7 +3,7 @@
 // Distributed under the MIT software license, see the accompanying
 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 
-#include "wallet_ismine.h"
+#include "ismine.h"
 
 #include "key.h"
 #include "keystore.h"
diff --git a/src/wallet/wallet_ismine.h b/src/script/ismine.h
similarity index 90%
rename from src/wallet/wallet_ismine.h
rename to src/script/ismine.h
index 51afd1b14..4b7db8802 100644
--- a/src/wallet/wallet_ismine.h
+++ b/src/script/ismine.h
@@ -3,8 +3,8 @@
 // Distributed under the MIT software license, see the accompanying
 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 
-#ifndef BITCOIN_WALLET_WALLET_ISMINE_H
-#define BITCOIN_WALLET_WALLET_ISMINE_H
+#ifndef BITCOIN_SCRIPT_ISMINE_H
+#define BITCOIN_SCRIPT_ISMINE_H
 
 #include "script/standard.h"
 
@@ -31,4 +31,4 @@ typedef uint8_t isminefilter;
 isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
 isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest);
 
-#endif // BITCOIN_WALLET_WALLET_ISMINE_H
+#endif // BITCOIN_SCRIPT_ISMINE_H
diff --git a/src/test/multisig_tests.cpp b/src/test/multisig_tests.cpp
index edf1650ca..d48a68ba5 100644
--- a/src/test/multisig_tests.cpp
+++ b/src/test/multisig_tests.cpp
@@ -9,12 +9,10 @@
 #include "script/script_error.h"
 #include "script/interpreter.h"
 #include "script/sign.h"
+#include "script/ismine.h"
 #include "uint256.h"
 #include "test/test_bitcoin.h"
 
-#ifdef ENABLE_WALLET
-#include "wallet/wallet_ismine.h"
-#endif
 
 #include <boost/foreach.hpp>
 #include <boost/test/unit_test.hpp>
@@ -210,10 +208,8 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1)
         CTxDestination addr;
         BOOST_CHECK(ExtractDestination(s, addr));
         BOOST_CHECK(addr == keyaddr[0]);
-#ifdef ENABLE_WALLET
         BOOST_CHECK(IsMine(keystore, s));
         BOOST_CHECK(!IsMine(emptykeystore, s));
-#endif
     }
     {
         vector<valtype> solutions;
@@ -225,10 +221,8 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1)
         CTxDestination addr;
         BOOST_CHECK(ExtractDestination(s, addr));
         BOOST_CHECK(addr == keyaddr[0]);
-#ifdef ENABLE_WALLET
         BOOST_CHECK(IsMine(keystore, s));
         BOOST_CHECK(!IsMine(emptykeystore, s));
-#endif
     }
     {
         vector<valtype> solutions;
@@ -239,11 +233,9 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1)
         BOOST_CHECK_EQUAL(solutions.size(), 4U);
         CTxDestination addr;
         BOOST_CHECK(!ExtractDestination(s, addr));
-#ifdef ENABLE_WALLET
         BOOST_CHECK(IsMine(keystore, s));
         BOOST_CHECK(!IsMine(emptykeystore, s));
         BOOST_CHECK(!IsMine(partialkeystore, s));
-#endif
     }
     {
         vector<valtype> solutions;
@@ -258,11 +250,9 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1)
         BOOST_CHECK(addrs[0] == keyaddr[0]);
         BOOST_CHECK(addrs[1] == keyaddr[1]);
         BOOST_CHECK(nRequired == 1);
-#ifdef ENABLE_WALLET
         BOOST_CHECK(IsMine(keystore, s));
         BOOST_CHECK(!IsMine(emptykeystore, s));
         BOOST_CHECK(!IsMine(partialkeystore, s));
-#endif
     }
     {
         vector<valtype> solutions;
diff --git a/src/test/script_P2SH_tests.cpp b/src/test/script_P2SH_tests.cpp
index 28b85e8d2..d10284fe9 100644
--- a/src/test/script_P2SH_tests.cpp
+++ b/src/test/script_P2SH_tests.cpp
@@ -9,12 +9,9 @@
 #include "script/script.h"
 #include "script/script_error.h"
 #include "script/sign.h"
+#include "script/ismine.h"
 #include "test/test_bitcoin.h"
 
-#ifdef ENABLE_WALLET
-#include "wallet/wallet_ismine.h"
-#endif
-
 #include <vector>
 
 #include <boost/test/unit_test.hpp>
@@ -101,9 +98,7 @@ BOOST_AUTO_TEST_CASE(sign)
         txTo[i].vin[0].prevout.n = i;
         txTo[i].vin[0].prevout.hash = txFrom.GetHash();
         txTo[i].vout[0].nValue = 1;
-#ifdef ENABLE_WALLET
         BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
-#endif
     }
     for (int i = 0; i < 8; i++)
     {
@@ -198,9 +193,7 @@ BOOST_AUTO_TEST_CASE(set)
         txTo[i].vin[0].prevout.hash = txFrom.GetHash();
         txTo[i].vout[0].nValue = 1*CENT;
         txTo[i].vout[0].scriptPubKey = inner[i];
-#ifdef ENABLE_WALLET
         BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
-#endif
     }
     for (int i = 0; i < 4; i++)
     {
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index aab4b217c..fa8740eb7 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -12,8 +12,8 @@
 #include "ui_interface.h"
 #include "utilstrencodings.h"
 #include "validationinterface.h"
+#include "script/ismine.h"
 #include "wallet/crypter.h"
-#include "wallet/wallet_ismine.h"
 #include "wallet/walletdb.h"
 #include "wallet/rpcwallet.h"
 

From b30fb42e499beee5899b7e0e6c7581101b14ee83 Mon Sep 17 00:00:00 2001
From: "Wladimir J. van der Laan" <laanwj@gmail.com>
Date: Mon, 18 Apr 2016 15:15:57 +0200
Subject: [PATCH 4/4] test: Rename wallet.dat to wallet_test.dat

Indicate that the file name is not hardcoded, and a little bit of safety
so that it never nukes the main wallet.

Suggestion by Marco Falke.
---
 src/wallet/test/wallet_test_fixture.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/wallet/test/wallet_test_fixture.cpp b/src/wallet/test/wallet_test_fixture.cpp
index 837030096..9036ee26d 100644
--- a/src/wallet/test/wallet_test_fixture.cpp
+++ b/src/wallet/test/wallet_test_fixture.cpp
@@ -10,7 +10,7 @@ WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
     bitdb.MakeMock();
 
     bool fFirstRun;
-    pwalletMain = new CWallet("wallet.dat");
+    pwalletMain = new CWallet("wallet_test.dat");
     pwalletMain->LoadWallet(fFirstRun);
     RegisterValidationInterface(pwalletMain);