You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

listtransactions.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 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. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. from test_framework.mininode import CTransaction, COIN
  9. from io import BytesIO
  10. def txFromHex(hexstring):
  11. tx = CTransaction()
  12. f = BytesIO(hex_str_to_bytes(hexstring))
  13. tx.deserialize(f)
  14. return tx
  15. class ListTransactionsTest(BitcoinTestFramework):
  16. def __init__(self):
  17. super().__init__()
  18. self.num_nodes = 4
  19. self.setup_clean_chain = False
  20. def setup_nodes(self):
  21. #This test requires mocktime
  22. enable_mocktime()
  23. return start_nodes(self.num_nodes, self.options.tmpdir)
  24. def run_test(self):
  25. # Simple send, 0 to 1:
  26. txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
  27. self.sync_all()
  28. assert_array_result(self.nodes[0].listtransactions(),
  29. {"txid":txid},
  30. {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":0})
  31. assert_array_result(self.nodes[1].listtransactions(),
  32. {"txid":txid},
  33. {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":0})
  34. # mine a block, confirmations should change:
  35. self.nodes[0].generate(1)
  36. self.sync_all()
  37. assert_array_result(self.nodes[0].listtransactions(),
  38. {"txid":txid},
  39. {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":1})
  40. assert_array_result(self.nodes[1].listtransactions(),
  41. {"txid":txid},
  42. {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":1})
  43. # send-to-self:
  44. txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
  45. assert_array_result(self.nodes[0].listtransactions(),
  46. {"txid":txid, "category":"send"},
  47. {"amount":Decimal("-0.2")})
  48. assert_array_result(self.nodes[0].listtransactions(),
  49. {"txid":txid, "category":"receive"},
  50. {"amount":Decimal("0.2")})
  51. # sendmany from node1: twice to self, twice to node2:
  52. send_to = { self.nodes[0].getnewaddress() : 0.11,
  53. self.nodes[1].getnewaddress() : 0.22,
  54. self.nodes[0].getaccountaddress("from1") : 0.33,
  55. self.nodes[1].getaccountaddress("toself") : 0.44 }
  56. txid = self.nodes[1].sendmany("", send_to)
  57. self.sync_all()
  58. assert_array_result(self.nodes[1].listtransactions(),
  59. {"category":"send","amount":Decimal("-0.11")},
  60. {"txid":txid} )
  61. assert_array_result(self.nodes[0].listtransactions(),
  62. {"category":"receive","amount":Decimal("0.11")},
  63. {"txid":txid} )
  64. assert_array_result(self.nodes[1].listtransactions(),
  65. {"category":"send","amount":Decimal("-0.22")},
  66. {"txid":txid} )
  67. assert_array_result(self.nodes[1].listtransactions(),
  68. {"category":"receive","amount":Decimal("0.22")},
  69. {"txid":txid} )
  70. assert_array_result(self.nodes[1].listtransactions(),
  71. {"category":"send","amount":Decimal("-0.33")},
  72. {"txid":txid} )
  73. assert_array_result(self.nodes[0].listtransactions(),
  74. {"category":"receive","amount":Decimal("0.33")},
  75. {"txid":txid, "account" : "from1"} )
  76. assert_array_result(self.nodes[1].listtransactions(),
  77. {"category":"send","amount":Decimal("-0.44")},
  78. {"txid":txid, "account" : ""} )
  79. assert_array_result(self.nodes[1].listtransactions(),
  80. {"category":"receive","amount":Decimal("0.44")},
  81. {"txid":txid, "account" : "toself"} )
  82. multisig = self.nodes[1].createmultisig(1, [self.nodes[1].getnewaddress()])
  83. self.nodes[0].importaddress(multisig["redeemScript"], "watchonly", False, True)
  84. txid = self.nodes[1].sendtoaddress(multisig["address"], 0.1)
  85. self.nodes[1].generate(1)
  86. self.sync_all()
  87. assert(len(self.nodes[0].listtransactions("watchonly", 100, 0, False)) == 0)
  88. assert_array_result(self.nodes[0].listtransactions("watchonly", 100, 0, True),
  89. {"category":"receive","amount":Decimal("0.1")},
  90. {"txid":txid, "account" : "watchonly"} )
  91. self.run_rbf_opt_in_test()
  92. # Check that the opt-in-rbf flag works properly, for sent and received
  93. # transactions.
  94. def run_rbf_opt_in_test(self):
  95. # Check whether a transaction signals opt-in RBF itself
  96. def is_opt_in(node, txid):
  97. rawtx = node.getrawtransaction(txid, 1)
  98. for x in rawtx["vin"]:
  99. if x["sequence"] < 0xfffffffe:
  100. return True
  101. return False
  102. # Find an unconfirmed output matching a certain txid
  103. def get_unconfirmed_utxo_entry(node, txid_to_match):
  104. utxo = node.listunspent(0, 0)
  105. for i in utxo:
  106. if i["txid"] == txid_to_match:
  107. return i
  108. return None
  109. # 1. Chain a few transactions that don't opt-in.
  110. txid_1 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
  111. assert(not is_opt_in(self.nodes[0], txid_1))
  112. assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_1}, {"bip125-replaceable":"no"})
  113. sync_mempools(self.nodes)
  114. assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_1}, {"bip125-replaceable":"no"})
  115. # Tx2 will build off txid_1, still not opting in to RBF.
  116. utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[0], txid_1)
  117. assert_equal(utxo_to_use["safe"], True)
  118. utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[1], txid_1)
  119. utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[1], txid_1)
  120. assert_equal(utxo_to_use["safe"], False)
  121. # Create tx2 using createrawtransaction
  122. inputs = [{"txid":utxo_to_use["txid"], "vout":utxo_to_use["vout"]}]
  123. outputs = {self.nodes[0].getnewaddress(): 0.999}
  124. tx2 = self.nodes[1].createrawtransaction(inputs, outputs)
  125. tx2_signed = self.nodes[1].signrawtransaction(tx2)["hex"]
  126. txid_2 = self.nodes[1].sendrawtransaction(tx2_signed)
  127. # ...and check the result
  128. assert(not is_opt_in(self.nodes[1], txid_2))
  129. assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_2}, {"bip125-replaceable":"no"})
  130. sync_mempools(self.nodes)
  131. assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_2}, {"bip125-replaceable":"no"})
  132. # Tx3 will opt-in to RBF
  133. utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[0], txid_2)
  134. inputs = [{"txid": txid_2, "vout":utxo_to_use["vout"]}]
  135. outputs = {self.nodes[1].getnewaddress(): 0.998}
  136. tx3 = self.nodes[0].createrawtransaction(inputs, outputs)
  137. tx3_modified = txFromHex(tx3)
  138. tx3_modified.vin[0].nSequence = 0
  139. tx3 = bytes_to_hex_str(tx3_modified.serialize())
  140. tx3_signed = self.nodes[0].signrawtransaction(tx3)['hex']
  141. txid_3 = self.nodes[0].sendrawtransaction(tx3_signed)
  142. assert(is_opt_in(self.nodes[0], txid_3))
  143. assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_3}, {"bip125-replaceable":"yes"})
  144. sync_mempools(self.nodes)
  145. assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_3}, {"bip125-replaceable":"yes"})
  146. # Tx4 will chain off tx3. Doesn't signal itself, but depends on one
  147. # that does.
  148. utxo_to_use = get_unconfirmed_utxo_entry(self.nodes[1], txid_3)
  149. inputs = [{"txid": txid_3, "vout":utxo_to_use["vout"]}]
  150. outputs = {self.nodes[0].getnewaddress(): 0.997}
  151. tx4 = self.nodes[1].createrawtransaction(inputs, outputs)
  152. tx4_signed = self.nodes[1].signrawtransaction(tx4)["hex"]
  153. txid_4 = self.nodes[1].sendrawtransaction(tx4_signed)
  154. assert(not is_opt_in(self.nodes[1], txid_4))
  155. assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_4}, {"bip125-replaceable":"yes"})
  156. sync_mempools(self.nodes)
  157. assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_4}, {"bip125-replaceable":"yes"})
  158. # Replace tx3, and check that tx4 becomes unknown
  159. tx3_b = tx3_modified
  160. tx3_b.vout[0].nValue -= int(Decimal("0.004") * COIN) # bump the fee
  161. tx3_b = bytes_to_hex_str(tx3_b.serialize())
  162. tx3_b_signed = self.nodes[0].signrawtransaction(tx3_b)['hex']
  163. txid_3b = self.nodes[0].sendrawtransaction(tx3_b_signed, True)
  164. assert(is_opt_in(self.nodes[0], txid_3b))
  165. assert_array_result(self.nodes[0].listtransactions(), {"txid": txid_4}, {"bip125-replaceable":"unknown"})
  166. sync_mempools(self.nodes)
  167. assert_array_result(self.nodes[1].listtransactions(), {"txid": txid_4}, {"bip125-replaceable":"unknown"})
  168. # Check gettransaction as well:
  169. for n in self.nodes[0:2]:
  170. assert_equal(n.gettransaction(txid_1)["bip125-replaceable"], "no")
  171. assert_equal(n.gettransaction(txid_2)["bip125-replaceable"], "no")
  172. assert_equal(n.gettransaction(txid_3)["bip125-replaceable"], "yes")
  173. assert_equal(n.gettransaction(txid_3b)["bip125-replaceable"], "yes")
  174. assert_equal(n.gettransaction(txid_4)["bip125-replaceable"], "unknown")
  175. # After mining a transaction, it's no longer BIP125-replaceable
  176. self.nodes[0].generate(1)
  177. assert(txid_3b not in self.nodes[0].getrawmempool())
  178. assert_equal(self.nodes[0].gettransaction(txid_3b)["bip125-replaceable"], "no")
  179. assert_equal(self.nodes[0].gettransaction(txid_4)["bip125-replaceable"], "unknown")
  180. if __name__ == '__main__':
  181. ListTransactionsTest().main()