Track mined blocks to detect and report orphans and mining revenue

Part of #1713
Closes #1716
This commit is contained in:
Jack Grigg 2016-10-30 14:36:36 -07:00
parent 1feaefac51
commit d793f94b8a
No known key found for this signature in database
GPG Key ID: 6A6914DAFBEA00DA
6 changed files with 56 additions and 4 deletions

View File

@ -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;

View File

@ -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<CDNSSeedData> vSeeds;
std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
std::string strNetworkID;
std::string strCurrencyUnits;
CBlock genesis;
std::vector<SeedSpec6> vFixedSeeds;
bool fRequireRPCPassword = false;

View File

@ -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 <boost/thread.hpp>
#include <boost/thread/synchronized_value.hpp>
@ -20,10 +22,18 @@ AtomicCounter ehSolverRuns;
AtomicCounter solutionTargetChecks;
AtomicCounter minedBlocks;
boost::synchronized_value<std::list<uint256>> trackedBlocks;
boost::synchronized_value<std::list<std::string>> messageBox;
boost::synchronized_value<std::string> 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<std::list<uint256>> 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<uint256>::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;

View File

@ -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 <atomic>
#include <string>
@ -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();

View File

@ -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;
}

View File

@ -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());
}