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.

abandonconflict.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 abandontransaction RPC.
  6. The abandontransaction RPC marks a transaction and all its in-wallet
  7. descendants as abandoned which allows their inputs to be respent. It can be
  8. used to replace "stuck" or evicted transactions. It only works on transactions
  9. which are not included in a block and are not currently in the mempool. It has
  10. no effect on transactions which are already conflicted or abandoned.
  11. """
  12. from test_framework.test_framework import BitcoinTestFramework
  13. from test_framework.util import *
  14. class AbandonConflictTest(BitcoinTestFramework):
  15. def set_test_params(self):
  16. self.num_nodes = 2
  17. self.extra_args = [["-minrelaytxfee=0.00001"], []]
  18. def run_test(self):
  19. self.nodes[1].generate(100)
  20. sync_blocks(self.nodes)
  21. balance = self.nodes[0].getbalance()
  22. txA = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10"))
  23. txB = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10"))
  24. txC = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10"))
  25. sync_mempools(self.nodes)
  26. self.nodes[1].generate(1)
  27. sync_blocks(self.nodes)
  28. newbalance = self.nodes[0].getbalance()
  29. assert(balance - newbalance < Decimal("0.001")) #no more than fees lost
  30. balance = newbalance
  31. # Disconnect nodes so node0's transactions don't get into node1's mempool
  32. disconnect_nodes(self.nodes[0], 1)
  33. # Identify the 10btc outputs
  34. nA = next(i for i, vout in enumerate(self.nodes[0].getrawtransaction(txA, 1)["vout"]) if vout["value"] == Decimal("10"))
  35. nB = next(i for i, vout in enumerate(self.nodes[0].getrawtransaction(txB, 1)["vout"]) if vout["value"] == Decimal("10"))
  36. nC = next(i for i, vout in enumerate(self.nodes[0].getrawtransaction(txC, 1)["vout"]) if vout["value"] == Decimal("10"))
  37. inputs =[]
  38. # spend 10btc outputs from txA and txB
  39. inputs.append({"txid":txA, "vout":nA})
  40. inputs.append({"txid":txB, "vout":nB})
  41. outputs = {}
  42. outputs[self.nodes[0].getnewaddress()] = Decimal("14.99998")
  43. outputs[self.nodes[1].getnewaddress()] = Decimal("5")
  44. signed = self.nodes[0].signrawtransaction(self.nodes[0].createrawtransaction(inputs, outputs))
  45. txAB1 = self.nodes[0].sendrawtransaction(signed["hex"])
  46. # Identify the 14.99998btc output
  47. nAB = next(i for i, vout in enumerate(self.nodes[0].getrawtransaction(txAB1, 1)["vout"]) if vout["value"] == Decimal("14.99998"))
  48. #Create a child tx spending AB1 and C
  49. inputs = []
  50. inputs.append({"txid":txAB1, "vout":nAB})
  51. inputs.append({"txid":txC, "vout":nC})
  52. outputs = {}
  53. outputs[self.nodes[0].getnewaddress()] = Decimal("24.9996")
  54. signed2 = self.nodes[0].signrawtransaction(self.nodes[0].createrawtransaction(inputs, outputs))
  55. txABC2 = self.nodes[0].sendrawtransaction(signed2["hex"])
  56. # In mempool txs from self should increase balance from change
  57. newbalance = self.nodes[0].getbalance()
  58. assert_equal(newbalance, balance - Decimal("30") + Decimal("24.9996"))
  59. balance = newbalance
  60. # Restart the node with a higher min relay fee so the parent tx is no longer in mempool
  61. # TODO: redo with eviction
  62. self.stop_node(0)
  63. self.start_node(0, extra_args=["-minrelaytxfee=0.0001"])
  64. # Verify txs no longer in either node's mempool
  65. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  66. assert_equal(len(self.nodes[1].getrawmempool()), 0)
  67. # Not in mempool txs from self should only reduce balance
  68. # inputs are still spent, but change not received
  69. newbalance = self.nodes[0].getbalance()
  70. assert_equal(newbalance, balance - Decimal("24.9996"))
  71. # Unconfirmed received funds that are not in mempool, also shouldn't show
  72. # up in unconfirmed balance
  73. unconfbalance = self.nodes[0].getunconfirmedbalance() + self.nodes[0].getbalance()
  74. assert_equal(unconfbalance, newbalance)
  75. # Also shouldn't show up in listunspent
  76. assert(not txABC2 in [utxo["txid"] for utxo in self.nodes[0].listunspent(0)])
  77. balance = newbalance
  78. # Abandon original transaction and verify inputs are available again
  79. # including that the child tx was also abandoned
  80. self.nodes[0].abandontransaction(txAB1)
  81. newbalance = self.nodes[0].getbalance()
  82. assert_equal(newbalance, balance + Decimal("30"))
  83. balance = newbalance
  84. # Verify that even with a low min relay fee, the tx is not reaccepted from wallet on startup once abandoned
  85. self.stop_node(0)
  86. self.start_node(0, extra_args=["-minrelaytxfee=0.00001"])
  87. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  88. assert_equal(self.nodes[0].getbalance(), balance)
  89. # But if its received again then it is unabandoned
  90. # And since now in mempool, the change is available
  91. # But its child tx remains abandoned
  92. self.nodes[0].sendrawtransaction(signed["hex"])
  93. newbalance = self.nodes[0].getbalance()
  94. assert_equal(newbalance, balance - Decimal("20") + Decimal("14.99998"))
  95. balance = newbalance
  96. # Send child tx again so its unabandoned
  97. self.nodes[0].sendrawtransaction(signed2["hex"])
  98. newbalance = self.nodes[0].getbalance()
  99. assert_equal(newbalance, balance - Decimal("10") - Decimal("14.99998") + Decimal("24.9996"))
  100. balance = newbalance
  101. # Remove using high relay fee again
  102. self.stop_node(0)
  103. self.start_node(0, extra_args=["-minrelaytxfee=0.0001"])
  104. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  105. newbalance = self.nodes[0].getbalance()
  106. assert_equal(newbalance, balance - Decimal("24.9996"))
  107. balance = newbalance
  108. # Create a double spend of AB1 by spending again from only A's 10 output
  109. # Mine double spend from node 1
  110. inputs =[]
  111. inputs.append({"txid":txA, "vout":nA})
  112. outputs = {}
  113. outputs[self.nodes[1].getnewaddress()] = Decimal("9.9999")
  114. tx = self.nodes[0].createrawtransaction(inputs, outputs)
  115. signed = self.nodes[0].signrawtransaction(tx)
  116. self.nodes[1].sendrawtransaction(signed["hex"])
  117. self.nodes[1].generate(1)
  118. connect_nodes(self.nodes[0], 1)
  119. sync_blocks(self.nodes)
  120. # Verify that B and C's 10 BTC outputs are available for spending again because AB1 is now conflicted
  121. newbalance = self.nodes[0].getbalance()
  122. assert_equal(newbalance, balance + Decimal("20"))
  123. balance = newbalance
  124. # There is currently a minor bug around this and so this test doesn't work. See Issue #7315
  125. # Invalidate the block with the double spend and B's 10 BTC output should no longer be available
  126. # Don't think C's should either
  127. self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
  128. newbalance = self.nodes[0].getbalance()
  129. #assert_equal(newbalance, balance - Decimal("10"))
  130. self.log.info("If balance has not declined after invalidateblock then out of mempool wallet tx which is no longer")
  131. self.log.info("conflicted has not resumed causing its inputs to be seen as spent. See Issue #7315")
  132. self.log.info(str(balance) + " -> " + str(newbalance) + " ?")
  133. if __name__ == '__main__':
  134. AbandonConflictTest().main()