From 352b0c6cf65445368724776a7345f2045efbba51 Mon Sep 17 00:00:00 2001 From: airk42 Date: Sun, 7 Jan 2018 03:19:53 -0500 Subject: [PATCH] Validation of fork blocks against tnxs in file (AcceptBlock); Final Cleanup --- configure.ac | 18 ++++++++++++++++++ src/main.cpp | 29 ++++++++++++++--------------- src/main.h | 14 +++++--------- src/miner.cpp | 8 +++----- src/miner.h | 4 ---- src/rpcclient.cpp | 9 --------- src/rpcmining.cpp | 23 ----------------------- src/rpcserver.cpp | 5 ----- src/rpcserver.h | 8 -------- src/txdb.h | 4 ---- 10 files changed, 40 insertions(+), 82 deletions(-) diff --git a/configure.ac b/configure.ac index 721c1912..0cb00941 100644 --- a/configure.ac +++ b/configure.ac @@ -82,6 +82,13 @@ AC_ARG_ENABLE([wallet], [enable_wallet=$enableval], [enable_wallet=yes]) +# Enable fork mining +AC_ARG_ENABLE([fork], + [AS_HELP_STRING([--enable-fork], + [enable fork (default is yes)])], + [enable_fork=$enableval], + [enable_fork=yes]) + AC_ARG_ENABLE([mining], [AS_HELP_STRING([--enable-mining], [enable mining (default is yes)])], @@ -856,6 +863,16 @@ else AC_MSG_RESULT(no) fi +dnl enable fork_mining +AC_MSG_CHECKING([if fork mining should be enabled]) +if test x$enable_fork != xno; then + AC_MSG_RESULT(yes) + AC_DEFINE(FORK_CB_INPUT, 1, [Define to 1 to enable fork mining functions]) + +else + AC_MSG_RESULT(no) +fi + dnl enable mining AC_MSG_CHECKING([if mining should be enabled]) if test x$enable_mining != xno; then @@ -931,6 +948,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([FORK_CB_INPUT],[test x$enable_fork = xyes]) AM_CONDITIONAL([ENABLE_MINING],[test x$enable_mining = xyes]) AM_CONDITIONAL([ENABLE_RUST],[test x$enable_rust = xyes]) AM_CONDITIONAL([ENABLE_TESTS],[test x$BUILD_TEST = xyes]) diff --git a/src/main.cpp b/src/main.cpp index 11cf8386..c01c04f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1383,7 +1383,7 @@ bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos, const CMessageHeader::M bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos #ifdef FORK_CB_INPUT - , int nHeight = -1 + , int nHeight #endif ) { @@ -1403,7 +1403,7 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos } #ifdef FORK_CB_INPUT - if (!isFork(nHeight)) { //when block is un fork region - don't check Solution and PoW + if (!isForkBlock(nHeight)) { //when block is in fork region - don't check Solution and PoW #endif // Check the header @@ -1415,7 +1415,6 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos } #endif - return true; } @@ -1934,12 +1933,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex int nNonCBIdx = 0; // restore inputs -#ifdef FORK_CB_INPUT - if (isFork(pindex->nHeight)){ //when block in forking region - all transcations are coinbase - nNonCBIdx = forkCBPerBlock; - } - else -#endif + // Check that all outputs are available and match the outputs in the block itself // exactly. { @@ -1968,6 +1962,11 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex } } +#ifdef FORK_CB_INPUT + if (isForkBlock(pindex->nHeight)){ //when block in forking region - all transcations are coinbase + nNonCBIdx = forkCBPerBlock; + } +#endif if (i > nNonCBIdx) { // not coinbases const CTxUndo &txundo = blockUndo.vtxundo[i-1]; if (txundo.vprevout.size() != tx.vin.size()) @@ -2243,7 +2242,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime1 - nTimeStart), 0.001 * (nTime1 - nTimeStart) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime1 - nTimeStart) / (nInputs-1), nTimeConnect * 0.000001); #ifdef FORK_CB_INPUT - if (!isFork(pindex->nHeight)){ //when block is in forking region - don't check coinbase amount + if (!isForkBlock(pindex->nHeight)){ //when block is in forking region - don't check coinbase amount #endif CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus()); if (block.vtx[0].GetValueOut() > blockReward) @@ -3031,7 +3030,7 @@ bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool f REJECT_INVALID, "version-too-low"); #ifdef FORK_CB_INPUT - if (!isFork()) { //when in FORK mode (tip is in forking region) - don't check Solution and PoW + if (!isTipInForkRange()) { //when in FORK mode (tip is in forking region) - don't check Solution and PoW #endif // Check Equihash solution is valid @@ -3098,7 +3097,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, REJECT_INVALID, "bad-cb-missing"); #ifdef FORK_CB_INPUT - if (isFork()) { //when in FORK mode (tip is in forking region) blocks might have up to fork pre-defined value coinbases + if (isTipInForkRange()) { //when in FORK mode (tip is in forking region) blocks might have up to fork pre-defined value coinbases if (block.vtx.size() > forkCBPerBlock) return state.DoS(100, error("CheckBlock(): it is forking block but there are more than %d coinbase txns", forkCBPerBlock), REJECT_INVALID, "bad-cb-multiple"); @@ -3143,7 +3142,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta // Check proof of work #ifdef FORK_CB_INPUT - if (!isFork(nHeight)) { //If current block is FORK, don't check work required + if (!isForkBlock(nHeight)) { //If current block is FORK, don't check work required #endif if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams)) return state.DoS(100, error("%s: incorrect proof of work", __func__), @@ -3297,7 +3296,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, int nHeight = pindex->nHeight; #ifdef FORK_CB_INPUT - if (isFork(nHeight)) { //if block is in forking region validate it agains file records + if (isForkBlock(nHeight)) { //if block is in forking region validate it agains file records if (!fCalledFromMiner && !forkUtxoPath.empty()) { std::string utxo_file_path = GetUTXOFileName(nHeight); @@ -3315,7 +3314,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, char term = 0; char coin[8] = {}; if (!if_utxo.read(coin, 8)) { - LogPrintf("AcceptBlock(): FORK Block - UTXO file corrupted? - No more data (Amount)\n"); + LogPrintf("AcceptBlock(): FORK Block - No more data in the file \n"); break; } uint64_t amount = bytes2uint64(coin); diff --git a/src/main.h b/src/main.h index f4aaee0d..ecbff34b 100644 --- a/src/main.h +++ b/src/main.h @@ -37,10 +37,6 @@ #include -#ifndef FORK_CB_INPUT -#define FORK_CB_INPUT -#endif - class CBlockIndex; class CBlockTreeDB; class CBloomFilter; @@ -557,17 +553,17 @@ extern int64_t forkCBPerBlock; std::string GetUTXOFileName(int nHeight); -inline bool isFork(int nHeight) +inline bool isForkBlock(int nHeight) { return (nHeight >= forkStartHeight && nHeight < forkStartHeight + forkHeightRange); } -inline bool isFork() +inline bool isTipInForkRange() { - return chainActive.Tip()? isFork(chainActive.Tip()->nHeight): false; + return chainActive.Tip()? isForkBlock(chainActive.Tip()->nHeight): false; } -inline bool isNextFork() +inline bool isNextTipInForkRange() { - return chainActive.Tip()? isFork(chainActive.Tip()->nHeight+1): false; + return chainActive.Tip()? isForkBlock(chainActive.Tip()->nHeight + 1): false; } inline uint64_t bytes2uint64(char *array) { diff --git a/src/miner.cpp b/src/miner.cpp index 86c1a833..65f9b6f9 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -194,7 +194,7 @@ CBlockTemplate* CreateNewForkBlock(bool& bFileNotFound) txNew.vout[0].nValue = amount; - txNew.vin[0].scriptSig = CScript() << nHeight << OP_0; + txNew.vin[0].scriptSig = CScript() << nHeight+nBlockTx << OP_0; unsigned int nTxSize = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION); if (nBlockSize + nTxSize >= nBlockMaxSize) @@ -594,7 +594,7 @@ static bool ProcessBlockFound(CBlock* pblock) #endif // ENABLE_WALLET { #ifdef FORK_CB_INPUT - if (!isNextFork()) { + if (!isNextTipInForkRange()) { #endif LogPrintf("%s\n", pblock->ToString()); #ifdef FORK_CB_INPUT @@ -678,7 +678,6 @@ void static BitcoinMiner() try { while (true) { -#ifndef FORK_CB_INPUT ///!!!! this is only for TESTING if (chainparams.MiningRequiresPeers()) { // Busy-wait for the network to come online so we don't waste time mining // on an obsolete chain. In regtest mode we expect to fly solo. @@ -695,7 +694,6 @@ void static BitcoinMiner() } while (true); miningTimer.start(); } -#endif CBlockIndex* pindexPrev = chainActive.Tip(); CBlock *pblock = nullptr; @@ -707,7 +705,7 @@ void static BitcoinMiner() unique_ptr pblocktemplate; #ifdef FORK_CB_INPUT - bool isNextBlockFork = isFork(pindexPrev->nHeight+1); + bool isNextBlockFork = isForkBlock(pindexPrev->nHeight+1); if (isNextBlockFork) { if (!bForkModeStarted) { LogPrintf("BTCPrivate Miner: switching into fork mode\n"); diff --git a/src/miner.h b/src/miner.h index c6458bd2..1ae86dca 100644 --- a/src/miner.h +++ b/src/miner.h @@ -49,10 +49,6 @@ void GenerateBitcoins(bool fGenerate, int nThreads); void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev); -#ifndef FORK_CB_INPUT -#define FORK_CB_INPUT -#endif - #ifdef FORK_CB_INPUT CBlockTemplate* CreateNewForkBlock(); #endif diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index ae9a56ab..07adf65f 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -13,10 +13,6 @@ #include -#ifndef FORK_CB_INPUT -#define FORK_CB_INPUT -#endif - using namespace std; class CRPCConvertParam @@ -116,11 +112,6 @@ static const CRPCConvertParam vRPCConvertParams[] = { "z_getoperationstatus", 0}, { "z_getoperationresult", 0}, { "z_importkey", 2 }, - -#ifdef FORK_CB_INPUT - { "startfork", 0 }, - { "startforkat", 1 }, -#endif }; class CRPCConvertTable diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 1beeca2b..36a4cfa6 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -157,29 +157,6 @@ UniValue getgenerate(const UniValue& params, bool fHelp) return GetBoolArg("-gen", false); } -#ifdef FORK_CB_INPUT -UniValue startfork(const UniValue& params, bool fHelp) -{ - CBlockIndex* pindexPrev = chainActive.Tip(); - forkStartHeight = pindexPrev->nHeight; - - return NullUniValue; -} -UniValue startforkat(const UniValue& params, bool fHelp) -{ - if (params.size() == 1) { - forkStartHeight = params[0].get_int(); - } else { - throw runtime_error( - "startfork at chain height\n" - + HelpExampleCli("startforkat", "100") - ); - } - - return NullUniValue; -} -#endif - UniValue generate(const UniValue& params, bool fHelp) { if (fHelp || params.size() < 1 || params.size() > 1) diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index ddb45fe3..efce2f6e 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -309,11 +309,6 @@ static const CRPCCommand vRPCCommands[] = { "generating", "generate", &generate, true }, #endif -#ifdef FORK_CB_INPUT - { "generating", "startfork", &startfork, true }, - { "generating", "startforkat", &startforkat, true }, -#endif - /* Raw transactions */ { "rawtransactions", "createrawtransaction", &createrawtransaction, true }, { "rawtransactions", "decoderawtransaction", &decoderawtransaction, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index cf48cbd8..1124801c 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -20,10 +20,6 @@ #include -#ifndef FORK_CB_INPUT -#define FORK_CB_INPUT -#endif - class AsyncRPCQueue; class CRPCCommand; @@ -196,10 +192,6 @@ extern UniValue importwallet(const UniValue& params, bool fHelp); extern UniValue getgenerate(const UniValue& params, bool fHelp); // in rpcmining.cpp extern UniValue setgenerate(const UniValue& params, bool fHelp); extern UniValue generate(const UniValue& params, bool fHelp); -#ifdef FORK_CB_INPUT -extern UniValue startfork(const UniValue& params, bool fHelp); -extern UniValue startforkat(const UniValue& params, bool fHelp); -#endif extern UniValue getlocalsolps(const UniValue& params, bool fHelp); extern UniValue getnetworksolps(const UniValue& params, bool fHelp); extern UniValue getnetworkhashps(const UniValue& params, bool fHelp); diff --git a/src/txdb.h b/src/txdb.h index 9447e3e0..a1f86d09 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -68,10 +68,6 @@ public: bool WriteFlag(const std::string &name, bool fValue); bool ReadFlag(const std::string &name, bool &fValue); -#ifndef FORK_CB_INPUT -#define FORK_CB_INPUT -#endif - #ifdef FORK_CB_INPUT bool LoadBlockIndexGuts(int64_t forkStart, int64_t forkStop); #else