2013-07-31 15:43:35 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2009-2015 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.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
#ifndef BITCOIN_MINER_H
|
|
|
|
#define BITCOIN_MINER_H
|
|
|
|
|
2014-10-12 05:26:42 +02:00
|
|
|
#include "primitives/block.h"
|
2015-12-15 21:26:44 +01:00
|
|
|
#include "txmempool.h"
|
2014-10-12 05:26:42 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
2015-12-15 21:26:44 +01:00
|
|
|
#include <memory>
|
2016-02-21 03:02:44 +01:00
|
|
|
#include "boost/multi_index_container.hpp"
|
|
|
|
#include "boost/multi_index/ordered_index.hpp"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
class CBlockIndex;
|
2015-04-10 12:49:01 +02:00
|
|
|
class CChainParams;
|
2013-04-13 07:13:08 +02:00
|
|
|
class CReserveKey;
|
|
|
|
class CScript;
|
|
|
|
class CWallet;
|
2015-12-15 21:26:44 +01:00
|
|
|
|
2015-04-16 10:32:47 +02:00
|
|
|
namespace Consensus { struct Params; };
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2015-06-27 21:21:41 +02:00
|
|
|
static const bool DEFAULT_PRINTPRIORITY = false;
|
|
|
|
|
2014-10-12 05:26:42 +02:00
|
|
|
struct CBlockTemplate
|
|
|
|
{
|
|
|
|
CBlock block;
|
|
|
|
std::vector<CAmount> vTxFees;
|
2016-01-03 18:54:50 +01:00
|
|
|
std::vector<int64_t> vTxSigOpsCost;
|
2015-11-06 01:42:38 +01:00
|
|
|
std::vector<unsigned char> vchCoinbaseCommitment;
|
2014-10-12 05:26:42 +02:00
|
|
|
};
|
2014-05-10 14:54:20 +02:00
|
|
|
|
2016-02-21 03:02:44 +01:00
|
|
|
// Container for tracking updates to ancestor feerate as we include (parent)
|
|
|
|
// transactions in a block
|
|
|
|
struct CTxMemPoolModifiedEntry {
|
|
|
|
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
|
|
|
|
{
|
|
|
|
iter = entry;
|
|
|
|
nSizeWithAncestors = entry->GetSizeWithAncestors();
|
|
|
|
nModFeesWithAncestors = entry->GetModFeesWithAncestors();
|
2016-01-03 18:54:50 +01:00
|
|
|
nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
|
2016-02-21 03:02:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPool::txiter iter;
|
|
|
|
uint64_t nSizeWithAncestors;
|
|
|
|
CAmount nModFeesWithAncestors;
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t nSigOpCostWithAncestors;
|
2016-02-21 03:02:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Comparator for CTxMemPool::txiter objects.
|
|
|
|
* It simply compares the internal memory address of the CTxMemPoolEntry object
|
|
|
|
* pointed to. This means it has no meaning, and is only useful for using them
|
|
|
|
* as key in other indexes.
|
|
|
|
*/
|
|
|
|
struct CompareCTxMemPoolIter {
|
|
|
|
bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
|
|
|
|
{
|
|
|
|
return &(*a) < &(*b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct modifiedentry_iter {
|
|
|
|
typedef CTxMemPool::txiter result_type;
|
|
|
|
result_type operator() (const CTxMemPoolModifiedEntry &entry) const
|
|
|
|
{
|
|
|
|
return entry.iter;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This matches the calculation in CompareTxMemPoolEntryByAncestorFee,
|
|
|
|
// except operating on CTxMemPoolModifiedEntry.
|
|
|
|
// TODO: refactor to avoid duplication of this logic.
|
|
|
|
struct CompareModifiedEntry {
|
|
|
|
bool operator()(const CTxMemPoolModifiedEntry &a, const CTxMemPoolModifiedEntry &b)
|
|
|
|
{
|
|
|
|
double f1 = (double)a.nModFeesWithAncestors * b.nSizeWithAncestors;
|
|
|
|
double f2 = (double)b.nModFeesWithAncestors * a.nSizeWithAncestors;
|
|
|
|
if (f1 == f2) {
|
|
|
|
return CTxMemPool::CompareIteratorByHash()(a.iter, b.iter);
|
|
|
|
}
|
|
|
|
return f1 > f2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A comparator that sorts transactions based on number of ancestors.
|
|
|
|
// This is sufficient to sort an ancestor package in an order that is valid
|
|
|
|
// to appear in a block.
|
|
|
|
struct CompareTxIterByAncestorCount {
|
|
|
|
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b)
|
|
|
|
{
|
|
|
|
if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
|
|
|
|
return a->GetCountWithAncestors() < b->GetCountWithAncestors();
|
|
|
|
return CTxMemPool::CompareIteratorByHash()(a, b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef boost::multi_index_container<
|
|
|
|
CTxMemPoolModifiedEntry,
|
|
|
|
boost::multi_index::indexed_by<
|
|
|
|
boost::multi_index::ordered_unique<
|
|
|
|
modifiedentry_iter,
|
|
|
|
CompareCTxMemPoolIter
|
|
|
|
>,
|
|
|
|
// sorted by modified ancestor fee rate
|
|
|
|
boost::multi_index::ordered_non_unique<
|
|
|
|
// Reuse same tag from CTxMemPool's similar index
|
|
|
|
boost::multi_index::tag<ancestor_score>,
|
|
|
|
boost::multi_index::identity<CTxMemPoolModifiedEntry>,
|
|
|
|
CompareModifiedEntry
|
|
|
|
>
|
|
|
|
>
|
|
|
|
> indexed_modified_transaction_set;
|
|
|
|
|
|
|
|
typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
|
|
|
|
typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
|
|
|
|
|
|
|
|
struct update_for_parent_inclusion
|
|
|
|
{
|
|
|
|
update_for_parent_inclusion(CTxMemPool::txiter it) : iter(it) {}
|
|
|
|
|
|
|
|
void operator() (CTxMemPoolModifiedEntry &e)
|
|
|
|
{
|
|
|
|
e.nModFeesWithAncestors -= iter->GetFee();
|
|
|
|
e.nSizeWithAncestors -= iter->GetTxSize();
|
2016-01-03 18:54:50 +01:00
|
|
|
e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
|
2016-02-21 03:02:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPool::txiter iter;
|
|
|
|
};
|
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
/** Generate a new block, without valid proof-of-work */
|
2015-12-15 21:26:44 +01:00
|
|
|
class BlockAssembler
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// The constructed block template
|
|
|
|
std::unique_ptr<CBlockTemplate> pblocktemplate;
|
|
|
|
// A convenience pointer that always refers to the CBlock in pblocktemplate
|
|
|
|
CBlock* pblock;
|
|
|
|
|
|
|
|
// Configuration parameters for the block size
|
2015-11-06 01:42:38 +01:00
|
|
|
bool fIncludeWitness;
|
2016-07-18 19:28:26 +02:00
|
|
|
unsigned int nBlockMaxWeight, nBlockMaxSize;
|
2016-01-03 18:54:50 +01:00
|
|
|
bool fNeedSizeAccounting;
|
2015-12-15 21:26:44 +01:00
|
|
|
|
|
|
|
// Information on the current status of the block
|
2016-07-18 19:28:26 +02:00
|
|
|
uint64_t nBlockWeight;
|
2015-12-15 21:26:44 +01:00
|
|
|
uint64_t nBlockSize;
|
|
|
|
uint64_t nBlockTx;
|
2016-01-03 18:54:50 +01:00
|
|
|
uint64_t nBlockSigOpsCost;
|
2015-12-15 21:26:44 +01:00
|
|
|
CAmount nFees;
|
|
|
|
CTxMemPool::setEntries inBlock;
|
|
|
|
|
|
|
|
// Chain context for the block
|
|
|
|
int nHeight;
|
|
|
|
int64_t nLockTimeCutoff;
|
|
|
|
const CChainParams& chainparams;
|
|
|
|
|
2016-06-30 17:37:38 +02:00
|
|
|
// Variables used for addPriorityTxs
|
2015-12-15 21:26:44 +01:00
|
|
|
int lastFewTxs;
|
|
|
|
bool blockFinished;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BlockAssembler(const CChainParams& chainparams);
|
|
|
|
/** Construct a new block template with coinbase to scriptPubKeyIn */
|
2016-06-18 19:38:28 +02:00
|
|
|
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
|
2015-12-15 21:26:44 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
// utility functions
|
|
|
|
/** Clear the block's state and prepare for assembling a new block */
|
|
|
|
void resetBlock();
|
|
|
|
/** Add a tx to the block */
|
|
|
|
void AddToBlock(CTxMemPool::txiter iter);
|
|
|
|
|
|
|
|
// Methods for how to add transactions to a block.
|
|
|
|
/** Add transactions based on tx "priority" */
|
|
|
|
void addPriorityTxs();
|
2016-02-21 03:02:44 +01:00
|
|
|
/** Add transactions based on feerate including unconfirmed ancestors */
|
|
|
|
void addPackageTxs();
|
2015-12-15 21:26:44 +01:00
|
|
|
|
2016-06-30 17:37:38 +02:00
|
|
|
// helper function for addPriorityTxs
|
2015-12-15 21:26:44 +01:00
|
|
|
/** Test if tx will still "fit" in the block */
|
|
|
|
bool TestForBlock(CTxMemPool::txiter iter);
|
|
|
|
/** Test if tx still has unconfirmed parents not yet in block */
|
|
|
|
bool isStillDependent(CTxMemPool::txiter iter);
|
2016-02-21 03:02:44 +01:00
|
|
|
|
|
|
|
// helper functions for addPackageTxs()
|
|
|
|
/** Remove confirmed (inBlock) entries from given set */
|
|
|
|
void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
|
|
|
|
/** Test if a new package would "fit" in the block */
|
2016-01-03 18:54:50 +01:00
|
|
|
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
|
2016-06-30 17:22:31 +02:00
|
|
|
/** Perform checks on each transaction in a package:
|
|
|
|
* locktime, premature-witness, serialized size (if necessary)
|
|
|
|
* These checks should always succeed, and they're here
|
|
|
|
* only as an extra check in case of suboptimal node configuration */
|
|
|
|
bool TestPackageTransactions(const CTxMemPool::setEntries& package);
|
2016-02-21 03:02:44 +01:00
|
|
|
/** Return true if given transaction from mapTx has already been evaluated,
|
|
|
|
* or if the transaction's cached data in mapTx is incorrect. */
|
|
|
|
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);
|
|
|
|
/** Sort the package in an order that is valid to appear in a block */
|
|
|
|
void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
|
|
|
|
/** Add descendants of given transactions to mapModifiedTx with ancestor
|
|
|
|
* state updated assuming given transactions are inBlock. */
|
|
|
|
void UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
|
2015-12-15 21:26:44 +01:00
|
|
|
};
|
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
/** Modify the extranonce in a block */
|
2015-08-08 18:18:41 +02:00
|
|
|
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
|
2015-05-22 23:49:50 +02:00
|
|
|
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
|
|
|
#endif // BITCOIN_MINER_H
|