Skip to content

Commit 0dfd869

Browse files
committed
Add getmempooldescendants RPC call
1 parent 8f7b5dc commit 0dfd869

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/rpc/blockchain.cpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ UniValue getmempoolancestors(const UniValue& params, bool fHelp)
302302
"1. \"txid\" (string, required) The transaction id (must be in mempool)\n"
303303
"2. verbose (boolean, optional, default=false) true for a json object, false for array of transaction ids\n"
304304
"\nResult (for verbose=false):\n"
305-
"[ (json array of string)\n"
305+
"[ (json array of strings)\n"
306306
" \"transactionid\" (string) The transaction id of an in-mempool ancestor transaction\n"
307307
" ,...\n"
308308
"]\n"
@@ -356,6 +356,70 @@ UniValue getmempoolancestors(const UniValue& params, bool fHelp)
356356
}
357357
}
358358

359+
UniValue getmempooldescendants(const UniValue& params, bool fHelp)
360+
{
361+
if (fHelp || params.size() < 1 || params.size() > 2) {
362+
throw runtime_error(
363+
"getmempooldescendants txid (verbose)\n"
364+
"\nIf txid is in the mempool, returns all in-mempool descendants.\n"
365+
"\nArguments:\n"
366+
"1. \"txid\" (string, required) The transaction id (must be in mempool)\n"
367+
"2. verbose (boolean, optional, default=false) true for a json object, false for array of transaction ids\n"
368+
"\nResult (for verbose=false):\n"
369+
"[ (json array of strings)\n"
370+
" \"transactionid\" (string) The transaction id of an in-mempool descendant transaction\n"
371+
" ,...\n"
372+
"]\n"
373+
"\nResult (for verbose=true):\n"
374+
"{ (json object)\n"
375+
" \"transactionid\" : { (json object)\n"
376+
+ EntryDescriptionString()
377+
+ " }, ...\n"
378+
"}\n"
379+
"\nExamples\n"
380+
+ HelpExampleCli("getmempooldescendants", "\"mytxid\"")
381+
+ HelpExampleRpc("getmempooldescendants", "\"mytxid\"")
382+
);
383+
}
384+
385+
bool fVerbose = false;
386+
if (params.size() > 1)
387+
fVerbose = params[1].get_bool();
388+
389+
uint256 hash = ParseHashV(params[0], "parameter 1");
390+
391+
LOCK(mempool.cs);
392+
393+
CTxMemPool::txiter it = mempool.mapTx.find(hash);
394+
if (it == mempool.mapTx.end()) {
395+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
396+
}
397+
398+
CTxMemPool::setEntries setDescendants;
399+
mempool.CalculateDescendants(it, setDescendants);
400+
// CTxMemPool::CalculateDescendants will include the given tx
401+
setDescendants.erase(it);
402+
403+
if (!fVerbose) {
404+
UniValue o(UniValue::VARR);
405+
BOOST_FOREACH(CTxMemPool::txiter descendantIt, setDescendants) {
406+
o.push_back(descendantIt->GetTx().GetHash().ToString());
407+
}
408+
409+
return o;
410+
} else {
411+
UniValue o(UniValue::VOBJ);
412+
BOOST_FOREACH(CTxMemPool::txiter descendantIt, setDescendants) {
413+
const CTxMemPoolEntry &e = *descendantIt;
414+
const uint256& hash = e.GetTx().GetHash();
415+
UniValue info(UniValue::VOBJ);
416+
entryToJSON(info, e);
417+
o.push_back(Pair(hash.ToString(), info));
418+
}
419+
return o;
420+
}
421+
}
422+
359423
UniValue getblockhash(const UniValue& params, bool fHelp)
360424
{
361425
if (fHelp || params.size() != 1)
@@ -1081,6 +1145,7 @@ static const CRPCCommand commands[] =
10811145
{ "blockchain", "getchaintips", &getchaintips, true },
10821146
{ "blockchain", "getdifficulty", &getdifficulty, true },
10831147
{ "blockchain", "getmempoolancestors", &getmempoolancestors, true },
1148+
{ "blockchain", "getmempooldescendants", &getmempooldescendants, true },
10841149
{ "blockchain", "getmempoolinfo", &getmempoolinfo, true },
10851150
{ "blockchain", "getrawmempool", &getrawmempool, true },
10861151
{ "blockchain", "gettxout", &gettxout, true },

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
103103
{ "setban", 2 },
104104
{ "setban", 3 },
105105
{ "getmempoolancestors", 1 },
106+
{ "getmempooldescendants", 1 },
106107
};
107108

108109
class CRPCConvertTable

0 commit comments

Comments
 (0)