Skip to content
Permalink
Browse files

Merge mining_priority

  • Loading branch information...
luke-jr committed May 2, 2019
2 parents a8ddb52 + 1bb17a0 commit 7441128513677940f95ffea04c4bc5b56f35f704
@@ -167,6 +167,7 @@ BITCOIN_CORE_H = \
noui.h \
optional.h \
outputtype.h \
policy/coin_age_priority.h \
policy/feerate.h \
policy/fees.h \
policy/policy.h \
@@ -275,6 +276,7 @@ libbitcoin_server_a_SOURCES = \
node/transaction.cpp \
noui.cpp \
outputtype.cpp \
policy/coin_age_priority.cpp \
policy/fees.cpp \
policy/policy.cpp \
policy/rbf.cpp \
@@ -12,13 +12,14 @@
static void AddTx(const CTransactionRef& tx, const CAmount& nFee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
{
int64_t nTime = 0;
double dPriority = 10.0;
unsigned int nHeight = 1;
bool spendsCoinbase = false;
unsigned int sigOpCost = 4;
LockPoints lp;
pool.addUnchecked(CTxMemPoolEntry(
tx, nFee, nTime, nHeight,
spendsCoinbase, sigOpCost, lp));
tx, nFee, nTime, dPriority, nHeight,
tx->GetValueOut(), spendsCoinbase, sigOpCost, lp));
}

// Right now this is only testing eviction performance in an extremely small
@@ -517,7 +517,7 @@ void SetupServerArgs()
gArgs.AddArg("-maxtipage=<n>", strprintf("Maximum tip age in seconds to consider node in initial block download (default: %u)", DEFAULT_MAX_TIP_AGE), true, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-maxtxfee=<amt>", strprintf("Maximum total fees (in %s) to use in a single wallet transaction or raw transaction; setting this too low may abort large transactions (default: %s)",
CURRENCY_UNIT, FormatMoney(DEFAULT_TRANSACTION_MAXFEE)), false, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-printpriority", strprintf("Log transaction fee per kB when mining blocks (default: %u)", DEFAULT_PRINTPRIORITY), true, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-printpriority", strprintf("Log transaction priority and fee per kB when mining blocks (default: %u)", DEFAULT_PRINTPRIORITY), true, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -daemon. To disable logging to file, set -nodebuglogfile)", false, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)", false, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-uacomment=<cmt>", "Append comment to the user agent string", false, OptionsCategory::DEBUG_TEST);
@@ -540,6 +540,7 @@ void SetupServerArgs()
gArgs.AddArg("-blockmaxsize=<n>", strprintf("Set maximum block size in bytes (default: %d)", DEFAULT_BLOCK_MAX_SIZE), false, OptionsCategory::BLOCK_CREATION);
gArgs.AddArg("-blockmaxweight=<n>", strprintf("Set maximum BIP141 block weight (default: %d)", DEFAULT_BLOCK_MAX_WEIGHT), false, OptionsCategory::BLOCK_CREATION);
gArgs.AddArg("-blockmintxfee=<amt>", strprintf("Set lowest fee rate (in %s/kB) for transactions to be included in block creation. (default: %s)", CURRENCY_UNIT, FormatMoney(DEFAULT_BLOCK_MIN_TX_FEE)), false, OptionsCategory::BLOCK_CREATION);
gArgs.AddArg("-blockprioritysize=<n>", strprintf("Set maximum size of high-priority/low-fee transactions in bytes (default: %d)", DEFAULT_BLOCK_PRIORITY_SIZE), false, OptionsCategory::BLOCK_CREATION);
gArgs.AddArg("-blockversion=<n>", "Override block version to test forking scenarios", true, OptionsCategory::BLOCK_CREATION);

gArgs.AddArg("-rest", strprintf("Accept public REST requests (default: %u)", DEFAULT_REST_ENABLE), false, OptionsCategory::RPC);
@@ -106,6 +106,9 @@ void BlockAssembler::resetBlock()
// These counters do not include coinbase tx
nBlockTx = 0;
nFees = 0;

