From af2e212d7a7e9db67ee79645ad0a6608a310fa5d Mon Sep 17 00:00:00 2001 From: miketout Date: Thu, 11 Oct 2018 21:35:09 -0700 Subject: [PATCH] Remove spurios opret in tx builder, don't wait forever for blocks before mining, add delay to getblocktemplate --- src/main.cpp | 21 ++++++++++++-------- src/main.h | 6 ++++-- src/miner.cpp | 23 ++++++++++++++++++++-- src/rpc/mining.cpp | 39 +++++++++++++++++++++++++++++++++++-- src/transaction_builder.cpp | 4 ++-- 5 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 7f0a3c086..de810d871 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1566,7 +1566,8 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa string reason; if (Params().RequireStandard() && !IsStandardTx(tx, reason, nextBlockHeight)) { - //fprintf(stderr,"AcceptToMemoryPool reject nonstandard transaction: %s\n",reason.c_str()); + // + //fprintf(stderr,"AcceptToMemoryPool reject nonstandard transaction: %s\nscriptPubKey: %s\n",reason.c_str(),tx.vout[0].scriptPubKey.ToString().c_str()); return state.DoS(0,error("AcceptToMemoryPool: nonstandard transaction: %s", reason),REJECT_NONSTANDARD, reason); } // Only accept nLockTime-using transactions that can be mined in the next @@ -2186,22 +2187,22 @@ bool IsInitialBlockDownload() } // determine if we are in sync with the best chain -bool IsInSync() +int IsNotInSync() { const CChainParams& chainParams = Params(); LOCK(cs_main); if (fImporting || fReindex) { - //fprintf(stderr,"IsInSync: fImporting %d || %d fReindex\n",(int32_t)fImporting,(int32_t)fReindex); - return false; + //fprintf(stderr,"IsNotInSync: fImporting %d || %d fReindex\n",(int32_t)fImporting,(int32_t)fReindex); + return true; } if (fCheckpointsEnabled) { if (fCheckpointsEnabled && chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints())) { - //fprintf(stderr,"IsInSync: checkpoint -> initialdownload chainActive.Height().%d GetTotalBlocksEstimate(chainParams.Checkpoints().%d\n", chainActive.Height(), Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints())); - return false; + //fprintf(stderr,"IsNotInSync: checkpoint -> initialdownload chainActive.Height().%d GetTotalBlocksEstimate(chainParams.Checkpoints().%d\n", chainActive.Height(), Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints())); + return true; } } @@ -2211,9 +2212,13 @@ bool IsInSync() (pindexBestHeader == 0) || ((pindexBestHeader->GetHeight() - 1) > pbi->GetHeight()) || (longestchain != 0 && longestchain > pbi->GetHeight()) ) - return false; + { + return (pbi && pindexBestHeader && (pindexBestHeader->GetHeight() - 1) > pbi->GetHeight()) ? + pindexBestHeader->GetHeight() - pbi->GetHeight() : + true; + } - return true; + return false; } static bool fLargeWorkForkFound = false; diff --git a/src/main.h b/src/main.h index c981622c0..2bfb782a0 100644 --- a/src/main.h +++ b/src/main.h @@ -228,8 +228,10 @@ void ThreadScriptCheck(); void PartitionCheck(bool (*initialDownloadCheck)(), CCriticalSection& cs, const CBlockIndex *const &bestHeader, int64_t nPowTargetSpacing); /** Check whether we are doing an initial block download (synchronizing from disk or network) */ bool IsInitialBlockDownload(); -/** Check if the daemon is in sync */ -bool IsInSync(); +/** Check if the daemon is in sync, if not, it returns 1 or if due to best header only, the difference in best + * header and activeChain tip + */ +int IsNotInSync(); /** Format a string that describes several potential problems detected by the core */ std::string GetWarnings(const std::string& strFor); /** Retrieve a transaction (from memory pool, or from disk, if possible) */ diff --git a/src/miner.cpp b/src/miner.cpp index d3ac229f5..6452533b4 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -923,8 +923,10 @@ int32_t waitForPeers(const CChainParams &chainparams) LOCK(cs_vNodes); fvNodesEmpty = vNodes.empty(); } - if (fvNodesEmpty || !IsInSync()) + if (fvNodesEmpty || IsNotInSync()) { + int loops = 0, blockDiff = 0, newDiff = 0; + do { if (fvNodesEmpty) MilliSleep(1000 + rand() % 4000); @@ -932,8 +934,25 @@ int32_t waitForPeers(const CChainParams &chainparams) boost::this_thread::interruption_point(); LOCK(cs_vNodes); fvNodesEmpty = vNodes.empty(); + loops = 0; + blockDiff = 0; } - } while (fvNodesEmpty || !IsInSync()); + if ((newDiff = IsNotInSync()) > 1) + { + if (blockDiff != newDiff) + { + blockDiff = newDiff; + } + else + { + if (++loops <= 90) + { + MilliSleep(1000); + } + else break; + } + } + } while (fvNodesEmpty || IsNotInSync()); MilliSleep(100 + rand() % 400); } } diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 07ac5195f..8f4782228 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -604,8 +604,43 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) if (strMode != "template") throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode"); - if (Params().MiningRequiresPeers() && (!IsInSync() || vNodes.empty())) - throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Cannot get a block template while no peers are connected or chain not in sync!"); + bool fvNodesEmpty; + { + LOCK(cs_vNodes); + fvNodesEmpty = vNodes.empty(); + } + if (Params().MiningRequiresPeers() && (IsNotInSync() || fvNodesEmpty)) + { + int loops = 0, blockDiff = 0, newDiff = 0; + + do { + if (fvNodesEmpty) + MilliSleep(1000 + rand() % 4000); + { + LOCK(cs_vNodes); + fvNodesEmpty = vNodes.empty(); + loops = 0; + blockDiff = 0; + } + if ((newDiff = IsNotInSync()) > 1) + { + if (blockDiff != newDiff) + { + blockDiff = newDiff; + } + else + { + if (++loops <= 30) + { + MilliSleep(1000); + } + else break; + } + } + } while (fvNodesEmpty || IsNotInSync()); + if (loops > 30) + throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Cannot get a block template while no peers are connected or chain not in sync!"); + } //if (IsInitialBlockDownload()) // throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Zcash is downloading blocks..."); diff --git a/src/transaction_builder.cpp b/src/transaction_builder.cpp index 62f28470b..4fd34e3bc 100644 --- a/src/transaction_builder.cpp +++ b/src/transaction_builder.cpp @@ -85,9 +85,9 @@ bool TransactionBuilder::AddOpRetLast() if (opReturn) { s = opReturn.value(); + CTxOut out(0, s); + mtx.vout.push_back(out); } - CTxOut out(0, s); - mtx.vout.push_back(out); return true; }