From 2cc0a252adab4d01750755688db9eec614b24ef7 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 6 Nov 2016 13:40:34 -0600 Subject: [PATCH] Add compile flag to disable compilation of mining code --- configure.ac | 17 +++++++++++++++++ src/Makefile.am | 20 +++++++++++++------- src/crypto/equihash.cpp | 14 ++++++++++++++ src/crypto/equihash.h | 4 ++++ src/gtest/test_equihash.cpp | 6 ++++++ src/init.cpp | 4 +++- src/metrics.cpp | 8 ++++++++ src/miner.cpp | 6 ++++++ src/miner.h | 2 ++ src/rpcmining.cpp | 11 ++++++++--- src/rpcserver.cpp | 2 +- src/test/equihash_tests.cpp | 8 ++++++++ src/wallet/rpcwallet.cpp | 2 ++ src/zcbenchmarks.cpp | 2 ++ zcutil/build.sh | 15 +++++++++++++-- 15 files changed, 107 insertions(+), 14 deletions(-) diff --git a/configure.ac b/configure.ac index 8a666ec6b..ea0d39b6c 100644 --- a/configure.ac +++ b/configure.ac @@ -82,6 +82,12 @@ AC_ARG_ENABLE([wallet], [enable_wallet=$enableval], [enable_wallet=yes]) +AC_ARG_ENABLE([mining], + [AS_HELP_STRING([--enable-mining], + [enable mining (default is yes)])], + [enable_mining=$enableval], + [enable_mining=yes]) + AC_ARG_WITH([miniupnpc], [AS_HELP_STRING([--with-miniupnpc], [enable UPNP (default is yes if libminiupnpc is found)])], @@ -790,6 +796,16 @@ else AC_MSG_RESULT(no) fi +dnl enable mining +AC_MSG_CHECKING([if mining should be enabled]) +if test x$enable_mining != xno; then + AC_MSG_RESULT(yes) + AC_DEFINE(ENABLE_MINING, 1, [Define to 1 to enable mining functions]) + +else + AC_MSG_RESULT(no) +fi + dnl enable upnp support AC_MSG_CHECKING([whether to build with support for UPnP]) if test x$have_miniupnpc = xno; then @@ -881,6 +897,7 @@ AM_CONDITIONAL([TARGET_DARWIN], [test x$TARGET_OS = xdarwin]) AM_CONDITIONAL([BUILD_DARWIN], [test x$BUILD_OS = xdarwin]) AM_CONDITIONAL([TARGET_WINDOWS], [test x$TARGET_OS = xwindows]) AM_CONDITIONAL([ENABLE_WALLET],[test x$enable_wallet = xyes]) +AM_CONDITIONAL([ENABLE_MINING],[test x$enable_mining = xyes]) AM_CONDITIONAL([ENABLE_TESTS],[test x$BUILD_TEST = xyes]) AM_CONDITIONAL([ENABLE_QT],[test x$bitcoin_enable_qt = xyes]) AM_CONDITIONAL([ENABLE_QT_TESTS],[test x$BUILD_TEST_QT = xyes]) diff --git a/src/Makefile.am b/src/Makefile.am index f18071678..e5fcaf121 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -248,13 +248,8 @@ libbitcoin_wallet_a_SOURCES = \ $(BITCOIN_CORE_H) \ $(LIBZCASH_H) -EQUIHASH_TROMP_SOURCES = \ - pow/tromp/equi_miner.h \ - pow/tromp/equi.h \ - pow/tromp/osx_barrier.h - # crypto primitives library -crypto_libbitcoin_crypto_a_CPPFLAGS = $(BITCOIN_CONFIG_INCLUDES) -DEQUIHASH_TROMP_ATOMIC +crypto_libbitcoin_crypto_a_CPPFLAGS = $(BITCOIN_CONFIG_INCLUDES) crypto_libbitcoin_crypto_a_SOURCES = \ crypto/common.h \ crypto/equihash.cpp \ @@ -271,8 +266,19 @@ crypto_libbitcoin_crypto_a_SOURCES = \ crypto/sha256.cpp \ crypto/sha256.h \ crypto/sha512.cpp \ - crypto/sha512.h \ + crypto/sha512.h + +if ENABLE_MINING +EQUIHASH_TROMP_SOURCES = \ + pow/tromp/equi_miner.h \ + pow/tromp/equi.h \ + pow/tromp/osx_barrier.h + +crypto_libbitcoin_crypto_a_CPPFLAGS += \ + -DEQUIHASH_TROMP_ATOMIC +crypto_libbitcoin_crypto_a_SOURCES += \ ${EQUIHASH_TROMP_SOURCES} +endif # univalue JSON library univalue_libbitcoin_univalue_a_SOURCES = \ diff --git a/src/crypto/equihash.cpp b/src/crypto/equihash.cpp index bc2437e17..d22244aa9 100644 --- a/src/crypto/equihash.cpp +++ b/src/crypto/equihash.cpp @@ -12,6 +12,10 @@ // NDSS ’16, 21-24 February 2016, San Diego, CA, USA // https://www.internetsociety.org/sites/default/files/blogs-media/equihash-asymmetric-proof-of-work-based-generalized-birthday-problem.pdf +#if defined(HAVE_CONFIG_H) +#include "config/bitcoin-config.h" +#endif + #include "crypto/equihash.h" #include "util.h" @@ -319,6 +323,7 @@ std::shared_ptr TruncatedStepRow::GetTruncatedIndices(size_t le return p; } +#ifdef ENABLE_MINING template bool Equihash::BasicSolve(const eh_HashState& base_state, const std::function)> validBlock, @@ -711,6 +716,7 @@ invalidsolution: return false; } +#endif // ENABLE_MINING template bool Equihash::IsValidSolution(const eh_HashState& base_state, std::vector soln) @@ -762,40 +768,48 @@ bool Equihash::IsValidSolution(const eh_HashState& base_state, std::vector< // Explicit instantiations for Equihash<96,3> template int Equihash<96,3>::InitialiseState(eh_HashState& base_state); +#ifdef ENABLE_MINING template bool Equihash<96,3>::BasicSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); template bool Equihash<96,3>::OptimisedSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); +#endif template bool Equihash<96,3>::IsValidSolution(const eh_HashState& base_state, std::vector soln); // Explicit instantiations for Equihash<200,9> template int Equihash<200,9>::InitialiseState(eh_HashState& base_state); +#ifdef ENABLE_MINING template bool Equihash<200,9>::BasicSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); template bool Equihash<200,9>::OptimisedSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); +#endif template bool Equihash<200,9>::IsValidSolution(const eh_HashState& base_state, std::vector soln); // Explicit instantiations for Equihash<96,5> template int Equihash<96,5>::InitialiseState(eh_HashState& base_state); +#ifdef ENABLE_MINING template bool Equihash<96,5>::BasicSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); template bool Equihash<96,5>::OptimisedSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); +#endif template bool Equihash<96,5>::IsValidSolution(const eh_HashState& base_state, std::vector soln); // Explicit instantiations for Equihash<48,5> template int Equihash<48,5>::InitialiseState(eh_HashState& base_state); +#ifdef ENABLE_MINING template bool Equihash<48,5>::BasicSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); template bool Equihash<48,5>::OptimisedSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); +#endif template bool Equihash<48,5>::IsValidSolution(const eh_HashState& base_state, std::vector soln); diff --git a/src/crypto/equihash.h b/src/crypto/equihash.h index 3f1c300b9..6691844ba 100644 --- a/src/crypto/equihash.h +++ b/src/crypto/equihash.h @@ -182,12 +182,14 @@ public: Equihash() { } int InitialiseState(eh_HashState& base_state); +#ifdef ENABLE_MINING bool BasicSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); bool OptimisedSolve(const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled); +#endif bool IsValidSolution(const eh_HashState& base_state, std::vector soln); }; @@ -211,6 +213,7 @@ static Equihash<48,5> Eh48_5; throw std::invalid_argument("Unsupported Equihash parameters"); \ } +#ifdef ENABLE_MINING inline bool EhBasicSolve(unsigned int n, unsigned int k, const eh_HashState& base_state, const std::function)> validBlock, const std::function cancelled) @@ -258,6 +261,7 @@ inline bool EhOptimisedSolveUncancellable(unsigned int n, unsigned int k, const return EhOptimisedSolve(n, k, base_state, validBlock, [](EhSolverCancelCheck pos) { return false; }); } +#endif // ENABLE_MINING #define EhIsValidSolution(n, k, base_state, soln, ret) \ if (n == 96 && k == 3) { \ diff --git a/src/gtest/test_equihash.cpp b/src/gtest/test_equihash.cpp index b7d9562f1..6134e43c9 100644 --- a/src/gtest/test_equihash.cpp +++ b/src/gtest/test_equihash.cpp @@ -1,3 +1,7 @@ +#if defined(HAVE_CONFIG_H) +#include "config/bitcoin-config.h" +#endif + #include #include @@ -76,6 +80,7 @@ TEST(equihash_tests, is_probably_duplicate) { ASSERT_TRUE(IsProbablyDuplicate<4>(p3, 4)); } +#ifdef ENABLE_MINING TEST(equihash_tests, check_basic_solver_cancelled) { Equihash<48,5> Eh48_5; crypto_generichash_blake2b_state state; @@ -283,3 +288,4 @@ TEST(equihash_tests, check_optimised_solver_cancelled) { }), EhSolverCancelledException); } } +#endif // ENABLE_MINING diff --git a/src/init.cpp b/src/init.cpp index 80304bf2c..8cb48163e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -163,7 +163,9 @@ void Shutdown() #ifdef ENABLE_WALLET if (pwalletMain) pwalletMain->Flush(false); + #ifdef ENABLE_MINING GenerateBitcoins(false, NULL, 0); + #endif #endif StopNode(); UnregisterNodeSignals(GetNodeSignals()); @@ -1500,7 +1502,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) boost::ref(cs_main), boost::cref(pindexBestHeader), nPowTargetSpacing); scheduler.scheduleEvery(f, nPowTargetSpacing); -#ifdef ENABLE_WALLET +#if defined(ENABLE_WALLET) && defined(ENABLE_MINING) // Generate coins in the background if (pwalletMain) GenerateBitcoins(GetBoolArg("-gen", false), pwalletMain, GetArg("-genproclimit", 1)); diff --git a/src/metrics.cpp b/src/metrics.cpp index 9f3795e00..cbca89671 100644 --- a/src/metrics.cpp +++ b/src/metrics.cpp @@ -128,6 +128,7 @@ int printNetworkStats() int printMiningStatus(bool mining) { +#ifdef ENABLE_MINING // Number of lines that are always displayed int lines = 1; @@ -151,6 +152,9 @@ int printMiningStatus(bool mining) std::cout << std::endl; return lines; +#else // ENABLE_MINING + return 0; +#endif // !ENABLE_MINING } int printMetrics(size_t cols, bool mining) @@ -342,7 +346,11 @@ void ThreadShowMetricsScreen() } // Miner status +#ifdef ENABLE_MINING bool mining = GetBoolArg("-gen", false); +#else + bool mining = false; +#endif if (loaded) { lines += printNetworkStats(); diff --git a/src/miner.cpp b/src/miner.cpp index 85d4a3d01..0306871a4 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -4,7 +4,9 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "miner.h" +#if defined(ENABLE_WALLET) && defined(ENABLE_MINING) #include "pow/tromp/equi_miner.h" +#endif #include "amount.h" #include "chainparams.h" @@ -21,7 +23,9 @@ #include "util.h" #include "utilmoneystr.h" #ifdef ENABLE_WALLET + #ifdef ENABLE_MINING #include "crypto/equihash.h" + #endif #include "wallet/wallet.h" #include #endif @@ -414,6 +418,7 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) return CreateNewBlock(scriptPubKey); } +#ifdef ENABLE_MINING static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) { LogPrintf("%s\n", pblock->ToString()); @@ -687,4 +692,5 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads) minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet)); } +#endif // ENABLE_MINING #endif // ENABLE_WALLET diff --git a/src/miner.h b/src/miner.h index 96a6b70ec..8c8402771 100644 --- a/src/miner.h +++ b/src/miner.h @@ -23,8 +23,10 @@ struct CBlockTemplate std::vector vTxSigOps; }; +#ifdef ENABLE_MINING /** Run the miner threads */ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads); +#endif /** Generate a new block, without valid proof-of-work */ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn); CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey); diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 474541bd4..1fcca5bca 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -137,7 +137,7 @@ Value getnetworkhashps(const Array& params, bool fHelp) return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1); } -#ifdef ENABLE_WALLET +#if defined(ENABLE_WALLET) && defined(ENABLE_MINING) Value getgenerate(const Array& params, bool fHelp) { if (fHelp || params.size() != 0) @@ -343,7 +343,7 @@ Value getmininginfo(const Array& params, bool fHelp) obj.push_back(Pair("pooledtx", (uint64_t)mempool.size())); obj.push_back(Pair("testnet", Params().TestnetToBeDeprecatedFieldRPC())); obj.push_back(Pair("chain", Params().NetworkIDString())); -#ifdef ENABLE_WALLET +#if defined(ENABLE_WALLET) && defined(ENABLE_MINING) obj.push_back(Pair("generate", getgenerate(params, false))); #endif return obj; @@ -466,9 +466,10 @@ Value getblocktemplate(const Array& params, bool fHelp) LOCK(cs_main); +#ifdef ENABLE_WALLET // Wallet is required because we support coinbasetxn if (pwalletMain == NULL) { - throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)"); + throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (wallet disabled)"); } std::string strMode = "template"; @@ -694,6 +695,10 @@ Value getblocktemplate(const Array& params, bool fHelp) result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1))); return result; +#else // ENABLE_WALLET + // Wallet is required because we support coinbasetxn + throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (wallet support not built)"); +#endif // !ENABLE_WALLET } #endif diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 5e64a82ba..658ccf8d4 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -313,7 +313,7 @@ static const CRPCCommand vRPCCommands[] = { "mining", "submitblock", &submitblock, true }, { "mining", "getblocksubsidy", &getblocksubsidy, true }, -#ifdef ENABLE_WALLET +#if defined(ENABLE_WALLET) && defined(ENABLE_MINING) /* Coin generation */ { "generating", "getgenerate", &getgenerate, true }, { "generating", "setgenerate", &setgenerate, true }, diff --git a/src/test/equihash_tests.cpp b/src/test/equihash_tests.cpp index f6fb7ecf5..6f2f0b858 100644 --- a/src/test/equihash_tests.cpp +++ b/src/test/equihash_tests.cpp @@ -3,6 +3,10 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#if defined(HAVE_CONFIG_H) +#include "config/bitcoin-config.h" +#endif + #include "arith_uint256.h" #include "crypto/sha256.h" #include "crypto/equihash.h" @@ -40,6 +44,7 @@ void PrintSolutions(std::stringstream &strm, std::set> sol strm << "\n}"; } +#ifdef ENABLE_MINING void TestEquihashSolvers(unsigned int n, unsigned int k, const std::string &I, const arith_uint256 &nonce, const std::set> &solns) { size_t cBitLen { n/(k+1) }; crypto_generichash_blake2b_state state; @@ -78,6 +83,7 @@ void TestEquihashSolvers(unsigned int n, unsigned int k, const std::string &I, c BOOST_CHECK(retOpt == solns); BOOST_CHECK(retOpt == ret); } +#endif void TestEquihashValidator(unsigned int n, unsigned int k, const std::string &I, const arith_uint256 &nonce, std::vector soln, bool expected) { size_t cBitLen { n/(k+1) }; @@ -95,6 +101,7 @@ void TestEquihashValidator(unsigned int n, unsigned int k, const std::string &I, BOOST_CHECK(isValid == expected); } +#ifdef ENABLE_MINING BOOST_AUTO_TEST_CASE(solver_testvectors) { TestEquihashSolvers(96, 5, "block header", 0, { {976, 126621, 100174, 123328, 38477, 105390, 38834, 90500, 6411, 116489, 51107, 129167, 25557, 92292, 38525, 56514, 1110, 98024, 15426, 74455, 3185, 84007, 24328, 36473, 17427, 129451, 27556, 119967, 31704, 62448, 110460, 117894}, @@ -147,6 +154,7 @@ BOOST_AUTO_TEST_CASE(solver_testvectors) { {8144, 33053, 33933, 77498, 21356, 110495, 42805, 116575, 27360, 48574, 100682, 102629, 50754, 64608, 96899, 120978, 11924, 74422, 49240, 106822, 12787, 68290, 44314, 50005, 38056, 49716, 83299, 95307, 41798, 82309, 94504, 96161} }); } +#endif BOOST_AUTO_TEST_CASE(validator_testvectors) { // Original valid solution diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1a329227a..36e383787 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2498,6 +2498,7 @@ Value zc_benchmark(const json_spirit::Array& params, bool fHelp) } } else if (benchmarktype == "verifyjoinsplit") { sample_times.push_back(benchmark_verify_joinsplit(samplejoinsplit)); +#ifdef ENABLE_MINING } else if (benchmarktype == "solveequihash") { if (params.size() < 3) { sample_times.push_back(benchmark_solve_equihash()); @@ -2506,6 +2507,7 @@ Value zc_benchmark(const json_spirit::Array& params, bool fHelp) std::vector vals = benchmark_solve_equihash_threaded(nThreads); sample_times.insert(sample_times.end(), vals.begin(), vals.end()); } +#endif } else if (benchmarktype == "verifyequihash") { sample_times.push_back(benchmark_verify_equihash()); } else if (benchmarktype == "validatelargetx") { diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index e6f8440ac..14eabebff 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -126,6 +126,7 @@ double benchmark_verify_joinsplit(const JSDescription &joinsplit) return timer_stop(tv_start); } +#ifdef ENABLE_MINING double benchmark_solve_equihash() { CBlock pblock; @@ -173,6 +174,7 @@ std::vector benchmark_solve_equihash_threaded(int nThreads) } return ret; } +#endif // ENABLE_MINING double benchmark_verify_equihash() { diff --git a/zcutil/build.sh b/zcutil/build.sh index c4823ed61..8ec020f80 100755 --- a/zcutil/build.sh +++ b/zcutil/build.sh @@ -33,13 +33,16 @@ Usage: $0 --help Show this help message and exit. -$0 [ --enable-lcov || --disable-tests ] [ MAKEARGS... ] +$0 [ --enable-lcov || --disable-tests ] [ --disable-mining ] [ MAKEARGS... ] Build Zcash and most of its transitive dependencies from source. MAKEARGS are applied to both dependencies and Zcash itself. If --enable-lcov is passed, Zcash is configured to add coverage instrumentation, thus enabling "make cov" to work. If --disable-tests is passed instead, the Zcash tests are not built. + + If --disable-mining is passed, Zcash is configured to not build any mining + code. It must be passed after the test arguments, if present. EOF exit 0 fi @@ -62,9 +65,17 @@ then shift fi +# If --disable-mining is the next argument, disable mining code: +MINING_ARG='' +if [ "x${1:-}" = 'x--disable-mining' ] +then + MINING_ARG='--enable-mining=no' + shift +fi + PREFIX="$(pwd)/depends/$BUILD/" HOST="$HOST" BUILD="$BUILD" "$MAKE" "$@" -C ./depends/ V=1 NO_QT=1 ./autogen.sh -CC="$CC" CXX="$CXX" ./configure --prefix="${PREFIX}" --host="$HOST" --build="$BUILD" --with-gui=no "$HARDENING_ARG" "$LCOV_ARG" "$TEST_ARG" CXXFLAGS='-fwrapv -fno-strict-aliasing -Werror -g' +CC="$CC" CXX="$CXX" ./configure --prefix="${PREFIX}" --host="$HOST" --build="$BUILD" --with-gui=no "$HARDENING_ARG" "$LCOV_ARG" "$TEST_ARG" "$MINING_ARG" CXXFLAGS='-fwrapv -fno-strict-aliasing -Werror -g' "$MAKE" "$@" V=1