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 11KB

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