Skip to content

Commit ade8bad

Browse files
committed
RPC: Restore "startingpriority" and "currentpriority" in mempool entries
1 parent 7a0133b commit ade8bad

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

src/bench/rpc_mempool.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <test/util/setup_common.h>
1010
#include <txmempool.h>
1111
#include <util/chaintype.h>
12+
#include <validation.h>
1213

1314
#include <univalue.h>
1415

@@ -21,7 +22,8 @@ static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& poo
2122

2223
static void RpcMempool(benchmark::Bench& bench)
2324
{
24-
const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
25+
const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN);
26+
auto& chainman = *testing_setup->m_node.chainman;
2527
CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
2628
LOCK2(cs_main, pool.cs);
2729

@@ -38,7 +40,7 @@ static void RpcMempool(benchmark::Bench& bench)
3840
}
3941

4042
bench.run([&] {
41-
(void)MempoolToJSON(pool, /*verbose=*/true);
43+
(void)MempoolToJSON(chainman, pool, /*verbose=*/true);
4244
});
4345
}
4446

src/rest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,10 @@ static bool rest_mempool(const std::any& context, HTTPRequest* req, const std::s
685685
if (verbose && mempool_sequence) {
686686
return RESTERR(req, HTTP_BAD_REQUEST, "Verbose results cannot contain mempool sequence values. (hint: set \"verbose=false\")");
687687
}
688-
str_json = MempoolToJSON(*mempool, verbose, mempool_sequence).write() + "\n";
688+
ChainstateManager* maybe_chainman = GetChainman(context, req);
689+
if (!maybe_chainman) return false;
690+
ChainstateManager& chainman = *maybe_chainman;
691+
str_json = MempoolToJSON(chainman, *mempool, verbose, mempool_sequence).write() + "\n";
689692
} else if (param == "info/with_fee_histogram") {
690693
str_json = MempoolInfoToJSON(*mempool, MempoolInfoToJSON_const_histogram_floors).write() + "\n";
691694
} else {

src/rpc/mempool.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ static std::vector<RPCResult> MempoolEntryDescription()
305305
RPCResult{RPCResult::Type::NUM, "weight", "transaction weight as defined in BIP 141."},
306306
RPCResult{RPCResult::Type::NUM_TIME, "time", "local time transaction entered pool in seconds since 1 Jan 1970 GMT"},
307307
RPCResult{RPCResult::Type::NUM, "height", "block height when transaction entered pool"},
308+
RPCResult{RPCResult::Type::NUM, "startingpriority", "Priority when transaction entered pool"},
309+
RPCResult{RPCResult::Type::NUM, "currentpriority", "Transaction priority now"},
308310
RPCResult{RPCResult::Type::NUM, "descendantcount", "number of in-mempool descendant transactions (including this one)"},
309311
RPCResult{RPCResult::Type::NUM, "descendantsize", "virtual transaction size of in-mempool descendants (including this one)"},
310312
RPCResult{RPCResult::Type::NUM, "ancestorcount", "number of in-mempool ancestor transactions (including this one)"},
@@ -326,14 +328,16 @@ static std::vector<RPCResult> MempoolEntryDescription()
326328
};
327329
}
328330

329-
static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
331+
static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e, const int next_block_height) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
330332
{
331333
AssertLockHeld(pool.cs);
332334

333335
info.pushKV("vsize", (int)e.GetTxSize());
334336
info.pushKV("weight", (int)e.GetTxWeight());
335337
info.pushKV("time", count_seconds(e.GetTime()));
336338
info.pushKV("height", (int)e.GetHeight());
339+
info.pushKV("startingpriority", e.GetStartingPriority());
340+
info.pushKV("currentpriority", e.GetPriority(next_block_height));
337341
info.pushKV("descendantcount", e.GetCountWithDescendants());
338342
info.pushKV("descendantsize", e.GetSizeWithDescendants());
339343
info.pushKV("ancestorcount", e.GetCountWithAncestors());
@@ -383,17 +387,22 @@ static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPool
383387
info.pushKV("unbroadcast", pool.IsUnbroadcastTx(tx.GetHash()));
384388
}
385389

