Skip to content

Commit cd5e438

Browse files
committed
Merge #12479: RPC: Add child transactions to getrawmempool verbose output
1dfb4e7 [Tests] Check output of parent/child tx list from getrawmempool, getmempooldescendants, getmempoolancestors, and REST interface (Conor Scott) fc44cb1 [RPC] Add list of child transactions to verbose output of getrawmempool (Conor Scott) Pull request description: `bitcoin-cli getrawmempool true` only lists a transaction's parents in the `depends` field. This change adds a `spentby` field to the json response, which lists the transaction's children in the mempool. Currently the only way to find child transactions is to use `getrawmempool` or make another call to `getmempooldescendants` and search the response for transactions that list the parent_txid in the `depends` list, which is inefficient. This change allows direct lookup of children. Example Output ``` "9a9b5733c0d89f207908cfa3fe17809bee71f629aa095c9f8754524e29e98ba4": { ...other geterawmempool data... "wtxid": "9a9b5733c0d89f207908cfa3fe17809bee71f629aa095c9f8754524e29e98ba4", "depends": [ "bdd92851d5766a42aeb62af667bb422a116cab4e032bba5e3dd6efe5b4b40aa0" ], "spentby": [ "dc5d3ec388a9121421208738a041ac30a22163bc2e17758f2275b6c51a15ba7b" ] }, ``` Tree-SHA512: 83da7d421c9799a40ef65af3b7fdb586d6d87385f3f2ede3afd2c311725444b858f9d91cc110422a0fa31905779934fee07211ca6fe6b746792b83692c94b3ce
2 parents 12ac2f0 + 1dfb4e7 commit cd5e438

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

src/rpc/blockchain.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ std::string EntryDescriptionString()
372372
" \"wtxid\" : hash, (string) hash of serialized transaction, including witness data\n"
373373
" \"depends\" : [ (array) unconfirmed transactions used as inputs for this transaction\n"
374374
" \"transactionid\", (string) parent transaction id\n"
375+
" ... ]\n"
376+
" \"spentby\" : [ (array) unconfirmed transactions spending outputs from this transaction\n"
377+
" \"transactionid\", (string) child transaction id\n"
375378
" ... ]\n";
376379
}
377380

@@ -406,6 +409,15 @@ void entryToJSON(UniValue &info, const CTxMemPoolEntry &e)
406409
}
407410

408411
info.pushKV("depends", depends);
412+
413+
UniValue spent(UniValue::VARR);
414+
const CTxMemPool::txiter &it = mempool.mapTx.find(tx.GetHash());
415+
const CTxMemPool::setEntries &setChildren = mempool.GetMemPoolChildren(it);
416+
for (const CTxMemPool::txiter &childiter : setChildren) {
417+
spent.push_back(childiter->GetTx().GetHash().ToString());
418+
}
419+
420+
info.pushKV("spentby", spent);
409421
}
410422

411423
UniValue mempoolToJSON(bool fVerbose)

test/functional/interface_rest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,10 @@ def run_test(self):
296296
# check that there are our submitted transactions in the TX memory pool
297297
json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents'+self.FORMAT_SEPARATOR+'json')
298298
json_obj = json.loads(json_string)
299-
for tx in txs:
299+
for i, tx in enumerate(txs):
300300
assert_equal(tx in json_obj, True)
301+
assert_equal(json_obj[tx]['spentby'], txs[i+1:i+2])
302+
assert_equal(json_obj[tx]['depends'], txs[i-1:i])
301303

302304
# now mine the transactions
303305
newblockhash = self.nodes[1].generate(1)

test/functional/mempool_packages.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ def run_test(self):
4747
value = sent_value
4848
chain.append(txid)
4949

50-
# Check mempool has MAX_ANCESTORS transactions in it, and descendant
50+
# Check mempool has MAX_ANCESTORS transactions in it, and descendant and ancestor
5151
# count and fees should look correct
5252
mempool = self.nodes[0].getrawmempool(True)
5353
assert_equal(len(mempool), MAX_ANCESTORS)
5454
descendant_count = 1
5555
descendant_fees = 0
5656
descendant_size = 0
5757

58+
ancestor_size = sum([mempool[tx]['size'] for tx in mempool])
59+
ancestor_count = MAX_ANCESTORS
60+
ancestor_fees = sum([mempool[tx]['fee'] for tx in mempool])
61+
5862
descendants = []
5963
ancestors = list(chain)
6064
for x in reversed(chain):
@@ -71,14 +75,43 @@ def run_test(self):
7175
assert_equal(mempool[x]['descendantsize'], descendant_size)
7276
descendant_count += 1
7377

