2013-07-31 15:43:35 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-02-08 22:50:24 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
2013-07-31 15:43:35 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2012-07-11 20:52:41 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
#include "miner.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include "core.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"
|
2013-12-18 20:46:43 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "wallet.h"
|
2013-12-18 20:46:43 +01:00
|
|
|
#endif
|
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>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-26 02:16:23 +02:00
|
|
|
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
|
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
|
|
|
|
|
|
|
|
// 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
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t nFees = 0;
|
2014-05-10 14:47:16 +02:00
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
|
|
|
LOCK2(cs_main, mempool.cs);
|
2013-10-10 23:07:44 +02:00
|
|
|
CBlockIndex* pindexPrev = chainActive.Tip();
|
2013-07-31 15:43:35 +02:00
|
|
|
CCoinsViewCache view(*pcoinsTip, true);
|
|
|
|
|
|
|
|
// 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();
|
2014-01-27 03:50:15 +01:00
|
|
|
if (tx.IsCoinBase() || !IsFinalTx(tx, pindexPrev->nHeight + 1))
|
2013-07-31 15:43:35 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
COrphan* porphan = NULL;
|
|
|
|
double dPriority = 0;
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t 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;
|
|
|
|
}
|
|
|
|
const CCoins &coins = view.GetCoins(txin.prevout.hash);
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t nValueIn = coins.vout[txin.prevout.n].nValue;
|
2013-07-31 15:43:35 +02:00
|
|
|
nTotalIn += nValueIn;
|
|
|
|
|
|
|
|
int nConf = pindexPrev->nHeight - coins.nHeight + 1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
int64_t nFeeDelta = 0;
|
|
|
|
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;
|
|
|
|
|
2013-11-11 07:03:51 +01:00
|
|
|
int64_t 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-03-10 22:31:46 +01:00
|
|
|
if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS))
|
2013-07-31 15:43:35 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
CTxUndo txundo;
|
2014-06-09 10:02:00 +02:00
|
|
|
UpdateCoins(tx, state, view, txundo, pindexPrev->nHeight+1);
|
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.
|
|
|
|
txNew.vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees);
|
|
|
|
txNew.vin[0].scriptSig = CScript() << OP_0 << OP_0;
|
|
|
|
pblock->vtx[0] = txNew;
|
2013-07-31 15:43:35 +02:00
|
|
|
pblocktemplate->vTxFees[0] = -nFees;
|
|
|
|
|
|
|
|
// Fill in header
|
|
|
|
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
|
|
|
|
UpdateTime(*pblock, pindexPrev);
|
|
|
|
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
|
|
|
|
pblock->nNonce = 0;
|
|
|
|
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
|
|
|
|
|
|
|
|
CBlockIndex indexDummy(*pblock);
|
|
|
|
indexDummy.pprev = pindexPrev;
|
|
|
|
indexDummy.nHeight = pindexPrev->nHeight + 1;
|
|
|
|
CCoinsViewCache viewNew(*pcoinsTip, true);
|
|
|
|
CValidationState state;
|
|
|
|
if (!ConnectBlock(*pblock, state, &indexDummy, viewNew, true))
|
|
|
|
throw std::runtime_error("CreateNewBlock() : ConnectBlock failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08 15:26:08 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
2013-12-09 08:55:56 +01:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Internal miner
|
|
|
|
//
|
|
|
|
double dHashesPerSec = 0.0;
|
|
|
|
int64_t nHPSTimerStart = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// ScanHash scans nonces looking for a hash with at least some zero bits.
|
2014-04-27 23:34:02 +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
|
|
|
//
|
2014-05-10 14:47:16 +02:00
|
|
|
bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash)
|
|
|
|
{
|
2014-04-27 23:34:02 +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) {
|
2013-12-09 08:55:56 +01:00
|
|
|
nNonce++;
|
2014-04-27 23:34:02 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// If nothing found after trying for a while, return -1
|
|
|
|
if ((nNonce & 0xffff) == 0)
|
2014-04-27 23:34:02 +02:00
|
|
|
return false;
|
2013-12-09 08:55:56 +01:00
|
|
|
if ((nNonce & 0xfff) == 0)
|
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
|
|
|
|
{
|
|
|
|
CPubKey pubkey;
|
|
|
|
if (!reservekey.GetReservedKey(pubkey))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
|
|
|
|
return CreateNewBlock(scriptPubKey);
|
|
|
|
}
|
|
|
|
|
2014-07-07 07:40:40 +02:00
|
|
|
bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
|
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())
|
2013-07-31 15:43:35 +02:00
|
|
|
return error("BitcoinMiner : generated block is stale");
|
2014-05-07 17:10:35 +02:00
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-07 17:10:35 +02:00
|
|
|
// Remove key from key pool
|
|
|
|
reservekey.KeepKey();
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-07 17:10:35 +02:00
|
|
|
// Track how many getdata requests this block gets
|
|
|
|
{
|
|
|
|
LOCK(wallet.cs_wallet);
|
|
|
|
wallet.mapRequestCount[pblock->GetHash()] = 0;
|
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;
|
|
|
|
if (!ProcessBlock(state, NULL, pblock))
|
|
|
|
return error("BitcoinMiner : ProcessBlock, block not accepted");
|
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void static BitcoinMiner(CWallet *pwallet)
|
|
|
|
{
|
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");
|
|
|
|
|
|
|
|
// Each thread has its own key and counter
|
|
|
|
CReserveKey reservekey(pwallet);
|
|
|
|
unsigned int nExtraNonce = 0;
|
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
if (Params().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())
|
|
|
|
MilliSleep(1000);
|
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
//
|
|
|
|
// Create new block
|
|
|
|
//
|
|
|
|
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
|
|
|
|
CBlockIndex* pindexPrev = chainActive.Tip();
|
|
|
|
|
|
|
|
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
|
|
|
|
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();
|
|
|
|
uint256 hashTarget = uint256().SetCompact(pblock->nBits);
|
|
|
|
uint256 hash;
|
|
|
|
uint32_t nNonce = 0;
|
|
|
|
uint32_t nOldNonce = 0;
|
|
|
|
while (true) {
|
|
|
|
bool fFound = ScanHash(pblock, nNonce, &hash);
|
|
|
|
uint32_t nHashesDone = nNonce - nOldNonce;
|
|
|
|
nOldNonce = nNonce;
|
|
|
|
|
|
|
|
// Check if something found
|
|
|
|
if (fFound)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2014-05-10 14:47:16 +02:00
|
|
|
if (hash <= hashTarget)
|
|
|
|
{
|
|
|
|
// Found a solution
|
|
|
|
pblock->nNonce = nNonce;
|
|
|
|
assert(hash == pblock->GetHash());
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
SetThreadPriority(THREAD_PRIORITY_NORMAL);
|
2014-07-07 07:40:40 +02:00
|
|
|
LogPrintf("BitcoinMiner:\n");
|
|
|
|
LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex());
|
|
|
|
ProcessBlockFound(pblock, *pwallet, reservekey);
|
2014-05-10 14:47:16 +02:00
|
|
|
SetThreadPriority(THREAD_PRIORITY_LOWEST);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
// In regression test mode, stop mining after a block is found.
|
|
|
|
if (Params().MineBlocksOnDemand())
|
|
|
|
throw boost::thread_interrupted();
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
// Meter hashes/sec
|
|
|
|
static int64_t nHashCounter;
|
|
|
|
if (nHPSTimerStart == 0)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2014-05-10 14:47:16 +02:00
|
|
|
nHPSTimerStart = GetTimeMillis();
|
|
|
|
nHashCounter = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nHashCounter += nHashesDone;
|
|
|
|
if (GetTimeMillis() - nHPSTimerStart > 4000)
|
|
|
|
{
|
|
|
|
static CCriticalSection cs;
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2014-05-10 14:47:16 +02:00
|
|
|
LOCK(cs);
|
|
|
|
if (GetTimeMillis() - nHPSTimerStart > 4000)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2014-05-10 14:47:16 +02:00
|
|
|
dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
|
|
|
|
nHPSTimerStart = GetTimeMillis();
|
|
|
|
nHashCounter = 0;
|
|
|
|
static int64_t nLogTime;
|
|
|
|
if (GetTime() - nLogTime > 30 * 60)
|
|
|
|
{
|
|
|
|
nLogTime = GetTime();
|
|
|
|
LogPrintf("hashmeter %6.0f khash/s\n", dHashesPerSec/1000.0);
|
|
|
|
}
|
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
|
|
|
|
if (vNodes.empty() && Params().MiningRequiresPeers())
|
|
|
|
break;
|
|
|
|
if (nNonce >= 0xffff0000)
|
|
|
|
break;
|
|
|
|
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
|
|
|
|
break;
|
|
|
|
if (pindexPrev != chainActive.Tip())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Update nTime every few seconds
|
|
|
|
UpdateTime(*pblock, pindexPrev);
|
|
|
|
if (Params().AllowMinDifficultyBlocks())
|
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
catch (boost::thread_interrupted)
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("BitcoinMiner terminated\n");
|
2013-07-31 15:43:35 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-21 05:07:55 +01:00
|
|
|
void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
|
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;
|
|
|
|
|
|
|
|
minerThreads = new boost::thread_group();
|
|
|
|
for (int i = 0; i < nThreads; i++)
|
|
|
|
minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet));
|
|
|
|
}
|
|
|
|
|
2014-05-10 14:47:16 +02:00
|
|
|
#endif // ENABLE_WALLET
|