Merge mining_priority

This commit is contained in:
Luke Dashjr 2025-03-05 03:27:08 +00:00
commit 07464b1321
35 changed files with 809 additions and 70 deletions

View File

@ -253,6 +253,7 @@ BITCOIN_CORE_H = \
node/warnings.h \
noui.h \
outputtype.h \
policy/coin_age_priority.h \
policy/feerate.h \
policy/fees.h \
policy/fees_args.h \
@ -457,6 +458,7 @@ libbitcoin_node_a_SOURCES = \
node/utxo_snapshot.cpp \
node/warnings.cpp \
noui.cpp \
policy/coin_age_priority.cpp \
policy/fees.cpp \
policy/fees_args.cpp \
policy/packages.cpp \

View File

@ -12,13 +12,15 @@
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;
uint64_t sequence = 0;
bool spendsCoinbase = false;
unsigned int sigOpCost = 4;
LockPoints lp;
pool.addUnchecked(CTxMemPoolEntry(
tx, nFee, nTime, nHeight, sequence,
tx, nFee, nTime, dPriority, nHeight, sequence,
tx->GetValueOut(),
spendsCoinbase, sigOpCost, lp));
}

View File

@ -16,12 +16,13 @@
static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
{
int64_t nTime = 0;
constexpr double coin_age{10.0};
unsigned int nHeight = 1;
uint64_t sequence = 0;
bool spendsCoinbase = false;
unsigned int sigOpCost = 4;
LockPoints lp;
pool.addUnchecked(CTxMemPoolEntry(tx, 1000, nTime, nHeight, sequence, spendsCoinbase, sigOpCost, lp));
pool.addUnchecked(CTxMemPoolEntry(tx, 1000, nTime, nHeight, sequence, /*entry_tx_inputs_coin_age=*/coin_age, tx->GetValueOut(), spendsCoinbase, sigOpCost, lp));
}
struct Available {

View File

@ -9,6 +9,7 @@
#include <test/util/setup_common.h>
#include <txmempool.h>
#include <util/chaintype.h>
#include <validation.h>
#include <univalue.h>
@ -16,12 +17,13 @@
static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
{
LockPoints lp;
pool.addUnchecked(CTxMemPoolEntry(tx, fee, /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/4, lp));
pool.addUnchecked(CTxMemPoolEntry(tx, fee, /*time=*/0, /*entry_height=*/0, /*entry_sequence=*/0, /*entry_tx_inputs_coin_age=*/0.0, /*in_chain_input_value=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/4, lp));
}
static void RpcMempool(benchmark::Bench& bench)
{
const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN);
auto& chainman = *testing_setup->m_node.chainman;
CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
LOCK2(cs_main, pool.cs);
@ -38,7 +40,7 @@ static void RpcMempool(benchmark::Bench& bench)
}
bench.run([&] {
(void)MempoolToJSON(pool, /*verbose=*/true);
(void)MempoolToJSON(chainman, pool, /*verbose=*/true);
});
}

View File

