|
24 | 24 | from test_framework.test_framework import BitcoinTestFramework
|
25 | 25 | from test_framework.util import (
|
26 | 26 | assert_equal,
|
| 27 | + assert_fee_amount, |
27 | 28 | assert_greater_than,
|
28 | 29 | assert_raises_rpc_error,
|
29 | 30 | get_fee,
|
| 31 | + find_vout_for_address, |
30 | 32 | )
|
31 | 33 | from test_framework.wallet import MiniWallet
|
32 | 34 |
|
@@ -109,6 +111,8 @@ def run_test(self):
|
109 | 111 | test_small_output_with_feerate_succeeds(self, rbf_node, dest_address)
|
110 | 112 | test_no_more_inputs_fails(self, rbf_node, dest_address)
|
111 | 113 | self.test_bump_back_to_yourself()
|
| 114 | + self.test_provided_change_pos(rbf_node) |
| 115 | + self.test_single_output() |
112 | 116 |
|
113 | 117 | # Context independent tests
|
114 | 118 | test_feerate_checks_replaced_outputs(self, rbf_node, peer_node)
|
@@ -174,6 +178,13 @@ def test_invalid_parameters(self, rbf_node, peer_node, dest_address):
|
174 | 178 | assert_raises_rpc_error(-8, "Invalid parameter, duplicate key: data",
|
175 | 179 | rbf_node.bumpfee, rbfid, {"outputs": [{"data": "deadbeef"}, {"data": "deadbeef"}]})
|
176 | 180 |
|
| 181 | + self.log.info("Test reduce_output option") |
| 182 | + assert_raises_rpc_error(-1, "JSON integer out of range", rbf_node.bumpfee, rbfid, {"reduce_output": -1}) |
| 183 | + assert_raises_rpc_error(-8, "Change position is out of range", rbf_node.bumpfee, rbfid, {"reduce_output": 2}) |
| 184 | + |
| 185 | + self.log.info("Test outputs and reduce_output cannot both be provided") |
| 186 | + assert_raises_rpc_error(-8, "Cannot specify both new outputs to use and an output index to reduce", rbf_node.bumpfee, rbfid, {"reduce_output": 2, "outputs": [{dest_address: 0.1}]}) |
| 187 | + |
177 | 188 | self.clear_mempool()
|
178 | 189 |
|
179 | 190 | def test_bump_back_to_yourself(self):
|
@@ -225,6 +236,72 @@ def test_bump_back_to_yourself(self):
|
225 | 236 |
|
226 | 237 | node.unloadwallet("back_to_yourself")
|
227 | 238 |
|
| 239 | + def test_provided_change_pos(self, rbf_node): |
| 240 | + self.log.info("Test the reduce_output option") |
| 241 | + |
| 242 | + change_addr = rbf_node.getnewaddress() |
| 243 | + dest_addr = rbf_node.getnewaddress() |
| 244 | + assert_equal(rbf_node.getaddressinfo(change_addr)["ischange"], False) |
| 245 | + assert_equal(rbf_node.getaddressinfo(dest_addr)["ischange"], False) |
| 246 | + |
| 247 | + send_res = rbf_node.send(outputs=[{dest_addr: 1}], options={"change_address": change_addr}) |
| 248 | + assert send_res["complete"] |
| 249 | + txid = send_res["txid"] |
| 250 | + |
| 251 | + tx = rbf_node.gettransaction(txid=txid, verbose=True) |
| 252 | + assert_equal(len(tx["decoded"]["vout"]), 2) |
| 253 | + |
| 254 | + change_pos = find_vout_for_address(rbf_node, txid, change_addr) |
| 255 | + change_value = tx["decoded"]["vout"][change_pos]["value"] |
| 256 | + |
| 257 | + bumped = rbf_node.bumpfee(txid, {"reduce_output": change_pos}) |
| 258 | + new_txid = bumped["txid"] |
| 259 | + |
| 260 | + new_tx = rbf_node.gettransaction(txid=new_txid, verbose=True) |
| 261 | + assert_equal(len(new_tx["decoded"]["vout"]), 2) |
| 262 | + new_change_pos = find_vout_for_address(rbf_node, new_txid, change_addr) |
| 263 | + new_change_value = new_tx["decoded"]["vout"][new_change_pos]["value"] |
| 264 | + |
| 265 | + assert_greater_than(change_value, new_change_value) |
| 266 | + |
| 267 | + |
| 268 | + def test_single_output(self): |
| 269 | + self.log.info("Test that single output txs can be bumped") |
| 270 | + node = self.nodes[1] |
| 271 | + |
| 272 | + node.createwallet("single_out_rbf") |
| 273 | + wallet = node.get_wallet_rpc("single_out_rbf") |
| 274 | + |
| 275 | + addr = wallet.getnewaddress() |
| 276 | + amount = Decimal("0.001") |
| 277 | + # Make 2 UTXOs |
| 278 | + self.nodes[0].sendtoaddress(addr, amount) |
| 279 | + self.nodes[0].sendtoaddress(addr, amount) |
| 280 | + self.generate(self.nodes[0], 1) |
| 281 | + utxos = wallet.listunspent() |
| 282 | + |
| 283 | + tx = wallet.sendall(recipients=[wallet.getnewaddress()], fee_rate=2, options={"inputs": [utxos[0]]}) |
| 284 | + |
| 285 | + # Reduce the only output with a crazy high feerate, should fail as the output would be dust |
| 286 | + assert_raises_rpc_error(-4, "The transaction amount is too small to pay the fee", wallet.bumpfee, txid=tx["txid"], options={"fee_rate": 1100, "reduce_output": 0}) |
| 287 | + |
| 288 | + # Reduce the only output successfully |
| 289 | + bumped = wallet.bumpfee(txid=tx["txid"], options={"fee_rate": 10, "reduce_output": 0}) |
| 290 | + bumped_tx = wallet.gettransaction(txid=bumped["txid"], verbose=True) |
| 291 | + assert_equal(len(bumped_tx["decoded"]["vout"]), 1) |
| 292 | + assert_equal(len(bumped_tx["decoded"]["vin"]), 1) |
| 293 | + assert_equal(bumped_tx["decoded"]["vout"][0]["value"] + bumped["fee"], amount) |
| 294 | + assert_fee_amount(bumped["fee"], bumped_tx["decoded"]["vsize"], Decimal(10) / Decimal(1e8) * 1000) |
| 295 | + |
| 296 | + # Bumping without reducing adds a new input and output |
| 297 | + bumped = wallet.bumpfee(txid=bumped["txid"], options={"fee_rate": 20}) |
| 298 | + bumped_tx = wallet.gettransaction(txid=bumped["txid"], verbose=True) |
| 299 | + assert_equal(len(bumped_tx["decoded"]["vout"]), 2) |
| 300 | + assert_equal(len(bumped_tx["decoded"]["vin"]), 2) |
| 301 | + assert_fee_amount(bumped["fee"], bumped_tx["decoded"]["vsize"], Decimal(20) / Decimal(1e8) * 1000) |
| 302 | + |
| 303 | + wallet.unloadwallet() |
| 304 | + |
228 | 305 | def test_simple_bumpfee_succeeds(self, mode, rbf_node, peer_node, dest_address):
|
229 | 306 | self.log.info('Test simple bumpfee: {}'.format(mode))
|
230 | 307 | rbfid = spend_one_input(rbf_node, dest_address)
|
|
0 commit comments