1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
10 #include "chainparams.h"
12 #include "consensus/consensus.h"
13 #include "consensus/merkle.h"
14 #include "consensus/validation.h"
18 #include "policy/policy.h"
20 #include "primitives/transaction.h"
21 #include "script/standard.h"
23 #include "txmempool.h"
25 #include "utilmoneystr.h"
26 #include "validationinterface.h"
28 #include <boost/thread.hpp>
29 #include <boost/tuple/tuple.hpp>
33 //////////////////////////////////////////////////////////////////////////////
39 // Unconfirmed transactions in the memory pool often depend on other
40 // transactions in the memory pool. When we select transactions from the
41 // pool, we select by highest priority or fee rate, so we might consider
42 // transactions that depend on transactions that aren't yet in the block.
43 // The COrphan class keeps track of these 'temporary orphans' while
44 // CreateBlock is figuring out which transactions to include.
49 const CTransaction
* ptx
;
50 set
<uint256
> setDependsOn
;
54 COrphan(const CTransaction
* ptxIn
) : ptx(ptxIn
), feeRate(0), dPriority(0)
59 uint64_t nLastBlockTx
= 0;
60 uint64_t nLastBlockSize
= 0;
62 // We want to sort transactions by priority and fee rate, so:
63 typedef boost::tuple
<double, CFeeRate
, const CTransaction
*> TxPriority
;
64 class TxPriorityCompare
69 TxPriorityCompare(bool _byFee
) : byFee(_byFee
) { }
71 bool operator()(const TxPriority
& a
, const TxPriority
& b
)
75 if (a
.get
<1>() == b
.get
<1>())
76 return a
.get
<0>() < b
.get
<0>();
77 return a
.get
<1>() < b
.get
<1>();
81 if (a
.get
<0>() == b
.get
<0>())
82 return a
.get
<1>() < b
.get
<1>();
83 return a
.get
<0>() < b
.get
<0>();
88 int64_t UpdateTime(CBlockHeader
* pblock
, const Consensus::Params
& consensusParams
, const CBlockIndex
* pindexPrev
)
90 int64_t nOldTime
= pblock
->nTime
;
91 int64_t nNewTime
= std::max(pindexPrev
->GetMedianTimePast()+1, GetAdjustedTime());
93 if (nOldTime
< nNewTime
)
94 pblock
->nTime
= nNewTime
;
96 // Updating time can change work required on testnet:
97 if (consensusParams
.fPowAllowMinDifficultyBlocks
)
98 pblock
->nBits
= GetNextWorkRequired(pindexPrev
, pblock
, consensusParams
);
100 return nNewTime
- nOldTime
;
103 CBlockTemplate
* CreateNewBlock(const CChainParams
& chainparams
, const CScript
& scriptPubKeyIn
)
106 auto_ptr
<CBlockTemplate
> pblocktemplate(new CBlockTemplate());
107 if(!pblocktemplate
.get())
109 CBlock
*pblock
= &pblocktemplate
->block
; // pointer for convenience
111 // -regtest only: allow overriding block.nVersion with
112 // -blockversion=N to test forking scenarios
113 if (chainparams
.MineBlocksOnDemand())
114 pblock
->nVersion
= GetArg("-blockversion", pblock
->nVersion
);
116 // Create coinbase tx
117 CMutableTransaction txNew
;
119 txNew
.vin
[0].prevout
.SetNull();
120 txNew
.vout
.resize(1);
121 txNew
.vout
[0].scriptPubKey
= scriptPubKeyIn
;
123 // Add dummy coinbase tx as first transaction
124 pblock
->vtx
.push_back(CTransaction());
125 pblocktemplate
->vTxFees
.push_back(-1); // updated at end
126 pblocktemplate
->vTxSigOps
.push_back(-1); // updated at end
128 // Largest block you're willing to create:
129 unsigned int nBlockMaxSize
= GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE
);
130 // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
131 nBlockMaxSize
= std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE
-1000), nBlockMaxSize
));
133 // How much of the block should be dedicated to high-priority transactions,
134 // included regardless of the fees they pay
135 unsigned int nBlockPrioritySize
= GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE
);
136 nBlockPrioritySize
= std::min(nBlockMaxSize
, nBlockPrioritySize
);
138 // Minimum block size you want to create; block will be filled with free transactions
139 // until there are no more or the block reaches this size:
140 unsigned int nBlockMinSize
= GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE
);
141 nBlockMinSize
= std::min(nBlockMaxSize
, nBlockMinSize
);
143 // Collect memory pool transactions into the block
147 LOCK2(cs_main
, mempool
.cs
);
148 CBlockIndex
* pindexPrev
= chainActive
.Tip();
149 const int nHeight
= pindexPrev
->nHeight
+ 1;
150 pblock
->nTime
= GetAdjustedTime();
151 const int64_t nMedianTimePast
= pindexPrev
->GetMedianTimePast();
152 CCoinsViewCache
view(pcoinsTip
);
154 // Priority order to process transactions
155 list
<COrphan
> vOrphan
; // list memory doesn't move
156 map
<uint256
, vector
<COrphan
*> > mapDependers
;
157 bool fPrintPriority
= GetBoolArg("-printpriority", false);
159 // This vector will be sorted into a priority queue:
160 vector
<TxPriority
> vecPriority
;
161 vecPriority
.reserve(mempool
.mapTx
.size());
162 for (CTxMemPool::indexed_transaction_set::iterator mi
= mempool
.mapTx
.begin();
163 mi
!= mempool
.mapTx
.end(); ++mi
)
165 const CTransaction
& tx
= mi
->GetTx();
167 int64_t nLockTimeCutoff
= (STANDARD_LOCKTIME_VERIFY_FLAGS
& LOCKTIME_MEDIAN_TIME_PAST
)
169 : pblock
->GetBlockTime();
171 if (tx
.IsCoinBase() || !IsFinalTx(tx
, nHeight
, nLockTimeCutoff
))
174 COrphan
* porphan
= NULL
;
175 double dPriority
= 0;
176 CAmount nTotalIn
= 0;
177 bool fMissingInputs
= false;
178 BOOST_FOREACH(const CTxIn
& txin
, tx
.vin
)
180 // Read prev transaction
181 if (!view
.HaveCoins(txin
.prevout
.hash
))
183 // This should never happen; all transactions in the memory
184 // pool should connect to either transactions in the chain
185 // or other transactions in the memory pool.
186 if (!mempool
.mapTx
.count(txin
.prevout
.hash
))
188 LogPrintf("ERROR: mempool transaction missing input\n");
189 if (fDebug
) assert("mempool transaction missing input" == 0);
190 fMissingInputs
= true;
196 // Has to wait for dependencies
199 // Use list for automatic deletion
200 vOrphan
.push_back(COrphan(&tx
));
201 porphan
= &vOrphan
.back();
203 mapDependers
[txin
.prevout
.hash
].push_back(porphan
);
204 porphan
->setDependsOn
.insert(txin
.prevout
.hash
);
205 nTotalIn
+= mempool
.mapTx
.find(txin
.prevout
.hash
)->GetTx().vout
[txin
.prevout
.n
].nValue
;
208 const CCoins
* coins
= view
.AccessCoins(txin
.prevout
.hash
);
211 CAmount nValueIn
= coins
->vout
[txin
.prevout
.n
].nValue
;
212 nTotalIn
+= nValueIn
;
214 int nConf
= nHeight
- coins
->nHeight
;
216 dPriority
+= (double)nValueIn
* nConf
;
218 if (fMissingInputs
) continue;
220 // Priority is sum(valuein * age) / modified_txsize
221 unsigned int nTxSize
= ::GetSerializeSize(tx
, SER_NETWORK
, PROTOCOL_VERSION
);
222 dPriority
= tx
.ComputePriority(dPriority
, nTxSize
);
224 uint256 hash
= tx
.GetHash();
225 mempool
.ApplyDeltas(hash
, dPriority
, nTotalIn
);
227 CFeeRate
feeRate(nTotalIn
-tx
.GetValueOut(), nTxSize
);
231 porphan
->dPriority
= dPriority
;
232 porphan
->feeRate
= feeRate
;
235 vecPriority
.push_back(TxPriority(dPriority
, feeRate
, &(mi
->GetTx())));
238 // Collect transactions into block
239 uint64_t nBlockSize
= 1000;
240 uint64_t nBlockTx
= 0;
241 int nBlockSigOps
= 100;
242 bool fSortedByFee
= (nBlockPrioritySize
<= 0);
244 TxPriorityCompare
comparer(fSortedByFee
);
245 std::make_heap(vecPriority
.begin(), vecPriority
.end(), comparer
);
247 while (!vecPriority
.empty())
249 // Take highest priority transaction off the priority queue:
250 double dPriority
= vecPriority
.front().get
<0>();
251 CFeeRate feeRate
= vecPriority
.front().get
<1>();
252 const CTransaction
& tx
= *(vecPriority
.front().get
<2>());
254 std::pop_heap(vecPriority
.begin(), vecPriority
.end(), comparer
);
255 vecPriority
.pop_back();
258 unsigned int nTxSize
= ::GetSerializeSize(tx
, SER_NETWORK
, PROTOCOL_VERSION
);
259 if (nBlockSize
+ nTxSize
>= nBlockMaxSize
)
262 // Legacy limits on sigOps:
263 unsigned int nTxSigOps
= GetLegacySigOpCount(tx
);
264 if (nBlockSigOps
+ nTxSigOps
>= MAX_BLOCK_SIGOPS
)
267 // Skip free transactions if we're past the minimum block size:
268 const uint256
& hash
= tx
.GetHash();
269 double dPriorityDelta
= 0;
270 CAmount nFeeDelta
= 0;
271 mempool
.ApplyDeltas(hash
, dPriorityDelta
, nFeeDelta
);
272 if (fSortedByFee
&& (dPriorityDelta
<= 0) && (nFeeDelta
<= 0) && (feeRate
< ::minRelayTxFee
) && (nBlockSize
+ nTxSize
>= nBlockMinSize
))
275 // Prioritise by fee once past the priority size or we run out of high-priority
278 ((nBlockSize
+ nTxSize
>= nBlockPrioritySize
) || !AllowFree(dPriority
)))
281 comparer
= TxPriorityCompare(fSortedByFee
);
282 std::make_heap(vecPriority
.begin(), vecPriority
.end(), comparer
);
285 if (!view
.HaveInputs(tx
))
288 CAmount nTxFees
= view
.GetValueIn(tx
)-tx
.GetValueOut();
290 nTxSigOps
+= GetP2SHSigOpCount(tx
, view
);
291 if (nBlockSigOps
+ nTxSigOps
>= MAX_BLOCK_SIGOPS
)
294 // Note that flags: we don't want to set mempool/IsStandard()
295 // policy here, but we still have to ensure that the block we
296 // create only contains transactions that are valid in new blocks.
297 CValidationState state
;
298 if (!CheckInputs(tx
, state
, view
, true, MANDATORY_SCRIPT_VERIFY_FLAGS
, true))
301 UpdateCoins(tx
, state
, view
, nHeight
);
304 pblock
->vtx
.push_back(tx
);
305 pblocktemplate
->vTxFees
.push_back(nTxFees
);
306 pblocktemplate
->vTxSigOps
.push_back(nTxSigOps
);
307 nBlockSize
+= nTxSize
;
309 nBlockSigOps
+= nTxSigOps
;
314 LogPrintf("priority %.1f fee %s txid %s\n",
315 dPriority
, feeRate
.ToString(), tx
.GetHash().ToString());
318 // Add transactions that depend on this one to the priority queue
319 if (mapDependers
.count(hash
))
321 BOOST_FOREACH(COrphan
* porphan
, mapDependers
[hash
])
323 if (!porphan
->setDependsOn
.empty())
325 porphan
->setDependsOn
.erase(hash
);
326 if (porphan
->setDependsOn
.empty())
328 vecPriority
.push_back(TxPriority(porphan
->dPriority
, porphan
->feeRate
, porphan
->ptx
));
329 std::push_heap(vecPriority
.begin(), vecPriority
.end(), comparer
);
336 nLastBlockTx
= nBlockTx
;
337 nLastBlockSize
= nBlockSize
;
338 LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize
);
340 // Compute final coinbase transaction.
341 txNew
.vout
[0].nValue
= nFees
+ GetBlockSubsidy(nHeight
, chainparams
.GetConsensus());
342 txNew
.vin
[0].scriptSig
= CScript() << nHeight
<< OP_0
;
343 pblock
->vtx
[0] = txNew
;
344 pblocktemplate
->vTxFees
[0] = -nFees
;
347 pblock
->hashPrevBlock
= pindexPrev
->GetBlockHash();
348 UpdateTime(pblock
, chainparams
.GetConsensus(), pindexPrev
);
349 pblock
->nBits
= GetNextWorkRequired(pindexPrev
, pblock
, chainparams
.GetConsensus());
351 pblocktemplate
->vTxSigOps
[0] = GetLegacySigOpCount(pblock
->vtx
[0]);
353 CValidationState state
;
354 if (!TestBlockValidity(state
, chainparams
, *pblock
, pindexPrev
, false, false))
355 throw std::runtime_error("CreateNewBlock(): TestBlockValidity failed");
358 return pblocktemplate
.release();
361 void IncrementExtraNonce(CBlock
* pblock
, const CBlockIndex
* pindexPrev
, unsigned int& nExtraNonce
)
363 // Update nExtraNonce
364 static uint256 hashPrevBlock
;
365 if (hashPrevBlock
!= pblock
->hashPrevBlock
)
368 hashPrevBlock
= pblock
->hashPrevBlock
;
371 unsigned int nHeight
= pindexPrev
->nHeight
+1; // Height first in coinbase required for block.version=2
372 CMutableTransaction
txCoinbase(pblock
->vtx
[0]);
373 txCoinbase
.vin
[0].scriptSig
= (CScript() << nHeight
<< CScriptNum(nExtraNonce
)) + COINBASE_FLAGS
;
374 assert(txCoinbase
.vin
[0].scriptSig
.size() <= 100);
376 pblock
->vtx
[0] = txCoinbase
;
377 pblock
->hashMerkleRoot
= BlockMerkleRoot(*pblock
);
380 //////////////////////////////////////////////////////////////////////////////
386 // ScanHash scans nonces looking for a hash with at least some zero bits.
387 // The nonce is usually preserved between calls, but periodically or if the
388 // nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at
391 bool static ScanHash(const CBlockHeader
*pblock
, uint32_t& nNonce
, uint256
*phash
)
393 // Write the first 76 bytes of the block header to a double-SHA256 state.
395 CDataStream
ss(SER_NETWORK
, PROTOCOL_VERSION
);
397 assert(ss
.size() == 80);
398 hasher
.Write((unsigned char*)&ss
[0], 76);
403 // Write the last 4 bytes of the block header (the nonce) to a copy of
404 // the double-SHA256 state, and compute the result.
405 CHash256(hasher
).Write((unsigned char*)&nNonce
, 4).Finalize((unsigned char*)phash
);
407 // Return the nonce if the hash has at least some zero bits,
408 // caller will check if it has enough to reach the target
409 if (((uint16_t*)phash
)[15] == 0)
412 // If nothing found after trying for a while, return -1
413 if ((nNonce
& 0xfff) == 0)
418 static bool ProcessBlockFound(const CBlock
* pblock
, const CChainParams
& chainparams
)
420 LogPrintf("%s\n", pblock
->ToString());
421 LogPrintf("generated %s\n", FormatMoney(pblock
->vtx
[0].vout
[0].nValue
));
426 if (pblock
->hashPrevBlock
!= chainActive
.Tip()->GetBlockHash())
427 return error("BitcoinMiner: generated block is stale");
430 // Inform about the new block
431 GetMainSignals().BlockFound(pblock
->GetHash());
433 // Process this block the same as if we had received it from another node
434 CValidationState state
;
435 if (!ProcessNewBlock(state
, chainparams
, NULL
, pblock
, true, NULL
))
436 return error("BitcoinMiner: ProcessNewBlock, block not accepted");
441 void static BitcoinMiner(const CChainParams
& chainparams
)
443 LogPrintf("BitcoinMiner started\n");
444 SetThreadPriority(THREAD_PRIORITY_LOWEST
);
445 RenameThread("bitcoin-miner");
447 unsigned int nExtraNonce
= 0;
449 boost::shared_ptr
<CReserveScript
> coinbaseScript
;
450 GetMainSignals().ScriptForMining(coinbaseScript
);
453 // Throw an error if no script was provided. This can happen
454 // due to some internal error but also if the keypool is empty.
455 // In the latter case, already the pointer is NULL.
456 if (!coinbaseScript
|| coinbaseScript
->reserveScript
.empty())
457 throw std::runtime_error("No coinbase script available (mining requires a wallet)");
460 if (chainparams
.MiningRequiresPeers()) {
461 // Busy-wait for the network to come online so we don't waste time mining
462 // on an obsolete chain. In regtest mode we expect to fly solo.
467 fvNodesEmpty
= vNodes
.empty();
469 if (!fvNodesEmpty
&& !IsInitialBlockDownload())
478 unsigned int nTransactionsUpdatedLast
= mempool
.GetTransactionsUpdated();
479 CBlockIndex
* pindexPrev
= chainActive
.Tip();
481 auto_ptr
<CBlockTemplate
> pblocktemplate(CreateNewBlock(chainparams
, coinbaseScript
->reserveScript
));
482 if (!pblocktemplate
.get())
484 LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
487 CBlock
*pblock
= &pblocktemplate
->block
;
488 IncrementExtraNonce(pblock
, pindexPrev
, nExtraNonce
);
490 LogPrintf("Running BitcoinMiner with %u transactions in block (%u bytes)\n", pblock
->vtx
.size(),
491 ::GetSerializeSize(*pblock
, SER_NETWORK
, PROTOCOL_VERSION
));
496 int64_t nStart
= GetTime();
497 arith_uint256 hashTarget
= arith_uint256().SetCompact(pblock
->nBits
);
501 // Check if something found
502 if (ScanHash(pblock
, nNonce
, &hash
))
504 if (UintToArith256(hash
) <= hashTarget
)
507 pblock
->nNonce
= nNonce
;
508 assert(hash
== pblock
->GetHash());
510 SetThreadPriority(THREAD_PRIORITY_NORMAL
);
511 LogPrintf("BitcoinMiner:\n");
512 LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash
.GetHex(), hashTarget
.GetHex());
513 ProcessBlockFound(pblock
, chainparams
);
514 SetThreadPriority(THREAD_PRIORITY_LOWEST
);
515 coinbaseScript
->KeepScript();
517 // In regression test mode, stop mining after a block is found.
518 if (chainparams
.MineBlocksOnDemand())
519 throw boost::thread_interrupted();
525 // Check for stop or if block needs to be rebuilt
526 boost::this_thread::interruption_point();
527 // Regtest mode doesn't require peers
528 if (vNodes
.empty() && chainparams
.MiningRequiresPeers())
530 if (nNonce
>= 0xffff0000)
532 if (mempool
.GetTransactionsUpdated() != nTransactionsUpdatedLast
&& GetTime() - nStart
> 60)
534 if (pindexPrev
!= chainActive
.Tip())
537 // Update nTime every few seconds
538 if (UpdateTime(pblock
, chainparams
.GetConsensus(), pindexPrev
) < 0)
539 break; // Recreate the block if the clock has run backwards,
540 // so that we can use the correct time.
541 if (chainparams
.GetConsensus().fPowAllowMinDifficultyBlocks
)
543 // Changing pblock->nTime can change work required on testnet:
544 hashTarget
.SetCompact(pblock
->nBits
);
549 catch (const boost::thread_interrupted
&)
551 LogPrintf("BitcoinMiner terminated\n");
554 catch (const std::runtime_error
&e
)
556 LogPrintf("BitcoinMiner runtime error: %s\n", e
.what());
561 void GenerateBitcoins(bool fGenerate
, int nThreads
, const CChainParams
& chainparams
)
563 static boost::thread_group
* minerThreads
= NULL
;
566 nThreads
= GetNumCores();
568 if (minerThreads
!= NULL
)
570 minerThreads
->interrupt_all();
575 if (nThreads
== 0 || !fGenerate
)
578 minerThreads
= new boost::thread_group();
579 for (int i
= 0; i
< nThreads
; i
++)
580 minerThreads
->create_thread(boost::bind(&BitcoinMiner
, boost::cref(chainparams
)));