/ test / functional / wallet_listtransactions.py
wallet_listtransactions.py
  1  #!/usr/bin/env python3
  2  # Copyright (c) 2014-present The Bitcoin Core developers
  3  # Distributed under the MIT software license, see the accompanying
  4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5  """Test the listtransactions API."""
  6  
  7  from decimal import Decimal
  8  import time
  9  import os
 10  import shutil
 11  
 12  from test_framework.blocktools import MAX_FUTURE_BLOCK_TIME
 13  from test_framework.descriptors import descsum_create
 14  from test_framework.messages import (
 15      COIN,
 16      tx_from_hex,
 17  )
 18  from test_framework.test_framework import BitcoinTestFramework
 19  from test_framework.util import (
 20      assert_not_equal,
 21      assert_array_result,
 22      assert_equal,
 23      assert_raises_rpc_error,
 24      find_vout_for_address,
 25  )
 26  from test_framework.wallet_util import get_generate_key
 27  
 28  
 29  class ListTransactionsTest(BitcoinTestFramework):
 30      def set_test_params(self):
 31          self.num_nodes = 3
 32          # whitelist peers to speed up tx relay / mempool sync
 33          self.noban_tx_relay = True
 34          self.extra_args = [["-walletrbf=0"]] * self.num_nodes
 35  
 36      def skip_test_if_missing_module(self):
 37          self.skip_if_no_wallet()
 38  
 39      def run_test(self):
 40          self.log.info("Test simple send from node0 to node1")
 41          txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
 42          self.sync_all()
 43          assert_array_result(self.nodes[0].listtransactions(),
 44                              {"txid": txid},
 45                              {"category": "send", "amount": Decimal("-0.1"), "confirmations": 0, "trusted": True})
 46          assert_array_result(self.nodes[1].listtransactions(),
 47                              {"txid": txid},
 48                              {"category": "receive", "amount": Decimal("0.1"), "confirmations": 0, "trusted": False})
 49          self.log.info("Test confirmations change after mining a block")
 50          blockhash = self.generate(self.nodes[0], 1)[0]
 51          blockheight = self.nodes[0].getblockheader(blockhash)['height']
 52          assert_array_result(self.nodes[0].listtransactions(),
 53                              {"txid": txid},
 54                              {"category": "send", "amount": Decimal("-0.1"), "confirmations": 1, "blockhash": blockhash, "blockheight": blockheight})
 55          assert_array_result(self.nodes[1].listtransactions(),
 56                              {"txid": txid},
 57                              {"category": "receive", "amount": Decimal("0.1"), "confirmations": 1, "blockhash": blockhash, "blockheight": blockheight})
 58  
 59          self.log.info("Test send-to-self on node0")
 60          txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
 61          assert_array_result(self.nodes[0].listtransactions(),
 62                              {"txid": txid, "category": "send"},
 63                              {"amount": Decimal("-0.2")})
 64          assert_array_result(self.nodes[0].listtransactions(),
 65                              {"txid": txid, "category": "receive"},
 66                              {"amount": Decimal("0.2")})
 67  
 68          self.log.info("Test sendmany from node1: twice to self, twice to node0")
 69          send_to = {self.nodes[0].getnewaddress(): 0.11,
 70                     self.nodes[1].getnewaddress(): 0.22,
 71                     self.nodes[0].getnewaddress(): 0.33,
 72                     self.nodes[1].getnewaddress(): 0.44}
 73          txid = self.nodes[1].sendmany("", send_to)
 74          self.sync_all()
 75          assert_array_result(self.nodes[1].listtransactions(),
 76                              {"category": "send", "amount": Decimal("-0.11")},
 77                              {"txid": txid})
 78          assert_array_result(self.nodes[0].listtransactions(),
 79                              {"category": "receive", "amount": Decimal("0.11")},
 80                              {"txid": txid})
 81          assert_array_result(self.nodes[1].listtransactions(),
 82                              {"category": "send", "amount": Decimal("-0.22")},
 83                              {"txid": txid})
 84          assert_array_result(self.nodes[1].listtransactions(),
 85                              {"category": "receive", "amount": Decimal("0.22")},
 86                              {"txid": txid})
 87          assert_array_result(self.nodes[1].listtransactions(),
 88                              {"category": "send", "amount": Decimal("-0.33")},
 89                              {"txid": txid})
 90          assert_array_result(self.nodes[0].listtransactions(),
 91                              {"category": "receive", "amount": Decimal("0.33")},
 92                              {"txid": txid})
 93          assert_array_result(self.nodes[1].listtransactions(),
 94                              {"category": "send", "amount": Decimal("-0.44")},
 95                              {"txid": txid})
 96          assert_array_result(self.nodes[1].listtransactions(),
 97                              {"category": "receive", "amount": Decimal("0.44")},
 98                              {"txid": txid})
 99  
