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
|
2021-04-20 21:33:02 +02:00
|
|
|
// Copyright (c) 2014-2021 The Dash 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.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <miner.h>
|
|
|
|
|
|
|
|
#include <amount.h>
|
|
|
|
#include <chain.h>
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <consensus/consensus.h>
|
|
|
|
#include <consensus/merkle.h>
|
2019-02-15 14:56:43 +01:00
|
|
|
#include <consensus/tx_verify.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <consensus/validation.h>
|
|
|
|
#include <policy/feerate.h>
|
|
|
|
#include <policy/policy.h>
|
|
|
|
#include <pow.h>
|
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <timedata.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/moneystr.h>
|
2019-02-15 14:56:43 +01:00
|
|
|
#include <util/system.h>
|
2021-06-25 08:07:28 +02:00
|
|
|
#include <util/validation.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
|
|
|
|
#include <evo/specialtx.h>
|
|
|
|
#include <evo/cbtx.h>
|
|
|
|
#include <evo/simplifiedmns.h>
|
2021-10-02 19:32:24 +02:00
|
|
|
#include <llmq/blockprocessor.h>
|
|
|
|
#include <llmq/chainlocks.h>
|
|
|
|
#include <llmq/utils.h>
|
2021-10-01 21:19:08 +02:00
|
|
|
#include <masternode/payments.h>
|
2018-11-23 15:42:09 +01:00
|
|
|
|
2016-06-16 18:42:45 +02:00
|
|
|
#include <algorithm>
|
2016-10-18 21:11:22 +02:00
|
|
|
#include <utility>
|
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
|
|
|
|
2015-05-22 23:49:50 +02:00
|
|
|
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
|
2014-10-22 01:31:01 +02:00
|
|
|
{
|
2015-05-22 23:49:50 +02:00
|
|
|
int64_t nOldTime = pblock->nTime;
|
|
|
|
int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
|
|
|
|
|
|
|
|
if (nOldTime < nNewTime)
|
|
|
|
pblock->nTime = nNewTime;
|
2014-10-22 01:31:01 +02:00
|
|
|
|
|
|
|
// 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);
|
2015-05-22 23:49:50 +02:00
|
|
|
|
|
|
|
return nNewTime - nOldTime;
|
2014-10-22 01:31:01 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 12:37:42 +01:00
|
|
|
BlockAssembler::Options::Options() {
|
|
|
|
blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
|
|
|
|
nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockAssembler::BlockAssembler(const CChainParams& params, const Options& options) : chainparams(params)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
2017-03-01 12:37:42 +01:00
|
|
|
blockMinFeeRate = options.blockMinFeeRate;
|
2019-01-23 23:15:44 +01:00
|
|
|
// Limit size to between 1K and MaxBlockSize()-1K for sanity:
|
2017-03-01 12:37:42 +01:00
|
|
|
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MaxBlockSize(fDIP0001ActiveAtTip) - 1000), (unsigned int)options.nBlockMaxSize));
|
|
|
|
}
|
2017-01-16 19:32:51 +01:00
|
|
|
|
2018-07-30 16:30:43 +02:00
|
|
|
static BlockAssembler::Options DefaultOptions()
|
2017-03-01 12:37:42 +01:00
|
|
|
{
|
|
|
|
// Block resource limits
|
|
|
|
BlockAssembler::Options options;
|
|
|
|
options.nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
|
2019-06-24 18:44:27 +02:00
|
|
|
if (gArgs.IsArgSet("-blockmaxsize")) {
|
|
|
|
options.nBlockMaxSize = gArgs.GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
|
2017-03-01 12:37:42 +01:00
|
|
|
}
|
2021-03-22 18:10:27 +01:00
|
|
|
CAmount n = 0;
|
|
|
|
if (gArgs.IsArgSet("-blockmintxfee") && ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) {
|
2017-03-01 12:37:42 +01:00
|
|
|
options.blockMinFeeRate = CFeeRate(n);
|
2017-01-16 19:32:51 +01:00
|
|
|
} else {
|
2017-03-01 12:37:42 +01:00
|
|
|
options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
|
2017-01-16 19:32:51 +01:00
|
|
|
}
|
2017-03-01 12:37:42 +01:00
|
|
|
return options;
|
2016-06-13 11:27:11 +02:00
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2018-07-30 16:30:43 +02:00
|
|
|
BlockAssembler::BlockAssembler(const CChainParams& params) : BlockAssembler(params, DefaultOptions()) {}
|
2017-03-01 12:37:42 +01:00
|
|
|
|
2016-06-13 11:27:11 +02:00
|
|
|
void BlockAssembler::resetBlock()
|
|
|
|
{
|
|
|
|
inBlock.clear();
|
2015-11-03 16:35:39 +01:00
|
|
|
|
2016-06-13 11:27:11 +02:00
|
|
|
// Reserve space for coinbase tx
|
|
|
|
nBlockSize = 1000;
|
|
|
|
nBlockSigOps = 100;
|
2015-11-03 16:35:39 +01:00
|
|
|
|
2016-06-13 11:27:11 +02:00
|
|
|
// These counters do not include coinbase tx
|
|
|
|
nBlockTx = 0;
|
|
|
|
nFees = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:56:43 +01:00
|
|
|
Optional<int64_t> BlockAssembler::m_last_block_num_txs{nullopt};
|
|
|
|
Optional<int64_t> BlockAssembler::m_last_block_size{nullopt};
|
|
|
|
|
2016-10-18 21:11:22 +02:00
|
|
|
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
|
2016-06-13 11:27:11 +02:00
|
|
|
{
|
2017-03-30 20:53:31 +02:00
|
|
|
int64_t nTimeStart = GetTimeMicros();
|
|
|
|
|
2016-06-13 11:27:11 +02:00
|
|
|
resetBlock();
|
|
|
|
|
|
|
|
pblocktemplate.reset(new CBlockTemplate());
|
|
|
|
|
|
|
|
if(!pblocktemplate.get())
|
2016-10-18 21:11:22 +02:00
|
|
|
return nullptr;
|
2020-07-09 20:24:13 +02:00
|
|
|
CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
|
2016-06-13 11:27:11 +02:00
|
|
|
|
|
|
|
// Add dummy coinbase tx as first transaction
|
2016-11-21 10:51:32 +01:00
|
|
|
pblock->vtx.emplace_back();
|
2016-06-13 11:27:11 +02:00
|
|
|
pblocktemplate->vTxFees.push_back(-1); // updated at end
|
|
|
|
pblocktemplate->vTxSigOps.push_back(-1); // updated at end
|
|
|
|
|
|
|
|
LOCK2(cs_main, mempool.cs);
|
2018-04-06 11:00:10 +02:00
|
|
|
|
2021-10-16 12:54:22 +02:00
|
|
|
CBlockIndex* pindexPrev = ::ChainActive().Tip();
|
2017-09-06 23:53:55 +02:00
|
|
|
assert(pindexPrev != nullptr);
|
2016-06-13 11:27:11 +02:00
|
|
|
nHeight = pindexPrev->nHeight + 1;
|
|
|
|
|
2019-04-25 17:39:04 +02:00
|
|
|
bool fDIP0003Active_context = nHeight >= chainparams.GetConsensus().DIP0003Height;
|
2020-12-28 12:21:01 +01:00
|
|
|
bool fDIP0008Active_context = nHeight >= chainparams.GetConsensus().DIP0008Height;
|
2019-04-25 17:39:04 +02:00
|
|
|
|
2018-10-26 18:42:52 +02:00
|
|
|
pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus(), chainparams.BIP9CheckMasternodesUpgraded());
|
2016-06-13 11:27:11 +02:00
|
|
|
// -regtest only: allow overriding block.nVersion with
|
|
|
|
// -blockversion=N to test forking scenarios
|
|
|
|
if (chainparams.MineBlocksOnDemand())
|
2019-06-24 18:44:27 +02:00
|
|
|
pblock->nVersion = gArgs.GetArg("-blockversion", pblock->nVersion);
|
2016-06-13 11:27:11 +02:00
|
|
|
|
|
|
|
pblock->nTime = GetAdjustedTime();
|
|
|
|
const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();
|
|
|
|
|
|
|
|
nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
|
|
|
|
? nMedianTimePast
|
|
|
|
: pblock->GetBlockTime();
|
|
|
|
|
2018-11-23 15:42:09 +01:00
|
|
|
if (fDIP0003Active_context) {
|
2021-10-28 21:10:43 +02:00
|
|
|
for (const Consensus::LLMQParams& params : llmq::CLLMQUtils::GetEnabledQuorumParams(pindexPrev)) {
|
2018-11-23 15:42:09 +01:00
|
|
|
CTransactionRef qcTx;
|
2021-10-28 21:10:43 +02:00
|
|
|
if (llmq::quorumBlockProcessor->GetMineableCommitmentTx(params,
|
2021-04-15 20:19:03 +02:00
|
|
|
nHeight,
|
|
|
|
qcTx)) {
|
2018-11-23 15:42:09 +01:00
|
|
|
pblock->vtx.emplace_back(qcTx);
|
|
|
|
pblocktemplate->vTxFees.emplace_back(0);
|
2018-11-25 14:27:56 +01:00
|
|
|
pblocktemplate->vTxSigOps.emplace_back(0);
|
2019-01-29 15:54:57 +01:00
|
|
|
nBlockSize += qcTx->GetTotalSize();
|
|
|
|
++nBlockTx;
|
2018-11-23 15:42:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:53:31 +02:00
|
|
|
int nPackagesSelected = 0;
|
|
|
|
int nDescendantsUpdated = 0;
|
|
|
|
addPackageTxs(nPackagesSelected, nDescendantsUpdated);
|
|
|
|
|
|
|
|
int64_t nTime1 = GetTimeMicros();
|
2016-06-13 11:27:11 +02:00
|
|
|
|
2019-02-15 14:56:43 +01:00
|
|
|
m_last_block_num_txs = nBlockTx;
|
|
|
|
m_last_block_size = nBlockSize;
|
2016-06-13 11:27:11 +02:00
|
|
|
LogPrintf("CreateNewBlock(): total size %u txs: %u fees: %ld sigops %d\n", nBlockSize, nBlockTx, nFees, nBlockSigOps);
|
|
|
|
|
|
|
|
// Create coinbase transaction.
|
|
|
|
CMutableTransaction coinbaseTx;
|
|
|
|
coinbaseTx.vin.resize(1);
|
|
|
|
coinbaseTx.vin[0].prevout.SetNull();
|
|
|
|
coinbaseTx.vout.resize(1);
|
|
|
|
coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
|
|
|
|
|
|
|
|
// NOTE: unlike in bitcoin, we need to pass PREVIOUS block height here
|
|
|
|
CAmount blockReward = nFees + GetBlockSubsidy(pindexPrev->nBits, pindexPrev->nHeight, Params().GetConsensus());
|
|
|
|
|
|
|
|
// Compute regular coinbase transaction.
|
|
|
|
coinbaseTx.vout[0].nValue = blockReward;
|
2018-04-06 11:00:10 +02:00
|
|
|
|
|
|
|
if (!fDIP0003Active_context) {
|
|
|
|
coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
|
|
|
|
} else {
|
|
|
|
coinbaseTx.vin[0].scriptSig = CScript() << OP_RETURN;
|
|
|
|
|
|
|
|
coinbaseTx.nVersion = 3;
|
|
|
|
coinbaseTx.nType = TRANSACTION_COINBASE;
|
|
|
|
|
|
|
|
CCbTx cbTx;
|
2019-04-04 10:25:50 +02:00
|
|
|
|
|
|
|
if (fDIP0008Active_context) {
|
|
|
|
cbTx.nVersion = 2;
|
|
|
|
} else {
|
|
|
|
cbTx.nVersion = 1;
|
|
|
|
}
|
|
|
|
|
2018-04-06 11:00:10 +02:00
|
|
|
cbTx.nHeight = nHeight;
|
2018-04-09 10:35:43 +02:00
|
|
|
|
|
|
|
CValidationState state;
|
2019-07-24 17:45:04 +02:00
|
|
|
if (!CalcCbTxMerkleRootMNList(*pblock, pindexPrev, cbTx.merkleRootMNList, state, ::ChainstateActive().CoinsTip())) {
|
2019-04-04 17:38:23 +02:00
|
|
|
throw std::runtime_error(strprintf("%s: CalcCbTxMerkleRootMNList failed: %s", __func__, FormatStateMessage(state)));
|
2018-04-09 10:35:43 +02:00
|
|
|
}
|
2019-04-04 10:25:50 +02:00
|
|
|
if (fDIP0008Active_context) {
|
|
|
|
if (!CalcCbTxMerkleRootQuorums(*pblock, pindexPrev, cbTx.merkleRootQuorums, state)) {
|
2019-04-04 17:38:23 +02:00
|
|
|
throw std::runtime_error(strprintf("%s: CalcCbTxMerkleRootQuorums failed: %s", __func__, FormatStateMessage(state)));
|
2019-04-04 10:25:50 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-09 10:35:43 +02:00
|
|
|
|
2018-04-06 11:00:10 +02:00
|
|
|
SetTxPayload(coinbaseTx, cbTx);
|
|
|
|
}
|
2016-06-13 11:27:11 +02:00
|
|
|
|
|
|
|
// Update coinbase transaction with additional info about masternode and governance payments,
|
|
|
|
// get some info back to pass to getblocktemplate
|
2018-08-14 11:58:38 +02:00
|
|
|
FillBlockPayments(coinbaseTx, nHeight, blockReward, pblocktemplate->voutMasternodePayments, pblocktemplate->voutSuperblockPayments);
|
2016-06-13 11:27:11 +02:00
|
|
|
|
2016-11-21 10:51:32 +01:00
|
|
|
pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
|
2016-06-13 11:27:11 +02:00
|
|
|
pblocktemplate->vTxFees[0] = -nFees;
|
|
|
|
|
|
|
|
// Fill in header
|
|
|
|
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
|
|
|
|
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
|
|
|
|
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
|
|
|
|
pblock->nNonce = 0;
|
2018-08-11 00:28:30 +02:00
|
|
|
pblocktemplate->nPrevBits = pindexPrev->nBits;
|
2016-11-21 10:51:32 +01:00
|
|
|
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(*pblock->vtx[0]);
|
2016-06-13 11:27:11 +02:00
|
|
|
|
|
|
|
CValidationState state;
|
|
|
|
if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
|
|
|
|
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
|
|
|
|
}
|
2017-03-30 20:53:31 +02:00
|
|
|
int64_t nTime2 = GetTimeMicros();
|
|
|
|
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::BENCHMARK, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n", 0.001 * (nTime1 - nTimeStart), nPackagesSelected, nDescendantsUpdated, 0.001 * (nTime2 - nTime1), 0.001 * (nTime2 - nTimeStart));
|
2014-05-10 14:47:16 +02:00
|
|
|
|
2016-10-18 21:11:22 +02:00
|
|
|
return std::move(pblocktemplate);
|
2016-06-13 11:27:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-16 18:42:45 +02:00
|
|
|
void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
|
|
|
|
{
|
|
|
|
for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
|
|
|
|
// Only test txs not already in the block
|
|
|
|
if (inBlock.count(*iit)) {
|
|
|
|
testSet.erase(iit++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 02:09:10 +02:00
|
|
|
bool BlockAssembler::TestPackage(uint64_t packageSize, unsigned int packageSigOps) const
|
2016-06-16 18:42:45 +02:00
|
|
|
{
|
|
|
|
if (nBlockSize + packageSize >= nBlockMaxSize)
|
|
|
|
return false;
|
|
|
|
if (nBlockSigOps + packageSigOps >= MaxBlockSigOps(fDIP0001ActiveAtTip))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2016-07-18 08:23:38 +02:00
|
|
|
// Perform transaction-level checks before adding to block:
|
|
|
|
// - transaction finality (locktime)
|
2019-03-06 07:58:27 +01:00
|
|
|
// - safe TXs in regard to ChainLocks
|
2016-07-18 08:23:38 +02:00
|
|
|
bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package)
|
2016-06-16 18:42:45 +02:00
|
|
|
{
|
2018-06-24 16:34:56 +02:00
|
|
|
for (CTxMemPool::txiter it : package) {
|
2016-06-16 18:42:45 +02:00
|
|
|
if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
|
|
|
|
return false;
|
2019-03-06 07:58:27 +01:00
|
|
|
if (!llmq::chainLocksHandler->IsTxSafeForMining(it->GetTx().GetHash())) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 18:42:45 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2016-06-13 11:27:11 +02:00
|
|
|
void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
|
|
|
|
{
|
2020-07-09 20:24:13 +02:00
|
|
|
pblocktemplate->block.vtx.emplace_back(iter->GetSharedTx());
|
2016-06-13 11:27:11 +02:00
|
|
|
pblocktemplate->vTxFees.push_back(iter->GetFee());
|
|
|
|
pblocktemplate->vTxSigOps.push_back(iter->GetSigOpCount());
|
|
|
|
nBlockSize += iter->GetTxSize();
|
|
|
|
++nBlockTx;
|
|
|
|
nBlockSigOps += iter->GetSigOpCount();
|
|
|
|
nFees += iter->GetFee();
|
|
|
|
inBlock.insert(iter);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2019-06-24 18:44:27 +02:00
|
|
|
bool fPrintPriority = gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY);
|
2016-06-13 11:27:11 +02:00
|
|
|
if (fPrintPriority) {
|
2019-03-14 15:44:42 +01:00
|
|
|
LogPrintf("fee %s txid %s\n",
|
2016-06-13 11:27:11 +02:00
|
|
|
CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
|
|
|
|
iter->GetTx().GetHash().ToString());
|
|
|
|
}
|
|
|
|
}
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2017-03-30 20:53:31 +02:00
|
|
|
int BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded,
|
2016-06-16 18:42:45 +02:00
|
|
|
indexed_modified_transaction_set &mapModifiedTx)
|
|
|
|
{
|
2017-03-30 20:53:31 +02:00
|
|
|
int nDescendantsUpdated = 0;
|
2018-06-24 16:34:56 +02:00
|
|
|
for (CTxMemPool::txiter it : alreadyAdded) {
|
2016-06-16 18:42:45 +02:00
|
|
|
CTxMemPool::setEntries descendants;
|
|
|
|
mempool.CalculateDescendants(it, descendants);
|
|
|
|
// Insert all descendants (not yet in block) into the modified set
|
2019-07-05 09:06:28 +02:00
|
|
|
for (CTxMemPool::txiter desc : descendants) {
|
2016-06-16 18:42:45 +02:00
|
|
|
if (alreadyAdded.count(desc))
|
|
|
|
continue;
|
2017-03-30 20:53:31 +02:00
|
|
|
++nDescendantsUpdated;
|
2016-06-16 18:42:45 +02:00
|
|
|
modtxiter mit = mapModifiedTx.find(desc);
|
|
|
|
if (mit == mapModifiedTx.end()) {
|
|
|
|
CTxMemPoolModifiedEntry modEntry(desc);
|
|
|
|
modEntry.nSizeWithAncestors -= it->GetTxSize();
|
|
|
|
modEntry.nModFeesWithAncestors -= it->GetModifiedFee();
|
|
|
|
modEntry.nSigOpCountWithAncestors -= it->GetSigOpCount();
|
|
|
|
mapModifiedTx.insert(modEntry);
|
|
|
|
} else {
|
|
|
|
mapModifiedTx.modify(mit, update_for_parent_inclusion(it));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 20:53:31 +02:00
|
|
|
return nDescendantsUpdated;
|
2016-06-16 18:42:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip entries in mapTx that are already in a block or are present
|
|
|
|
// in mapModifiedTx (which implies that the mapTx ancestor state is
|
|
|
|
// stale due to ancestor inclusion in the block)
|
|
|
|
// Also skip transactions that we've already failed to add. This can happen if
|
|
|
|
// we consider a transaction in mapModifiedTx and it fails: we can then
|
|
|
|
// potentially consider it again while walking mapTx. It's currently
|
|
|
|
// guaranteed to fail again, but as a belt-and-suspenders check we put it in
|
|
|
|
// failedTx and avoid re-evaluation, since the re-evaluation would be using
|
|
|
|
// cached size/sigops/fee values that are not actually correct.
|
|
|
|
bool BlockAssembler::SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx)
|
|
|
|
{
|
|
|
|
assert (it != mempool.mapTx.end());
|
2017-05-02 14:50:25 +02:00
|
|
|
return mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it);
|
2016-06-16 18:42:45 +02:00
|
|
|
}
|
|
|
|
|
2018-03-16 00:12:47 +01:00
|
|
|
void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries)
|
2016-06-16 18:42:45 +02:00
|
|
|
{
|
|
|
|
// Sort package by ancestor count
|
|
|
|
// If a transaction A depends on transaction B, then A's ancestor count
|
|
|
|
// must be greater than B's. So this is sufficient to validly order the
|
|
|
|
// transactions for block inclusion.
|
|
|
|
sortedEntries.clear();
|
|
|
|
sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
|
|
|
|
std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This transaction selection algorithm orders the mempool based
|
|
|
|
// on feerate of a transaction including all unconfirmed ancestors.
|
|
|
|
// Since we don't remove transactions from the mempool as we select them
|
|
|
|
// for block inclusion, we need an alternate method of updating the feerate
|
|
|
|
// of a transaction with its not-yet-selected ancestors as we go.
|
|
|
|
// This is accomplished by walking the in-mempool descendants of selected
|
|
|
|
// transactions and storing a temporary modified state in mapModifiedTxs.
|
|
|
|
// Each time through the loop, we compare the best transaction in
|
|
|
|
// mapModifiedTxs with the next transaction in the mempool to decide what
|
|
|
|
// transaction package to work on next.
|
2017-03-30 20:53:31 +02:00
|
|
|
void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
|
2016-06-16 18:42:45 +02:00
|
|
|
{
|
|
|
|
// mapModifiedTx will store sorted packages after they are modified
|
|
|
|
// because some of their txs are already in the block
|
|
|
|
indexed_modified_transaction_set mapModifiedTx;
|
|
|
|
// Keep track of entries that failed inclusion, to avoid duplicate work
|
|
|
|
CTxMemPool::setEntries failedTx;
|
|
|
|
|
|
|
|
// Start by adding all descendants of previously added txs to mapModifiedTx
|
|
|
|
// and modifying them for their already included ancestors
|
|
|
|
UpdatePackagesForAdded(inBlock, mapModifiedTx);
|
|
|
|
|
|
|
|
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
|
|
|
|
CTxMemPool::txiter iter;
|
2017-03-30 20:53:31 +02:00
|
|
|
|
|
|
|
// Limit the number of attempts to add transactions to the block when it is
|
|
|
|
// close to full; this is just a simple heuristic to finish quickly if the
|
|
|
|
// mempool has a lot of entries.
|
|
|
|
const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
|
|
|
|
int64_t nConsecutiveFailed = 0;
|
|
|
|
|
2016-06-16 18:42:45 +02:00
|
|
|
while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
|
|
|
|
{
|
|
|
|
// First try to find a new transaction in mapTx to evaluate.
|
|
|
|
if (mi != mempool.mapTx.get<ancestor_score>().end() &&
|
|
|
|
SkipMapTxEntry(mempool.mapTx.project<0>(mi), mapModifiedTx, failedTx)) {
|
|
|
|
++mi;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that mi is not stale, determine which transaction to evaluate:
|
|
|
|
// the next entry from mapTx, or the best from mapModifiedTx?
|
|
|
|
bool fUsingModified = false;
|
|
|
|
|
|
|
|
modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
|
|
|
|
if (mi == mempool.mapTx.get<ancestor_score>().end()) {
|
|
|
|
// We're out of entries in mapTx; use the entry from mapModifiedTx
|
|
|
|
iter = modit->iter;
|
|
|
|
fUsingModified = true;
|
|
|
|
} else {
|
|
|
|
// Try to compare the mapTx entry to the mapModifiedTx entry
|
|
|
|
iter = mempool.mapTx.project<0>(mi);
|
|
|
|
if (modit != mapModifiedTx.get<ancestor_score>().end() &&
|
2018-01-15 15:36:25 +01:00
|
|
|
CompareTxMemPoolEntryByAncestorFee()(*modit, CTxMemPoolModifiedEntry(iter))) {
|
2016-06-16 18:42:45 +02:00
|
|
|
// The best entry in mapModifiedTx has higher score
|
|
|
|
// than the one from mapTx.
|
|
|
|
// Switch which transaction (package) to consider
|
|
|
|
iter = modit->iter;
|
|
|
|
fUsingModified = true;
|
|
|
|
} else {
|
|
|
|
// Either no entry in mapModifiedTx, or it's worse than mapTx.
|
|
|
|
// Increment mi for the next loop iteration.
|
|
|
|
++mi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
|
|
|
|
// contain anything that is inBlock.
|
|
|
|
assert(!inBlock.count(iter));
|
|
|
|
|
|
|
|
uint64_t packageSize = iter->GetSizeWithAncestors();
|
|
|
|
CAmount packageFees = iter->GetModFeesWithAncestors();
|
|
|
|
unsigned int packageSigOps = iter->GetSigOpCountWithAncestors();
|
|
|
|
if (fUsingModified) {
|
|
|
|
packageSize = modit->nSizeWithAncestors;
|
|
|
|
packageFees = modit->nModFeesWithAncestors;
|
|
|
|
packageSigOps = modit->nSigOpCountWithAncestors;
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:32:51 +01:00
|
|
|
if (packageFees < blockMinFeeRate.GetFee(packageSize)) {
|
2016-06-16 18:42:45 +02:00
|
|
|
// Everything else we might consider has a lower fee rate
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TestPackage(packageSize, packageSigOps)) {
|
|
|
|
if (fUsingModified) {
|
|
|
|
// Since we always look at the best entry in mapModifiedTx,
|
|
|
|
// we must erase failed entries so that we can consider the
|
|
|
|
// next best entry on the next loop iteration
|
|
|
|
mapModifiedTx.get<ancestor_score>().erase(modit);
|
|
|
|
failedTx.insert(iter);
|
|
|
|
}
|
2017-03-30 20:53:31 +02:00
|
|
|
|
|
|
|
++nConsecutiveFailed;
|
|
|
|
|
2018-01-26 17:31:20 +01:00
|
|
|
if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockSize > nBlockMaxSize - 1000) {
|
2017-03-30 20:53:31 +02:00
|
|
|
// Give up if we're close to full and haven't succeeded in a while
|
|
|
|
break;
|
|
|
|
}
|
2016-06-16 18:42:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPool::setEntries ancestors;
|
|
|
|
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
|
|
|
|
std::string dummy;
|
|
|
|
mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
|
|
|
|
|
|
|
|
onlyUnconfirmed(ancestors);
|
|
|
|
ancestors.insert(iter);
|
|
|
|
|
2019-03-06 07:58:27 +01:00
|
|
|
// Test if all tx's are Final and safe
|
2016-07-18 08:23:38 +02:00
|
|
|
if (!TestPackageTransactions(ancestors)) {
|
2016-06-16 18:42:45 +02:00
|
|
|
if (fUsingModified) {
|
|
|
|
mapModifiedTx.get<ancestor_score>().erase(modit);
|
|
|
|
failedTx.insert(iter);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:53:31 +02:00
|
|
|
// This transaction will make it in; reset the failed counter.
|
|
|
|
nConsecutiveFailed = 0;
|
|
|
|
|
2016-06-16 18:42:45 +02:00
|
|
|
// Package can be added. Sort the entries in a valid order.
|
2017-01-30 13:13:07 +01:00
|
|
|
std::vector<CTxMemPool::txiter> sortedEntries;
|
2018-03-16 00:12:47 +01:00
|
|
|
SortForBlock(ancestors, sortedEntries);
|
2016-06-16 18:42:45 +02:00
|
|
|
|
|
|
|
for (size_t i=0; i<sortedEntries.size(); ++i) {
|
|
|
|
AddToBlock(sortedEntries[i]);
|
|
|
|
// Erase from the modified set, if present
|
|
|
|
mapModifiedTx.erase(sortedEntries[i]);
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:53:31 +02:00
|
|
|
++nPackagesSelected;
|
|
|
|
|
2016-06-16 18:42:45 +02:00
|
|
|
// Update transactions that depend on each of these
|
2017-03-30 20:53:31 +02:00
|
|
|
nDescendantsUpdated += UpdatePackagesForAdded(ancestors, mapModifiedTx);
|
2016-06-16 18:42:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-08 18:18:41 +02:00
|
|
|
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
|
2013-07-31 15:43:35 +02:00
|
|
|
{
|
|
|
|
// 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
|
2016-11-21 10:51:32 +01:00
|
|
|
CMutableTransaction txCoinbase(*pblock->vtx[0]);
|
2021-08-31 18:51:34 +02:00
|
|
|
txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce));
|
2014-06-07 13:53:27 +02:00
|
|
|
assert(txCoinbase.vin[0].scriptSig.size() <= 100);
|
2013-07-31 15:43:35 +02:00
|
|
|
|
2016-11-21 10:51:32 +01:00
|
|
|
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
|
2015-11-17 17:35:44 +01:00
|
|
|
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
|
2013-07-31 15:43:35 +02:00
|
|
|
}
|