From d793f94b8a33040afa35be6899ea6c81e32f26ee Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 30 Oct 2016 14:36:36 -0700 Subject: [PATCH] Track mined blocks to detect and report orphans and mining revenue Part of #1713 Closes #1716 --- src/chainparams.cpp | 3 +++ src/chainparams.h | 2 ++ src/metrics.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++- src/metrics.h | 5 ++++- src/miner.cpp | 2 +- src/rpcmining.cpp | 1 - 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 8691455e8..59869dff9 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -36,6 +36,7 @@ class CMainParams : public CChainParams { public: CMainParams() { strNetworkID = "main"; + strCurrencyUnits = "ZEC"; consensus.fCoinbaseMustBeProtected = true; consensus.nSubsidySlowStartInterval = 20000; consensus.nSubsidyHalvingInterval = 840000; @@ -202,6 +203,7 @@ class CTestNetParams : public CMainParams { public: CTestNetParams() { strNetworkID = "test"; + strCurrencyUnits = "TAZ"; consensus.nMajorityEnforceBlockUpgrade = 51; consensus.nMajorityRejectBlockOutdated = 75; consensus.nMajorityWindow = 400; @@ -287,6 +289,7 @@ class CRegTestParams : public CTestNetParams { public: CRegTestParams() { strNetworkID = "regtest"; + strCurrencyUnits = "REG"; consensus.fCoinbaseMustBeProtected = false; consensus.nSubsidySlowStartInterval = 0; consensus.nSubsidyHalvingInterval = 150; diff --git a/src/chainparams.h b/src/chainparams.h index ecb4f94c3..0af9920de 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -67,6 +67,7 @@ public: int64_t PruneAfterHeight() const { return nPruneAfterHeight; } unsigned int EquihashN() const { return nEquihashN; } unsigned int EquihashK() const { return nEquihashK; } + std::string CurrencyUnits() const { return strCurrencyUnits; } /** Make miner stop after a block is found. In RPC, don't return until nGenProcLimit blocks are generated */ bool MineBlocksOnDemand() const { return fMineBlocksOnDemand; } /** In the future use NetworkIDString() for RPC fields */ @@ -99,6 +100,7 @@ protected: std::vector vSeeds; std::vector base58Prefixes[MAX_BASE58_TYPES]; std::string strNetworkID; + std::string strCurrencyUnits; CBlock genesis; std::vector vFixedSeeds; bool fRequireRPCPassword = false; diff --git a/src/metrics.cpp b/src/metrics.cpp index a5480838e..78b3b4ecc 100644 --- a/src/metrics.cpp +++ b/src/metrics.cpp @@ -5,9 +5,11 @@ #include "metrics.h" #include "chainparams.h" +#include "main.h" #include "ui_interface.h" #include "util.h" #include "utiltime.h" +#include "utilmoneystr.h" #include #include @@ -20,10 +22,18 @@ AtomicCounter ehSolverRuns; AtomicCounter solutionTargetChecks; AtomicCounter minedBlocks; +boost::synchronized_value> trackedBlocks; + boost::synchronized_value> messageBox; boost::synchronized_value initMessage; bool loaded = false; +void TrackMinedBlock(uint256 hash) +{ + minedBlocks.increment(); + trackedBlocks->push_back(hash); +} + static bool metrics_ThreadSafeMessageBox(const std::string& message, const std::string& caption, unsigned int style) @@ -121,8 +131,43 @@ int printMetrics(size_t cols, int64_t nStart, bool mining) int mined = minedBlocks.get(); if (mined > 0) { + LOCK(cs_main); + boost::strict_lock_ptr> u = trackedBlocks.synchronize(); + auto consensusParams = Params().GetConsensus(); + auto tipHeight = chainActive.Height(); + std::string units = Params().CurrencyUnits(); + + // Update orphans and calculate subsidies + CAmount immature {0}; + CAmount mature {0}; + for (std::list::iterator it = u->begin(); it != u->end(); it++) { + auto hash = *it; + if (mapBlockIndex.count(hash) > 0 && + chainActive.Contains(mapBlockIndex[hash])) { + int height = mapBlockIndex[hash]->nHeight; + CAmount subsidy = GetBlockSubsidy(height, consensusParams); + if ((height > 0) && (height <= consensusParams.GetLastFoundersRewardBlockHeight())) { + subsidy -= subsidy/5; + } + if (std::max(0, COINBASE_MATURITY - (tipHeight - height)) > 0) { + immature += subsidy; + } else { + mature += subsidy; + } + } else { + it = u->erase(it); + } + } + int orphaned = mined - u->size(); + std::cout << "- " << strprintf(_("You have mined %d blocks!"), mined) << std::endl; - lines++; + std::cout << " " + << strprintf(_("Orphaned: %d blocks, Immature: %u %s, Mature: %u %s"), + orphaned, + FormatMoney(immature), units, + FormatMoney(mature), units) + << std::endl; + lines += 2; } } std::cout << std::endl; diff --git a/src/metrics.h b/src/metrics.h index 4a2ca2264..b2cacd03a 100644 --- a/src/metrics.h +++ b/src/metrics.h @@ -2,6 +2,8 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "uint256.h" + #include #include @@ -26,7 +28,8 @@ struct AtomicCounter { extern AtomicCounter transactionsValidated; extern AtomicCounter ehSolverRuns; extern AtomicCounter solutionTargetChecks; -extern AtomicCounter minedBlocks; + +void TrackMinedBlock(uint256 hash); void ConnectMetricsScreen(); void ThreadShowMetricsScreen(); diff --git a/src/miner.cpp b/src/miner.cpp index bde9babd5..9ffe6b9c8 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -438,7 +438,7 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese if (!ProcessNewBlock(state, NULL, pblock, true, NULL)) return error("ZcashMiner: ProcessNewBlock, block not accepted"); - minedBlocks.increment(); + TrackMinedBlock(pblock->GetHash()); return true; } diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 3759bd03a..a23d6b991 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -205,7 +205,6 @@ endloop: CValidationState state; if (!ProcessNewBlock(state, NULL, pblock, true, NULL)) throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted"); - minedBlocks.increment(); ++nHeight; blockHashes.push_back(pblock->GetHash().GetHex()); }