lastFewTxs = 0;
blockFinished = false;
}

Optional<int64_t> BlockAssembler::m_last_block_num_txs{nullopt};
@@ -128,6 +131,10 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
pblock->vtx.emplace_back();
pblocktemplate->vTxFees.push_back(-1); // updated at end
pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
bool fPrintPriority = gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY);
if (fPrintPriority) {
pblocktemplate->vTxPriorities.push_back(-1); // n/a
}

LOCK2(cs_main, mempool.cs);
CBlockIndex* pindexPrev = chainActive.Tip();
@@ -160,6 +167,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc

int nPackagesSelected = 0;
int nDescendantsUpdated = 0;
addPriorityTxs(nPackagesSelected);
addPackageTxs(nPackagesSelected, nDescendantsUpdated);

int64_t nTime1 = GetTimeMicros();
@@ -268,9 +276,14 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)

bool fPrintPriority = gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY);
if (fPrintPriority) {
LogPrintf("fee %s txid %s\n",
double dPriority = iter->GetPriority(nHeight);
CAmount dummy;
mempool.ApplyDeltas(iter->GetTx().GetHash(), dPriority, dummy);
LogPrintf("priority %.1f fee %s txid %s\n",
dPriority,
CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
iter->GetTx().GetHash().ToString());
pblocktemplate->vTxPriorities.push_back(dPriority);
}
}

@@ -347,7 +360,7 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda

// Start by adding all descendants of previously added txs to mapModifiedTx
// and modifying them for their already included ancestors
UpdatePackagesForAdded(inBlock, mapModifiedTx);
nDescendantsUpdated += UpdatePackagesForAdded(inBlock, mapModifiedTx);

CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
CTxMemPool::txiter iter;
@@ -30,6 +30,7 @@ struct CBlockTemplate
CBlock block;
std::vector<CAmount> vTxFees;
std::vector<int64_t> vTxSigOpsCost;
std::vector<double> vTxPriorities;
std::vector<unsigned char> vchCoinbaseCommitment;
};

@@ -151,6 +152,10 @@ class BlockAssembler
int64_t nLockTimeCutoff;
const CChainParams& chainparams;

// Variables used for addPriorityTxs
int lastFewTxs;
bool blockFinished;

public:
struct Options {
Options();
@@ -177,11 +182,19 @@ class BlockAssembler
void AddToBlock(CTxMemPool::txiter iter);

// Methods for how to add transactions to a block.
/** Add transactions based on tx "priority" */
void addPriorityTxs(int &nPackagesSelected) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
/** Add transactions based on feerate including unconfirmed ancestors
* Increments nPackagesSelected / nDescendantsUpdated with corresponding
* statistics from the package selection (for logging statistics). */
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);

// helper function for addPriorityTxs
/** 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) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);

// helper functions for addPackageTxs()
/** Remove confirmed (inBlock) entries from given set */
void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
@@ -0,0 +1,255 @@
// Copyright (c) 2012-2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <policy/coin_age_priority.h>

#include <coins.h>
#include <consensus/validation.h>
#include <miner.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
#include <txmempool.h>
#include <util/system.h>
#include <validation.h>

unsigned int CalculateModifiedSize(const CTransaction& tx, unsigned int nTxSize)
{
// In order to avoid disincentivizing cleaning up the UTXO set we don't count
// the constant overhead for each txin and up to 110 bytes of scriptSig (which
// is enough to cover a compressed pubkey p2sh redemption) for priority.
// Providing any more cleanup incentive than making additional inputs free would
// risk encouraging people to create junk outputs to redeem later.
if (nTxSize == 0)
nTxSize = (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
for (std::vector<CTxIn>::const_iterator it(tx.vin.begin()); it != tx.vin.end(); ++it)
{
unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size());
if (nTxSize > offset)
nTxSize -= offset;
}
return nTxSize;
}

