Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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