@ -663,7 +663,7 @@ void SetupServerArgs(ArgsManager& argsman)
strprintf("Maximum tip age in seconds to consider node in initial block download (default: %u)",
Ticks<std::chrono::seconds>(DEFAULT_MAX_TIP_AGE)),
ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-printpriority", strprintf("Log transaction fee rate in " + CURRENCY_UNIT + "/kvB when mining blocks (default: %u)", DEFAULT_PRINT_MODIFIED_FEE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-printpriority", strprintf("Log transaction priority and fee rate in " + CURRENCY_UNIT + "/kvB when mining blocks (default: %u)", DEFAULT_PRINT_MODIFIED_FEE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-uaappend=<cmt>", "Append literal to the user agent string (should only be used for software embedding)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-uacomment=<cmt>", "Append comment to the user agent string", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
@ -692,6 +692,7 @@ void SetupServerArgs(ArgsManager& argsman)
argsman.AddArg("-blockmaxsize=<n>", strprintf("Set maximum block size in bytes (default: %d)", DEFAULT_BLOCK_MAX_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
argsman.AddArg("-blockmaxweight=<n>", strprintf("Set maximum BIP141 block weight (default: %d)", DEFAULT_BLOCK_MAX_WEIGHT), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
argsman.AddArg("-blockmintxfee=<amt>", strprintf("Set lowest fee rate (in %s/kvB) for transactions to be included in block creation. (default: %s)", CURRENCY_UNIT, FormatMoney(DEFAULT_BLOCK_MIN_TX_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
argsman.AddArg("-blockprioritysize=<n>", strprintf("Set maximum size of high-priority/low-fee transactions in bytes (default: %d)", DEFAULT_BLOCK_PRIORITY_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
argsman.AddArg("-blockversion=<n>", "Override block version to test forking scenarios", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::BLOCK_CREATION);
argsman.AddArg("-rest", strprintf("Accept public REST requests (default: %u)", DEFAULT_REST_ENABLE), ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
@ -1900,7 +1901,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
}
// Load mempool from disk
if (auto* pool{chainman.ActiveChainstate().GetMempool()}) {
LoadMempool(*pool, ShouldPersistMempool(args) ? MempoolPath(args) : fs::path{}, chainman.ActiveChainstate(), {});
LoadMempool(*pool, ShouldPersistMempool(args) ? MempoolPath(args) : fs::path{}, chainman.ActiveChainstate(), {
.load_knots_data = true,
});
pool->SetLoadTried(!chainman.m_interrupt);
}
});

View File

@ -8,12 +8,14 @@
#include <consensus/amount.h>
#include <consensus/validation.h>
#include <core_memusage.h>
#include <policy/coin_age_priority.h>
#include <policy/policy.h>
#include <policy/settings.h>
#include <primitives/transaction.h>
#include <util/epochguard.h>
#include <util/overflow.h>
#include <cassert>
#include <chrono>
#include <functional>
#include <memory>
@ -84,9 +86,14 @@ private:
const size_t nUsageSize; //!< ... and total memory usage
const int64_t nTime; //!< Local time when entering the mempool
const uint64_t entry_sequence; //!< Sequence number used to determine whether this transaction is too recent for relay
const unsigned int entryHeight; //!< Chain height when entering the mempool
const bool spendsCoinbase; //!< keep track of transactions that spend a coinbase
const int64_t sigOpCost; //!< Total sigop cost
const size_t nModSize; //!< Cached modified size for priority
const double entryPriority; //!< Priority when entering the mempool
const unsigned int entryHeight; //!< Chain height when entering the mempool
double cachedPriority; //!< Last calculated priority
unsigned int cachedHeight; //!< Height at which priority was last calculated
CAmount inChainInputValue; //!< Sum of all txin values that are already in blockchain
const bool spendsCoinbase; //!< keep track of transactions that spend a coinbase
CAmount m_modified_fee; //!< Used for determining the priority of the transaction for mining in a block
mutable LockPoints lockPoints; //!< Track the height and time at which tx was final
@ -108,6 +115,8 @@ private:
public:
CTxMemPoolEntry(const CTransactionRef& tx, CAmount fee,
int64_t time, unsigned int entry_height, uint64_t entry_sequence,
double entry_tx_inputs_coin_age,
CAmount in_chain_input_value,
bool spends_coinbase,
int64_t sigops_cost, LockPoints lp)
: tx{tx},
@ -116,16 +125,25 @@ public:
nUsageSize{RecursiveDynamicUsage(tx)},
nTime{time},
entry_sequence{entry_sequence},
entryHeight{entry_height},
spendsCoinbase{spends_coinbase},
sigOpCost{sigops_cost},
nModSize{CalculateModifiedSize(*tx, GetTxSize())},
entryPriority{ComputePriority2(entry_tx_inputs_coin_age, nModSize)},
entryHeight{entry_height},
cachedPriority{entryPriority},
// Since entries arrive *after* the tip's height, their entry priority is for the height+1
cachedHeight{entry_height + 1},
inChainInputValue{in_chain_input_value},
spendsCoinbase{spends_coinbase},
m_modified_fee{nFee},
lockPoints{lp},
nSizeWithDescendants{GetTxSize()},
nModFeesWithDescendants{nFee},
nSizeWithAncestors{GetTxSize()},
nModFeesWithAncestors{nFee},
nSigOpCostWithAncestors{sigOpCost} {}
nSigOpCostWithAncestors{sigOpCost} {
CAmount nValueIn = tx->GetValueOut() + nFee;
assert(inChainInputValue <= nValueIn);
}
CTxMemPoolEntry(ExplicitCopyTag, const CTxMemPoolEntry& entry) : CTxMemPoolEntry(entry) {}
CTxMemPoolEntry& operator=(const CTxMemPoolEntry&) = delete;
@ -136,6 +154,17 @@ public:
const CTransaction& GetTx() const { return *this->tx; }
CTransactionRef GetSharedTx() const { return this->tx; }
double GetStartingPriority() const {return entryPriority; }
/**
* Fast calculation of priority as update from cached value, but only valid if
* currentHeight is greater than last height it was recalculated.
*/
double GetPriority(unsigned int currentHeight) const;
/**
* Recalculate the cached priority as of currentHeight and adjust inChainInputValue by
* valueInCurrentBlock which represents input that was just added to or removed from the blockchain.
*/
void UpdateCachedPriority(unsigned int currentHeight, CAmount valueInCurrentBlock);
const CAmount& GetFee() const { return nFee; }
int32_t GetTxSize() const
{

View File

@ -749,7 +749,7 @@ public:
{
if (!m_node.mempool) return {};
LockPoints lp;
CTxMemPoolEntry entry(tx, 0, 0, 0, 0, false, 0, lp);
CTxMemPoolEntry entry(tx, 0, 0, 0, 0, 0, 0, false, 0, lp);
LOCK(m_node.mempool->cs);
return m_node.mempool->CheckPackageLimits({tx}, entry.GetTxSize());
}

View File

@ -16,6 +16,7 @@
#include <uint256.h>
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/serfloat.h>
#include <util/signalinterrupt.h>
#include <util/syserror.h>
#include <util/time.h>
@ -38,6 +39,39 @@ namespace node {
static const uint64_t MEMPOOL_DUMP_VERSION_NO_XOR_KEY{1};
static const uint64_t MEMPOOL_DUMP_VERSION{2};
static constexpr uint64_t MEMPOOL_KNOTS_DUMP_VERSION = 0;
bool LoadMempoolKnots(CTxMemPool& pool, const fs::path& knots_filepath, FopenFn mockable_fopen_function)
{
AutoFile file{mockable_fopen_function(knots_filepath, "rb")};
if (file.IsNull()) {
// Typically missing if there's nothing to save
return false;
}
try {
uint64_t version;
file >> version;
if (version != MEMPOOL_KNOTS_DUMP_VERSION) {
return false;
}
const unsigned int priority_deltas_count = ReadCompactSize(file);
uint256 txid;
uint64_t encoded_priority;
for (unsigned int i = 0; i < priority_deltas_count; ++i) {
Unserialize(file, txid);
Unserialize(file, encoded_priority);
const double priority = DecodeDouble(encoded_priority);
pool.PrioritiseTransaction(txid, priority, 0);
}
} catch (const std::exception& e) {
LogInfo("Failed to deserialize mempool-knots data on file: %s. Continuing anyway.\n", e.what());
return false;
}
return true;
}
bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active_chainstate, ImportMempoolOptions&& opts)
{
@ -143,6 +177,12 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
return false;
}
if (opts.load_knots_data) {
auto knots_filepath = load_path;
knots_filepath.replace_filename("mempool-knots.dat");
LoadMempoolKnots(pool, knots_filepath, opts.mockable_fopen_function);
}
LogInfo("Imported mempool transactions from file: %i succeeded, %i failed, %i expired, %i already there, %i waiting for initial broadcast\n", count, failed, expired, already_there, unbroadcast);
return true;
}
@ -152,6 +192,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
auto start = SteadyClock::now();
std::map<uint256, CAmount> mapDeltas;
std::map<uint256, double> priority_deltas;
std::vector<TxMempoolInfo> vinfo;
std::set<uint256> unbroadcast_txids;
@ -161,7 +202,12 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
{
LOCK(pool.cs);
for (const auto &i : pool.mapDeltas) {
mapDeltas[i.first] = i.second;
if (i.second.first) { // priority delta
priority_deltas[i.first] = i.second.first;
}
if (i.second.second) { // fee delta
mapDeltas[i.first] = i.second.second;
}
}
vinfo = pool.infoAll();
unbroadcast_txids = pool.GetUnbroadcastTxs();
@ -207,6 +253,39 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
throw std::runtime_error(
strprintf("Error closing %s: %s", fs::PathToString(file_fspath), SysErrorString(errno)));
}
auto knots_filepath = dump_path;
knots_filepath.replace_filename("mempool-knots.dat");
LogInfo("Writing %u mempool prioritizations to file...\n", priority_deltas.size());
if (priority_deltas.size()) {
auto knots_tmppath = knots_filepath;
knots_tmppath += ".new";
AutoFile file{mockable_fopen_function(knots_tmppath, "wb")};
if (file.IsNull()) return false;
uint64_t version = MEMPOOL_KNOTS_DUMP_VERSION;
file << version;
WriteCompactSize(file, priority_deltas.size());
for (const auto& [txid, priority] : priority_deltas) {
Serialize(file, txid);
const uint64_t encoded_priority = EncodeDouble(priority);
Serialize(file, encoded_priority);
}
if (!file.Commit()) throw std::runtime_error("Commit failed");
if (file.fclose() != 0) {
throw std::runtime_error(
strprintf("Error closing %s: %s", fs::PathToString(knots_tmppath), SysErrorString(errno)));
}
if (!RenameOver(knots_tmppath, knots_filepath)) {
throw std::runtime_error("Rename failed (mempool-knots.dat)");
}
} else {
fs::remove(knots_filepath);
}
if (!RenameOver(dump_path + ".new", dump_path)) {
throw std::runtime_error("Rename failed");
}
@ -215,6 +294,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
LogInfo("Dumped mempool: %.3fs to copy, %.3fs to dump, %d bytes dumped to file\n",
Ticks<SecondsDouble>(mid - start),
Ticks<SecondsDouble>(last - mid),
(priority_deltas.empty() ? 0 : fs::file_size(knots_filepath)) +
fs::file_size(dump_path));
} catch (const std::exception& e) {
LogInfo("Failed to dump mempool: %s. Continuing anyway.\n", e.what());

View File

@ -22,6 +22,7 @@ struct ImportMempoolOptions {
bool use_current_time{false};
bool apply_fee_delta_priority{true};
bool apply_unbroadcast_set{true};
bool load_knots_data{false};
};
/** Import the file and attempt to add its contents to the mempool. */
bool LoadMempool(CTxMemPool& pool, const fs::path& load_path,

View File

@ -124,6 +124,9 @@ void BlockAssembler::resetBlock()
// These counters do not include coinbase tx
nBlockTx = 0;
nFees = 0;
lastFewTxs = 0;
blockFinished = false;
}
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
@ -143,6 +146,9 @@ 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
if (m_options.print_modified_fee) {
pblocktemplate->vTxPriorities.push_back(-1); // n/a
}
LOCK(::cs_main);
CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
@ -163,6 +169,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
int nDescendantsUpdated = 0;
if (m_mempool) {
LOCK(m_mempool->cs);
addPriorityTxs(*m_mempool, nPackagesSelected);
addPackageTxs(*m_mempool, nPackagesSelected, nDescendantsUpdated);
}
@ -217,7 +224,7 @@ 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)->GetSharedTx()->GetHash())) {
if (inBlock.count(*iit)) {
testSet.erase(iit++);
} else {
iit++;
@ -258,7 +265,7 @@ bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& packa
return true;
}
void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
void BlockAssembler::AddToBlock(const CTxMemPool& mempool, CTxMemPool::txiter iter)
{
pblocktemplate->block.vtx.emplace_back(iter->GetSharedTx());
pblocktemplate->vTxFees.push_back(iter->GetFee());
@ -270,12 +277,17 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
++nBlockTx;
nBlockSigOpsCost += iter->GetSigOpCost();
nFees += iter->GetFee();
inBlock.insert(iter->GetSharedTx()->GetHash());
inBlock.insert(iter);
if (m_options.print_modified_fee) {
LogPrintf("fee rate %s txid %s\n",
double dPriority = iter->GetPriority(nHeight);
CAmount dummy;
mempool.ApplyDeltas(iter->GetTx().GetHash(), dPriority, dummy);
LogPrintf("priority %.1f fee rate %s txid %s\n",
dPriority,
CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
iter->GetTx().GetHash().ToString());
pblocktemplate->vTxPriorities.push_back(dPriority);
}
}
@ -338,8 +350,11 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
// 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
std::set<Txid> failedTx;
CTxMemPool::setEntries failedTx;
// Start by adding all descendants of previously added txs to mapModifiedTx
// and modifying them for their already included ancestors
nDescendantsUpdated += UpdatePackagesForAdded(mempool, inBlock, mapModifiedTx);
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
CTxMemPool::txiter iter;
@ -366,7 +381,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
if (mi != mempool.mapTx.get<ancestor_score>().end()) {
auto it = mempool.mapTx.project<0>(mi);
assert(it != mempool.mapTx.end());
if (mapModifiedTx.count(it) || inBlock.count(it->GetSharedTx()->GetHash()) || failedTx.count(it->GetSharedTx()->GetHash())) {
if (mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it)) {
++mi;
continue;
}
@ -400,7 +415,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
// We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
// contain anything that is inBlock.
assert(!inBlock.count(iter->GetSharedTx()->GetHash()));
assert(!inBlock.count(iter));
uint64_t packageSize = iter->GetSizeWithAncestors();
CAmount packageFees = iter->GetModFeesWithAncestors();
@ -422,7 +437,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
// 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->GetSharedTx()->GetHash());
failedTx.insert(iter);
}
++nConsecutiveFailed;
@ -444,7 +459,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
if (!TestPackageTransactions(ancestors)) {
if (fUsingModified) {
mapModifiedTx.get<ancestor_score>().erase(modit);
failedTx.insert(iter->GetSharedTx()->GetHash());
failedTx.insert(iter);
}
if (fNeedSizeAccounting) {
@ -466,7 +481,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
SortForBlock(ancestors, sortedEntries);
for (size_t i = 0; i < sortedEntries.size(); ++i) {
AddToBlock(sortedEntries[i]);
AddToBlock(mempool, sortedEntries[i]);
// Erase from the modified set, if present
mapModifiedTx.erase(sortedEntries[i]);
}

View File

@ -38,6 +38,7 @@ struct CBlockTemplate
CBlock block;
std::vector<CAmount> vTxFees;
std::vector<int64_t> vTxSigOpsCost;
std::vector<double> vTxPriorities;
std::vector<unsigned char> vchCoinbaseCommitment;
};
@ -150,7 +151,7 @@ private:
uint64_t nBlockTx;
uint64_t nBlockSigOpsCost;
CAmount nFees;
std::unordered_set<Txid, SaltedTxidHasher> inBlock;
CTxMemPool::setEntries inBlock;
// Chain context for the block
int nHeight;
@ -160,6 +161,10 @@ private:
const CTxMemPool* const m_mempool;
Chainstate& m_chainstate;
// Variables used for addPriorityTxs
int lastFewTxs;
bool blockFinished;
public:
struct Options : BlockCreateOptions {
// Configuration parameters for the block size
@ -187,14 +192,22 @@ private:
/** 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);
void AddToBlock(const CTxMemPool& mempool, CTxMemPool::txiter iter) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
// Methods for how to add transactions to a block.
/** Add transactions based on tx "priority" */
void addPriorityTxs(const CTxMemPool& mempool, 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(const CTxMemPool& mempool, 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(const CTxMemPool& mempool, CTxMemPool::txiter iter) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
// helper functions for addPackageTxs()
/** Remove confirmed (inBlock) entries from given set */
void onlyUnconfirmed(CTxMemPool::setEntries& testSet);

View File

@ -0,0 +1,256 @@
// 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 <common/args.h>
#include <consensus/validation.h>
#include <node/miner.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
#include <txmempool.h>
#include <util/check.h>
#include <validation.h>
using node::BlockAssembler;
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.
Assert(nTxSize > 0);
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 ComputePriority2(double inputs_coin_age, unsigned int mod_vsize)
{
if (mod_vsize == 0) return 0.0;
return inputs_coin_age / mod_vsize;
}
double GetCoinAge(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 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(const CTxMemPool& mempool, CTxMemPool::txiter iter)
{
assert(iter != mempool.mapTx.end());
for (const auto& parent : iter->GetMemPoolParentsConst()) {
auto parent_it = mempool.mapTx.iterator_to(parent);
if (!inBlock.count(parent_it)) {
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 > m_options.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 > m_options.nBlockMaxWeight - 4000) {
++lastFewTxs;
}
return false;
}
CTxMemPool::setEntries package;
package.insert(iter);
if (!TestPackageTransactions(package)) {
if (nBlockSize > m_options.nBlockMaxSize - 100 || lastFewTxs > 50) {
blockFinished = true;
return false;
}
if (nBlockSize > m_options.nBlockMaxSize - 1000) {
++lastFewTxs;
}
return false;
}
return true;
}
void BlockAssembler::addPriorityTxs(const CTxMemPool& mempool, int &nPackagesSelected)
{
AssertLockHeld(mempool.cs);
// How much of the block should be dedicated to high-priority transactions,
// included regardless of the fees they pay
uint64_t nBlockPrioritySize = gArgs.GetIntArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
if (m_options.nBlockMaxSize < nBlockPrioritySize) {
nBlockPrioritySize = m_options.nBlockMaxSize;
}
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, CompareIteratorByHash> waitPriMap;
typedef std::map<CTxMemPool::txiter, double, CompareIteratorByHash>::iterator waitPriIter;
double actualPriority = -1;
vecPriority.reserve(mempool.mapTx.size());
for (auto mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi) {
double dPriority = mi->GetPriority(nHeight);
CAmount dummy;
mempool.ApplyDeltas(mi->GetTx().GetHash(), dPriority, dummy);
vecPriority.emplace_back(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;
}
// If tx is dependent on other mempool txs which haven't yet been included
// then put it in the waitSet
if (isStillDependent(mempool, 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(mempool, 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 (const auto& child : iter->GetMemPoolChildrenConst())
{
auto child_it = mempool.mapTx.iterator_to(child);
waitPriIter wpiter = waitPriMap.find(child_it);
if (wpiter != waitPriMap.end()) {
vecPriority.emplace_back(wpiter->second, child_it);
std::push_heap(vecPriority.begin(), vecPriority.end(), pricomparer);
waitPriMap.erase(wpiter);
}
}
}
}
fNeedSizeAccounting = fSizeAccounting;
}

View File

@ -0,0 +1,28 @@
// 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.
#ifndef BITCOIN_POLICY_COIN_AGE_PRIORITY_H
#define BITCOIN_POLICY_COIN_AGE_PRIORITY_H
#include <consensus/amount.h>
class CCoinsViewCache;
class CTransaction;
// Compute modified tx vsize for priority calculation
unsigned int CalculateModifiedSize(const CTransaction& tx, unsigned int nTxSize);
// Compute priority, given sum coin-age of inputs and modified tx vsize
// CAUTION: Original ComputePriority accepted UNMODIFIED tx vsize and did the modification internally
double ComputePriority2(double inputs_coin_age, unsigned int mod_vsize);
/**
* Return sum coin-age of tx inputs at height nHeight. Also calculate the sum of the values of the inputs
* that are already in the chain. These are the inputs that will age and increase priority as
* new blocks are added to the chain.
* CAUTION: Original GetPriority also called ComputePriority and returned the final coin-age priority
*/
double GetCoinAge(const CTransaction &tx, const CCoinsViewCache& view, int nHeight, CAmount &inChainInputValue);
#endif // BITCOIN_POLICY_COIN_AGE_PRIORITY_H

View File

@ -22,6 +22,10 @@ class CScript;
/** Default for -blockmaxsize, which controls the maximum size of block the mining code will create **/
static const unsigned int DEFAULT_BLOCK_MAX_SIZE = MAX_BLOCK_SERIALIZED_SIZE;
/** Default for -blockprioritysize, maximum space for zero/low-fee transactions **/
static const unsigned int DEFAULT_BLOCK_PRIORITY_SIZE = 0;
/** Minimum priority for transactions to be accepted into the priority area **/
static const double MINIMUM_TX_PRIORITY = COIN * 144 / 250;
/** Default for -blockmaxweight, which controls the range of block weights the mining code will create **/
static constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT{MAX_BLOCK_WEIGHT - 4000};
/** Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by mining code **/

View File

@ -726,7 +726,10 @@ static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::s
if (verbose && mempool_sequence) {
return RESTERR(req, HTTP_BAD_REQUEST, "Verbose results cannot contain mempool sequence values. (hint: set \"verbose=false\")");
}
str_json = MempoolToJSON(*mempool, verbose, mempool_sequence).write() + "\n";
ChainstateManager* maybe_chainman = GetChainman(context, req);
if (!maybe_chainman) return false;
ChainstateManager& chainman = *maybe_chainman;
str_json = MempoolToJSON(chainman, *mempool, verbose, mempool_sequence).write() + "\n";
} else if (param == "info/with_fee_histogram") {
str_json = MempoolInfoToJSON(*mempool, MempoolInfoToJSON_const_histogram_floors).write() + "\n";
} else {

View File

@ -288,7 +288,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "estimatesmartfee", 0, "conf_target" },
{ "estimaterawfee", 0, "conf_target" },
{ "estimaterawfee", 1, "threshold" },
{ "prioritisetransaction", 1, "dummy" },
{ "prioritisetransaction", 1, "priority_delta" },
{ "prioritisetransaction", 2, "fee_delta" },
{ "setban", 2, "bantime" },
{ "setban", 3, "absolute" },

View File

@ -313,6 +313,8 @@ static std::vector<RPCResult> MempoolEntryDescription()
RPCResult{RPCResult::Type::NUM, "weight", "transaction weight as defined in BIP 141."},
RPCResult{RPCResult::Type::NUM_TIME, "time", "local time transaction entered pool in seconds since 1 Jan 1970 GMT"},
RPCResult{RPCResult::Type::NUM, "height", "block height when transaction entered pool"},
RPCResult{RPCResult::Type::NUM, "startingpriority", "Priority when transaction entered pool"},
RPCResult{RPCResult::Type::NUM, "currentpriority", "Transaction priority now"},
RPCResult{RPCResult::Type::NUM, "descendantcount", "number of in-mempool descendant transactions (including this one)"},
RPCResult{RPCResult::Type::NUM, "descendantsize", "virtual transaction size of in-mempool descendants (including this one)"},
RPCResult{RPCResult::Type::NUM, "ancestorcount", "number of in-mempool ancestor transactions (including this one)"},
@ -335,7 +337,7 @@ static std::vector<RPCResult> MempoolEntryDescription()
};
}
static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e, const int next_block_height) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
{
AssertLockHeld(pool.cs);
@ -343,6 +345,8 @@ static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPool
info.pushKV("weight", (int)e.GetTxWeight());
info.pushKV("time", count_seconds(e.GetTime()));
info.pushKV("height", (int)e.GetHeight());
info.pushKV("startingpriority", e.GetStartingPriority());
info.pushKV("currentpriority", e.GetPriority(next_block_height));
info.pushKV("descendantcount", e.GetCountWithDescendants());
info.pushKV("descendantsize", e.GetSizeWithDescendants());
info.pushKV("ancestorcount", e.GetCountWithAncestors());
@ -393,17 +397,22 @@ static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPool
info.pushKV("unbroadcast", pool.IsUnbroadcastTx(tx.GetHash()));
}
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
UniValue MempoolToJSON(ChainstateManager &chainman, const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
{
if (verbose) {
if (include_mempool_sequence) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
}
LOCK(::cs_main);
const CChain& active_chain = chainman.ActiveChain();
const int next_block_height = active_chain.Height() + 1;
LOCK(pool.cs);
// TODO: Release cs_main after mempool.cs acquired
UniValue o(UniValue::VOBJ);
for (const CTxMemPoolEntry& e : pool.entryAll()) {
UniValue info(UniValue::VOBJ);
entryToJSON(pool, info, e);
entryToJSON(pool, info, e, next_block_height);
// Mempool has unique entries so there is no advantage in using
// UniValue::pushKV, which checks if the key already exists in O(N).
// UniValue::pushKVEnd is used instead which currently is O(1).
@ -606,7 +615,9 @@ static RPCHelpMan getrawmempool()
include_mempool_sequence = request.params[1].get_bool();
}
return MempoolToJSON(EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
NodeContext& node = EnsureAnyNodeContext(request.context);
ChainstateManager& chainman = EnsureChainman(node);
return MempoolToJSON(chainman, EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
},
};
}
@ -642,7 +653,12 @@ static RPCHelpMan getmempoolancestors()
uint256 hash = ParseHashV(request.params[0], "parameter 1");
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
ChainstateManager& chainman = EnsureAnyChainman(request.context);
LOCK(::cs_main);
const CChain& active_chain = chainman.ActiveChain();
const int next_block_height = active_chain.Height() + 1;
LOCK(mempool.cs);
// TODO: Release cs_main after mempool.cs acquired
const auto entry{mempool.GetEntry(Txid::FromUint256(hash))};
if (entry == nullptr) {
@ -663,7 +679,7 @@ static RPCHelpMan getmempoolancestors()
const CTxMemPoolEntry &e = *ancestorIt;
const uint256& _hash = e.GetTx().GetHash();
UniValue info(UniValue::VOBJ);
entryToJSON(mempool, info, e);
entryToJSON(mempool, info, e, next_block_height);
o.pushKV(_hash.ToString(), std::move(info));
}
return o;
@ -703,7 +719,12 @@ static RPCHelpMan getmempooldescendants()
uint256 hash = ParseHashV(request.params[0], "parameter 1");
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
ChainstateManager& chainman = EnsureAnyChainman(request.context);
LOCK(::cs_main);
const CChain& active_chain = chainman.ActiveChain();
const int next_block_height = active_chain.Height() + 1;
LOCK(mempool.cs);
// TODO: Release cs_main after mempool.cs acquired
const auto it{mempool.GetIter(hash)};
if (!it) {
@ -728,7 +749,7 @@ static RPCHelpMan getmempooldescendants()
const CTxMemPoolEntry &e = *descendantIt;
const uint256& _hash = e.GetTx().GetHash();
UniValue info(UniValue::VOBJ);
entryToJSON(mempool, info, e);
entryToJSON(mempool, info, e, next_block_height);
o.pushKV(_hash.ToString(), std::move(info));
}
return o;
@ -755,7 +776,12 @@ static RPCHelpMan getmempoolentry()
uint256 hash = ParseHashV(request.params[0], "parameter 1");
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
ChainstateManager& chainman = EnsureAnyChainman(request.context);
LOCK(::cs_main);
const CChain& active_chain = chainman.ActiveChain();
const int next_block_height = active_chain.Height() + 1;
LOCK(mempool.cs);
// TODO: Release cs_main after mempool.cs acquired
const auto entry{mempool.GetEntry(Txid::FromUint256(hash))};
if (entry == nullptr) {
@ -763,7 +789,7 @@ static RPCHelpMan getmempoolentry()
}
UniValue info(UniValue::VOBJ);
entryToJSON(mempool, info, *entry);
entryToJSON(mempool, info, *entry, next_block_height);
return info;
},
};

View File

@ -10,6 +10,7 @@
#include <optional>
#include <vector>
class ChainstateManager;
class CTxMemPool;
class UniValue;
@ -28,7 +29,7 @@ static const MempoolHistogramFeeRates MempoolInfoToJSON_const_histogram_floors{
UniValue MempoolInfoToJSON(const CTxMemPool& pool, const std::optional<MempoolHistogramFeeRates>& histogram_floors);
/** Mempool to JSON */
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false);
UniValue MempoolToJSON(ChainstateManager& chainman, const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false);
/** Mempool Txs to JSON */
UniValue MempoolTxsToJSON(const CTxMemPool& pool, bool verbose = false, uint64_t sequence_start = 0);

View File

@ -476,9 +476,10 @@ static RPCHelpMan prioritisetransaction()
"Accepts the transaction into mined blocks at a higher (or lower) priority\n",
{
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id."},
{"dummy", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "API-Compatibility for previous API. Must be zero or null.\n"
" DEPRECATED. For forward compatibility use named arguments and omit this parameter."},
{"fee_delta", RPCArg::Type::NUM, RPCArg::Optional::NO, "The fee value (in satoshis) to add (or subtract, if negative).\n"
{"priority_delta", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The priority to add or subtract.\n"
" The transaction selection algorithm considers the tx as it would have a higher priority.\n"
" (priority of a transaction is calculated: coinage * value_in_satoshis / txsize)\n"},
{"fee_delta", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The fee value (in satoshis) to add (or subtract, if negative).\n"
" Note, that this value is not a fee rate. It is a value to modify absolute fee of the TX.\n"
" The fee is not actually paid, only the algorithm for selecting transactions into a block\n"
" considers the transaction as it would have paid a higher (or lower) fee."},
@ -494,14 +495,17 @@ static RPCHelpMan prioritisetransaction()
LOCK(cs_main);
uint256 hash(ParseHashV(request.params[0], "txid"));
const auto dummy{self.MaybeArg<double>("dummy")};
CAmount nAmount = request.params[2].getInt<int64_t>();
double priority_delta = 0;
CAmount nAmount = 0;
if (dummy && *dummy != 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.");
if (!request.params[1].isNull()) {
priority_delta = request.params[1].get_real();
}
if (!request.params[2].isNull()) {
nAmount = request.params[2].getInt<int64_t>();
}
EnsureAnyMemPool(request.context).PrioritiseTransaction(hash, nAmount);
EnsureAnyMemPool(request.context).PrioritiseTransaction(hash, priority_delta, nAmount);
return true;
},
};
@ -519,6 +523,7 @@ static RPCHelpMan getprioritisedtransactions()
{RPCResult::Type::NUM, "fee_delta", "transaction fee delta in satoshis"},
{RPCResult::Type::BOOL, "in_mempool", "whether this transaction is currently in mempool"},
{RPCResult::Type::NUM, "modified_fee", /*optional=*/true, "modified fee in satoshis. Only returned if in_mempool=true"},
{RPCResult::Type::NUM, "priority_delta", /*optional=*/true, "transaction coin-age priority delta"},
}}
},
},
@ -538,6 +543,7 @@ static RPCHelpMan getprioritisedtransactions()
if (delta_info.in_mempool) {
result_inner.pushKV("modified_fee", *delta_info.modified_fee);
}
result_inner.pushKV("priority_delta", delta_info.priority_delta);
rpc_result.pushKV(delta_info.txid.GetHex(), std::move(result_inner));
}
return rpc_result;
@ -634,6 +640,7 @@ static RPCHelpMan getblocktemplate()
{RPCResult::Type::NUM, "", "transactions before this one (by 1-based index in 'transactions' list) that must be present in the final block if this one is"},
}},
{RPCResult::Type::NUM, "fee", "difference in value between transaction inputs and outputs (in satoshis); for coinbase transactions, this is a negative Number of the total collected block fees (ie, not including the block subsidy); if key is not present, fee is unknown and clients MUST NOT assume there isn't one"},
{RPCResult::Type::NUM, "priority", /*optional=*/true, "transaction coin-age priority (non-standard)"},
{RPCResult::Type::NUM, "sigops", "total SigOps cost, as counted for purposes of block limits; if key is not present, sigop cost is unknown and clients MUST NOT assume it is zero"},
{RPCResult::Type::NUM, "weight", "total transaction weight, as counted for purposes of block limits"},
}},
@ -876,6 +883,9 @@ static RPCHelpMan getblocktemplate()
}
entry.pushKV("sigops", nTxSigOps);
entry.pushKV("weight", GetTransactionWeight(tx));
if (index_in_template && !pblocktemplate->vTxPriorities.empty()) {
entry.pushKV("priority", pblocktemplate->vTxPriorities[index_in_template]);
}
transactions.push_back(std::move(entry));
}

