2013-07-31 15:43:35 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +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
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <primitives/block.h>
|
|
|
|
#include <txmempool.h>
|
2018-05-14 16:29:17 +02:00
|
|
|
#include <validation.h>
|
2014-10-12 05:26:42 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
2016-06-13 11:27:11 +02:00
|
|
|
#include <memory>
|
2017-09-05 22:25:42 +02: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;
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
|
|
|
class CConnman;
|
2013-04-13 07:13:08 +02:00
|
|
|
class CScript;
|
2016-06-13 11:27:11 +02: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;
|
|
|
|
std::vector<int64_t> vTxSigOps;
|
2018-08-11 00:28:30 +02:00
|
|
|
uint32_t nPrevBits; // nBits of previous block (for subsidy calculation)
|
2018-08-14 11:58:38 +02:00
|
|
|
std::vector<CTxOut> voutMasternodePayments; // masternode payment
|
|
|
|
std::vector<CTxOut> voutSuperblockPayments; // superblock payment
|
2014-10-12 05:26:42 +02:00
|
|
|
};
|
2014-05-10 14:54:20 +02:00
|
|
|
|
2016-06-16 18:42:45 +02:00
|
|
|
// Container for tracking updates to ancestor feerate as we include (parent)
|
|
|
|
// transactions in a block
|
|
|
|
struct CTxMemPoolModifiedEntry {
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
|
2016-06-16 18:42:45 +02:00
|
|
|
{
|
|
|
|
iter = entry;
|
|
|
|
nSizeWithAncestors = entry->GetSizeWithAncestors();
|
|
|
|
nModFeesWithAncestors = entry->GetModFeesWithAncestors();
|
|
|
|
nSigOpCountWithAncestors = entry->GetSigOpCountWithAncestors();
|
|
|
|
}
|
|
|
|
|
2018-01-15 15:36:25 +01:00
|
|
|
int64_t GetModifiedFee() const { return iter->GetModifiedFee(); }
|
|
|
|
uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
|
|
|
|
CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
|
|
|
|
size_t GetTxSize() const { return iter->GetTxSize(); }
|
|
|
|
const CTransaction& GetTx() const { return iter->GetTx(); }
|
|
|
|
|
2016-06-16 18:42:45 +02:00
|
|
|
CTxMemPool::txiter iter;
|
|
|
|
uint64_t nSizeWithAncestors;
|
|
|
|
CAmount nModFeesWithAncestors;
|
|
|
|
unsigned int nSigOpCountWithAncestors;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 {
|
2017-12-07 18:01:22 +01:00
|
|
|
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
|
2016-06-16 18:42:45 +02:00
|
|
|
{
|
|
|
|
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>,
|
2018-01-15 15:36:25 +01:00
|
|
|
CompareTxMemPoolEntryByAncestorFee
|
2016-06-16 18:42:45 +02:00
|
|
|
>
|
|
|
|
>
|
|
|
|
> 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
|
|
|
|
{
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit update_for_parent_inclusion(CTxMemPool::txiter it) : iter(it) {}
|
2016-06-16 18:42:45 +02:00
|
|
|
|
|
|
|
void operator() (CTxMemPoolModifiedEntry &e)
|
|
|
|
{
|
|
|
|
e.nModFeesWithAncestors -= iter->GetFee();
|
|
|
|
e.nSizeWithAncestors -= iter->GetTxSize();
|
|
|
|
e.nSigOpCountWithAncestors -= iter->GetSigOpCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPool::txiter iter;
|
|
|
|
};
|
|
|
|
|
2013-07-31 15:43:35 +02:00
|
|
|
/** Generate a new block, without valid proof-of-work */
|
2016-06-13 11:27:11 +02:00
|
|
|
class BlockAssembler
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// The constructed block template
|
|
|
|
std::unique_ptr<CBlockTemplate> pblocktemplate;
|
|
|
|
|
|
|
|
// Configuration parameters for the block size
|
2016-07-18 08:23:38 +02:00
|
|
|
unsigned int nBlockMaxSize;
|
2017-01-16 19:32:51 +01:00
|
|
|
CFeeRate blockMinFeeRate;
|
2016-06-13 11:27:11 +02:00
|
|
|
|
|
|
|
// Information on the current status of the block
|
|
|
|
uint64_t nBlockSize;
|
|
|
|
uint64_t nBlockTx;
|
|
|
|
unsigned int nBlockSigOps;
|
|
|
|
CAmount nFees;
|
|
|
|
CTxMemPool::setEntries inBlock;
|
|
|
|
|
|
|
|
// Chain context for the block
|
|
|
|
int nHeight;
|
|
|
|
int64_t nLockTimeCutoff;
|
|
|
|
const CChainParams& chainparams;
|
|
|
|
|
|
|
|
public:
|
2017-03-01 12:37:42 +01:00
|
|
|
struct Options {
|
|
|
|
Options();
|
|
|
|
size_t nBlockMaxSize;
|
|
|
|
CFeeRate blockMinFeeRate;
|
|
|
|
};
|
|
|
|
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit BlockAssembler(const CChainParams& params);
|
2017-03-01 12:37:42 +01:00
|
|
|
BlockAssembler(const CChainParams& params, const Options& options);
|
|
|
|
|
2016-06-13 11:27:11 +02:00
|
|
|
/** Construct a new block template with coinbase to scriptPubKeyIn */
|
2016-10-18 21:11:22 +02:00
|
|
|
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
|
2016-06-13 11:27:11 +02: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.
|
2017-03-30 20:53:31 +02:00
|
|
|
/** Add transactions based on feerate including unconfirmed ancestors
|
|
|
|
* Increments nPackagesSelected / nDescendantsUpdated with corresponding
|
|
|
|
* statistics from the package selection (for logging statistics). */
|
2018-05-14 16:29:17 +02:00
|
|
|
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
|
2016-06-13 11:27:11 +02:00
|
|
|
|
2016-06-16 18:42:45 +02: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 */
|
2017-08-16 02:09:10 +02:00
|
|
|
bool TestPackage(uint64_t packageSize, unsigned int packageSigOps) const;
|
2016-07-18 08:23:38 +02:00
|
|
|
/** Perform checks on each transaction in a package:
|
2017-09-18 13:22:21 +02:00
|
|
|
* locktime
|
2016-07-18 08:23:38 +02:00
|
|
|
* 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-06-16 18:42:45 +02:00
|
|
|
/** Return true if given transaction from mapTx has already been evaluated,
|
|
|
|
* or if the transaction's cached data in mapTx is incorrect. */
|
2018-05-14 16:29:17 +02:00
|
|
|
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
|
2016-06-16 18:42:45 +02:00
|
|
|
/** Sort the package in an order that is valid to appear in a block */
|
2018-03-16 00:12:47 +01:00
|
|
|
void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
|
2016-06-16 18:42:45 +02:00
|
|
|
/** Add descendants of given transactions to mapModifiedTx with ancestor
|
2017-03-30 20:53:31 +02:00
|
|
|
* state updated assuming given transactions are inBlock. Returns number
|
|
|
|
* of updated descendants. */
|
2018-05-14 16:29:17 +02:00
|
|
|
int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
|
2016-06-13 11:27:11 +02: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
|