2013-07-31 15:43:35 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-07-31 15:43:35 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "miner.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2014-10-23 02:05:11 +02:00
|
|
|
#include "amount.h"
|
2015-04-10 18:42:50 +02:00
|
|
|
#include "chainparams.h"
|
2015-01-24 15:29:29 +01:00
|
|
|
#include "consensus/consensus.h"
|
2015-01-24 15:57:12 +01:00
|
|
|
#include "consensus/validation.h"
|
2014-04-27 23:34:02 +02:00
|
|
|
#include "hash.h"
|
2013-07-31 15:43:35 +02:00
|
|
|
#include "main.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "net.h"
|
2014-03-10 16:46:53 +01:00
|
|
|
#include "pow.h"
|
2015-04-10 18:42:50 +02:00
|
|
|
#include "primitives/transaction.h"
|
2014-10-22 01:31:01 +02:00
|
|
|
#include "timedata.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "utilmoneystr.h"
|
2015-04-10 12:49:01 +02:00
|
|
|
#include "validationinterface.h"
|
2014-06-16 16:30:38 +02:00
|
|
|
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include <boost/thread.hpp>
|
2014-10-21 06:33:06 +02:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2014-06-16 16:30:38 +02:00
|
|
|
using namespace std;
|
2014-04-26 19:26:34 +02:00
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// BitcoinMiner
|
|
|
|
//
|
|
|
|
|
2014-04-10 20:14:18 +02:00
|
|
|
//
|
|
|
|
// Unconfirmed transactions in the memory pool often depend on other
|
|
|
|
// transactions in the memory pool. When we select transactions from the
|
|
|
|
// pool, we select by highest priority or fee rate, so we might consider
|
|
|
|
// transactions that depend on transactions that aren't yet in the block.
|
|
|
|
// The COrphan class keeps track of these 'temporary orphans' while
|
|
|
|
// CreateBlock is figuring out which transactions to include.
|
|
|
|
//
|
2013-07-31 15:43:35 +02:00
|
|
|
class COrphan
|
|
|
|
{
|
|
|
|
public:
|
2013-11-11 08:35:14 +01:00
|
|
|
const CTransaction* ptx;
|
2013-07-31 15:43:35 +02:00
|
|
|
set<uint256> setDependsOn;
|
2014-04-10 20:14:18 +02:00
|
|
|
CFeeRate feeRate;
|
2014-06-07 13:57:58 +02:00
|
|
|
double dPriority;
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-04-10 20:14:18 +02:00
|
|
|
COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
uint64_t nLastBlockTx = 0;
|
|
|
|
uint64_t nLastBlockSize = 0;
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-04-10 20:14:18 +02:00
|
|
|
// We want to sort transactions by priority and fee rate, so:
|
|
|
|
typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
|
2013-07-31 15:43:35 +02:00
|
|
|
class TxPriorityCompare
|
|
|
|
{
|
|
|
|
bool byFee;
|
2014-05-10 14:47:16 +02:00
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
public:
|
|
|
|
TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
|
2014-05-10 14:47:16 +02:00
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
bool operator()(const TxPriority& a, const TxPriority& b)
|
|
|
|
{
|
|
|
|
if (byFee)
|
|
|
|
{
|
|
|
|
if (a.get<1>() == b.get<1>())
|
|
|
|
return a.get<0>() < b.get<0>();
|
|
|
|
return a.get<1>() < b.get<1>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (a.get<0>() == b.get<0>())
|
|
|
|
return a.get<1>() < b.get<1>();
|
|
|
|
return a.get<0>() < b.get<0>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-10 18:42:50 +02:00
|
|
|
void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
|
2014-10-22 01:31:01 +02:00
|
|
|
{
|
|
|
|
pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
|
|
|
|
|
|
|
|
// Updating time can change work required on testnet:
|
2015-04-10 18:42:50 +02:00
|
|
|
if (consensusParams.fPowAllowMinDifficultyBlocks)
|
|
|
|
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
|
2014-10-22 01:31:01 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 07:33:06 +02:00
|
|
|
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2015-04-01 16:03:11 +02:00
|
|
|
const CChainParams& chainparams = Params();
|
2013-07-31 15:43:35 +02:00
|
|
|
// Create new block
|
|
|
|
auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
|
|
|
|
if(!pblocktemplate.get())
|
|
|
|
return NULL;
|
|
|
|
CBlock *pblock = &pblocktemplate->block; // pointer for convenience
|
|
|
|
|
2014-10-07 20:22:58 +02:00
|
|
|
// -regtest only: allow overriding block.nVersion with
|
|
|
|
// -blockversion=N to test forking scenarios
|
|
|
|
if (Params().MineBlocksOnDemand())
|
|
|
|
pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
|
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
// Create coinbase tx
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txNew;
|
2013-07-31 15:43:35 +02:00
|
|
|
txNew.vin.resize(1);
|
|
|
|
txNew.vin[0].prevout.SetNull();
|
|
|
|
txNew.vout.resize(1);
|
2013-08-24 06:33:46 +02:00
|
|
|
txNew.vout[0].scriptPubKey = scriptPubKeyIn;
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
// Add dummy coinbase tx as first transaction
|
|
|
|
pblock->vtx.push_back(CTransaction());
|
2013-07-31 15:43:35 +02:00
|
|
|
pblocktemplate->vTxFees.push_back(-1); // updated at end
|
|
|
|
pblocktemplate->vTxSigOps.push_back(-1); // updated at end
|
|
|
|
|
|
|
|
// Largest block you're willing to create:
|
2013-11-28 04:03:41 +01:00
|
|
|
unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
|
2013-07-31 15:43:35 +02:00
|
|
|
// Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
|
|
|
|
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
|
|
|
|
|
|
|
|
// How much of the block should be dedicated to high-priority transactions,
|
|
|
|
// included regardless of the fees they pay
|
|
|
|
unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
|
|
|
|
nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
|
|
|
|
|
|
|
|
// Minimum block size you want to create; block will be filled with free transactions
|
|
|
|
// until there are no more or the block reaches this size:
|
2014-03-10 18:59:12 +01:00
|
|
|
unsigned int nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
|
2013-07-31 15:43:35 +02:00
|
|
|
nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
|
|
|
|
|
|
|
|
// Collect memory pool transactions into the block
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nFees = 0;
|
2014-05-10 14:47:16 +02:00
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
|
|
|
LOCK2(cs_main, mempool.cs);
|
2015-04-10 07:33:06 +02:00
|
|
|
CBlockIndex* pindexPrev = chainActive.Tip();
|
2014-11-20 03:23:20 +01:00
|
|
|
const int nHeight = pindexPrev->nHeight + 1;
|
2015-05-25 06:48:33 +02:00
|
|
|
pblock->nTime = GetAdjustedTime();
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewCache view(pcoinsTip);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
|
|
|
// Priority order to process transactions
|
|
|
|
list<COrphan> vOrphan; // list memory doesn't move
|
|
|
|
map<uint256, vector<COrphan*> > mapDependers;
|
|
|
|
bool fPrintPriority = GetBoolArg("-printpriority", false);
|
|
|
|
|
|
|
|
// This vector will be sorted into a priority queue:
|
|
|
|
vector<TxPriority> vecPriority;
|
|
|
|
vecPriority.reserve(mempool.mapTx.size());
|
2013-11-11 08:35:14 +01:00
|
|
|
for (map<uint256, CTxMemPoolEntry>::iterator mi = mempool.mapTx.begin();
|
|
|
|
mi != mempool.mapTx.end(); ++mi)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2013-11-11 08:35:14 +01:00
|
|
|
const CTransaction& tx = mi->second.GetTx();
|
2015-05-25 06:48:33 +02:00
|
|
|
if (tx.IsCoinBase() || !IsFinalTx(tx, nHeight, pblock->nTime))
|
2013-07-31 15:43:35 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
COrphan* porphan = NULL;
|
|
|
|
double dPriority = 0;
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nTotalIn = 0;
|
2013-07-31 15:43:35 +02:00
|
|
|
bool fMissingInputs = false;
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, tx.vin)
|
|
|
|
{
|
|
|
|
// Read prev transaction
|
|
|
|
if (!view.HaveCoins(txin.prevout.hash))
|
|
|
|
{
|
|
|
|
// This should never happen; all transactions in the memory
|
|
|
|
// pool should connect to either transactions in the chain
|
|
|
|
// or other transactions in the memory pool.
|
|
|
|
if (!mempool.mapTx.count(txin.prevout.hash))
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("ERROR: mempool transaction missing input\n");
|
2013-07-31 15:43:35 +02:00
|
|
|
if (fDebug) assert("mempool transaction missing input" == 0);
|
|
|
|
fMissingInputs = true;
|
|
|
|
if (porphan)
|
|
|
|
vOrphan.pop_back();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has to wait for dependencies
|
|
|
|
if (!porphan)
|
|
|
|
{
|
|
|
|
// Use list for automatic deletion
|
|
|
|
vOrphan.push_back(COrphan(&tx));
|
|
|
|
porphan = &vOrphan.back();
|
|
|
|
}
|
|
|
|
mapDependers[txin.prevout.hash].push_back(porphan);
|
|
|
|
porphan->setDependsOn.insert(txin.prevout.hash);
|
2013-11-11 08:35:14 +01:00
|
|
|
nTotalIn += mempool.mapTx[txin.prevout.hash].GetTx().vout[txin.prevout.n].nValue;
|
2013-07-31 15:43:35 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-09-02 21:21:15 +02:00
|
|
|
const CCoins* coins = view.AccessCoins(txin.prevout.hash);
|
|
|
|
assert(coins);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nValueIn = coins->vout[txin.prevout.n].nValue;
|
2013-07-31 15:43:35 +02:00
|
|
|
nTotalIn += nValueIn;
|
|
|
|
|
2014-11-20 03:23:20 +01:00
|
|
|
int nConf = nHeight - coins->nHeight;
|
2013-07-31 15:43:35 +02:00
|
|
|
|
|
|
|
dPriority += (double)nValueIn * nConf;
|
|
|
|
}
|
|
|
|
if (fMissingInputs) continue;
|
|
|
|
|
2013-08-28 11:15:39 +02:00
|
|
|
// Priority is sum(valuein * age) / modified_txsize
|
2013-07-31 15:43:35 +02:00
|
|
|
unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
|
2013-11-11 08:35:14 +01:00
|
|
|
dPriority = tx.ComputePriority(dPriority, nTxSize);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2012-07-11 20:52:41 +02:00
|
|
|
uint256 hash = tx.GetHash();
|
|
|
|
mempool.ApplyDeltas(hash, dPriority, nTotalIn);
|
|
|
|
|
2014-04-10 20:14:18 +02:00
|
|
|
CFeeRate feeRate(nTotalIn-tx.GetValueOut(), nTxSize);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
|
|
|
if (porphan)
|
|
|
|
{
|
|
|
|
porphan->dPriority = dPriority;
|
2014-04-10 20:14:18 +02:00
|
|
|
porphan->feeRate = feeRate;
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|
|
|
|
else
|
2014-04-10 20:14:18 +02:00
|
|
|
vecPriority.push_back(TxPriority(dPriority, feeRate, &mi->second.GetTx()));
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect transactions into block
|
2013-04-13 07:13:08 +02:00
|
|
|
uint64_t nBlockSize = 1000;
|
|
|
|
uint64_t nBlockTx = 0;
|
2013-07-31 15:43:35 +02:00
|
|
|
int nBlockSigOps = 100;
|
|
|
|
bool fSortedByFee = (nBlockPrioritySize <= 0);
|
|
|
|
|
|
|
|
TxPriorityCompare comparer(fSortedByFee);
|
|
|
|
std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
|
|
|
|
|
|
|
|
while (!vecPriority.empty())
|
|
|
|
{
|
|
|
|
// Take highest priority transaction off the priority queue:
|
|
|
|
double dPriority = vecPriority.front().get<0>();
|
2014-04-10 20:14:18 +02:00
|
|
|
CFeeRate feeRate = vecPriority.front().get<1>();
|
2013-11-11 08:35:14 +01:00
|
|
|
const CTransaction& tx = *(vecPriority.front().get<2>());
|
2013-07-31 15:43:35 +02:00
|
|
|
|
|
|
|
std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
|
|
|
|
vecPriority.pop_back();
|
|
|
|
|
|
|
|
// Size limits
|
|
|
|
unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
if (nBlockSize + nTxSize >= nBlockMaxSize)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Legacy limits on sigOps:
|
|
|
|
unsigned int nTxSigOps = GetLegacySigOpCount(tx);
|
|
|
|
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Skip free transactions if we're past the minimum block size:
|
2012-07-11 20:52:41 +02:00
|
|
|
const uint256& hash = tx.GetHash();
|
|
|
|
double dPriorityDelta = 0;
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nFeeDelta = 0;
|
2012-07-11 20:52:41 +02:00
|
|
|
mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
|
2014-07-03 20:25:32 +02:00
|
|
|
if (fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
|
2013-07-31 15:43:35 +02:00
|
|
|
continue;
|
|
|
|
|
2012-07-11 20:52:41 +02:00
|
|
|
// Prioritise by fee once past the priority size or we run out of high-priority
|
2013-07-31 15:43:35 +02:00
|
|
|
// transactions:
|
|
|
|
if (!fSortedByFee &&
|
|
|
|
((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
|
|
|
|
{
|
|
|
|
fSortedByFee = true;
|
|
|
|
comparer = TxPriorityCompare(fSortedByFee);
|
|
|
|
std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!view.HaveInputs(tx))
|
|
|
|
continue;
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
|
2013-07-31 15:43:35 +02:00
|
|
|
|
|
|
|
nTxSigOps += GetP2SHSigOpCount(tx, view);
|
|
|
|
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
|
|
|
|
continue;
|
|
|
|
|
2014-03-10 22:31:46 +01:00
|
|
|
// Note that flags: we don't want to set mempool/IsStandard()
|
|
|
|
// policy here, but we still have to ensure that the block we
|
|
|
|
// create only contains transactions that are valid in new blocks.
|
2013-07-31 15:43:35 +02:00
|
|
|
CValidationState state;
|
2014-09-14 04:48:32 +02:00
|
|
|
if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true))
|
2013-07-31 15:43:35 +02:00
|
|
|
continue;
|
|
|
|
|
2014-10-27 16:07:50 +01:00
|
|
|
UpdateCoins(tx, state, view, nHeight);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
|
|
|
// Added
|
|
|
|
pblock->vtx.push_back(tx);
|
|
|
|
pblocktemplate->vTxFees.push_back(nTxFees);
|
|
|
|
pblocktemplate->vTxSigOps.push_back(nTxSigOps);
|
|
|
|
nBlockSize += nTxSize;
|
|
|
|
++nBlockTx;
|
|
|
|
nBlockSigOps += nTxSigOps;
|
|
|
|
nFees += nTxFees;
|
|
|
|
|
|
|
|
if (fPrintPriority)
|
|
|
|
{
|
2014-04-10 20:14:18 +02:00
|
|
|
LogPrintf("priority %.1f fee %s txid %s\n",
|
2014-05-10 14:47:16 +02:00
|
|
|
dPriority, feeRate.ToString(), tx.GetHash().ToString());
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add transactions that depend on this one to the priority queue
|
|
|
|
if (mapDependers.count(hash))
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
|
|
|
|
{
|
|
|
|
if (!porphan->setDependsOn.empty())
|
|
|
|
{
|
|
|
|
porphan->setDependsOn.erase(hash);
|
|
|
|
if (porphan->setDependsOn.empty())
|
|
|
|
{
|
2014-04-10 20:14:18 +02:00
|
|
|
vecPriority.push_back(TxPriority(porphan->dPriority, porphan->feeRate, porphan->ptx));
|
2013-07-31 15:43:35 +02:00
|
|
|
std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nLastBlockTx = nBlockTx;
|
|
|
|
nLastBlockSize = nBlockSize;
|
2014-02-24 09:08:56 +01:00
|
|
|
LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
// Compute final coinbase transaction.
|
2015-04-01 16:03:11 +02:00
|
|
|
txNew.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
|
2014-11-20 03:23:20 +01:00
|
|
|
txNew.vin[0].scriptSig = CScript() << nHeight << OP_0;
|
2014-06-07 13:53:27 +02:00
|
|
|
pblock->vtx[0] = txNew;
|
2013-07-31 15:43:35 +02:00
|
|
|
pblocktemplate->vTxFees[0] = -nFees;
|
|
|
|
|
|
|
|
// Fill in header
|
|
|
|
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
|
2015-04-10 18:42:50 +02:00
|
|
|
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
|
2015-02-15 02:21:42 +01:00
|
|
|
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
|
2013-07-31 15:43:35 +02:00
|
|
|
pblock->nNonce = 0;
|
|
|
|
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
|
|
|
|
|
|
|
|
CValidationState state;
|
2014-10-20 04:10:03 +02:00
|
|
|
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false))
|
2015-01-08 11:44:25 +01:00
|
|
|
throw std::runtime_error("CreateNewBlock(): TestBlockValidity failed");
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pblocktemplate.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
|
|
|
|
{
|
|
|
|
// Update nExtraNonce
|
|
|
|
static uint256 hashPrevBlock;
|
|
|
|
if (hashPrevBlock != pblock->hashPrevBlock)
|
|
|
|
{
|
|
|
|
nExtraNonce = 0;
|
|
|
|
hashPrevBlock = pblock->hashPrevBlock;
|
|
|
|
}
|
|
|
|
++nExtraNonce;
|
|
|
|
unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txCoinbase(pblock->vtx[0]);
|
|
|
|
txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
|
|
|
|
assert(txCoinbase.vin[0].scriptSig.size() <= 100);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
pblock->vtx[0] = txCoinbase;
|
2013-07-31 15:43:35 +02:00
|
|
|
pblock->hashMerkleRoot = pblock->BuildMerkleTree();
|
|
|
|
}
|
|
|
|
|
2013-12-09 08:55:56 +01:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Internal miner
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// ScanHash scans nonces looking for a hash with at least some zero bits.
|
2015-04-10 07:33:06 +02:00
|
|
|
// The nonce is usually preserved between calls, but periodically or if the
|
|
|
|
// nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at
|
|
|
|
// zero.
|
2013-12-09 08:55:56 +01:00
|
|
|
//
|
2015-04-10 07:33:06 +02:00
|
|
|
bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash)
|
2014-05-10 14:47:16 +02:00
|
|
|
{
|
2015-04-10 07:33:06 +02:00
|
|
|
// Write the first 76 bytes of the block header to a double-SHA256 state.
|
|
|
|
CHash256 hasher;
|
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ss << *pblock;
|
|
|
|
assert(ss.size() == 80);
|
|
|
|
hasher.Write((unsigned char*)&ss[0], 76);
|
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
while (true) {
|
2015-04-10 07:33:06 +02:00
|
|
|
nNonce++;
|
|
|
|
|
|
|
|
// Write the last 4 bytes of the block header (the nonce) to a copy of
|
|
|
|
// the double-SHA256 state, and compute the result.
|
|
|
|
CHash256(hasher).Write((unsigned char*)&nNonce, 4).Finalize((unsigned char*)phash);
|
2013-12-09 08:55:56 +01:00
|
|
|
|
|
|
|
// Return the nonce if the hash has at least some zero bits,
|
|
|
|
// caller will check if it has enough to reach the target
|
2014-04-27 23:34:02 +02:00
|
|
|
if (((uint16_t*)phash)[15] == 0)
|
|
|
|
return true;
|
2013-12-09 08:55:56 +01:00
|
|
|
|
2015-04-10 07:33:06 +02:00
|
|
|
// If nothing found after trying for a while, return -1
|
|
|
|
if ((nNonce & 0xfff) == 0)
|
2014-10-10 11:19:36 +02:00
|
|
|
return false;
|
2013-12-09 08:55:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 12:49:01 +02:00
|
|
|
static bool ProcessBlockFound(CBlock* pblock, const CChainParams& chainparams)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2014-08-20 10:26:27 +02:00
|
|
|
LogPrintf("%s\n", pblock->ToString());
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue));
|
2013-07-31 15:43:35 +02:00
|
|
|
|
|
|
|
// Found a solution
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2013-10-10 23:07:44 +02:00
|
|
|
if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
|
2015-01-08 11:44:25 +01:00
|
|
|
return error("BitcoinMiner: generated block is stale");
|
2014-05-07 17:10:35 +02:00
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2015-04-10 12:49:01 +02:00
|
|
|
// Inform about the new block
|
|
|
|
GetMainSignals().BlockFound(*pblock);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-07 17:10:35 +02:00
|
|
|
// Process this block the same as if we had received it from another node
|
|
|
|
CValidationState state;
|
2015-04-09 19:21:11 +02:00
|
|
|
if (!ProcessNewBlock(state, NULL, pblock, true, NULL))
|
2015-01-08 11:44:25 +01:00
|
|
|
return error("BitcoinMiner: ProcessNewBlock, block not accepted");
|
2014-05-07 17:10:35 +02:00
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-10 12:49:01 +02:00
|
|
|
void static BitcoinMiner(const CChainParams& chainparams, const CScript& coinbaseScript)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("BitcoinMiner started\n");
|
2013-07-31 15:43:35 +02:00
|
|
|
SetThreadPriority(THREAD_PRIORITY_LOWEST);
|
|
|
|
RenameThread("bitcoin-miner");
|
|
|
|
|
|
|
|
unsigned int nExtraNonce = 0;
|
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
try {
|
|
|
|
while (true) {
|
2015-04-10 18:42:50 +02:00
|
|
|
if (chainparams.MiningRequiresPeers()) {
|
2014-05-10 14:47:16 +02:00
|
|
|
// 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.
|
2015-04-29 02:00:50 +02:00
|
|
|
do {
|
|
|
|
bool fvNodesEmpty;
|
|
|
|
{
|
|
|
|
LOCK(cs_vNodes);
|
|
|
|
fvNodesEmpty = vNodes.empty();
|
|
|
|
}
|
|
|
|
if (!fvNodesEmpty && !IsInitialBlockDownload())
|
|
|
|
break;
|
2014-05-10 14:47:16 +02:00
|
|
|
MilliSleep(1000);
|
2015-04-29 02:00:50 +02:00
|
|
|
} while (true);
|
2014-05-10 14:47:16 +02:00
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
//
|
|
|
|
// Create new block
|
|
|
|
//
|
|
|
|
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
|
2015-04-10 07:33:06 +02:00
|
|
|
CBlockIndex* pindexPrev = chainActive.Tip();
|
2014-05-10 14:47:16 +02:00
|
|
|
|
2015-04-10 12:49:01 +02:00
|
|
|
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript));
|
2014-05-10 14:47:16 +02:00
|
|
|
if (!pblocktemplate.get())
|
2014-06-16 14:45:32 +02:00
|
|
|
{
|
|
|
|
LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
|
2014-05-10 14:47:16 +02:00
|
|
|
return;
|
2014-06-16 14:45:32 +02:00
|
|
|
}
|
2014-05-10 14:47:16 +02:00
|
|
|
CBlock *pblock = &pblocktemplate->block;
|
|
|
|
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
|
|
|
|
|
|
|
|
LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
|
|
|
|
::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Search
|
|
|
|
//
|
|
|
|
int64_t nStart = GetTime();
|
2015-04-10 07:33:06 +02:00
|
|
|
arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
|
|
|
|
uint256 hash;
|
|
|
|
uint32_t nNonce = 0;
|
2014-05-10 14:47:16 +02:00
|
|
|
while (true) {
|
|
|
|
// Check if something found
|
2015-04-10 07:33:06 +02:00
|
|
|
if (ScanHash(pblock, nNonce, &hash))
|
|
|
|
{
|
|
|
|
if (UintToArith256(hash) <= hashTarget)
|
|
|
|
{
|
|
|
|
// Found a solution
|
|
|
|
pblock->nNonce = nNonce;
|
|
|
|
assert(hash == pblock->GetHash());
|
|
|
|
|
|
|
|
SetThreadPriority(THREAD_PRIORITY_NORMAL);
|
|
|
|
LogPrintf("BitcoinMiner:\n");
|
|
|
|
LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex());
|
2015-04-10 12:49:01 +02:00
|
|
|
ProcessBlockFound(pblock, chainparams);
|
2015-04-10 07:33:06 +02:00
|
|
|
SetThreadPriority(THREAD_PRIORITY_LOWEST);
|
|
|
|
|
|
|
|
// In regression test mode, stop mining after a block is found.
|
2015-04-10 18:42:50 +02:00
|
|
|
if (chainparams.MineBlocksOnDemand())
|
2015-04-10 07:33:06 +02:00
|
|
|
throw boost::thread_interrupted();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
// Check for stop or if block needs to be rebuilt
|
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
// Regtest mode doesn't require peers
|
2015-04-10 18:42:50 +02:00
|
|
|
if (vNodes.empty() && chainparams.MiningRequiresPeers())
|
2014-05-10 14:47:16 +02:00
|
|
|
break;
|
2015-04-10 07:33:06 +02:00
|
|
|
if (nNonce >= 0xffff0000)
|
2014-05-10 14:47:16 +02:00
|
|
|
break;
|
|
|
|
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
|
|
|
|
break;
|
|
|
|
if (pindexPrev != chainActive.Tip())
|
|
|
|
break;
|
|
|
|
|
2015-04-10 07:33:06 +02:00
|
|
|
// Update nTime every few seconds
|
2015-04-10 18:42:50 +02:00
|
|
|
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
|
|
|
|
if (chainparams.GetConsensus().fPowAllowMinDifficultyBlocks)
|
2015-04-10 07:33:06 +02:00
|
|
|
{
|
|
|
|
// Changing pblock->nTime can change work required on testnet:
|
|
|
|
hashTarget.SetCompact(pblock->nBits);
|
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-10 14:47:16 +02:00
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const boost::thread_interrupted&)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("BitcoinMiner terminated\n");
|
2013-07-31 15:43:35 +02:00
|
|
|
throw;
|
|
|
|
}
|
2015-04-29 02:00:50 +02:00
|
|
|
catch (const std::runtime_error &e)
|
|
|
|
{
|
|
|
|
LogPrintf("BitcoinMiner runtime error: %s\n", e.what());
|
|
|
|
return;
|
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 12:49:01 +02:00
|
|
|
void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainparams)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
|
|
|
static boost::thread_group* minerThreads = NULL;
|
|
|
|
|
|
|
|
if (nThreads < 0) {
|
2014-03-08 07:47:56 +01:00
|
|
|
// In regtest threads defaults to 1
|
|
|
|
if (Params().DefaultMinerThreads())
|
|
|
|
nThreads = Params().DefaultMinerThreads();
|
2013-07-31 15:43:35 +02:00
|
|
|
else
|
|
|
|
nThreads = boost::thread::hardware_concurrency();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minerThreads != NULL)
|
|
|
|
{
|
|
|
|
minerThreads->interrupt_all();
|
|
|
|
delete minerThreads;
|
|
|
|
minerThreads = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nThreads == 0 || !fGenerate)
|
|
|
|
return;
|
|
|
|
|
2015-04-10 12:49:01 +02:00
|
|
|
CScript coinbaseScript;
|
|
|
|
GetMainSignals().ScriptForMining(coinbaseScript);
|
|
|
|
|
|
|
|
//throw an error if no script was provided
|
|
|
|
if (!coinbaseScript.size())
|
|
|
|
throw std::runtime_error("No coinbase script available (mining requires a wallet)");
|
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
minerThreads = new boost::thread_group();
|
|
|
|
for (int i = 0; i < nThreads; i++)
|
2015-04-10 12:49:01 +02:00
|
|
|
minerThreads->create_thread(boost::bind(&BitcoinMiner, boost::cref(chainparams), coinbaseScript));
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|