100          self.run_rbf_opt_in_test()
101          self.run_externally_generated_address_test()
102          self.run_coinjoin_test()
103          self.run_invalid_parameters_test()
104          self.test_op_return()
105          self.test_from_me_status_change()
106  
107      def run_rbf_opt_in_test(self):
108          """Test the opt-in-rbf flag for sent and received transactions."""
109  
110          def is_opt_in(node, txid):
111              """Check whether a transaction signals opt-in RBF itself."""
112              rawtx = node.getrawtransaction(txid, 1)
113              for x in rawtx["vin"]:
114                  if x["sequence"] < 0xfffffffe:
115                      return True
116              return False
117  
118          def get_unconfirmed_utxo_entry(node, txid_to_match):
119              """Find an unconfirmed output matching a certain txid."""
120              utxo = node.listunspent(0, 0)
121              for i in utxo:
122                  if i["txid"] == txid_to_match:
123                      return i
124              return None
125  
126          self.log.info("Test txs w/o opt-in RBF (bip125-replaceable=no)")
127          # Chain a few transactions that don't opt in.
128          txid_1 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
129          assert not is_opt_in(self.nodes[0], txid_1)
130          assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_1}, {"bip125-replaceable": "no"})
131          self.sync_mempools()
132          assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_1}, {"bip125-replaceable": "no"})
133  
134          # Tx2 will build off tx1, still not opting in to RBF.
135          utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[0], txid_1)
136          assert_equal(utxo_to_use["safe"], True)
137          utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[1], txid_1)
138          assert_equal(utxo_to_use["safe"], False)
139  
140          # Create tx2 using createrawtransaction
141          inputs = [{"txid": utxo_to_use["txid"], "vout": utxo_to_use["vout"]}]
142          outputs = {self.nodes[0].getnewaddress(): 0.999}
143          tx2 = self.nodes[1].createrawtransaction(inputs=inputs, outputs=outputs, replaceable=False)
144          tx2_signed = self.nodes[1].signrawtransactionwithwallet(tx2)["hex"]
145          txid_2 = self.nodes[1].sendrawtransaction(tx2_signed)
146  
147          # ...and check the result
148          assert not is_opt_in(self.nodes[1], txid_2)
149          assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_2}, {"bip125-replaceable": "no"})
150          self.sync_mempools()
151          assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_2}, {"bip125-replaceable": "no"})
152  
153          self.log.info("Test txs with opt-in RBF (bip125-replaceable=yes)")
154          # Tx3 will opt-in to RBF
155          utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[0], txid_2)
156          inputs = [{"txid": txid_2, "vout": utxo_to_use["vout"]}]
157          outputs = {self.nodes[1].getnewaddress(): 0.998}
158          tx3 = self.nodes[0].createrawtransaction(inputs, outputs)
159          tx3_modified = tx_from_hex(tx3)
160          tx3_modified.vin[0].nSequence = 0
161          tx3 = tx3_modified.serialize().hex()
162          tx3_signed = self.nodes[0].signrawtransactionwithwallet(tx3)['hex']
163          txid_3 = self.nodes[0].sendrawtransaction(tx3_signed)
164  
165          assert is_opt_in(self.nodes[0], txid_3)
166          assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_3}, {"bip125-replaceable": "yes"})
167          self.sync_mempools()
168          assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_3}, {"bip125-replaceable": "yes"})
169  
170          # Tx4 will chain off tx3.  Doesn't signal itself, but depends on one
171          # that does.
172          utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[1], txid_3)
173          inputs = [{"txid": txid_3, "vout": utxo_to_use["vout"]}]
174          outputs = {self.nodes[0].getnewaddress(): 0.997}
175          tx4 = self.nodes[1].createrawtransaction(inputs=inputs, outputs=outputs, replaceable=False)
176          tx4_signed = self.nodes[1].signrawtransactionwithwallet(tx4)["hex"]
177          txid_4 = self.nodes[1].sendrawtransaction(tx4_signed)
178  
179          assert not is_opt_in(self.nodes[1], txid_4)
180          assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_4}, {"bip125-replaceable": "yes"})
181          self.sync_mempools()
182          assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_4}, {"bip125-replaceable": "yes"})
183  
184          self.log.info("Test tx with unknown RBF state (bip125-replaceable=unknown)")
185          # Replace tx3, and check that tx4 becomes unknown
186          tx3_b = tx3_modified
187          tx3_b.vout[0].nValue -= int(Decimal("0.004") * COIN)  # bump the fee
188          tx3_b = tx3_b.serialize().hex()
189          tx3_b_signed = self.nodes[0].signrawtransactionwithwallet(tx3_b)['hex']
190          txid_3b = self.nodes[0].sendrawtransaction(tx3_b_signed, 0)
191          assert is_opt_in(self.nodes[0], txid_3b)
192  
193          assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_4}, {"bip125-replaceable": "unknown"})
194          self.sync_mempools()
195          assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_4}, {"bip125-replaceable": "unknown"})
196  
197          self.log.info("Test bip125-replaceable status with gettransaction RPC")
198          for n in self.nodes[0:2]:
199              assert_equal(n.gettransaction(txid_1)["bip125-replaceable"], "no")
200              assert_equal(n.gettransaction(txid_2)["bip125-replaceable"], "no")
201              assert_equal(n.gettransaction(txid_3)["bip125-replaceable"], "yes")
202              assert_equal(n.gettransaction(txid_3b)["bip125-replaceable"], "yes")
203              assert_equal(n.gettransaction(txid_4)["bip125-replaceable"], "unknown")
204  
205          self.log.info("Test bip125-replaceable status with listsinceblock")
206          for n in self.nodes[0:2]:
207              txs = {tx['txid']: tx['bip125-replaceable'] for tx in n.listsinceblock()['transactions']}
208              assert_equal(txs[txid_1], "no")
209              assert_equal(txs[txid_2], "no")
210              assert_equal(txs[txid_3], "yes")
211              assert_equal(txs[txid_3b], "yes")
212              assert_equal(txs[txid_4], "unknown")
213  
214          self.log.info("Test mined transactions are no longer bip125-replaceable")
215          self.generate(self.nodes[0], 1)
216          assert txid_3b not in self.nodes[0].getrawmempool()
217          assert_equal(self.nodes[0].gettransaction(txid_3b)["bip125-replaceable"], "no")
218          assert_equal(self.nodes[0].gettransaction(txid_4)["bip125-replaceable"], "unknown")
219  
220      def run_externally_generated_address_test(self):
221          """Test behavior when receiving address is not in the address book."""
222  
223          self.log.info("Setup the same wallet on two nodes")
224          # refill keypool otherwise the second node wouldn't recognize addresses generated on the first nodes
225          self.nodes[0].keypoolrefill(1000)
226          self.stop_nodes()
227          wallet0 = os.path.join(self.nodes[0].chain_path, self.default_wallet_name, "wallet.dat")
228          wallet2 = os.path.join(self.nodes[2].chain_path, self.default_wallet_name, "wallet.dat")
229          shutil.copyfile(wallet0, wallet2)
230          self.start_nodes()
231          # reconnect nodes
232          self.connect_nodes(0, 1)
233          self.connect_nodes(1, 2)
234          self.connect_nodes(2, 0)
235  
236          addr1 = self.nodes[0].getnewaddress("pizza1", 'legacy')
237          addr2 = self.nodes[0].getnewaddress("pizza2", 'p2sh-segwit')
238          addr3 = self.nodes[0].getnewaddress("pizza3", 'bech32')
239  
240          self.log.info("Send to externally generated addresses")
241          # send to an address beyond the next to be generated to test the keypool gap
242          self.nodes[1].sendtoaddress(addr3, "0.001")
243          self.generate(self.nodes[1], 1)
244  
245          # send to an address that is already marked as used due to the keypool gap mechanics
246          self.nodes[1].sendtoaddress(addr2, "0.001")
247          self.generate(self.nodes[1], 1)
248  
249          # send to self transaction
250          self.nodes[0].sendtoaddress(addr1, "0.001")
251          self.generate(self.nodes[0], 1)
252  
253          self.log.info("Verify listtransactions is the same regardless of where the address was generated")
254          transactions0 = self.nodes[0].listtransactions()
255          transactions2 = self.nodes[2].listtransactions()
256  
257          # normalize results: remove fields that normally could differ and sort
258          def normalize_list(txs):
259              for tx in txs:
260                  tx.pop('label', None)
261                  tx.pop('time', None)
262                  tx.pop('timereceived', None)
263              txs.sort(key=lambda x: x['txid'])
264  
265          normalize_list(transactions0)
266          normalize_list(transactions2)
267          assert_equal(transactions0, transactions2)
268  
269          self.log.info("Verify labels are persistent on the node that generated the addresses")
270          assert_equal(['pizza1'], self.nodes[0].getaddressinfo(addr1)['labels'])
271          assert_equal(['pizza2'], self.nodes[0].getaddressinfo(addr2)['labels'])
272          assert_equal(['pizza3'], self.nodes[0].getaddressinfo(addr3)['labels'])
273  
274      def run_coinjoin_test(self):
275          self.log.info('Check "coin-join" transaction')
276          input_0 = next(i for i in self.nodes[0].listunspent(query_options={"minimumAmount": 0.2}, include_unsafe=False))
277          input_1 = next(i for i in self.nodes[1].listunspent(query_options={"minimumAmount": 0.2}, include_unsafe=False))
278          raw_hex = self.nodes[0].createrawtransaction(
279              inputs=[
280                  {
281                      "txid": input_0["txid"],
282                      "vout": input_0["vout"],
283                  },
284                  {
285                      "txid": input_1["txid"],
286                      "vout": input_1["vout"],
287                  },
288              ],
289              outputs={
290                  self.nodes[0].getnewaddress(): 0.123,
291                  self.nodes[1].getnewaddress(): 0.123,
292              },
293          )
294          raw_hex = self.nodes[0].signrawtransactionwithwallet(raw_hex)["hex"]
295          raw_hex = self.nodes[1].signrawtransactionwithwallet(raw_hex)["hex"]
296          txid_join = self.nodes[0].sendrawtransaction(hexstring=raw_hex, maxfeerate=0)
297          fee_join = self.nodes[0].getmempoolentry(txid_join)["fees"]["base"]
298          # Fee should be correct: assert_equal(fee_join, self.nodes[0].gettransaction(txid_join)['fee'])
299          # But it is not, see for example https://github.com/bitcoin/bitcoin/issues/14136:
300          assert_not_equal(fee_join, self.nodes[0].gettransaction(txid_join)["fee"])
301  
302      def run_invalid_parameters_test(self):
303          self.log.info("Test listtransactions RPC parameter validity")
304          assert_raises_rpc_error(-8, 'Label argument must be a valid label name or "*".', self.nodes[0].listtransactions, label="")
305          self.nodes[0].listtransactions(label="*")
306          assert_raises_rpc_error(-8, "Negative count", self.nodes[0].listtransactions, count=-1)
307          assert_raises_rpc_error(-8, "Negative from", self.nodes[0].listtransactions, skip=-1)
308  
309      def test_op_return(self):
310          """Test if OP_RETURN outputs will be displayed correctly."""
311          raw_tx = self.nodes[0].createrawtransaction([], [{'data': 'aa'}])
312          funded_tx = self.nodes[0].fundrawtransaction(raw_tx)
313          signed_tx = self.nodes[0].signrawtransactionwithwallet(funded_tx['hex'])
314          tx_id = self.nodes[0].sendrawtransaction(signed_tx['hex'])
315  
316          op_ret_tx = [tx for tx in self.nodes[0].listtransactions() if tx['txid'] == tx_id][0]
317  
318          assert 'address' not in op_ret_tx
319  
320      def test_from_me_status_change(self):
321          self.log.info("Test gettransaction after changing a transaction's 'from me' status")
322          self.nodes[0].createwallet("fromme")
323          default_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
324          wallet = self.nodes[0].get_wallet_rpc("fromme")
325  
326          # The 'fee' field of gettransaction is only added when the transaction is 'from me'
327          # Run twice, once for a transaction in the mempool, again when it confirms
328          for confirm in [False, True]:
329              key = get_generate_key()
330              descriptor = descsum_create(f"wpkh({key.privkey})")
331              default_wallet.importdescriptors([{"desc": descriptor, "timestamp": "now"}])
332  
333              send_res = default_wallet.send(outputs=[{key.p2wpkh_addr: 1}, {wallet.getnewaddress(): 1}])
334              assert_equal(send_res["complete"], True)
335              vout = find_vout_for_address(self.nodes[0], send_res["txid"], key.p2wpkh_addr)
336              utxos = [{"txid": send_res["txid"], "vout": vout}]
337              self.generate(self.nodes[0], 1, sync_fun=self.no_op)
338  
339              # Send to the test wallet, ensuring that one input is for the descriptor we will import,
340              # and that there are other inputs belonging to only the sending wallet
341              send_res = default_wallet.send(outputs=[{wallet.getnewaddress(): 1.5}], inputs=utxos, add_inputs=True)
342              assert_equal(send_res["complete"], True)
343              txid = send_res["txid"]
344              self.nodes[0].syncwithvalidationinterfacequeue()
345              tx_info = wallet.gettransaction(txid)
346              assert "fee" not in tx_info
347              assert_equal(any(detail["category"] == "send" for detail in tx_info["details"]), False)
348  
349              if confirm:
350                  self.generate(self.nodes[0], 1, sync_fun=self.no_op)
351                  # Mock time forward and generate blocks so that the import does not rescan the transaction
352                  self.nodes[0].setmocktime(int(time.time()) + MAX_FUTURE_BLOCK_TIME + 1)
353                  self.generate(self.nodes[0], 10, sync_fun=self.no_op)
354  
355              import_res = wallet.importdescriptors([{"desc": descriptor, "timestamp": "now"}])
356              assert_equal(import_res[0]["success"], True)
357              # TODO: We should check that the fee matches, but since the transaction spends inputs
358              # not known to the wallet, it is incorrectly calculating the fee.
359              # assert_equal(wallet.gettransaction(txid)["fee"], fee)
360              tx_info = wallet.gettransaction(txid)
361              assert "fee" in tx_info
362              assert_equal(any(detail["category"] == "send" for detail in tx_info["details"]), True)
363  
364  if __name__ == '__main__':
365      ListTransactionsTest(__file__).main()