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.8KB

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