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