View File

@ -26,6 +26,7 @@ void initialize_miner()
{
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
g_setup = testing_setup.get();
MineBlock(g_setup->m_node, CScript() << OP_FALSE);
for (uint32_t i = 0; i < uint32_t{100}; ++i) {
g_available_coins.emplace_back(Txid::FromUint256(uint256::ZERO), i);
}

View File

@ -76,7 +76,7 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
available.insert(i);
}
if (add_to_mempool && !pool.exists(GenTxid::Txid(tx->GetHash()))) {
if (add_to_mempool && (!pool.exists(GenTxid::Txid(tx->GetHash()))) && SanityCheckForConsumeTxMemPoolEntry(*tx)) {
LOCK2(cs_main, pool.cs);
pool.addUnchecked(ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
available.insert(i);

View File

@ -44,6 +44,7 @@ FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
return;
}
const CTransaction tx{*mtx};
if (!SanityCheckForConsumeTxMemPoolEntry(tx)) return;
const CTxMemPoolEntry& entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, tx);
const auto tx_submitted_in_package = fuzzed_data_provider.ConsumeBool();
const auto tx_has_mempool_parents = fuzzed_data_provider.ConsumeBool();
@ -68,6 +69,7 @@ FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
break;
}
const CTransaction tx{*mtx};
if (!SanityCheckForConsumeTxMemPoolEntry(tx)) return;
mempool_entries.emplace_back(CTxMemPoolEntry::ExplicitCopy, ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));
}
std::vector<RemovedMempoolTransactionInfo> txs;

