Skip to content

Commit 67b7fec

Browse files
committed
[mempool] clear mapDeltas entry if prioritisetransaction sets delta to 0
It's unnecessary to keep the data around, as it doesn't do anything. If prioritisetransaction is called again, we'll make a new entry in mapDeltas. These entries are only deleted when the transaction is mined or conflicted from a block (i.e. not in replacement or eviction), are persisted in mempool.dat, and never expire. If node operators use the RPC to regularly prioritise/de-prioritise transactions, these (meaningless) map entries may hang around forever and take up valuable mempool memory.
1 parent c1061ac commit 67b7fec

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/txmempool.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,17 @@ void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeD
870870
}
871871
++nTransactionsUpdated;
872872
}
873+
if (delta == 0) {
874+
mapDeltas.erase(hash);
875+
LogPrintf("PrioritiseTransaction: %s (%sin mempool) delta cleared\n", hash.ToString(), it == mapTx.end() ? "not " : "");
876+
} else {
877+
LogPrintf("PrioritiseTransaction: %s (%sin mempool) fee += %s, new delta=%s\n",
878+
hash.ToString(),
879+
it == mapTx.end() ? "not " : "",
880+
FormatMoney(nFeeDelta),
881+
FormatMoney(delta));
882+
}
873883
}
874-
LogPrintf("PrioritiseTransaction: %s fee += %s\n", hash.ToString(), FormatMoney(nFeeDelta));
875884
}
876885

877886
void CTxMemPool::ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const

test/functional/mining_prioritisetransaction.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def set_test_params(self):
3030
]] * self.num_nodes
3131
self.supports_cli = False
3232

33+
def clear_prioritisation(self, node):
34+
for txid, info in node.getprioritisedtransactions().items():
35+
delta = info["fee_delta"]
36+
node.prioritisetransaction(txid, 0, -delta)
37+
assert_equal(node.getprioritisedtransactions(), {})
38+
3339
def test_replacement(self):
3440
self.log.info("Test tx prioritisation stays after a tx is replaced")
3541
conflicting_input = self.wallet.get_utxo()
@@ -108,6 +114,10 @@ def test_diamond(self):
108114
prioritisation_map_in_mempool = self.nodes[0].getprioritisedtransactions()
109115
assert_equal(prioritisation_map_in_mempool[txid_b], {"fee_delta" : fee_delta_b*COIN, "in_mempool" : True})
110116
assert_equal(prioritisation_map_in_mempool[txid_c], {"fee_delta" : (fee_delta_c_1 + fee_delta_c_2)*COIN, "in_mempool" : True})
117+
# Clear prioritisation, otherwise the transactions' fee deltas are persisted to mempool.dat and loaded again when the node
118+
# is restarted at the end of this subtest. Deltas are removed when a transaction is mined, but only at that time. We do
119+
# not check whether mapDeltas transactions were mined when loading from mempool.dat.
120+
self.clear_prioritisation(node=self.nodes[0])
111121

112122
self.log.info("Test priority while txs are not in mempool")
113123
self.restart_node(0, extra_args=["-nopersistmempool"])
@@ -135,6 +145,7 @@ def test_diamond(self):
135145

136146
# Use default extra_args
137147
self.restart_node(0)
148+
assert_equal(self.nodes[0].getprioritisedtransactions(), {})
138149

139150
def run_test(self):
140151
self.wallet = MiniWallet(self.nodes[0])
@@ -202,10 +213,18 @@ def run_test(self):
202213
sizes[i] += mempool[j]['vsize']
203214
assert sizes[i] > MAX_BLOCK_WEIGHT // 4 # Fail => raise utxo_count
204215

216+
assert_equal(self.nodes[0].getprioritisedtransactions(), {})
205217
# add a fee delta to something in the cheapest bucket and make sure it gets mined
206218
# also check that a different entry in the cheapest bucket is NOT mined
207219
self.nodes[0].prioritisetransaction(txid=txids[0][0], fee_delta=int(3*base_fee*COIN))
208-
assert_equal(self.nodes[0].getprioritisedtransactions()[txids[0][0]], { "fee_delta" : 3*base_fee*COIN, "in_mempool" : True})
220+
assert_equal(self.nodes[0].getprioritisedtransactions(), {txids[0][0] : { "fee_delta" : 3*base_fee*COIN, "in_mempool" : True}})
221+
222+
# Priority disappears when prioritisetransaction is called with an inverse value...
223+
self.nodes[0].prioritisetransaction(txid=txids[0][0], fee_delta=int(-3*base_fee*COIN))
224+
assert txids[0][0] not in self.nodes[0].getprioritisedtransactions()
225+
# ... and reappears when prioritisetransaction is called again.
226+
self.nodes[0].prioritisetransaction(txid=txids[0][0], fee_delta=int(3*base_fee*COIN))
227+
assert txids[0][0] in self.nodes[0].getprioritisedtransactions()
209228

210229
self.generate(self.nodes[0], 1)
211230

@@ -277,8 +296,8 @@ def run_test(self):
277296
template = self.nodes[0].getblocktemplate({'rules': ['segwit']})
278297
self.nodes[0].prioritisetransaction(txid=tx_id, fee_delta=-int(self.relayfee*COIN))
279298

280-
assert tx_id in self.nodes[0].getprioritisedtransactions()
281-
assert_equal(self.nodes[0].getprioritisedtransactions()[tx_id]["fee_delta"], 0)
299+
# Calling prioritisetransaction with the inverse amount should delete its prioritisation entry
300+
assert tx_id not in self.nodes[0].getprioritisedtransactions()
282301

283302
self.nodes[0].setmocktime(mock_time+10)
284303
new_template = self.nodes[0].getblocktemplate({'rules': ['segwit']})

0 commit comments

Comments
 (0)