diff --git a/src/Makefile.am b/src/Makefile.am index d0c00b1e2a..083c06a998 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -249,6 +249,7 @@ BITCOIN_CORE_H = \ node/validation_cache_args.h \ noui.h \ outputtype.h \ + policy/coin_age_priority.h \ policy/v3_policy.h \ policy/feerate.h \ policy/fees.h \ @@ -451,6 +452,7 @@ libbitcoin_node_a_SOURCES = \ node/utxo_snapshot.cpp \ node/validation_cache_args.cpp \ noui.cpp \ + policy/coin_age_priority.cpp \ policy/v3_policy.cpp \ policy/fees.cpp \ policy/fees_args.cpp \ diff --git a/src/bench/mempool_eviction.cpp b/src/bench/mempool_eviction.cpp index 1a9b013277..e118fca314 100644 --- a/src/bench/mempool_eviction.cpp +++ b/src/bench/mempool_eviction.cpp @@ -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)); } diff --git a/src/bench/mempool_stress.cpp b/src/bench/mempool_stress.cpp index fe3e204fb3..9c1c1afca2 100644 --- a/src/bench/mempool_stress.cpp +++ b/src/bench/mempool_stress.cpp @@ -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 { diff --git a/src/bench/rpc_mempool.cpp b/src/bench/rpc_mempool.cpp index a55aa0c794..8eb83c4853 100644 --- a/src/bench/rpc_mempool.cpp +++ b/src/bench/rpc_mempool.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include @@ -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(ChainType::MAIN); + const auto testing_setup = MakeNoLogFileContext(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); }); } diff --git a/src/init.cpp b/src/init.cpp index 3fd7fba218..8ab362a116 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -642,7 +642,7 @@ void SetupServerArgs(ArgsManager& argsman) strprintf("Maximum tip age in seconds to consider node in initial block download (default: %u)", Ticks(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_PRINTPRIORITY), 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_PRINTPRIORITY), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST); argsman.AddArg("-uaappend=", "Append literal to the user agent string (should only be used for software embedding)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-uacomment=", "Append comment to the user agent string", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST); @@ -671,6 +671,7 @@ void SetupServerArgs(ArgsManager& argsman) argsman.AddArg("-blockmaxsize=", strprintf("Set maximum block size in bytes (default: %d)", DEFAULT_BLOCK_MAX_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION); argsman.AddArg("-blockmaxweight=", strprintf("Set maximum BIP141 block weight (default: %d)", DEFAULT_BLOCK_MAX_WEIGHT), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION); argsman.AddArg("-blockmintxfee=", 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=", 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=", "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); @@ -1811,7 +1812,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); } }); diff --git a/src/kernel/mempool_entry.h b/src/kernel/mempool_entry.h index 5db9c2cac0..085e06ceda 100644 --- a/src/kernel/mempool_entry.h +++ b/src/kernel/mempool_entry.h @@ -8,12 +8,14 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -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 { diff --git a/src/kernel/mempool_persist.cpp b/src/kernel/mempool_persist.cpp index 12c8fb7dda..32ddb7779f 100644 --- a/src/kernel/mempool_persist.cpp +++ b/src/kernel/mempool_persist.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,39 @@ namespace kernel { 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) { + LogPrintf("Failed to deserialize mempool-knots data on disk: %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); + } + LogPrintf("Imported mempool transactions from disk: %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 mapDeltas; + std::map priority_deltas; std::vector vinfo; std::set 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(); @@ -205,6 +251,38 @@ 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"); + 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 (!FileCommit(file.Get())) throw std::runtime_error("FileCommit 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"); } diff --git a/src/kernel/mempool_persist.h b/src/kernel/mempool_persist.h index e124a8eadf..cc5eaaac01 100644 --- a/src/kernel/mempool_persist.h +++ b/src/kernel/mempool_persist.h @@ -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, diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 9360ce0533..457f6a2154 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -738,7 +738,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()); } diff --git a/src/node/miner.cpp b/src/node/miner.cpp index 9d6cbabe64..8cc36d3469 100644 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -120,6 +120,9 @@ void BlockAssembler::resetBlock() // These counters do not include coinbase tx nBlockTx = 0; nFees = 0; + + lastFewTxs = 0; + blockFinished = false; } std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn) @@ -139,6 +142,10 @@ std::unique_ptr 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 + } LOCK(::cs_main); CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip(); @@ -159,6 +166,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc int nDescendantsUpdated = 0; if (m_mempool) { LOCK(m_mempool->cs); + addPriorityTxs(*m_mempool, nPackagesSelected); addPackageTxs(*m_mempool, nPackagesSelected, nDescendantsUpdated); } @@ -213,7 +221,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++; @@ -254,7 +262,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()); @@ -266,13 +274,18 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter) ++nBlockTx; nBlockSigOpsCost += iter->GetSigOpCost(); nFees += iter->GetFee(); - inBlock.insert(iter->GetSharedTx()->GetHash()); + inBlock.insert(iter); bool fPrintPriority = gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY); if (fPrintPriority) { - 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); } } @@ -335,8 +348,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 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::type::iterator mi = mempool.mapTx.get().begin(); CTxMemPool::txiter iter; @@ -363,7 +379,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele if (mi != mempool.mapTx.get().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; } @@ -397,7 +413,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(); @@ -419,7 +435,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().erase(modit); - failedTx.insert(iter->GetSharedTx()->GetHash()); + failedTx.insert(iter); } ++nConsecutiveFailed; @@ -441,7 +457,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele if (!TestPackageTransactions(ancestors)) { if (fUsingModified) { mapModifiedTx.get().erase(modit); - failedTx.insert(iter->GetSharedTx()->GetHash()); + failedTx.insert(iter); } if (fNeedSizeAccounting) { @@ -463,7 +479,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]); } diff --git a/src/node/miner.h b/src/node/miner.h index fe83dfd09b..954e6a93bf 100644 --- a/src/node/miner.h +++ b/src/node/miner.h @@ -37,6 +37,7 @@ struct CBlockTemplate CBlock block; std::vector vTxFees; std::vector vTxSigOpsCost; + std::vector vTxPriorities; std::vector vchCoinbaseCommitment; }; @@ -145,7 +146,7 @@ private: uint64_t nBlockTx; uint64_t nBlockSigOpsCost; CAmount nFees; - std::unordered_set inBlock; + CTxMemPool::setEntries inBlock; // Chain context for the block int nHeight; @@ -155,6 +156,10 @@ private: const CTxMemPool* const m_mempool; Chainstate& m_chainstate; + // Variables used for addPriorityTxs + int lastFewTxs; + bool blockFinished; + public: struct Options { // Configuration parameters for the block size @@ -182,14 +187,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); diff --git a/src/policy/coin_age_priority.cpp b/src/policy/coin_age_priority.cpp new file mode 100644 index 0000000000..162e0a234d --- /dev/null +++ b/src/policy/coin_age_priority.cpp @@ -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 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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::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 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 vecPriority; + TxCoinAgePriorityCompare pricomparer; + std::map waitPriMap; + typedef std::map::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; +} diff --git a/src/policy/coin_age_priority.h b/src/policy/coin_age_priority.h new file mode 100644 index 0000000000..e741039127 --- /dev/null +++ b/src/policy/coin_age_priority.h @@ -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 + +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 diff --git a/src/policy/policy.h b/src/policy/policy.h index 8cff480a45..11493df1c8 100644 --- a/src/policy/policy.h +++ b/src/policy/policy.h @@ -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 **/ diff --git a/src/rest.cpp b/src/rest.cpp index eb5f6382fd..87617b0514 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -730,7 +730,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 { diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 6c68f858ff..a1e9c90681 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -277,7 +277,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" }, diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp index bc9394f65b..687f631dac 100644 --- a/src/rpc/mempool.cpp +++ b/src/rpc/mempool.cpp @@ -306,6 +306,8 @@ static std::vector 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)"}, @@ -328,7 +330,7 @@ static std::vector 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); @@ -336,6 +338,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()); @@ -386,17 +390,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). @@ -599,7 +608,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); }, }; } @@ -635,7 +646,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) { @@ -656,7 +672,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; @@ -696,7 +712,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) { @@ -721,7 +742,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; @@ -748,7 +769,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) { @@ -756,7 +782,7 @@ static RPCHelpMan getmempoolentry() } UniValue info(UniValue::VOBJ); - entryToJSON(mempool, info, *entry); + entryToJSON(mempool, info, *entry, next_block_height); return info; }, }; diff --git a/src/rpc/mempool.h b/src/rpc/mempool.h index 6b76c0e97d..894999614e 100644 --- a/src/rpc/mempool.h +++ b/src/rpc/mempool.h @@ -10,6 +10,7 @@ #include #include +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& 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); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 27f0d47092..2e9922badc 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -464,9 +464,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."}, @@ -482,14 +483,17 @@ static RPCHelpMan prioritisetransaction() LOCK(cs_main); uint256 hash(ParseHashV(request.params[0], "txid")); - const auto dummy{self.MaybeArg(1)}; - CAmount nAmount = request.params[2].getInt(); + 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(); } - EnsureAnyMemPool(request.context).PrioritiseTransaction(hash, nAmount); + EnsureAnyMemPool(request.context).PrioritiseTransaction(hash, priority_delta, nAmount); return true; }, }; @@ -507,6 +511,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"}, }} }, }, @@ -526,6 +531,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; @@ -622,6 +628,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"}, }}, @@ -862,6 +869,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)); } diff --git a/src/test/fuzz/mini_miner.cpp b/src/test/fuzz/mini_miner.cpp index 84f9bb4ad0..767924dd89 100644 --- a/src/test/fuzz/mini_miner.cpp +++ b/src/test/fuzz/mini_miner.cpp @@ -24,6 +24,7 @@ void initialize_miner() { static const auto testing_setup = MakeNoLogFileContext(); 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); } diff --git a/src/test/fuzz/partially_downloaded_block.cpp b/src/test/fuzz/partially_downloaded_block.cpp index 4a4b46da60..7a81b02469 100644 --- a/src/test/fuzz/partially_downloaded_block.cpp +++ b/src/test/fuzz/partially_downloaded_block.cpp @@ -72,7 +72,7 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb) available.insert(i); } - if (add_to_mempool) { + if (add_to_mempool && SanityCheckForConsumeTxMemPoolEntry(*tx)) { LOCK2(cs_main, pool.cs); pool.addUnchecked(ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx)); available.insert(i); diff --git a/src/test/fuzz/policy_estimator.cpp b/src/test/fuzz/policy_estimator.cpp index 1a6c10af2b..2201e4a559 100644 --- a/src/test/fuzz/policy_estimator.cpp +++ b/src/test/fuzz/policy_estimator.cpp @@ -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 txs; diff --git a/src/test/fuzz/rbf.cpp b/src/test/fuzz/rbf.cpp index aa6385d12d..06a1611f97 100644 --- a/src/test/fuzz/rbf.cpp +++ b/src/test/fuzz/rbf.cpp @@ -37,6 +37,8 @@ FUZZ_TARGET(rbf, .init = initialize_rbf) if (!mtx) { return; } + const CTransaction tx{*mtx}; + if (!SanityCheckForConsumeTxMemPoolEntry(tx)) return; CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)}; @@ -47,13 +49,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)); diff --git a/src/test/fuzz/util/mempool.cpp b/src/test/fuzz/util/mempool.cpp index 8e7499a860..a648da19f2 100644 --- a/src/test/fuzz/util/mempool.cpp +++ b/src/test/fuzz/util/mempool.cpp @@ -14,6 +14,17 @@ #include #include +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(); const uint64_t entry_sequence{fuzzed_data_provider.ConsumeIntegral()}; - const unsigned int entry_height = fuzzed_data_provider.ConsumeIntegral(); + const double coin_age = fuzzed_data_provider.ConsumeFloatingPoint(); + const unsigned int entry_height = fuzzed_data_provider.ConsumeIntegralInRange(0, std::numeric_limits::max() - 1); const bool spends_coinbase = fuzzed_data_provider.ConsumeBool(); const unsigned int sig_op_cost = fuzzed_data_provider.ConsumeIntegralInRange(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, {}}; } diff --git a/src/test/fuzz/util/mempool.h b/src/test/fuzz/util/mempool.h index 31b578dc4b..740225a0ee 100644 --- a/src/test/fuzz/util/mempool.h +++ b/src/test/fuzz/util/mempool.h @@ -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 diff --git a/src/test/fuzz/validation_load_mempool.cpp b/src/test/fuzz/validation_load_mempool.cpp index 00678742c9..5120421d78 100644 --- a/src/test/fuzz/validation_load_mempool.cpp +++ b/src/test/fuzz/validation_load_mempool.cpp @@ -51,6 +51,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); diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 1f8dbac5be..08e63b5609 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -449,6 +450,8 @@ CMutableTransaction TestChain100Setup::CreateValidMempoolTransaction(CTransactio std::vector 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 mempool_transactions; std::deque> unspent_prevouts; std::transform(m_coinbase_txns.begin(), m_coinbase_txns.end(), std::back_inserter(unspent_prevouts), @@ -486,8 +489,12 @@ std::vector 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; @@ -518,6 +525,8 @@ void TestChain100Setup::MockMempoolMinFee(const CFeeRate& target_feerate) m_node.mempool->m_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); diff --git a/src/test/util/txmempool.cpp b/src/test/util/txmempool.cpp index 4ec2bec223..c79ca5036b 100644 --- a/src/test/util/txmempool.cpp +++ b/src/test/util/txmempool.cpp @@ -35,7 +35,9 @@ CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) co CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx) const { - return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch(time), nHeight, m_sequence, spendsCoinbase, sigOpCost, lp}; + constexpr double coin_age{0}; + const CAmount inChainValue = 0; + return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch(time), nHeight, m_sequence, /*entry_tx_inputs_coin_age=*/coin_age, inChainValue, spendsCoinbase, sigOpCost, lp}; } std::optional CheckPackageMempoolAcceptResult(const Package& txns, diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 238d1d85fd..54f7202ea4 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -434,8 +435,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) { @@ -633,6 +636,7 @@ void CTxMemPool::removeForBlock(const std::vector& 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; @@ -667,6 +671,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(); @@ -884,15 +897,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 &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) { @@ -907,27 +922,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::const_iterator pos = mapDeltas.find(hash); + std::map >::const_iterator pos = mapDeltas.find(hash); if (pos == mapDeltas.end()) return; - const CAmount &delta = pos->second; - nFeeDelta += delta; + const std::pair &deltas = pos->second; + dPriorityDelta += deltas.first; + nFeeDelta += deltas.second; } void CTxMemPool::ClearPrioritisation(const uint256& hash) @@ -947,7 +964,7 @@ std::vector CTxMemPool::GetPrioritisedTransactions() con const bool in_mempool{iter != mapTx.end()}; std::optional 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; } diff --git a/src/txmempool.h b/src/txmempool.h index 0103d30e6b..b3bea167bc 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -435,7 +435,7 @@ private: public: indirectmap mapNextTx GUARDED_BY(cs); - std::map mapDeltas GUARDED_BY(cs); + std::map > mapDeltas GUARDED_BY(cs); using Options = kernel::MemPoolOptions; @@ -498,10 +498,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 { @@ -509,6 +516,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 modified_fee; /** The prioritised transaction's txid. */ diff --git a/src/validation.cpp b/src/validation.cpp index 4571abd88f..d443e15f2c 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -861,7 +862,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. @@ -878,6 +884,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(); @@ -2955,6 +2963,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)) { diff --git a/test/functional/mining_coin_age_priority.py b/test/functional/mining_coin_age_priority.py new file mode 100755 index 0000000000..f4a935a4c3 --- /dev/null +++ b/test/functional/mining_coin_age_priority.py @@ -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().main() diff --git a/test/functional/mining_prioritisetransaction.py b/test/functional/mining_prioritisetransaction.py index c5f34e3ecb..1fdf0e1272 100755 --- a/test/functional/mining_prioritisetransaction.py +++ b/test/functional/mining_prioritisetransaction.py @@ -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') diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index a74c3b78e4..dde0d9eb37 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -346,6 +346,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: diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index fdc58f6d4f..e955db0a7d 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -197,6 +197,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', 'interface_rest.py',