View File

@ -57,6 +57,8 @@ FUZZ_TARGET(rbf, .init = initialize_rbf)
if (!mtx) {
return;
}
const CTransaction tx{*mtx};
if (!SanityCheckForConsumeTxMemPoolEntry(tx)) return;
bilingual_str error;
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
@ -69,13 +71,13 @@ FUZZ_TARGET(rbf, .init = initialize_rbf)
break;
}
const CTransaction another_tx{*another_mtx};
if (!SanityCheckForConsumeTxMemPoolEntry(another_tx)) break;
if (fuzzed_data_provider.ConsumeBool() && !mtx->vin.empty()) {
mtx->vin[0].prevout = COutPoint{another_tx.GetHash(), 0};
}
LOCK2(cs_main, pool.cs);
pool.addUnchecked(ConsumeTxMemPoolEntry(fuzzed_data_provider, another_tx));
}
const CTransaction tx{*mtx};
if (fuzzed_data_provider.ConsumeBool()) {
LOCK2(cs_main, pool.cs);
pool.addUnchecked(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));

View File

@ -14,6 +14,17 @@
#include <cstdint>
#include <limits>
bool SanityCheckForConsumeTxMemPoolEntry(const CTransaction& tx) noexcept
{
try {
(void)tx.GetValueOut();
return true;
} catch (const std::runtime_error&) {
return false;
}
}
// NOTE: Transaction must pass SanityCheckForConsumeTxMemPoolEntry first
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider, const CTransaction& tx) noexcept
{
// Avoid:
@ -24,8 +35,9 @@ CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider,
assert(MoneyRange(fee));
const int64_t time = fuzzed_data_provider.ConsumeIntegral<int64_t>();
const uint64_t entry_sequence{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
const unsigned int entry_height = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
const double coin_age = fuzzed_data_provider.ConsumeFloatingPoint<double>();
const unsigned int entry_height = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, std::numeric_limits<unsigned int>::max() - 1);
const bool spends_coinbase = fuzzed_data_provider.ConsumeBool();
const unsigned int sig_op_cost = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, MAX_BLOCK_SIGOPS_COST);
return CTxMemPoolEntry{MakeTransactionRef(tx), fee, time, entry_height, entry_sequence, spends_coinbase, sig_op_cost, {}};
return CTxMemPoolEntry{MakeTransactionRef(tx), fee, time, entry_height, entry_sequence, /*entry_tx_inputs_coin_age=*/coin_age, tx.GetValueOut(), spends_coinbase, sig_op_cost, {}};
}