double ComputePriority(const CTransaction& tx, double dPriorityInputs, unsigned int nTxSize)
{
nTxSize = CalculateModifiedSize(tx, nTxSize);
if (nTxSize == 0) return 0.0;

return dPriorityInputs / nTxSize;
}

double GetPriority(const CTransaction &tx, const CCoinsViewCache& view, int nHeight, CAmount &inChainInputValue)
{
inChainInputValue = 0;
if (tx.IsCoinBase())
return 0.0;
double dResult = 0.0;
for (const CTxIn& txin : tx.vin)
{
const Coin& coin = view.AccessCoin(txin.prevout);
if (coin.IsSpent()) {
continue;
}
if (coin.nHeight <= nHeight) {
dResult += (double)(coin.out.nValue) * (nHeight - coin.nHeight);
inChainInputValue += coin.out.nValue;
}
}
return ComputePriority(tx, dResult);
}

void CTxMemPoolEntry::UpdateCachedPriority(unsigned int currentHeight, CAmount valueInCurrentBlock)
{
int heightDiff = int(currentHeight) - int(cachedHeight);
double deltaPriority = ((double)heightDiff*inChainInputValue)/nModSize;
cachedPriority += deltaPriority;
cachedHeight = currentHeight;
inChainInputValue += valueInCurrentBlock;
assert(MoneyRange(inChainInputValue));
}

struct update_priority
{
update_priority(unsigned int _height, CAmount _value) :
height(_height), value(_value)
{}

void operator() (CTxMemPoolEntry &e)
{ e.UpdateCachedPriority(height, value); }

private:
unsigned int height;
CAmount value;
};

void CTxMemPool::UpdateDependentPriorities(const CTransaction &tx, unsigned int nBlockHeight, bool addToChain)
{
LOCK(cs);
for (unsigned int i = 0; i < tx.vout.size(); i++) {
auto it = mapNextTx.find(COutPoint(tx.GetHash(), i));
if (it == mapNextTx.end())
continue;
uint256 hash = it->second->GetHash();
txiter iter = mapTx.find(hash);
mapTx.modify(iter, update_priority(nBlockHeight, addToChain ? tx.vout[i].nValue : -tx.vout[i].nValue));
}
}

double
CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
{
// This will only return accurate results when currentHeight >= the heights
// at which all the in-chain inputs of the tx were included in blocks.
// Typical usage of GetPriority with chainActive.Height() will ensure this.
int heightDiff = currentHeight - cachedHeight;
double deltaPriority = ((double)heightDiff*inChainInputValue)/nModSize;
double dResult = cachedPriority + deltaPriority;
if (dResult < 0) // This should only happen if it was called with an invalid height
dResult = 0;
return dResult;
}

// We want to sort transactions by coin age priority
typedef std::pair<double, CTxMemPool::txiter> TxCoinAgePriority;

struct TxCoinAgePriorityCompare
{
bool operator()(const TxCoinAgePriority& a, const TxCoinAgePriority& b)
{
if (a.first == b.first)
return CompareTxMemPoolEntryByScore()(*(b.second), *(a.second)); //Reverse order to make sort less than
return a.first < b.first;
}
};

bool BlockAssembler::isStillDependent(CTxMemPool::txiter iter)
{
for (CTxMemPool::txiter parent : mempool.GetMemPoolParents(iter))
{
if (!inBlock.count(parent)) {
return true;
}
}
return false;
}

