Skip to content

Commit 6180638

Browse files
committed
Enable prioritisetransaction with priority delta
1 parent 678978f commit 6180638

File tree

9 files changed

+51
-30
lines changed

9 files changed

+51
-30
lines changed

src/node/mempool_persist.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
161161
{
162162
LOCK(pool.cs);
163163
for (const auto &i : pool.mapDeltas) {
164-
mapDeltas[i.first] = i.second;
164+
if (i.second.second) { // fee delta
165+
mapDeltas[i.first] = i.second.second;
166+
}
165167
}
166168
vinfo = pool.infoAll();
167169
unbroadcast_txids = pool.GetUnbroadcastTxs();

src/node/miner.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& packa
265265
return true;
266266
}
267267

268-
void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
268+
void BlockAssembler::AddToBlock(const CTxMemPool& mempool, CTxMemPool::txiter iter)
269269
{
270270
pblocktemplate->block.vtx.emplace_back(iter->GetSharedTx());
271271
pblocktemplate->vTxFees.push_back(iter->GetFee());
@@ -281,6 +281,8 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
281281

282282
if (m_options.print_modified_fee) {
283283
double dPriority = iter->GetPriority(nHeight);
284+
CAmount dummy;
285+
mempool.ApplyDeltas(iter->GetTx().GetHash(), dPriority, dummy);
284286
LogPrintf("priority %.1f fee rate %s txid %s\n",
285287
dPriority,
286288
CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
@@ -479,7 +481,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
479481
SortForBlock(ancestors, sortedEntries);
480482

481483
for (size_t i = 0; i < sortedEntries.size(); ++i) {
482-
AddToBlock(sortedEntries[i]);
484+
AddToBlock(mempool, sortedEntries[i]);
483485
// Erase from the modified set, if present
484486
mapModifiedTx.erase(sortedEntries[i]);
485487
}

src/node/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class BlockAssembler
192192
/** Clear the block's state and prepare for assembling a new block */
193193
void resetBlock();
194194
/** Add a tx to the block */
195-
void AddToBlock(CTxMemPool::txiter iter);
195+
void AddToBlock(const CTxMemPool& mempool, CTxMemPool::txiter iter) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
196196

197197
// Methods for how to add transactions to a block.
198198
/** Add transactions based on tx "priority" */

src/policy/coin_age_priority.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ void BlockAssembler::addPriorityTxs(const CTxMemPool& mempool, int &nPackagesSel
199199
vecPriority.reserve(mempool.mapTx.size());
200200
for (auto mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi) {
201201
double dPriority = mi->GetPriority(nHeight);
202+
CAmount dummy;
203+
mempool.ApplyDeltas(mi->GetTx().GetHash(), dPriority, dummy);
202204
vecPriority.emplace_back(dPriority, mi);
203205
}
204206
std::make_heap(vecPriority.begin(), vecPriority.end(), pricomparer);
@@ -225,7 +227,7 @@ void BlockAssembler::addPriorityTxs(const CTxMemPool& mempool, int &nPackagesSel
225227

226228
// If this tx fits in the block add it, otherwise keep looping
227229
if (TestForBlock(iter)) {
228-
AddToBlock(iter);
230+
AddToBlock(mempool, iter);
229231

230232
++nPackagesSelected;
231233

src/rpc/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
259259
{ "estimatesmartfee", 0, "conf_target" },
260260
{ "estimaterawfee", 0, "conf_target" },
261261
{ "estimaterawfee", 1, "threshold" },
262-
{ "prioritisetransaction", 1, "dummy" },
262+
{ "prioritisetransaction", 1, "priority_delta" },
263263
{ "prioritisetransaction", 2, "fee_delta" },
264264
{ "setban", 2, "bantime" },
265265
{ "setban", 3, "absolute" },

src/rpc/mining.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,10 @@ static RPCHelpMan prioritisetransaction()
469469
"Accepts the transaction into mined blocks at a higher (or lower) priority\n",
470470
{
471471
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id."},
472-
{"dummy", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "API-Compatibility for previous API. Must be zero or null.\n"
473-
" DEPRECATED. For forward compatibility use named arguments and omit this parameter."},
474-
{"fee_delta", RPCArg::Type::NUM, RPCArg::Optional::NO, "The fee value (in satoshis) to add (or subtract, if negative).\n"
472+
{"priority_delta", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The priority to add or subtract.\n"
473+
" The transaction selection algorithm considers the tx as it would have a higher priority.\n"
474+
" (priority of a transaction is calculated: coinage * value_in_satoshis / txsize)\n"},
475+
{"fee_delta", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The fee value (in satoshis) to add (or subtract, if negative).\n"
475476
" Note, that this value is not a fee rate. It is a value to modify absolute fee of the TX.\n"
476477
" The fee is not actually paid, only the algorithm for selecting transactions into a block\n"
477478
" considers the transaction as it would have paid a higher (or lower) fee."},
@@ -487,14 +488,17 @@ static RPCHelpMan prioritisetransaction()
487488
LOCK(cs_main);
488489

489490
uint256 hash(ParseHashV(request.params[0], "txid"));
490-
const auto dummy{self.MaybeArg<double>("dummy")};
491-
CAmount nAmount = request.params[2].getInt<int64_t>();
491+
double priority_delta = 0;
492+
CAmount nAmount = 0;
492493

493-
if (dummy && *dummy != 0) {
494-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.");
494+
if (!request.params[1].isNull()) {
495+
priority_delta = request.params[1].get_real();
496+
}
497+
if (!request.params[2].isNull()) {
498+
nAmount = request.params[2].getInt<int64_t>();
495499
}
496500

497-
EnsureAnyMemPool(request.context).PrioritiseTransaction(hash, nAmount);
501+
EnsureAnyMemPool(request.context).PrioritiseTransaction(hash, priority_delta, nAmount);
498502
return true;
499503
},
500504
};
@@ -869,6 +873,9 @@ static RPCHelpMan getblocktemplate()
869873
}
870874
entry.pushKV("sigops", nTxSigOps);
871875
entry.pushKV("weight", GetTransactionWeight(tx));
876+
if (index_in_template && !pblocktemplate->vTxPriorities.empty()) {
877+
entry.pushKV("priority", pblocktemplate->vTxPriorities[index_in_template]);
878+
}
872879

873880
transactions.push_back(std::move(entry));
874881
}

src/txmempool.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,10 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
438438
indexed_transaction_set::iterator newit = mapTx.emplace(CTxMemPoolEntry::ExplicitCopy, entry).first;
439439

440440
// Update transaction for any feeDelta created by PrioritiseTransaction
441+
double priority_delta{0.};
441442
CAmount delta{0};
442-
ApplyDelta(entry.GetTx().GetHash(), delta);
443+
ApplyDeltas(entry.GetTx().GetHash(), priority_delta, delta);
444+
// NOTE: priority_delta is handled in addPriorityTxs
443445
// The following call to UpdateModifiedFee assumes no previous fee modifications
444446
Assume(entry.GetFee() == entry.GetModifiedFee());
445447
if (delta) {
@@ -883,15 +885,17 @@ TxMempoolInfo CTxMemPool::info_for_relay(const GenTxid& gtxid, uint64_t last_seq
883885
}
884886
}
885887

886-
void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta)
888+
void CTxMemPool::PrioritiseTransaction(const uint256& hash, double dPriorityDelta, const CAmount& nFeeDelta)
887889
{
888890
{
889891
LOCK(cs);
890-
CAmount &delta = mapDeltas[hash];
891-
delta = SaturatingAdd(delta, nFeeDelta);
892+
std::pair<double, CAmount> &deltas = mapDeltas[hash];
893+
deltas.first += dPriorityDelta;
894+
deltas.second = SaturatingAdd(deltas.second, nFeeDelta);
892895
txiter it = mapTx.find(hash);
893896
if (it != mapTx.end()) {
894897
mapTx.modify(it, [&nFeeDelta](CTxMemPoolEntry& e) { e.UpdateModifiedFee(nFeeDelta); });
898+
895899
// Now update all ancestors' modified fees with descendants
896900
auto ancestors{AssumeCalculateMemPoolAncestors(__func__, *it, Limits::NoLimits(), /*fSearchForParents=*/false)};
897901
for (txiter ancestorIt : ancestors) {
@@ -906,27 +910,29 @@ void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeD
906910
}
907911
++nTransactionsUpdated;
908912
}
909-
if (delta == 0) {
913+
if (deltas.first == 0. && deltas.second == 0) {
910914
mapDeltas.erase(hash);
911915
LogPrintf("PrioritiseTransaction: %s (%sin mempool) delta cleared\n", hash.ToString(), it == mapTx.end() ? "not " : "");
912916
} else {
913-
LogPrintf("PrioritiseTransaction: %s (%sin mempool) fee += %s, new delta=%s\n",
917+
LogPrintf("PrioritiseTransaction: %s (%sin mempool) priority += %f, fee += %s, new delta=%s\n",
914918
hash.ToString(),
915919
it == mapTx.end() ? "not " : "",
920+
dPriorityDelta,
916921
FormatMoney(nFeeDelta),
917-
FormatMoney(delta));
922+
FormatMoney(deltas.second));
918923
}
919924
}
920925
}
921926

922-
void CTxMemPool::ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const
927+
void CTxMemPool::ApplyDeltas(const uint256& hash, double &dPriorityDelta, CAmount &nFeeDelta) const
923928
{
924929
AssertLockHeld(cs);
925-
std::map<uint256, CAmount>::const_iterator pos = mapDeltas.find(hash);
930+
std::map<uint256, std::pair<double, CAmount> >::const_iterator pos = mapDeltas.find(hash);
926931
if (pos == mapDeltas.end())
927932
return;
928-
const CAmount &delta = pos->second;
929-
nFeeDelta += delta;
933+
const std::pair<double, CAmount> &deltas = pos->second;
934+
dPriorityDelta += deltas.first;
935+
nFeeDelta += deltas.second;
930936
}
931937

932938
void CTxMemPool::ClearPrioritisation(const uint256& hash)
@@ -946,7 +952,7 @@ std::vector<CTxMemPool::delta_info> CTxMemPool::GetPrioritisedTransactions() con
946952
const bool in_mempool{iter != mapTx.end()};
947953
std::optional<CAmount> modified_fee;
948954
if (in_mempool) modified_fee = iter->GetModifiedFee();
949-
result.emplace_back(delta_info{in_mempool, delta, modified_fee, txid});
955+
result.emplace_back(delta_info{in_mempool, delta.second, modified_fee, txid});
950956
}
951957
return result;
952958
}

src/txmempool.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class CTxMemPool
432432

433433
public:
434434
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
435-
std::map<uint256, CAmount> mapDeltas GUARDED_BY(cs);
435+
std::map<uint256, std::pair<double, CAmount> > mapDeltas GUARDED_BY(cs);
436436

437437
using Options = kernel::MemPoolOptions;
438438

@@ -492,8 +492,9 @@ class CTxMemPool
492492
void UpdateDependentPriorities(const CTransaction &tx, unsigned int nBlockHeight, bool addToChain);
493493

494494
/** Affect CreateNewBlock prioritisation of transactions */
495-
void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
496-
void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
495+
void PrioritiseTransaction(const uint256& hash, double dPriorityDelta, const CAmount& nFeeDelta);
496+
void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta) { PrioritiseTransaction(hash, 0., nFeeDelta); }
497+
void ApplyDeltas(const uint256& hash, double &dPriorityDelta, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
497498
void ClearPrioritisation(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs);
498499

499500
struct delta_info {

src/validation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,8 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
933933

934934
// ws.m_modified_fees includes any fee deltas from PrioritiseTransaction
935935
ws.m_modified_fees = ws.m_base_fees;
936-
m_pool.ApplyDelta(hash, ws.m_modified_fees);
936+
double nPriorityDummy{0};
937+
m_pool.ApplyDeltas(hash, nPriorityDummy, ws.m_modified_fees);
937938

938939
CAmount inChainInputValue;
939940
// Since entries arrive *after* the tip's height, their priority is for the height+1

0 commit comments

Comments
 (0)