386-
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
390+
UniValue MempoolToJSON(ChainstateManager &chainman, const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
387391
{
388392
if (verbose) {
389393
if (include_mempool_sequence) {
390394
throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
391395
}
396+
LOCK(::cs_main);
397+
const CChain& active_chain = chainman.ActiveChain();
398+
const int next_block_height = active_chain.Height() + 1;
392399
LOCK(pool.cs);
400+
// TODO: Release cs_main after mempool.cs acquired
401+
393402
UniValue o(UniValue::VOBJ);
394403
for (const CTxMemPoolEntry& e : pool.entryAll()) {
395404
UniValue info(UniValue::VOBJ);
396-
entryToJSON(pool, info, e);
405+
entryToJSON(pool, info, e, next_block_height);
397406
// Mempool has unique entries so there is no advantage in using
398407
// UniValue::pushKV, which checks if the key already exists in O(N).
399408
// UniValue::pushKVEnd is used instead which currently is O(1).
@@ -466,7 +475,9 @@ static RPCHelpMan getrawmempool()
466475
include_mempool_sequence = request.params[1].get_bool();
467476
}
468477

469-
return MempoolToJSON(EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
478+
NodeContext& node = EnsureAnyNodeContext(request.context);
479+
ChainstateManager& chainman = EnsureChainman(node);
480+
return MempoolToJSON(chainman, EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
470481
},
471482
};
472483
}
@@ -502,7 +513,12 @@ static RPCHelpMan getmempoolancestors()
502513
uint256 hash = ParseHashV(request.params[0], "parameter 1");
503514

504515
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
516+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
517+
LOCK(::cs_main);
518+
const CChain& active_chain = chainman.ActiveChain();
519+
const int next_block_height = active_chain.Height() + 1;
505520
LOCK(mempool.cs);
521+
// TODO: Release cs_main after mempool.cs acquired
506522

507523
const auto entry{mempool.GetEntry(Txid::FromUint256(hash))};
508524
if (entry == nullptr) {
@@ -523,7 +539,7 @@ static RPCHelpMan getmempoolancestors()
523539
const CTxMemPoolEntry &e = *ancestorIt;
524540
const uint256& _hash = e.GetTx().GetHash();
525541
UniValue info(UniValue::VOBJ);
526-
entryToJSON(mempool, info, e);
542+
entryToJSON(mempool, info, e, next_block_height);
527543
o.pushKV(_hash.ToString(), std::move(info));
528544
}
529545
return o;
@@ -563,7 +579,12 @@ static RPCHelpMan getmempooldescendants()
563579
uint256 hash = ParseHashV(request.params[0], "parameter 1");
564580

565581
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
582+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
583+
LOCK(::cs_main);
584+
const CChain& active_chain = chainman.ActiveChain();
585+
const int next_block_height = active_chain.Height() + 1;
566586
LOCK(mempool.cs);
587+
// TODO: Release cs_main after mempool.cs acquired
567588

568589
const auto it{mempool.GetIter(hash)};
569590
if (!it) {
@@ -588,7 +609,7 @@ static RPCHelpMan getmempooldescendants()
588609
const CTxMemPoolEntry &e = *descendantIt;
589610
const uint256& _hash = e.GetTx().GetHash();
590611
UniValue info(UniValue::VOBJ);
591-
entryToJSON(mempool, info, e);
612+
entryToJSON(mempool, info, e, next_block_height);
592613
o.pushKV(_hash.ToString(), std::move(info));
593614
}
594615
return o;
@@ -615,15 +636,20 @@ static RPCHelpMan getmempoolentry()
615636
uint256 hash = ParseHashV(request.params[0], "parameter 1");
616637

617638
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
639+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
640+
LOCK(::cs_main);
641+
const CChain& active_chain = chainman.ActiveChain();
642+
const int next_block_height = active_chain.Height() + 1;
618643
LOCK(mempool.cs);
644+
// TODO: Release cs_main after mempool.cs acquired
619645

620646
const auto entry{mempool.GetEntry(Txid::FromUint256(hash))};
621647
if (entry == nullptr) {
622648
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
623649
}
624650

625651
UniValue info(UniValue::VOBJ);
626-
entryToJSON(mempool, info, *entry);
652+
entryToJSON(mempool, info, *entry, next_block_height);
627653
return info;
628654
},
629655
};

src/rpc/mempool.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <optional>
1111
#include <vector>
1212

13+
class ChainstateManager;
1314
class CTxMemPool;
1415
class UniValue;
1516

@@ -28,6 +29,6 @@ static const MempoolHistogramFeeRates MempoolInfoToJSON_const_histogram_floors{
2829
UniValue MempoolInfoToJSON(const CTxMemPool& pool, const std::optional<MempoolHistogramFeeRates>& histogram_floors);
2930

3031
/** Mempool to JSON */
31-
UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false);
32+
UniValue MempoolToJSON(ChainstateManager& chainman, const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false);
3233

3334
#endif // BITCOIN_RPC_MEMPOOL_H

0 commit comments

Comments
 (0)