1#!/usr/bin/env python3
2# Copyright (c) 2014-2019 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
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 abandoned.
12"""
13from decimal import Decimal
14
15from test_framework.test_framework import BitcoinTestFramework
16from test_framework.util import (
17    assert_equal,
18    assert_raises_rpc_error,
19    connect_nodes,
20    disconnect_nodes,
21    sync_blocks,
22    sync_mempools,
23)
24
25
26class AbandonConflictTest(BitcoinTestFramework):
27    def set_test_params(self):
28        self.num_nodes = 2
29        self.extra_args = [["-minrelaytxfee=0.00001"], []]
30
31    def skip_test_if_missing_module(self):
32        self.skip_if_no_wallet()
33
34    def run_test(self):
35        self.nodes[1].generate(100)
36        sync_blocks(self.nodes)
37        balance = self.nodes[0].getbalance()
38        txA = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10"))
39        txB = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10"))
40        txC = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10"))
41        sync_mempools(self.nodes)
42        self.nodes[1].generate(1)
43
44        # Can not abandon non-wallet transaction
45        assert_raises_rpc_error(-5, 'Invalid or non-wallet transaction id', lambda: self.nodes[0].abandontransaction(txid='ff' * 32))
46        # Can not abandon confirmed transaction
47        assert_raises_rpc_error(-5, 'Transaction not eligible for abandonment', lambda: self.nodes[0].abandontransaction(txid=txA))
48
49        sync_blocks(self.nodes)
50        newbalance = self.nodes[0].getbalance()
51        assert balance - newbalance < Decimal("0.01")  #no more than fees lost
52        balance = newbalance
53
54        # Disconnect nodes so node0's transactions don't get into node1's mempool
55        disconnect_nodes(self.nodes[0], 1)
56
57        # Identify the 10btc outputs
58        nA = next(tx_out["vout"] for tx_out in self.nodes[0].gettransaction(txA)["details"] if tx_out["amount"] == Decimal("10"))
59        nB = next(tx_out["vout"] for tx_out in self.nodes[0].gettransaction(txB)["details"] if tx_out["amount"] == Decimal("10"))
60        nC = next(tx_out["vout"] for tx_out in self.nodes[0].gettransaction(txC)["details"] if tx_out["amount"] == Decimal("10"))
61
62        inputs = []
63        # spend 10btc outputs from txA and txB
64        inputs.append({"txid": txA, "vout": nA})
65        inputs.append({"txid": txB, "vout": nB})
66        outputs = {}
67
68        outputs[self.nodes[0].getnewaddress()] = Decimal("14.99998")
69        outputs[self.nodes[1].getnewaddress()] = Decimal("5")
70        signed = self.nodes[0].signrawtransactionwithwallet(self.nodes[0].createrawtransaction(inputs, outputs))
71        txAB1 = self.nodes[0].sendrawtransaction(signed["hex"])
72
73        # Identify the 14.99998btc output
74        nAB = next(tx_out["vout"] for tx_out in self.nodes[0].gettransaction(txAB1)["details"] if tx_out["amount"] == Decimal("14.99998"))
75
76        #Create a child tx spending AB1 and C
77        inputs = []
78        inputs.append({"txid": txAB1, "vout": nAB})
79        inputs.append({"txid": txC, "vout": nC})
80        outputs = {}
81        outputs[self.nodes[0].getnewaddress()] = Decimal("24.9996")
82        signed2 = self.nodes[0].signrawtransactionwithwallet(self.nodes[0].createrawtransaction(inputs, outputs))
83        txABC2 = self.nodes[0].sendrawtransaction(signed2["hex"])
84
85        # Create a child tx spending ABC2
86        signed3_change = Decimal("24.999")
87        inputs = [{"txid": txABC2, "vout": 0}]
88        outputs = {self.nodes[0].getnewaddress(): signed3_change}
89        signed3 = self.nodes[0].signrawtransactionwithwallet(self.nodes[0].createrawtransaction(inputs, outputs))
90        # note tx is never directly referenced, only abandoned as a child of the above
91        self.nodes[0].sendrawtransaction(signed3["hex"])
92
93        # In mempool txs from self should increase balance from change
94        newbalance = self.nodes[0].getbalance()
95        assert_equal(newbalance, balance - Decimal("30") + signed3_change)
96        balance = newbalance
97
98        # Restart the node with a higher min relay fee so the parent tx is no longer in mempool
99        # TODO: redo with eviction
100        self.stop_node(0)
101        self.start_node(0, extra_args=["-minrelaytxfee=0.0001"])
102
103        # Verify txs no longer in either node's mempool
104        assert_equal(len(self.nodes[0].getrawmempool()), 0)
105        assert_equal(len(self.nodes[1].getrawmempool()), 0)
106
107        # Not in mempool txs from self should only reduce balance
108        # inputs are still spent, but change not received
109        newbalance = self.nodes[0].getbalance()
110        assert_equal(newbalance, balance - signed3_change)
111        # Unconfirmed received funds that are not in mempool, also shouldn't show
112        # up in unconfirmed balance
113        unconfbalance = self.nodes[0].getunconfirmedbalance() + self.nodes[0].getbalance()
114        assert_equal(unconfbalance, newbalance)
115        # Also shouldn't show up in listunspent
116        assert not txABC2 in [utxo["txid"] for utxo in self.nodes[0].listunspent(0)]
117        balance = newbalance
118
119        # Abandon original transaction and verify inputs are available again
120        # including that the child tx was also abandoned
121        self.nodes[0].abandontransaction(txAB1)
122        newbalance = self.nodes[0].getbalance()
123        assert_equal(newbalance, balance + Decimal("30"))
124        balance = newbalance
125
126        # Verify that even with a low min relay fee, the tx is not reaccepted from wallet on startup once abandoned
127        self.stop_node(0)
128        self.start_node(0, extra_args=["-minrelaytxfee=0.00001"])
129        assert_equal(len(self.nodes[0].getrawmempool()), 0)
130        assert_equal(self.nodes[0].getbalance(), balance)
131
132        # But if it is received again then it is unabandoned
133        # And since now in mempool, the change is available
134        # But its child tx remains abandoned
135        self.nodes[0].sendrawtransaction(signed["hex"])
136        newbalance = self.nodes[0].getbalance()
137        assert_equal(newbalance, balance - Decimal("20") + Decimal("14.99998"))
138        balance = newbalance
139
140        # Send child tx again so it is unabandoned
141        self.nodes[0].sendrawtransaction(signed2["hex"])
142        newbalance = self.nodes[0].getbalance()
143        assert_equal(newbalance, balance - Decimal("10") - Decimal("14.99998") + Decimal("24.9996"))
144        balance = newbalance
145
146        # Remove using high relay fee again
147        self.stop_node(0)
148        self.start_node(0, extra_args=["-minrelaytxfee=0.0001"])
149        assert_equal(len(self.nodes[0].getrawmempool()), 0)
150        newbalance = self.nodes[0].getbalance()
151        assert_equal(newbalance, balance - Decimal("24.9996"))
152        balance = newbalance
153
154        # Create a double spend of AB1 by spending again from only A's 10 output
155        # Mine double spend from node 1
156        inputs = []
157        inputs.append({"txid": txA, "vout": nA})
158        outputs = {}
159        outputs[self.nodes[1].getnewaddress()] = Decimal("9.9999")
160        tx = self.nodes[0].createrawtransaction(inputs, outputs)
161        signed = self.nodes[0].signrawtransactionwithwallet(tx)
162        self.nodes[1].sendrawtransaction(signed["hex"])
163        self.nodes[1].generate(1)
164
165        connect_nodes(self.nodes[0], 1)
166        sync_blocks(self.nodes)
167
168        # Verify that B and C's 10 BTC outputs are available for spending again because AB1 is now conflicted
169        newbalance = self.nodes[0].getbalance()
170        assert_equal(newbalance, balance + Decimal("20"))
171        balance = newbalance
172
173        # There is currently a minor bug around this and so this test doesn't work.  See Issue #7315
174        # Invalidate the block with the double spend and B's 10 BTC output should no longer be available
175        # Don't think C's should either
176        self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
177        newbalance = self.nodes[0].getbalance()
178        #assert_equal(newbalance, balance - Decimal("10"))
179        self.log.info("If balance has not declined after invalidateblock then out of mempool wallet tx which is no longer")
180        self.log.info("conflicted has not resumed causing its inputs to be seen as spent.  See Issue #7315")
181        self.log.info(str(balance) + " -> " + str(newbalance) + " ?")
182
183
184if __name__ == '__main__':
185    AbandonConflictTest().main()
186