Skip to content

Commit

Permalink
wallet, rpc: show mempool conflicts in gettransaction result
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaanam committed Mar 20, 2024
1 parent 54e07ee commit 5952292
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/wallet/rpc/transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ static void WalletTxToJSON(const CWallet& wallet, const CWalletTx& wtx, UniValue
for (const uint256& conflict : wallet.GetTxConflicts(wtx))
conflicts.push_back(conflict.GetHex());
entry.pushKV("walletconflicts", conflicts);
UniValue mempool_conflicts(UniValue::VARR);
for (const Txid& mempool_conflict : wtx.mempool_conflicts)
mempool_conflicts.push_back(mempool_conflict.GetHex());
entry.pushKV("mempoolconflicts", mempool_conflicts);
entry.pushKV("time", wtx.GetTxTime());
entry.pushKV("timereceived", int64_t{wtx.nTimeReceived});

Expand Down Expand Up @@ -417,6 +421,10 @@ static std::vector<RPCResult> TransactionDescriptionString()
}},
{RPCResult::Type::STR_HEX, "replaced_by_txid", /*optional=*/true, "Only if 'category' is 'send'. The txid if this tx was replaced."},
{RPCResult::Type::STR_HEX, "replaces_txid", /*optional=*/true, "Only if 'category' is 'send'. The txid if this tx replaces another."},
{RPCResult::Type::ARR, "mempoolconflicts", "Transactions that directly conflict with either this transaction or an ancestor transaction",
{
{RPCResult::Type::STR_HEX, "txid", "The transaction id."},
}},
{RPCResult::Type::STR, "to", /*optional=*/true, "If a comment to is associated with the transaction."},
{RPCResult::Type::NUM_TIME, "time", "The transaction time expressed in " + UNIX_EPOCH_TIME + "."},
{RPCResult::Type::NUM_TIME, "timereceived", "The time received expressed in " + UNIX_EPOCH_TIME + "."},
Expand Down
2 changes: 1 addition & 1 deletion test/functional/wallet_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def run_test(self):
"category": baz["category"],
"vout": baz["vout"]}
expected_fields = frozenset({'amount', 'bip125-replaceable', 'confirmations', 'details', 'fee',
'hex', 'lastprocessedblock', 'time', 'timereceived', 'trusted', 'txid', 'wtxid', 'walletconflicts'})
'hex', 'lastprocessedblock', 'time', 'timereceived', 'trusted', 'txid', 'wtxid', 'walletconflicts', 'mempoolconflicts'})
verbose_field = "decoded"
expected_verbose_fields = expected_fields | {verbose_field}

Expand Down
19 changes: 19 additions & 0 deletions test/functional/wallet_conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def test_mempool_conflict(self):
assert_equal(alice.listunspent(), [unspents[0]])
assert_equal(alice.getbalance(), 25)

assert_equal(alice.gettransaction(tx1_txid)["mempoolconflicts"], [tx2_txid])

self.log.info("Test scenario where a mempool conflict is removed")

# broadcast tx3, replaces tx2 in mempool
Expand All @@ -187,6 +189,7 @@ def test_mempool_conflict(self):
# tx1 is no longer conflicted.
alice.sendrawtransaction(tx3)

assert_equal(alice.gettransaction(tx1_txid)["mempoolconflicts"], [])
assert tx1_txid not in self.nodes[0].getrawmempool()

# now all of alice's outputs should be considered spent
Expand Down Expand Up @@ -262,6 +265,10 @@ def test_mempool_and_block_conflicts(self):
assert tx2_txid in bob.getrawmempool()
assert tx1_conflict_txid in bob.getrawmempool()

assert_equal(bob.gettransaction(tx1_txid)["mempoolconflicts"], [tx1_conflict_txid])
assert_equal(bob.gettransaction(tx2_txid)["mempoolconflicts"], [])
assert_equal(bob.gettransaction(tx3_txid)["mempoolconflicts"], [tx1_conflict_txid])

# check that tx3 is now conflicted, so the output from tx2 can now be spent
assert_equal(bob.getbalances()["mine"]["untrusted_pending"], Decimal("24.99990000"))

Expand Down Expand Up @@ -348,6 +355,8 @@ def test_descendants_with_mempool_conflicts(self):
assert_equal(alice.getbalance(), 25)
assert_equal(bob.getbalances()["mine"]["untrusted_pending"], Decimal("24.99990000"))

assert_equal(bob.gettransaction(tx1_txid)["mempoolconflicts"], [])

raw_tx = bob.createrawtransaction(inputs=[bob.listunspent(minconf=0)[0]], outputs=[{carol.getnewaddress() : 24.999}])
# Bob creates a child to tx1
tx1_child = bob.signrawtransactionwithwallet(raw_tx)['hex']
Expand All @@ -356,6 +365,8 @@ def test_descendants_with_mempool_conflicts(self):
self.sync_mempools()

# Currently neither tx1 nor tx1_child should have any conflicts
assert_equal(bob.gettransaction(tx1_txid)["mempoolconflicts"], [])
assert_equal(bob.gettransaction(tx1_child_txid)["mempoolconflicts"], [])
assert tx1_txid in bob.getrawmempool()
assert tx1_child_txid in bob.getrawmempool()
assert_equal(len(bob.getrawmempool()), 2)
Expand All @@ -378,13 +389,21 @@ def test_descendants_with_mempool_conflicts(self):
assert tx1_conflict_txid in bob.getrawmempool()
assert_equal(len(bob.getrawmempool()), 1)

# Now both tx1 and tx1_child are conflicted by tx1_conflict
assert_equal(bob.gettransaction(tx1_txid)["mempoolconflicts"], [tx1_conflict_txid])
assert_equal(bob.gettransaction(tx1_child_txid)["mempoolconflicts"], [tx1_conflict_txid])

# Now create a conflict to tx1_conflict, so that it gets kicked out of the mempool
raw_tx = alice.createrawtransaction(inputs=[unspents[1]], outputs=[{carol.getnewaddress() : 24.9895}])
tx1_conflict_conflict = alice.signrawtransactionwithwallet(raw_tx)['hex']
tx1_conflict_conflict_txid = alice.sendrawtransaction(tx1_conflict_conflict)

self.sync_mempools()

# Now that tx1_conflict has been removed, both tx1 and tx1_child
assert_equal(bob.gettransaction(tx1_txid)["mempoolconflicts"], [])
assert_equal(bob.gettransaction(tx1_child_txid)["mempoolconflicts"], [])

# Both tx1 and tx1_child are still not in the mempool because they have not be re-broadcasted
assert tx1_txid not in bob.getrawmempool()
assert tx1_child_txid not in bob.getrawmempool()
Expand Down

0 comments on commit 5952292

Please sign in to comment.