View File

@ -21,6 +21,7 @@ public:
}
};
[[nodiscard]] bool SanityCheckForConsumeTxMemPoolEntry(const CTransaction& tx) noexcept;
[[nodiscard]] CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider, const CTransaction& tx) noexcept;
#endif // BITCOIN_TEST_FUZZ_UTIL_MEMPOOL_H

View File

@ -55,6 +55,7 @@ FUZZ_TARGET(validation_load_mempool, .init = initialize_validation_load_mempool)
(void)LoadMempool(pool, MempoolPath(g_setup->m_args), chainstate,
{
.mockable_fopen_function = fuzzed_fopen,
.load_knots_data = true,
});
pool.SetLoadTried(true);
(void)DumpMempool(pool, MempoolPath(g_setup->m_args), fuzzed_fopen, true);

View File

@ -30,6 +30,7 @@
#include <node/peerman_args.h>
#include <node/warnings.h>
#include <noui.h>
#include <policy/coin_age_priority.h>
#include <policy/fees.h>
#include <pow.h>
#include <random.h>
@ -520,6 +521,8 @@ CMutableTransaction TestChain100Setup::CreateValidMempoolTransaction(CTransactio
std::vector<CTransactionRef> TestChain100Setup::PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit)
{
auto& active_chainstate = m_node.chainman->ActiveChainstate();
const auto height = active_chainstate.m_chain.Height();
std::vector<CTransactionRef> mempool_transactions;
std::deque<std::pair<COutPoint, CAmount>> unspent_prevouts;
std::transform(m_coinbase_txns.begin(), m_coinbase_txns.end(), std::back_inserter(unspent_prevouts),
@ -557,8 +560,12 @@ std::vector<CTransactionRef> TestChain100Setup::PopulateMempool(FastRandomContex
if (submit) {
LOCK2(cs_main, m_node.mempool->cs);
LockPoints lp;
CAmount in_chain_input_value;
const double coin_age = GetCoinAge(*ptx, active_chainstate.CoinsTip(), height + 1, in_chain_input_value);
m_node.mempool->addUnchecked(CTxMemPoolEntry(ptx, /*fee=*/(total_in - num_outputs * amount_per_output),
/*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0,
/*time=*/0, /*entry_height=*/ height, /*entry_sequence=*/0,
/*entry_tx_inputs_coin_age=*/coin_age,
in_chain_input_value,
/*spends_coinbase=*/false, /*sigops_cost=*/4, lp));
}
--num_transactions;
@ -589,6 +596,8 @@ void TestChain100Setup::MockMempoolMinFee(const CFeeRate& target_feerate)
m_node.mempool->m_opts.incremental_relay_feerate.GetFee(GetVirtualTransactionSize(*tx));
m_node.mempool->addUnchecked(CTxMemPoolEntry(tx, /*fee=*/tx_fee,
/*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0,
/*entry_tx_inputs_coin_age=*/0.0,
/*in_chain_input_value=*/0,
/*spends_coinbase=*/true, /*sigops_cost=*/1, lp));
m_node.mempool->TrimToSize(0);
assert(m_node.mempool->GetMinFee() == target_feerate);

View File

@ -37,7 +37,9 @@ CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) co
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx) const
{
return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch<std::chrono::seconds>(time), nHeight, m_sequence, spendsCoinbase, sigOpCost, lp};
constexpr double coin_age{0};
const CAmount inChainValue = 0;
return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch<std::chrono::seconds>(time), nHeight, m_sequence, /*entry_tx_inputs_coin_age=*/coin_age, inChainValue, spendsCoinbase, sigOpCost, lp};
}
std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,

