7
7
from copy import deepcopy
8
8
from decimal import Decimal
9
9
10
- from test_framework .blocktools import COINBASE_MATURITY
11
10
from test_framework .messages import (
12
11
BIP125_SEQUENCE_NUMBER ,
13
12
COIN ,
18
17
)
19
18
from test_framework .script import CScript , OP_DROP
20
19
from test_framework .test_framework import BitcoinTestFramework
21
- from test_framework .util import assert_equal , assert_raises_rpc_error , satoshi_round
22
- from test_framework .script_util import DUMMY_P2WPKH_SCRIPT , DUMMY_2_P2WPKH_SCRIPT
20
+ from test_framework .util import (
21
+ assert_equal ,
22
+ assert_greater_than ,
23
+ assert_raises_rpc_error ,
24
+ )
25
+ from test_framework .script_util import (
26
+ DUMMY_P2WPKH_SCRIPT ,
27
+ DUMMY_2_P2WPKH_SCRIPT ,
28
+ )
23
29
from test_framework .wallet import MiniWallet
24
30
31
+
25
32
MAX_REPLACEMENT_LIMIT = 100
26
33
class ReplaceByFeeTest (BitcoinTestFramework ):
27
34
def set_test_params (self ):
@@ -89,29 +96,23 @@ def run_test(self):
89
96
def make_utxo (self , node , amount , confirmed = True , scriptPubKey = DUMMY_P2WPKH_SCRIPT ):
90
97
"""Create a txout with a given amount and scriptPubKey
91
98
92
- Mines coins as needed.
99
+ Assumes that MiniWallet has enough funds to cover the amount and the fixed fee
100
+ (from it's internal utxos, the one with the largest value is taken).
93
101
94
102
confirmed - txouts created will be confirmed in the blockchain;
95
103
unconfirmed otherwise.
96
104
"""
97
- fee = 1 * COIN
98
- while node .getbalance () < satoshi_round ((amount + fee ) / COIN ):
99
- self .generate (node , COINBASE_MATURITY )
100
-
101
- new_addr = node .getnewaddress ()
102
- txid = node .sendtoaddress (new_addr , satoshi_round ((amount + fee ) / COIN ))
103
- tx1 = node .getrawtransaction (txid , 1 )
104
- txid = int (txid , 16 )
105
- i , _ = next (filter (lambda vout : new_addr == vout [1 ]['scriptPubKey' ]['address' ], enumerate (tx1 ['vout' ])))
106
-
107
- tx2 = CTransaction ()
108
- tx2 .vin = [CTxIn (COutPoint (txid , i ))]
109
- tx2 .vout = [CTxOut (amount , scriptPubKey )]
110
- tx2 .rehash ()
111
-
112
- signed_tx = node .signrawtransactionwithwallet (tx2 .serialize ().hex ())
113
-
114
- txid = node .sendrawtransaction (signed_tx ['hex' ], 0 )
105
+ # MiniWallet only supports sweeping utxos to its own internal scriptPubKey, so in
106
+ # order to create an output with arbitrary amount/scriptPubKey, we have to add it
107
+ # manually after calling the create_self_transfer method. The MiniWallet output's
108
+ # nValue has to be adapted accordingly (amount and fee deduction). To keep things
109
+ # simple, we use a fixed fee of 1000 Satoshis here.
110
+ fee = 1000
111
+ tx = self .wallet .create_self_transfer (from_node = node , fee_rate = 0 , mempool_valid = False )['tx' ]
112
+ assert_greater_than (tx .vout [0 ].nValue , amount + fee )
113
+ tx .vout [0 ].nValue -= (amount + fee ) # change output -> MiniWallet
114
+ tx .vout .append (CTxOut (amount , scriptPubKey )) # desired output -> to be returned
115
+ txid = self .wallet .sendrawtransaction (from_node = node , tx_hex = tx .serialize ().hex ())
115
116
116
117
# If requested, ensure txouts are confirmed.
117
118
if confirmed :
@@ -124,7 +125,7 @@ def make_utxo(self, node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRI
124
125
assert new_size < mempool_size
125
126
mempool_size = new_size
126
127
127
- return COutPoint (int (txid , 16 ), 0 )
128
+ return COutPoint (int (txid , 16 ), 1 )
128
129
129
130
def test_simple_doublespend (self ):
130
131
"""Simple doublespend"""
0 commit comments