78+
# Check that ancestor calculations are correct
79+
assert_equal(mempool[x]['ancestorcount'], ancestor_count)
80+
assert_equal(mempool[x]['ancestorfees'], ancestor_fees * COIN)
81+
assert_equal(mempool[x]['ancestorsize'], ancestor_size)
82+
ancestor_size -= mempool[x]['size']
83+
ancestor_fees -= mempool[x]['fee']
84+
ancestor_count -= 1
85+
86+
# Check that parent/child list is correct
87+
assert_equal(mempool[x]['spentby'], descendants[-1:])
88+
assert_equal(mempool[x]['depends'], ancestors[-2:-1])
89+
7490
# Check that getmempooldescendants is correct
7591
assert_equal(sorted(descendants), sorted(self.nodes[0].getmempooldescendants(x)))
92+
93+
# Check getmempooldescendants verbose output is correct
94+
for descendant, dinfo in self.nodes[0].getmempooldescendants(x, True).items():
95+
assert_equal(dinfo['depends'], [chain[chain.index(descendant)-1]])
96+
if dinfo['descendantcount'] > 1:
97+
assert_equal(dinfo['spentby'], [chain[chain.index(descendant)+1]])
98+
else:
99+
assert_equal(dinfo['spentby'], [])
76100
descendants.append(x)
77101

78102
# Check that getmempoolancestors is correct
79103
ancestors.remove(x)
80104
assert_equal(sorted(ancestors), sorted(self.nodes[0].getmempoolancestors(x)))
81105

106+
# Check that getmempoolancestors verbose output is correct
107+
for ancestor, ainfo in self.nodes[0].getmempoolancestors(x, True).items():
108+
assert_equal(ainfo['spentby'], [chain[chain.index(ancestor)+1]])
109+
if ainfo['ancestorcount'] > 1:
110+
assert_equal(ainfo['depends'], [chain[chain.index(ancestor)-1]])
111+
else:
112+
assert_equal(ainfo['depends'], [])
113+
114+
82115
# Check that getmempoolancestors/getmempooldescendants correctly handle verbose=true
83116
v_ancestors = self.nodes[0].getmempoolancestors(chain[-1], True)
84117
assert_equal(len(v_ancestors), len(chain)-1)
@@ -100,7 +133,7 @@ def run_test(self):
100133
for x in chain:
101134
ancestor_fees += mempool[x]['fee']
102135
assert_equal(mempool[x]['ancestorfees'], ancestor_fees * COIN + 1000)
103-
136+
104137
# Undo the prioritisetransaction for later tests
105138
self.nodes[0].prioritisetransaction(txid=chain[0], fee_delta=-1000)
106139

@@ -149,6 +182,7 @@ def run_test(self):
149182
vout = utxo[1]['vout']
150183

151184
transaction_package = []
185+
tx_children = []
152186
# First create one parent tx with 10 children
153187
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 10)
154188
parent_transaction = txid
@@ -159,11 +193,17 @@ def run_test(self):
159193
for i in range(MAX_DESCENDANTS - 1):
160194
utxo = transaction_package.pop(0)
161195
(txid, sent_value) = self.chain_transaction(self.nodes[0], utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
196+
if utxo['txid'] is parent_transaction:
197+
tx_children.append(txid)
162198
for j in range(10):
163199
transaction_package.append({'txid': txid, 'vout': j, 'amount': sent_value})
164200

165201
mempool = self.nodes[0].getrawmempool(True)
166202
assert_equal(mempool[parent_transaction]['descendantcount'], MAX_DESCENDANTS)
203+
assert_equal(sorted(mempool[parent_transaction]['spentby']), sorted(tx_children))
204+
205+
for child in tx_children:
206+
assert_equal(mempool[child]['depends'], [parent_transaction])
167207

168208
# Sending one more chained transaction will fail
169209
utxo = transaction_package.pop(0)
@@ -232,7 +272,7 @@ def run_test(self):
232272
signedtx = self.nodes[0].signrawtransactionwithwallet(rawtx)
233273
txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
234274
sync_mempools(self.nodes)
235-
275+
236276
# Now try to disconnect the tip on each node...
237277
self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
238278
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())

0 commit comments

Comments
 (0)