bool BlockAssembler::TestForBlock(CTxMemPool::txiter iter)
{
uint64_t packageSize = iter->GetSizeWithAncestors();
int64_t packageSigOps = iter->GetSigOpCostWithAncestors();
if (!TestPackage(packageSize, packageSigOps)) {
// If the block is so close to full that no more txs will fit
// or if we've tried more than 50 times to fill remaining space
// then flag that the block is finished
if (nBlockWeight > nBlockMaxWeight - 400 || nBlockSigOpsCost > MAX_BLOCK_SIGOPS_COST - 8 || lastFewTxs > 50) {
blockFinished = true;
return false;
}
// Once we're within 4000 weight of a full block, only look at 50 more txs
// to try to fill the remaining space.
if (nBlockWeight > nBlockMaxWeight - 4000) {
++lastFewTxs;
}
return false;
}

CTxMemPool::setEntries package;
package.insert(iter);
if (!TestPackageTransactions(package)) {
if (nBlockSize > nBlockMaxSize - 100 || lastFewTxs > 50) {
blockFinished = true;
return false;
}
if (nBlockSize > nBlockMaxSize - 1000) {
++lastFewTxs;
}
return false;
}

return true;
}

void BlockAssembler::addPriorityTxs(int &nPackagesSelected)
{
// How much of the block should be dedicated to high-priority transactions,
// included regardless of the fees they pay
uint64_t nBlockPrioritySize = gArgs.GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);

if (nBlockPrioritySize == 0) {
return;
}

bool fSizeAccounting = fNeedSizeAccounting;
fNeedSizeAccounting = true;

// This vector will be sorted into a priority queue:
std::vector<TxCoinAgePriority> vecPriority;
TxCoinAgePriorityCompare pricomparer;
std::map<CTxMemPool::txiter, double, CTxMemPool::CompareIteratorByHash> waitPriMap;
typedef std::map<CTxMemPool::txiter, double, CTxMemPool::CompareIteratorByHash>::iterator waitPriIter;
double actualPriority = -1;

vecPriority.reserve(mempool.mapTx.size());
for (CTxMemPool::indexed_transaction_set::iterator mi = mempool.mapTx.begin();
mi != mempool.mapTx.end(); ++mi)
{
double dPriority = mi->GetPriority(nHeight);
CAmount dummy;
mempool.ApplyDeltas(mi->GetTx().GetHash(), dPriority, dummy);
vecPriority.push_back(TxCoinAgePriority(dPriority, mi));
}
std::make_heap(vecPriority.begin(), vecPriority.end(), pricomparer);

CTxMemPool::txiter iter;
while (!vecPriority.empty() && !blockFinished) { // add a tx from priority queue to fill the blockprioritysize
iter = vecPriority.front().second;
actualPriority = vecPriority.front().first;
std::pop_heap(vecPriority.begin(), vecPriority.end(), pricomparer);
vecPriority.pop_back();

// If tx already in block, skip
if (inBlock.count(iter)) {
assert(false); // shouldn't happen for priority txs
continue;
}

// cannot accept witness transactions into a non-witness block
if (!fIncludeWitness && iter->GetTx().HasWitness())
continue;

// If tx is dependent on other mempool txs which haven't yet been included
// then put it in the waitSet
if (isStillDependent(iter)) {
waitPriMap.insert(std::make_pair(iter, actualPriority));
continue;
}

// If this tx fits in the block add it, otherwise keep looping
if (TestForBlock(iter)) {
AddToBlock(iter);

++nPackagesSelected;

// If now that this txs is added we've surpassed our desired priority size
// or have dropped below the minimum priority threshold, then we're done adding priority txs
if (nBlockSize >= nBlockPrioritySize || actualPriority <= MINIMUM_TX_PRIORITY) {
break;
}

// This tx was successfully added, so
// add transactions that depend on this one to the priority queue to try again
for (CTxMemPool::txiter child : mempool.GetMemPoolChildren(iter))
{
waitPriIter wpiter = waitPriMap.find(child);
if (wpiter != waitPriMap.end()) {
vecPriority.push_back(TxCoinAgePriority(wpiter->second,child));
std::push_heap(vecPriority.begin(), vecPriority.end(), pricomparer);
waitPriMap.erase(wpiter);
}
}
}
}
fNeedSizeAccounting = fSizeAccounting;
}
Oops, something went wrong.

0 comments on commit 7441128

Please sign in to comment.
You can’t perform that action at this time.