aboutsummaryrefslogtreecommitdiff
path: root/src/miner.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/miner.cpp')
-rw-r--r--src/miner.cpp41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/miner.cpp b/src/miner.cpp
index cf08b78229..804a688523 100644
--- a/src/miner.cpp
+++ b/src/miner.cpp
@@ -6,11 +6,13 @@
#include "miner.h"
#include "amount.h"
-#include "primitives/transaction.h"
+#include "chainparams.h"
+#include "consensus/consensus.h"
#include "hash.h"
#include "main.h"
#include "net.h"
#include "pow.h"
+#include "primitives/transaction.h"
#include "timedata.h"
#include "util.h"
#include "utilmoneystr.h"
@@ -78,17 +80,18 @@ public:
}
};
-void UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev)
+void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
{
pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
// Updating time can change work required on testnet:
- if (Params().AllowMinDifficultyBlocks())
- pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
+ if (consensusParams.fPowAllowMinDifficultyBlocks)
+ pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
}
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
{
+ const CChainParams& chainparams = Params();
// Create new block
auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
if(!pblocktemplate.get())
@@ -318,14 +321,14 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
// Compute final coinbase transaction.
- txNew.vout[0].nValue = GetBlockValue(nHeight, nFees);
+ txNew.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
txNew.vin[0].scriptSig = CScript() << nHeight << OP_0;
pblock->vtx[0] = txNew;
pblocktemplate->vTxFees[0] = -nFees;
// Fill in header
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
- UpdateTime(pblock, pindexPrev);
+ UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
pblock->nNonce = 0;
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
@@ -440,6 +443,7 @@ void static BitcoinMiner(CWallet *pwallet)
LogPrintf("BitcoinMiner started\n");
SetThreadPriority(THREAD_PRIORITY_LOWEST);
RenameThread("bitcoin-miner");
+ const CChainParams& chainparams = Params();
// Each thread has its own key and counter
CReserveKey reservekey(pwallet);
@@ -447,11 +451,19 @@ void static BitcoinMiner(CWallet *pwallet)
try {
while (true) {
- if (Params().MiningRequiresPeers()) {
+ 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.
- while (vNodes.empty())
+ do {
+ bool fvNodesEmpty;
+ {
+ LOCK(cs_vNodes);
+ fvNodesEmpty = vNodes.empty();
+ }
+ if (!fvNodesEmpty && !IsInitialBlockDownload())
+ break;
MilliSleep(1000);
+ } while (true);
}
//
@@ -496,7 +508,7 @@ void static BitcoinMiner(CWallet *pwallet)
SetThreadPriority(THREAD_PRIORITY_LOWEST);
// In regression test mode, stop mining after a block is found.
- if (Params().MineBlocksOnDemand())
+ if (chainparams.MineBlocksOnDemand())
throw boost::thread_interrupted();
break;
@@ -506,7 +518,7 @@ void static BitcoinMiner(CWallet *pwallet)
// Check for stop or if block needs to be rebuilt
boost::this_thread::interruption_point();
// Regtest mode doesn't require peers
- if (vNodes.empty() && Params().MiningRequiresPeers())
+ if (vNodes.empty() && chainparams.MiningRequiresPeers())
break;
if (nNonce >= 0xffff0000)
break;
@@ -516,8 +528,8 @@ void static BitcoinMiner(CWallet *pwallet)
break;
// Update nTime every few seconds
- UpdateTime(pblock, pindexPrev);
- if (Params().AllowMinDifficultyBlocks())
+ UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
+ if (chainparams.GetConsensus().fPowAllowMinDifficultyBlocks)
{
// Changing pblock->nTime can change work required on testnet:
hashTarget.SetCompact(pblock->nBits);
@@ -530,6 +542,11 @@ void static BitcoinMiner(CWallet *pwallet)
LogPrintf("BitcoinMiner terminated\n");
throw;
}
+ catch (const std::runtime_error &e)
+ {
+ LogPrintf("BitcoinMiner runtime error: %s\n", e.what());
+ return;
+ }
}
void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)