View File

@ -12,6 +12,7 @@
#include <consensus/tx_verify.h>
#include <consensus/validation.h>
#include <logging.h>
#include <policy/coin_age_priority.h>
#include <policy/policy.h>
#include <policy/settings.h>
#include <random.h>
@ -437,8 +438,10 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
indexed_transaction_set::iterator newit = mapTx.emplace(CTxMemPoolEntry::ExplicitCopy, entry).first;
// Update transaction for any feeDelta created by PrioritiseTransaction
double priority_delta{0.};
CAmount delta{0};
ApplyDelta(entry.GetTx().GetHash(), delta);
ApplyDeltas(entry.GetTx().GetHash(), priority_delta, delta);
// NOTE: priority_delta is handled in addPriorityTxs
// The following call to UpdateModifiedFee assumes no previous fee modifications
Assume(entry.GetFee() == entry.GetModifiedFee());
if (delta) {
@ -636,6 +639,7 @@ void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigne
txs_removed_for_block.reserve(vtx.size());
for (const auto& tx : vtx)
{
UpdateDependentPriorities(*tx, nBlockHeight, true);
txiter it = mapTx.find(tx->GetHash());
if (it != mapTx.end()) {
setEntries stage;
@ -672,6 +676,15 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
for (const auto& it : GetSortedDepthAndScore()) {
checkTotal += it->GetTxSize();
CAmount dummyValue;
const double fresh_coin_age = GetCoinAge(it->GetTx(), active_coins_tip, spendheight, dummyValue);
const auto fresh_mod_vsize = CalculateModifiedSize(it->GetTx(), it->GetTxSize());
const double freshPriority = ComputePriority2(fresh_coin_age, fresh_mod_vsize);
double cachePriority = it->GetPriority(spendheight);
double priDiff = cachePriority > freshPriority ? cachePriority - freshPriority : freshPriority - cachePriority;
// Verify that the difference between the on the fly calculation and a fresh calculation
// is small enough to be a result of double imprecision.
assert(priDiff < .0001 * freshPriority + 1);
check_total_fee += it->GetFee();
innerUsage += it->DynamicMemoryUsage();
const CTransaction& tx = it->GetTx();
@ -889,15 +902,17 @@ TxMempoolInfo CTxMemPool::info_for_relay(const GenTxid& gtxid, uint64_t last_seq
}
}
void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta)
void CTxMemPool::PrioritiseTransaction(const uint256& hash, double dPriorityDelta, const CAmount& nFeeDelta)
{
{
LOCK(cs);
CAmount &delta = mapDeltas[hash];
delta = SaturatingAdd(delta, nFeeDelta);
std::pair<double, CAmount> &deltas = mapDeltas[hash];
deltas.first += dPriorityDelta;
deltas.second = SaturatingAdd(deltas.second, nFeeDelta);
txiter it = mapTx.find(hash);
if (it != mapTx.end()) {
mapTx.modify(it, [&nFeeDelta](CTxMemPoolEntry& e) { e.UpdateModifiedFee(nFeeDelta); });
// Now update all ancestors' modified fees with descendants
auto ancestors{AssumeCalculateMemPoolAncestors(__func__, *it, Limits::NoLimits(), /*fSearchForParents=*/false)};
for (txiter ancestorIt : ancestors) {
@ -912,27 +927,29 @@ void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeD
}
++nTransactionsUpdated;
}
if (delta == 0) {
if (deltas.first == 0. && deltas.second == 0) {
mapDeltas.erase(hash);
LogPrintf("PrioritiseTransaction: %s (%sin mempool) delta cleared\n", hash.ToString(), it == mapTx.end() ? "not " : "");
} else {
LogPrintf("PrioritiseTransaction: %s (%sin mempool) fee += %s, new delta=%s\n",
LogPrintf("PrioritiseTransaction: %s (%sin mempool) priority += %f, fee += %s, new delta=%s\n",
hash.ToString(),
it == mapTx.end() ? "not " : "",
dPriorityDelta,
FormatMoney(nFeeDelta),
FormatMoney(delta));
FormatMoney(deltas.second));
}
}
}
void CTxMemPool::ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const
void CTxMemPool::ApplyDeltas(const uint256& hash, double &dPriorityDelta, CAmount &nFeeDelta) const
{
AssertLockHeld(cs);
std::map<uint256, CAmount>::const_iterator pos = mapDeltas.find(hash);
std::map<uint256, std::pair<double, CAmount> >::const_iterator pos = mapDeltas.find(hash);
if (pos == mapDeltas.end())
return;
const CAmount &delta = pos->second;
nFeeDelta += delta;
const std::pair<double, CAmount> &deltas = pos->second;
dPriorityDelta += deltas.first;
nFeeDelta += deltas.second;
}
void CTxMemPool::ClearPrioritisation(const uint256& hash)
@ -952,7 +969,7 @@ std::vector<CTxMemPool::delta_info> CTxMemPool::GetPrioritisedTransactions() con
const bool in_mempool{iter != mapTx.end()};
std::optional<CAmount> modified_fee;
if (in_mempool) modified_fee = iter->GetModifiedFee();
result.emplace_back(delta_info{in_mempool, delta, modified_fee, txid});
result.emplace_back(delta_info{in_mempool, delta.second, delta.first, modified_fee, txid});
}
return result;
}

View File

@ -437,7 +437,7 @@ private:
public:
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
std::map<uint256, CAmount> mapDeltas GUARDED_BY(cs);
std::map<uint256, std::pair<double, CAmount> > mapDeltas GUARDED_BY(cs);
using Options = kernel::MemPoolOptions;
@ -489,10 +489,17 @@ public:
* the tx is not dependent on other mempool transactions to be included in a block.
*/
bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
/**
* Update all transactions in the mempool which depend on tx to recalculate their priority
* and adjust the input value that will age to reflect that the inputs from this transaction have
* either just been added to the chain or just been removed.
*/
void UpdateDependentPriorities(const CTransaction &tx, unsigned int nBlockHeight, bool addToChain);
/** Affect CreateNewBlock prioritisation of transactions */
void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
void PrioritiseTransaction(const uint256& hash, double dPriorityDelta, const CAmount& nFeeDelta);
void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta) { PrioritiseTransaction(hash, 0., nFeeDelta); }
void ApplyDeltas(const uint256& hash, double &dPriorityDelta, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
void ClearPrioritisation(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs);
struct delta_info {
@ -500,6 +507,7 @@ public:
const bool in_mempool;
/** The fee delta added using PrioritiseTransaction(). */
const CAmount delta;
const double priority_delta;
/** The modified fee (base fee + delta) of this entry. Only present if in_mempool=true. */
std::optional<CAmount> modified_fee;
/** The prioritised transaction's txid. */

View File

@ -33,6 +33,7 @@
#include <logging/timer.h>
#include <node/blockstorage.h>
#include <node/utxo_snapshot.h>
#include <policy/coin_age_priority.h>
#include <policy/policy.h>
#include <policy/rbf.h>
#include <policy/settings.h>
@ -931,7 +932,12 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
// ws.m_modified_fees includes any fee deltas from PrioritiseTransaction
ws.m_modified_fees = ws.m_base_fees;
m_pool.ApplyDelta(hash, ws.m_modified_fees);
double nPriorityDummy{0};
m_pool.ApplyDeltas(hash, nPriorityDummy, ws.m_modified_fees);
CAmount inChainInputValue;
// Since entries arrive *after* the tip's height, their priority is for the height+1
const double coin_age = GetCoinAge(tx, m_view, m_active_chainstate.m_chain.Height() + 1, inChainInputValue);
// Keep track of transactions that spend a coinbase, which we re-scan
// during reorgs to ensure COINBASE_MATURITY is still met.
@ -948,6 +954,8 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
// reorg to be marked earlier than any child txs that were already in the mempool.
const uint64_t entry_sequence = args.m_ignore_rejects.count(rejectmsg_zero_mempool_entry_seq) ? 0 : m_pool.GetSequence();
entry.reset(new CTxMemPoolEntry(ptx, ws.m_base_fees, nAcceptTime, m_active_chainstate.m_chain.Height(), entry_sequence,
/*entry_tx_inputs_coin_age=*/coin_age,
inChainInputValue,
fSpendsCoinbase, nSigOpsCost, lock_points.value()));
ws.m_vsize = entry->GetTxSize();
@ -3172,6 +3180,9 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
}
if (disconnectpool && m_mempool) {
for (auto it = block.vtx.rbegin(); it != block.vtx.rend(); ++it) {
m_mempool->UpdateDependentPriorities(*(*it), pindexDelete->nHeight, false);
}
// Save transactions to re-add to mempool at end of reorg. If any entries are evicted for
// exceeding memory limits, remove them and their descendants from the mempool.
for (auto&& evicted_tx : disconnectpool->AddTransactionsFromBlock(block.vtx)) {

View File

@ -0,0 +1,191 @@
#!/usr/bin/env python3
# Copyright (c) 2016 The Bitcoin Core developers
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
from test_framework.blocktools import create_block
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
from binascii import b2a_hex
from decimal import Decimal
def find_unspent(node, txid, amount):
for utxo in node.listunspent(0):
if utxo['txid'] != txid:
continue
if utxo['amount'] != amount:
continue
return {'txid': utxo['txid'], 'vout': utxo['vout']}
def solve_template_hex(tmpl, txlist):
block = create_block(tmpl=tmpl, txlist=txlist)
block.solve()
b = block.serialize()
x = b2a_hex(b).decode('ascii')
return x
def get_modified_size(node, txdata):
decoded = node.decoderawtransaction(txdata)
size = decoded['vsize']
for inp in decoded['vin']:
offset = 41 + min(len(inp['scriptSig']['hex']) // 2, 110)
if offset <= size:
size -= offset
return size
def assert_approximate(a, b):
assert_equal(int(a), int(b))
BTC = Decimal('100000000')
class PriorityTest(BitcoinTestFramework):
def add_options(self, parser):
self.add_wallet_options(parser)
def set_test_params(self):
self.num_nodes = 3
self.testmsg_num = 0
def setup_nodes(self):
self.extra_args = [
['-blockmaxsize=0'],
['-blockprioritysize=1000000', '-blockmaxsize=1000000', '-printpriority'],
['-blockmaxsize=0'],
]
super().setup_nodes()
def assert_prio(self, txid, starting, current):
node = self.nodes[1]
tmpl = node.getblocktemplate({'rules':('segwit',)})
tmplentry = None
for tx in tmpl['transactions']:
if tx['txid'] == txid:
tmplentry = tx
break
# GBT does not expose starting priority, so we don't check that
assert_approximate(tmplentry['priority'], current)
mempoolentry = node.getrawmempool(True)[txid]
assert_approximate(mempoolentry['startingpriority'], starting)
assert_approximate(mempoolentry['currentpriority'], current)
def testmsg(self, msg):
self.testmsg_num += 1
self.log.info('Test %d: %s' % (self.testmsg_num, msg))
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
def run_test(self):
node = self.nodes[0]
miner = self.nodes[1]
self.generate(node, 50)
self.generate(miner, 101)
fee = Decimal('0.0001')
amt = Decimal('11')
txid_a = node.sendtoaddress(node.getnewaddress(), amt)
txdata_b = node.createrawtransaction([find_unspent(node, txid_a, amt)], {node.getnewaddress(): amt - fee})
txdata_b = node.signrawtransactionwithwallet(txdata_b)['hex']
txmodsize_b = get_modified_size(node, txdata_b)
txid_b = node.sendrawtransaction(txdata_b)
self.sync_all()
self.testmsg('priority starts at 0 with all unconfirmed inputs')
self.assert_prio(txid_b, 0, 0)
self.testmsg('priority increases correctly when that input is mined')
# Mine only the sendtoaddress transaction
tmpl = node.getblocktemplate({'rules':('segwit',)})
rawblock = solve_template_hex(tmpl, [node.getrawtransaction(txid_a)])
assert_equal(node.submitblock(rawblock), None)
self.sync_all()
self.assert_prio(txid_b, 0, amt * BTC / txmodsize_b)
self.testmsg('priority continues to increase the deeper the block confirming its inputs gets buried')
self.generate(node, 2)
self.assert_prio(txid_b, 0, amt * BTC * 3 / txmodsize_b)
self.testmsg('with a confirmed input, the initial priority is calculated correctly')
self.generate(miner, 4)
amt_c = (amt - fee) / 2
amt_c2 = amt_c - fee
txdata_c = node.createrawtransaction([find_unspent(node, txid_b, amt - fee)], {node.getnewaddress(): amt_c, node.getnewaddress(): amt_c2})
txdata_c = node.signrawtransactionwithwallet(txdata_c)['hex']
txmodsize_c = get_modified_size(node, txdata_c)
txid_c = node.sendrawtransaction(txdata_c)
self.sync_all()
txid_c_starting_prio = (amt - fee) * BTC * 4 / txmodsize_c
self.assert_prio(txid_c, txid_c_starting_prio, txid_c_starting_prio)
self.testmsg('with an input confirmed prior to the transaction, the priority gets incremented correctly as it gets buried deeper')
self.generate(node, 1)
self.assert_prio(txid_c, txid_c_starting_prio, (amt - fee) * BTC * 5 / txmodsize_c)
self.testmsg('with an input confirmed prior to the transaction, the priority gets incremented correctly as it gets buried deeper and deeper')
self.generate(node, 2)
self.assert_prio(txid_c, txid_c_starting_prio, (amt - fee) * BTC * 7 / txmodsize_c)
self.log.info('(preparing for reorg test)')
self.generate(miner, 1)
self.split_network()
node = self.nodes[0]
miner = self.nodes[1]
competing_miner = self.nodes[2]
txdata_d = node.createrawtransaction([find_unspent(node, txid_c, amt_c)], {node.getnewaddress(): amt_c - fee})
txdata_d = node.signrawtransactionwithwallet(txdata_d)['hex']
get_modified_size(node, txdata_d)
txid_d = node.sendrawtransaction(txdata_d)
self.sync_all(self.nodes[:2])
self.sync_all(self.nodes[2:])
self.generate(miner, 1, sync_fun=self.no_op)
self.sync_all(self.nodes[:2])
self.sync_all(self.nodes[2:])
txdata_e = node.createrawtransaction([find_unspent(node, txid_d, amt_c - fee), find_unspent(node, txid_c, amt_c2)], {node.getnewaddress(): (amt_c - fee) + amt_c2 - fee})
txdata_e = node.signrawtransactionwithwallet(txdata_e)['hex']
txmodsize_e = get_modified_size(node, txdata_e)
txid_e = node.sendrawtransaction(txdata_e)
self.sync_all(self.nodes[:2])
self.sync_all(self.nodes[2:])
txid_e_starting_prio = (((amt_c - fee) * BTC) + (amt_c2 * BTC * 2)) / txmodsize_e
self.assert_prio(txid_e, txid_e_starting_prio, txid_e_starting_prio) # Sanity check 1
self.generate(competing_miner, 5, sync_fun=self.no_op)
self.sync_all(self.nodes[:2])
self.sync_all(self.nodes[2:])
self.assert_prio(txid_e, txid_e_starting_prio, txid_e_starting_prio) # Sanity check 2
self.testmsg('priority is updated correctly when input-confirming block is reorganised out')
self.connect_nodes(1, 2)
self.sync_blocks()
txid_e_reorg_prio = (amt_c2 * BTC * 6) / txmodsize_e
self.assert_prio(txid_e, txid_e_starting_prio, txid_e_reorg_prio)
if __name__ == '__main__':
PriorityTest(__file__).main()

View File

@ -146,8 +146,6 @@ class PrioritiseTransactionTest(BitcoinTestFramework):
# Test `prioritisetransaction` required parameters
assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction)
assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction, '')
assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction, '', 0)
# Test `prioritisetransaction` invalid extra parameters
assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction, '', 0, 0, 0)
@ -160,10 +158,9 @@ class PrioritiseTransactionTest(BitcoinTestFramework):
assert_raises_rpc_error(-8, "txid must be of length 64 (not 3, for 'foo')", self.nodes[0].prioritisetransaction, txid='foo', fee_delta=0)
assert_raises_rpc_error(-8, "txid must be hexadecimal string (not 'Zd1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000')", self.nodes[0].prioritisetransaction, txid='Zd1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000', fee_delta=0)
# Test `prioritisetransaction` invalid `dummy`
# Test `prioritisetransaction` invalid `priority_delta`
txid = '1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000'
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type number", self.nodes[0].prioritisetransaction, txid, 'foo', 0)
assert_raises_rpc_error(-8, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.", self.nodes[0].prioritisetransaction, txid, 1, 0)
# Test `prioritisetransaction` invalid `fee_delta`
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type number", self.nodes[0].prioritisetransaction, txid=txid, fee_delta='foo')

View File

@ -363,6 +363,14 @@ class TestNode():
assert not invalid_call
return self.__getattr__('generatetodescriptor')(*args, **kwargs)
def getprioritisedtransactions(self, *args, **kwargs):
res = self.__getattr__('getprioritisedtransactions')(*args, **kwargs)
assert not (args or kwargs)
for res_val in res.values():
if res_val['priority_delta'] == 0:
del res_val['priority_delta']
return res
def setmocktime(self, timestamp):
"""Wrapper for setmocktime RPC, sets self.mocktime"""
if timestamp == 0:

View File

@ -196,6 +196,7 @@ BASE_SCRIPTS = [
'tool_signet_miner.py --descriptors',
'wallet_txn_clone.py',
'wallet_txn_clone.py --segwit',
'mining_coin_age_priority.py',
'rpc_getchaintips.py',
'rpc_misc.py',
'p2p_1p1c_network.py',