2016-12-09 19:45:27 +01:00
#!/usr/bin/env python3
2019-02-21 02:03:13 +01:00
# Copyright (c) 2016-2019 The Bitcoin Core developers
2016-12-09 19:45:27 +01:00
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-03-20 22:17:27 +01:00
""" Test the bumpfee RPC.
Verifies that the bumpfee RPC creates replacement transactions successfully when
its preconditions are met , and returns appropriate errors in other cases .
This module consists of around a dozen individual test cases implemented in the
top - level functions named as test_ < test_case_description > . The test functions
can be disabled or reordered if needed for debugging . If new test cases are
2017-06-20 00:57:31 +02:00
added in the future , they should try to follow the same convention and not
2017-03-20 22:17:27 +01:00
make assumptions about execution order .
"""
2018-07-07 00:10:35 +02:00
from decimal import Decimal
2018-09-23 14:50:47 +02:00
import io
2018-07-07 00:10:35 +02:00
from test_framework . blocktools import add_witness_commitment , create_block , create_coinbase , send_to_witness
from test_framework . messages import BIP125_SEQUENCE_NUMBER , CTransaction
2016-12-09 19:45:27 +01:00
from test_framework . test_framework import BitcoinTestFramework
2019-04-07 00:38:51 +02:00
from test_framework . util import (
assert_equal ,
assert_greater_than ,
assert_raises_rpc_error ,
2019-08-27 21:13:56 +02:00
connect_nodes ,
2019-04-07 00:38:51 +02:00
hex_str_to_bytes ,
)
2016-12-09 19:45:27 +01:00
WALLET_PASSPHRASE = " test "
WALLET_PASSPHRASE_TIMEOUT = 3600
class BumpFeeTest ( BitcoinTestFramework ) :
2017-06-10 00:21:21 +02:00
def set_test_params ( self ) :
2016-12-09 19:45:27 +01:00
self . num_nodes = 2
self . setup_clean_chain = True
2018-09-09 19:32:37 +02:00
self . extra_args = [ [
" -walletrbf= {} " . format ( i ) ,
" -mintxfee=0.00002 " ,
2019-05-09 22:31:57 +02:00
" -deprecatedrpc=totalFee " ,
2018-09-09 19:32:37 +02:00
] for i in range ( self . num_nodes ) ]
def skip_test_if_missing_module ( self ) :
self . skip_if_no_wallet ( )
2016-12-09 19:45:27 +01:00
2017-06-09 22:35:17 +02:00
def run_test ( self ) :
2016-12-09 19:45:27 +01:00
# Encrypt wallet for test_locked_wallet_fails test
2018-02-20 22:09:51 +01:00
self . nodes [ 1 ] . encryptwallet ( WALLET_PASSPHRASE )
2016-12-09 19:45:27 +01:00
self . nodes [ 1 ] . walletpassphrase ( WALLET_PASSPHRASE , WALLET_PASSPHRASE_TIMEOUT )
2019-08-27 21:13:56 +02:00
connect_nodes ( self . nodes [ 0 ] , 1 )
2016-12-09 19:45:27 +01:00
self . sync_all ( )
peer_node , rbf_node = self . nodes
rbf_node_address = rbf_node . getnewaddress ( )
# fund rbf node with 10 coins of 0.001 btc (100,000 satoshis)
2017-03-08 00:46:17 +01:00
self . log . info ( " Mining blocks... " )
2020-02-13 16:41:28 +01:00
peer_node . generate ( 160 )
2016-12-09 19:45:27 +01:00
self . sync_all ( )
for i in range ( 25 ) :
peer_node . sendtoaddress ( rbf_node_address , 0.001 )
self . sync_all ( )
peer_node . generate ( 1 )
self . sync_all ( )
assert_equal ( rbf_node . getbalance ( ) , Decimal ( " 0.025 " ) )
2017-03-08 00:46:17 +01:00
self . log . info ( " Running tests " )
2016-12-09 19:45:27 +01:00
dest_address = peer_node . getnewaddress ( )
2019-07-30 01:59:23 +02:00
test_simple_bumpfee_succeeds ( self , " default " , rbf_node , peer_node , dest_address )
test_simple_bumpfee_succeeds ( self , " fee_rate " , rbf_node , peer_node , dest_address )
test_feerate_args ( self , rbf_node , peer_node , dest_address )
2016-12-09 19:45:27 +01:00
test_segwit_bumpfee_succeeds ( rbf_node , dest_address )
test_nonrbf_bumpfee_fails ( peer_node , dest_address )
test_notmine_bumpfee_fails ( rbf_node , peer_node , dest_address )
test_bumpfee_with_descendant_fails ( rbf_node , rbf_node_address , dest_address )
2017-02-07 00:29:55 +01:00
test_small_output_fails ( rbf_node , dest_address )
test_dust_to_fee ( rbf_node , dest_address )
2016-12-09 19:45:27 +01:00
test_settxfee ( rbf_node , dest_address )
test_rebumping ( rbf_node , dest_address )
test_rebumping_not_replaceable ( rbf_node , dest_address )
test_unconfirmed_not_spendable ( rbf_node , rbf_node_address )
2017-02-02 23:34:22 +01:00
test_bumpfee_metadata ( rbf_node , dest_address )
2016-12-09 19:45:27 +01:00
test_locked_wallet_fails ( rbf_node , dest_address )
2019-03-08 22:43:40 +01:00
test_change_script_match ( rbf_node , dest_address )
2019-07-02 16:16:36 +02:00
test_maxtxfee_fails ( self , rbf_node , dest_address )
2019-03-08 17:30:53 +01:00
# These tests wipe out a number of utxos that are expected in other tests
test_small_output_with_feerate_succeeds ( rbf_node , dest_address )
test_no_more_inputs_fails ( rbf_node , dest_address )
2017-03-08 00:46:17 +01:00
self . log . info ( " Success " )
2016-12-09 19:45:27 +01:00
2019-07-30 01:59:23 +02:00
def test_simple_bumpfee_succeeds ( self , mode , rbf_node , peer_node , dest_address ) :
2017-02-07 00:27:24 +01:00
rbfid = spend_one_input ( rbf_node , dest_address )
2016-12-09 19:45:27 +01:00
rbftx = rbf_node . gettransaction ( rbfid )
2019-04-07 00:19:45 +02:00
self . sync_mempools ( ( rbf_node , peer_node ) )
2016-12-09 19:45:27 +01:00
assert rbfid in rbf_node . getrawmempool ( ) and rbfid in peer_node . getrawmempool ( )
2019-07-30 01:59:23 +02:00
if mode == " fee_rate " :
bumped_tx = rbf_node . bumpfee ( rbfid , { " fee_rate " : 0.0015 } )
else :
bumped_tx = rbf_node . bumpfee ( rbfid )
2017-05-24 22:15:26 +02:00
assert_equal ( bumped_tx [ " errors " ] , [ ] )
2019-12-01 15:46:44 +01:00
assert bumped_tx [ " fee " ] > - rbftx [ " fee " ]
assert_equal ( bumped_tx [ " origfee " ] , - rbftx [ " fee " ] )
2017-08-16 00:24:39 +02:00
# check that bumped_tx propagates, original tx was evicted and has a wallet conflict
2019-04-07 00:19:45 +02:00
self . sync_mempools ( ( rbf_node , peer_node ) )
2016-12-09 19:45:27 +01:00
assert bumped_tx [ " txid " ] in rbf_node . getrawmempool ( )
assert bumped_tx [ " txid " ] in peer_node . getrawmempool ( )
assert rbfid not in rbf_node . getrawmempool ( )
assert rbfid not in peer_node . getrawmempool ( )
oldwtx = rbf_node . gettransaction ( rbfid )
assert len ( oldwtx [ " walletconflicts " ] ) > 0
# check wallet transaction replaces and replaced_by values
bumpedwtx = rbf_node . gettransaction ( bumped_tx [ " txid " ] )
assert_equal ( oldwtx [ " replaced_by_txid " ] , bumped_tx [ " txid " ] )
assert_equal ( bumpedwtx [ " replaces_txid " ] , rbfid )
2019-07-30 01:59:23 +02:00
def test_feerate_args ( self , rbf_node , peer_node , dest_address ) :
rbfid = spend_one_input ( rbf_node , dest_address )
self . sync_mempools ( ( rbf_node , peer_node ) )
assert rbfid in rbf_node . getrawmempool ( ) and rbfid in peer_node . getrawmempool ( )
assert_raises_rpc_error ( - 8 , " confTarget can ' t be set with totalFee or fee_rate. Please provide either a confirmation target in blocks for automatic fee estimation, or an explicit fee rate. " , rbf_node . bumpfee , rbfid , { " fee_rate " : 0.00001 , " confTarget " : 1 } )
assert_raises_rpc_error ( - 8 , " confTarget can ' t be set with totalFee or fee_rate. Please provide either a confirmation target in blocks for automatic fee estimation, or an explicit fee rate. " , rbf_node . bumpfee , rbfid , { " totalFee " : 0.00001 , " confTarget " : 1 } )
assert_raises_rpc_error ( - 8 , " fee_rate can ' t be set along with totalFee. " , rbf_node . bumpfee , rbfid , { " fee_rate " : 0.00001 , " totalFee " : 0.001 } )
# Bumping to just above minrelay should fail to increase total fee enough, at least
assert_raises_rpc_error ( - 8 , " Insufficient total fee " , rbf_node . bumpfee , rbfid , { " fee_rate " : 0.00001000 } )
assert_raises_rpc_error ( - 3 , " Amount out of range " , rbf_node . bumpfee , rbfid , { " fee_rate " : - 1 } )
assert_raises_rpc_error ( - 4 , " is too high (cannot be higher than " , rbf_node . bumpfee , rbfid , { " fee_rate " : 1 } )
2016-12-09 19:45:27 +01:00
def test_segwit_bumpfee_succeeds ( rbf_node , dest_address ) :
# Create a transaction with segwit output, then create an RBF transaction
# which spends it, and make sure bumpfee can be called on it.
segwit_in = next ( u for u in rbf_node . listunspent ( ) if u [ " amount " ] == Decimal ( " 0.001 " ) )
2018-09-23 05:01:32 +02:00
segwit_out = rbf_node . getaddressinfo ( rbf_node . getnewaddress ( address_type = ' p2sh-segwit ' ) )
2016-12-09 19:45:27 +01:00
segwitid = send_to_witness (
2017-03-10 17:21:20 +01:00
use_p2wsh = False ,
2016-12-09 19:45:27 +01:00
node = rbf_node ,
utxo = segwit_in ,
pubkey = segwit_out [ " pubkey " ] ,
encode_p2sh = False ,
amount = Decimal ( " 0.0009 " ) ,
sign = True )
rbfraw = rbf_node . createrawtransaction ( [ {
' txid ' : segwitid ,
' vout ' : 0 ,
" sequence " : BIP125_SEQUENCE_NUMBER
} ] , { dest_address : Decimal ( " 0.0005 " ) ,
2017-02-06 19:50:08 +01:00
rbf_node . getrawchangeaddress ( ) : Decimal ( " 0.0003 " ) } )
2017-09-06 01:49:18 +02:00
rbfsigned = rbf_node . signrawtransactionwithwallet ( rbfraw )
2016-12-09 19:45:27 +01:00
rbfid = rbf_node . sendrawtransaction ( rbfsigned [ " hex " ] )
assert rbfid in rbf_node . getrawmempool ( )
bumped_tx = rbf_node . bumpfee ( rbfid )
assert bumped_tx [ " txid " ] in rbf_node . getrawmempool ( )
assert rbfid not in rbf_node . getrawmempool ( )
def test_nonrbf_bumpfee_fails ( peer_node , dest_address ) :
# cannot replace a non RBF transaction (from node which did not enable RBF)
2017-02-07 00:27:24 +01:00
not_rbfid = peer_node . sendtoaddress ( dest_address , Decimal ( " 0.00090000 " ) )
2017-07-12 16:33:46 +02:00
assert_raises_rpc_error ( - 4 , " not BIP 125 replaceable " , peer_node . bumpfee , not_rbfid )
2016-12-09 19:45:27 +01:00
def test_notmine_bumpfee_fails ( rbf_node , peer_node , dest_address ) :
# cannot bump fee unless the tx has only inputs that we own.
# here, the rbftx has a peer_node coin and then adds a rbf_node input
# Note that this test depends upon the RPC code checking input ownership prior to change outputs
# (since it can't use fundrawtransaction, it lacks a proper change output)
utxos = [ node . listunspent ( ) [ - 1 ] for node in ( rbf_node , peer_node ) ]
inputs = [ {
" txid " : utxo [ " txid " ] ,
" vout " : utxo [ " vout " ] ,
" address " : utxo [ " address " ] ,
" sequence " : BIP125_SEQUENCE_NUMBER
} for utxo in utxos ]
output_val = sum ( utxo [ " amount " ] for utxo in utxos ) - Decimal ( " 0.001 " )
rawtx = rbf_node . createrawtransaction ( inputs , { dest_address : output_val } )
2017-09-06 01:49:18 +02:00
signedtx = rbf_node . signrawtransactionwithwallet ( rawtx )
signedtx = peer_node . signrawtransactionwithwallet ( signedtx [ " hex " ] )
2016-12-09 19:45:27 +01:00
rbfid = rbf_node . sendrawtransaction ( signedtx [ " hex " ] )
2017-07-12 16:33:46 +02:00
assert_raises_rpc_error ( - 4 , " Transaction contains inputs that don ' t belong to this wallet " ,
2018-09-23 14:50:47 +02:00
rbf_node . bumpfee , rbfid )
2016-12-09 19:45:27 +01:00
def test_bumpfee_with_descendant_fails ( rbf_node , rbf_node_address , dest_address ) :
# cannot bump fee if the transaction has a descendant
# parent is send-to-self, so we don't have to check which output is change when creating the child tx
2017-02-07 00:27:24 +01:00
parent_id = spend_one_input ( rbf_node , rbf_node_address )
2016-12-09 19:45:27 +01:00
tx = rbf_node . createrawtransaction ( [ { " txid " : parent_id , " vout " : 0 } ] , { dest_address : 0.00020000 } )
2017-09-06 01:49:18 +02:00
tx = rbf_node . signrawtransactionwithwallet ( tx )
2017-07-20 23:10:37 +02:00
rbf_node . sendrawtransaction ( tx [ " hex " ] )
2017-07-12 16:33:46 +02:00
assert_raises_rpc_error ( - 8 , " Transaction has descendants in the wallet " , rbf_node . bumpfee , parent_id )
2016-12-09 19:45:27 +01:00
def test_small_output_fails ( rbf_node , dest_address ) :
# cannot bump fee with a too-small output
2017-02-07 00:27:24 +01:00
rbfid = spend_one_input ( rbf_node , dest_address )
rbf_node . bumpfee ( rbfid , { " totalFee " : 50000 } )
2016-12-09 19:45:27 +01:00
2017-02-07 00:27:24 +01:00
rbfid = spend_one_input ( rbf_node , dest_address )
2017-07-12 16:33:46 +02:00
assert_raises_rpc_error ( - 4 , " Change output is too small " , rbf_node . bumpfee , rbfid , { " totalFee " : 50001 } )
2016-12-09 19:45:27 +01:00
2019-03-08 17:30:53 +01:00
def test_small_output_with_feerate_succeeds ( rbf_node , dest_address ) :
# Make sure additional inputs exist
rbf_node . generatetoaddress ( 101 , rbf_node . getnewaddress ( ) )
rbfid = spend_one_input ( rbf_node , dest_address )
original_input_list = rbf_node . getrawtransaction ( rbfid , 1 ) [ " vin " ]
assert_equal ( len ( original_input_list ) , 1 )
original_txin = original_input_list [ 0 ]
# Keep bumping until we out-spend change output
tx_fee = 0
while tx_fee < Decimal ( " 0.0005 " ) :
new_input_list = rbf_node . getrawtransaction ( rbfid , 1 ) [ " vin " ]
new_item = list ( new_input_list ) [ 0 ]
assert_equal ( len ( original_input_list ) , 1 )
assert_equal ( original_txin [ " txid " ] , new_item [ " txid " ] )
assert_equal ( original_txin [ " vout " ] , new_item [ " vout " ] )
rbfid_new_details = rbf_node . bumpfee ( rbfid )
rbfid_new = rbfid_new_details [ " txid " ]
raw_pool = rbf_node . getrawmempool ( )
assert rbfid not in raw_pool
assert rbfid_new in raw_pool
rbfid = rbfid_new
tx_fee = rbfid_new_details [ " origfee " ]
# input(s) have been added
final_input_list = rbf_node . getrawtransaction ( rbfid , 1 ) [ " vin " ]
assert_greater_than ( len ( final_input_list ) , 1 )
# Original input is in final set
assert [ txin for txin in final_input_list
if txin [ " txid " ] == original_txin [ " txid " ]
and txin [ " vout " ] == original_txin [ " vout " ] ]
rbf_node . generatetoaddress ( 1 , rbf_node . getnewaddress ( ) )
assert_equal ( rbf_node . gettransaction ( rbfid ) [ " confirmations " ] , 1 )
2016-12-09 19:45:27 +01:00
def test_dust_to_fee ( rbf_node , dest_address ) :
# check that if output is reduced to dust, it will be converted to fee
2017-02-07 00:27:24 +01:00
# the bumped tx sets fee=49,900, but it converts to 50,000
rbfid = spend_one_input ( rbf_node , dest_address )
2016-12-09 19:45:27 +01:00
fulltx = rbf_node . getrawtransaction ( rbfid , 1 )
2018-03-28 21:39:26 +02:00
# (32-byte p2sh-pwpkh output size + 148 p2pkh spend estimate) * 10k(discard_rate) / 1000 = 1800
# P2SH outputs are slightly "over-discarding" due to the IsDust calculation assuming it will
# be spent as a P2PKH.
2018-09-23 14:50:47 +02:00
bumped_tx = rbf_node . bumpfee ( rbfid , { " totalFee " : 50000 - 1800 } )
2016-12-09 19:45:27 +01:00
full_bumped_tx = rbf_node . getrawtransaction ( bumped_tx [ " txid " ] , 1 )
2017-02-07 00:27:24 +01:00
assert_equal ( bumped_tx [ " fee " ] , Decimal ( " 0.00050000 " ) )
2016-12-09 19:45:27 +01:00
assert_equal ( len ( fulltx [ " vout " ] ) , 2 )
2018-09-23 14:50:47 +02:00
assert_equal ( len ( full_bumped_tx [ " vout " ] ) , 1 ) # change output is eliminated
2016-12-09 19:45:27 +01:00
def test_settxfee ( rbf_node , dest_address ) :
2018-08-20 19:34:39 +02:00
assert_raises_rpc_error ( - 8 , " txfee cannot be less than min relay tx fee " , rbf_node . settxfee , Decimal ( ' 0.000005 ' ) )
assert_raises_rpc_error ( - 8 , " txfee cannot be less than wallet min fee " , rbf_node . settxfee , Decimal ( ' 0.000015 ' ) )
2016-12-09 19:45:27 +01:00
# check that bumpfee reacts correctly to the use of settxfee (paytxfee)
2017-02-07 00:27:24 +01:00
rbfid = spend_one_input ( rbf_node , dest_address )
requested_feerate = Decimal ( " 0.00025000 " )
rbf_node . settxfee ( requested_feerate )
2016-12-09 19:45:27 +01:00
bumped_tx = rbf_node . bumpfee ( rbfid )
2017-12-01 01:49:11 +01:00
actual_feerate = bumped_tx [ " fee " ] * 1000 / rbf_node . getrawtransaction ( bumped_tx [ " txid " ] , True ) [ " vsize " ]
2017-02-07 00:27:24 +01:00
# Assert that the difference between the requested feerate and the actual
# feerate of the bumped transaction is small.
assert_greater_than ( Decimal ( " 0.00001000 " ) , abs ( requested_feerate - actual_feerate ) )
2016-12-09 19:45:27 +01:00
rbf_node . settxfee ( Decimal ( " 0.00000000 " ) ) # unset paytxfee
2019-07-02 16:16:36 +02:00
def test_maxtxfee_fails ( test , rbf_node , dest_address ) :
test . restart_node ( 1 , [ ' -maxtxfee=0.00003 ' ] + test . extra_args [ 1 ] )
rbf_node . walletpassphrase ( WALLET_PASSPHRASE , WALLET_PASSPHRASE_TIMEOUT )
rbfid = spend_one_input ( rbf_node , dest_address )
assert_raises_rpc_error ( - 4 , " Unable to create transaction: Fee exceeds maximum configured by -maxtxfee " , rbf_node . bumpfee , rbfid )
test . restart_node ( 1 , test . extra_args [ 1 ] )
rbf_node . walletpassphrase ( WALLET_PASSPHRASE , WALLET_PASSPHRASE_TIMEOUT )
2016-12-09 19:45:27 +01:00
def test_rebumping ( rbf_node , dest_address ) :
# check that re-bumping the original tx fails, but bumping the bumper succeeds
2017-02-07 00:27:24 +01:00
rbfid = spend_one_input ( rbf_node , dest_address )
bumped = rbf_node . bumpfee ( rbfid , { " totalFee " : 2000 } )
2017-07-12 16:33:46 +02:00
assert_raises_rpc_error ( - 4 , " already bumped " , rbf_node . bumpfee , rbfid , { " totalFee " : 3000 } )
2017-02-07 00:27:24 +01:00
rbf_node . bumpfee ( bumped [ " txid " ] , { " totalFee " : 3000 } )
2016-12-09 19:45:27 +01:00
def test_rebumping_not_replaceable ( rbf_node , dest_address ) :
# check that re-bumping a non-replaceable bump tx fails
2017-02-07 00:27:24 +01:00
rbfid = spend_one_input ( rbf_node , dest_address )
2016-12-09 19:45:27 +01:00
bumped = rbf_node . bumpfee ( rbfid , { " totalFee " : 10000 , " replaceable " : False } )
2017-07-12 16:33:46 +02:00
assert_raises_rpc_error ( - 4 , " Transaction is not BIP 125 replaceable " , rbf_node . bumpfee , bumped [ " txid " ] ,
2018-09-23 14:50:47 +02:00
{ " totalFee " : 20000 } )
2016-12-09 19:45:27 +01:00
def test_unconfirmed_not_spendable ( rbf_node , rbf_node_address ) :
# check that unconfirmed outputs from bumped transactions are not spendable
2017-02-07 00:27:24 +01:00
rbfid = spend_one_input ( rbf_node , rbf_node_address )
2016-12-09 19:45:27 +01:00
rbftx = rbf_node . gettransaction ( rbfid ) [ " hex " ]
assert rbfid in rbf_node . getrawmempool ( )
bumpid = rbf_node . bumpfee ( rbfid ) [ " txid " ]
assert bumpid in rbf_node . getrawmempool ( )
assert rbfid not in rbf_node . getrawmempool ( )
# check that outputs from the bump transaction are not spendable
# due to the replaces_txid check in CWallet::AvailableCoins
assert_equal ( [ t for t in rbf_node . listunspent ( minconf = 0 , include_unsafe = False ) if t [ " txid " ] == bumpid ] , [ ] )
# submit a block with the rbf tx to clear the bump tx out of the mempool,
2018-01-27 23:45:32 +01:00
# then invalidate the block so the rbf tx will be put back in the mempool.
# This makes it possible to check whether the rbf tx outputs are
2016-12-09 19:45:27 +01:00
# spendable before the rbf tx is confirmed.
block = submit_block_with_tx ( rbf_node , rbftx )
2018-01-27 23:45:32 +01:00
# Can not abandon conflicted tx
assert_raises_rpc_error ( - 5 , ' Transaction not eligible for abandonment ' , lambda : rbf_node . abandontransaction ( txid = bumpid ) )
2016-12-09 19:45:27 +01:00
rbf_node . invalidateblock ( block . hash )
2018-01-27 23:45:32 +01:00
# Call abandon to make sure the wallet doesn't attempt to resubmit
# the bump tx and hope the wallet does not rebroadcast before we call.
rbf_node . abandontransaction ( bumpid )
2016-12-09 19:45:27 +01:00
assert bumpid not in rbf_node . getrawmempool ( )
assert rbfid in rbf_node . getrawmempool ( )
# check that outputs from the rbf tx are not spendable before the
# transaction is confirmed, due to the replaced_by_txid check in
# CWallet::AvailableCoins
assert_equal ( [ t for t in rbf_node . listunspent ( minconf = 0 , include_unsafe = False ) if t [ " txid " ] == rbfid ] , [ ] )
# check that the main output from the rbf tx is spendable after confirmed
rbf_node . generate ( 1 )
assert_equal (
sum ( 1 for t in rbf_node . listunspent ( minconf = 0 , include_unsafe = False )
if t [ " txid " ] == rbfid and t [ " address " ] == rbf_node_address and t [ " spendable " ] ) , 1 )
2017-02-02 23:34:22 +01:00
def test_bumpfee_metadata ( rbf_node , dest_address ) :
2020-02-13 16:41:28 +01:00
assert ( rbf_node . getbalance ( ) < 0.98 )
2019-03-05 17:04:32 +01:00
rbf_node . generatetoaddress ( 101 , rbf_node . getnewaddress ( ) )
2020-02-13 16:41:28 +01:00
rbfid = rbf_node . sendtoaddress ( dest_address , 0.98 , " comment value " , " to value " )
2017-02-02 23:34:22 +01:00
bumped_tx = rbf_node . bumpfee ( rbfid )
bumped_wtx = rbf_node . gettransaction ( bumped_tx [ " txid " ] )
assert_equal ( bumped_wtx [ " comment " ] , " comment value " )
assert_equal ( bumped_wtx [ " to " ] , " to value " )
2016-12-09 19:45:27 +01:00
def test_locked_wallet_fails ( rbf_node , dest_address ) :
2017-02-07 00:27:24 +01:00
rbfid = spend_one_input ( rbf_node , dest_address )
2016-12-09 19:45:27 +01:00
rbf_node . walletlock ( )
2017-07-12 16:33:46 +02:00
assert_raises_rpc_error ( - 13 , " Please enter the wallet passphrase with walletpassphrase first. " ,
2018-09-23 14:50:47 +02:00
rbf_node . bumpfee , rbfid )
2019-03-08 17:30:53 +01:00
rbf_node . walletpassphrase ( WALLET_PASSPHRASE , WALLET_PASSPHRASE_TIMEOUT )
2016-12-09 19:45:27 +01:00
2019-03-08 22:43:40 +01:00
def test_change_script_match ( rbf_node , dest_address ) :
""" Test that the same change addresses is used for the replacement transaction when possible. """
def get_change_address ( tx ) :
tx_details = rbf_node . getrawtransaction ( tx , 1 )
txout_addresses = [ txout [ ' scriptPubKey ' ] [ ' addresses ' ] [ 0 ] for txout in tx_details [ " vout " ] ]
return [ address for address in txout_addresses if rbf_node . getaddressinfo ( address ) [ " ischange " ] ]
2016-12-09 19:45:27 +01:00
2019-03-08 22:43:40 +01:00
# Check that there is only one change output
rbfid = spend_one_input ( rbf_node , dest_address )
change_addresses = get_change_address ( rbfid )
assert_equal ( len ( change_addresses ) , 1 )
# Now find that address in each subsequent tx, and no other change
bumped_total_tx = rbf_node . bumpfee ( rbfid , { " totalFee " : 2000 } )
assert_equal ( change_addresses , get_change_address ( bumped_total_tx [ ' txid ' ] ) )
bumped_rate_tx = rbf_node . bumpfee ( bumped_total_tx [ " txid " ] )
assert_equal ( change_addresses , get_change_address ( bumped_rate_tx [ ' txid ' ] ) )
2016-12-09 19:45:27 +01:00
2019-03-08 17:30:53 +01:00
def spend_one_input ( node , dest_address , change_size = Decimal ( " 0.00049000 " ) ) :
2017-03-16 15:49:42 +01:00
tx_input = dict (
2017-02-07 00:27:24 +01:00
sequence = BIP125_SEQUENCE_NUMBER , * * next ( u for u in node . listunspent ( ) if u [ " amount " ] == Decimal ( " 0.00100000 " ) ) )
2019-03-08 17:30:53 +01:00
destinations = { dest_address : Decimal ( " 0.00050000 " ) }
if change_size > 0 :
destinations [ node . getrawchangeaddress ( ) ] = change_size
rawtx = node . createrawtransaction ( [ tx_input ] , destinations )
2017-09-06 01:49:18 +02:00
signedtx = node . signrawtransactionwithwallet ( rawtx )
2016-12-09 19:45:27 +01:00
txid = node . sendrawtransaction ( signedtx [ " hex " ] )
return txid
def submit_block_with_tx ( node , tx ) :
ctx = CTransaction ( )
ctx . deserialize ( io . BytesIO ( hex_str_to_bytes ( tx ) ) )
tip = node . getbestblockhash ( )
height = node . getblockcount ( ) + 1
block_time = node . getblockheader ( tip ) [ " mediantime " ] + 1
2018-07-07 00:10:35 +02:00
block = create_block ( int ( tip , 16 ) , create_coinbase ( height ) , block_time )
2016-12-09 19:45:27 +01:00
block . vtx . append ( ctx )
block . rehash ( )
block . hashMerkleRoot = block . calc_merkle_root ( )
2018-07-07 00:10:35 +02:00
add_witness_commitment ( block )
2016-12-09 19:45:27 +01:00
block . solve ( )
2019-05-08 16:03:06 +02:00
node . submitblock ( block . serialize ( ) . hex ( ) )
2016-12-09 19:45:27 +01:00
return block
2019-03-08 17:30:53 +01:00
def test_no_more_inputs_fails ( rbf_node , dest_address ) :
# feerate rbf requires confirmed outputs when change output doesn't exist or is insufficient
rbf_node . generatetoaddress ( 1 , dest_address )
# spend all funds, no change output
rbfid = rbf_node . sendtoaddress ( rbf_node . getnewaddress ( ) , rbf_node . getbalance ( ) , " " , " " , True )
assert_raises_rpc_error ( - 4 , " Unable to create transaction: Insufficient funds " , rbf_node . bumpfee , rbfid )
2016-12-09 19:45:27 +01:00
if __name__ == " __main__ " :
BumpFeeTest ( ) . main ( )