Added a field to the output of gettransaction/listtransactions to indicate whether the given transaction is in the mempool.

Github-Pull: #21260
Rebased-From: 46bf0b7b5d8c44bd7032c473f9878cfb59018161
This commit is contained in:
Dan Benjamin 2021-02-21 18:01:31 -05:00 committed by Luke Dashjr
parent 9c2aba5796
commit ac4327cbff
3 changed files with 57 additions and 0 deletions

View File

@ -32,6 +32,7 @@ static void WalletTxToJSON(const CWallet& wallet, const CWalletTx& wtx, UniValue
entry.pushKV("blocktime", block_time);
} else {
entry.pushKV("trusted", CachedTxIsTrusted(wallet, wtx));
entry.pushKV("in_mempool", wtx.InMempool());
}
uint256 hash = wtx.GetHash();
entry.pushKV("txid", hash.GetHex());
@ -409,6 +410,7 @@ static std::vector<RPCResult> TransactionDescriptionString()
{RPCResult::Type::BOOL, "generated", /*optional=*/true, "Only present if the transaction's only input is a coinbase one."},
{RPCResult::Type::BOOL, "trusted", /*optional=*/true, "Whether we consider the transaction to be trusted and safe to spend from.\n"
"Only present when the transaction has 0 confirmations (or negative confirmations, if conflicted)."},
{RPCResult::Type::BOOL, "in_mempool", /*optional=*/true, "True if the transaction is in this node's memory pool. Only present on unconfirmed transactions."},
{RPCResult::Type::STR_HEX, "blockhash", /*optional=*/true, "The block hash containing the transaction."},
{RPCResult::Type::NUM, "blockheight", /*optional=*/true, "The block height containing the transaction."},
{RPCResult::Type::NUM, "blockindex", /*optional=*/true, "The index of the transaction in the block that includes it."},

View File

@ -690,6 +690,7 @@ class WalletTest(BitcoinTestFramework):
'details',
'fee',
'hex',
'in_mempool',
'lastprocessedblock',
'mempoolconflicts',
'time',

View File

@ -114,6 +114,8 @@ class ListTransactionsTest(BitcoinTestFramework):
self.run_coinjoin_test()
self.run_invalid_parameters_test()
self.test_op_return()
self.test_listtransactions_display_in_mempool()
self.test_gettransaction_display_in_mempool()
def run_rbf_opt_in_test(self):
"""Test the opt-in-rbf flag for sent and received transactions."""
@ -329,5 +331,57 @@ class ListTransactionsTest(BitcoinTestFramework):
assert 'address' not in op_ret_tx
def create_and_send_transaction(self, utxo, address, amt, feeRate):
psbtx = self.nodes[0].walletcreatefundedpsbt([{"txid": utxo['txid'], "vout": utxo['vout']}],
{address: amt},
0,
{"replaceable":True, "feeRate":feeRate})['psbt']
signed_tx = self.nodes[0].walletprocesspsbt(psbtx)['psbt']
final_tx = self.nodes[0].finalizepsbt(signed_tx)['hex']
return self.nodes[0].sendrawtransaction(final_tx)
def test_listtransactions_display_in_mempool(self):
self.log.info('Testing that listtransactions correctly displays whether a transaction is in the mempool')
utxo = self.nodes[0].listunspent(query_options={'minimumAmount': 0.15})[0]
address = self.nodes[0].getnewaddress()
tx1_id = self.create_and_send_transaction(utxo, address, 0.1, 0.001)
new_txs = self.nodes[0].listtransactions(count=2)
for tx in new_txs:
assert_equal(tx['txid'], tx1_id)
assert_equal(tx['in_mempool'], True)
tx2_id = self.create_and_send_transaction(utxo, address, 0.1, 0.002)
new_txs = self.nodes[0].listtransactions(count=4)
for i in range(2):
assert_equal(new_txs[i]['txid'], tx1_id)
assert_equal(new_txs[i]['in_mempool'], False)
for i in range(2, 4):
assert_equal(new_txs[i]['txid'], tx2_id)
assert_equal(new_txs[i]['in_mempool'], True)
def test_gettransaction_display_in_mempool(self):
self.log.info('Testing that gettransaction correctly displays whether a transaction is in the mempool')
utxo = self.nodes[0].listunspent(query_options={'minimumAmount': 0.15})[0]
address = self.nodes[0].getnewaddress()
tx1_id = self.create_and_send_transaction(utxo, address, 0.1, 0.001)
tx1 = self.nodes[0].gettransaction(tx1_id)
assert_equal(tx1['txid'], tx1_id)
assert_equal(tx1['in_mempool'], True)
tx2_id = self.create_and_send_transaction(utxo, address, 0.1, 0.002)
tx1 = self.nodes[0].gettransaction(tx1_id)
tx2 = self.nodes[0].gettransaction(tx2_id)
assert_equal(tx1['txid'], tx1_id)
assert_equal(tx1['in_mempool'], False)
assert_equal(tx2['txid'], tx2_id)
assert_equal(tx2['in_mempool'], True)
if __name__ == '__main__':
ListTransactionsTest(__file__).main()