24
24
25
25
class WalletTest (BitcoinTestFramework ):
26
26
27
+ def check_fee_amount (self , curr_balance , balance_with_fee , fee_per_byte , tx_size ):
28
+ """Return curr_balance after asserting the fee was in range"""
29
+ fee = balance_with_fee - curr_balance
30
+ target_fee = fee_per_byte * tx_size
31
+ if fee < target_fee :
32
+ raise AssertionError ("Fee of %s BTC too low! (Should be %s BTC)" % (str (fee ), str (target_fee )))
33
+ # allow the node's estimation to be at most 2 bytes off
34
+ if fee > fee_per_byte * (tx_size + 2 ):
35
+ raise AssertionError ("Fee of %s BTC too high! (Should be %s BTC)" % (str (fee ), str (target_fee )))
36
+ return curr_balance
37
+
27
38
def setup_chain (self ):
28
39
print ("Initializing test directory " + self .options .tmpdir )
29
40
initialize_chain_clean (self .options .tmpdir , 4 )
@@ -104,33 +115,37 @@ def run_test (self):
104
115
105
116
# Send 10 BTC normal
106
117
address = self .nodes [0 ].getnewaddress ("test" )
107
- self .nodes [2 ].settxfee (Decimal ('0.001' ))
118
+ fee_per_byte = Decimal ('0.001' ) / 1000
119
+ self .nodes [2 ].settxfee (fee_per_byte * 1000 )
108
120
txid = self .nodes [2 ].sendtoaddress (address , 10 , "" , "" , False )
109
121
self .nodes [2 ].generate (1 )
110
122
self .sync_all ()
111
- assert_equal (self .nodes [2 ].getbalance (), Decimal ('89.99900000' ))
112
- assert_equal (self .nodes [0 ].getbalance (), Decimal ('10.00000000 ' ))
123
+ node_2_bal = self . check_fee_amount (self .nodes [2 ].getbalance (), Decimal ('90' ), fee_per_byte , count_bytes ( self . nodes [ 2 ]. getrawtransaction ( txid ) ))
124
+ assert_equal (self .nodes [0 ].getbalance (), Decimal ('10' ))
113
125
114
126
# Send 10 BTC with subtract fee from amount
115
127
txid = self .nodes [2 ].sendtoaddress (address , 10 , "" , "" , True )
116
128
self .nodes [2 ].generate (1 )
117
129
self .sync_all ()
118
- assert_equal (self .nodes [2 ].getbalance (), Decimal ('79.99900000' ))
119
- assert_equal (self .nodes [0 ].getbalance (), Decimal ('19.99900000' ))
130
+ node_2_bal -= Decimal ('10' )
131
+ assert_equal (self .nodes [2 ].getbalance (), node_2_bal )
132
+ node_0_bal = self .check_fee_amount (self .nodes [0 ].getbalance (), Decimal ('20' ), fee_per_byte , count_bytes (self .nodes [2 ].getrawtransaction (txid )))
120
133
121
134
# Sendmany 10 BTC
122
135
txid = self .nodes [2 ].sendmany ('from1' , {address : 10 }, 0 , "" , [])
123
136
self .nodes [2 ].generate (1 )
124
137
self .sync_all ()
125
- assert_equal (self .nodes [2 ].getbalance (), Decimal ('69.99800000' ))
126
- assert_equal (self .nodes [0 ].getbalance (), Decimal ('29.99900000' ))
138
+ node_0_bal += Decimal ('10' )
139
+ node_2_bal = self .check_fee_amount (self .nodes [2 ].getbalance (), node_2_bal - Decimal ('10' ), fee_per_byte , count_bytes (self .nodes [2 ].getrawtransaction (txid )))
140
+ assert_equal (self .nodes [0 ].getbalance (), node_0_bal )
127
141
128
142
# Sendmany 10 BTC with subtract fee from amount
129
143
txid = self .nodes [2 ].sendmany ('from1' , {address : 10 }, 0 , "" , [address ])
130
144
self .nodes [2 ].generate (1 )
131
145
self .sync_all ()
132
- assert_equal (self .nodes [2 ].getbalance (), Decimal ('59.99800000' ))
133
- assert_equal (self .nodes [0 ].getbalance (), Decimal ('39.99800000' ))
146
+ node_2_bal -= Decimal ('10' )
147
+ assert_equal (self .nodes [2 ].getbalance (), node_2_bal )
148
+ node_0_bal = self .check_fee_amount (self .nodes [0 ].getbalance (), node_0_bal + Decimal ('10' ), fee_per_byte , count_bytes (self .nodes [2 ].getrawtransaction (txid )))
134
149
135
150
# Test ResendWalletTransactions:
136
151
# Create a couple of transactions, then start up a fourth
@@ -191,14 +206,14 @@ def run_test (self):
191
206
txObjNotBroadcasted = self .nodes [0 ].gettransaction (txIdNotBroadcasted )
192
207
self .nodes [1 ].generate (1 ) #mine a block, tx should not be in there
193
208
self .sync_all ()
194
- assert_equal (self .nodes [2 ].getbalance (), Decimal ( '59.99800000' ) ); #should not be changed because tx was not broadcasted
209
+ assert_equal (self .nodes [2 ].getbalance (), node_2_bal ); #should not be changed because tx was not broadcasted
195
210
196
211
#now broadcast from another node, mine a block, sync, and check the balance
197
212
self .nodes [1 ].sendrawtransaction (txObjNotBroadcasted ['hex' ])
198
213
self .nodes [1 ].generate (1 )
199
214
self .sync_all ()
200
215
txObjNotBroadcasted = self .nodes [0 ].gettransaction (txIdNotBroadcasted )
201
- assert_equal (self .nodes [2 ].getbalance (), Decimal ('61.99800000 ' )); #should not be
216
+ assert_equal (self .nodes [2 ].getbalance (), node_2_bal + Decimal ('2 ' )); #should not be
202
217
203
218
#create another tx
204
219
txIdNotBroadcasted = self .nodes [0 ].sendtoaddress (self .nodes [2 ].getnewaddress (), 2 );
@@ -216,7 +231,7 @@ def run_test (self):
216
231
sync_blocks (self .nodes )
217
232
218
233
#tx should be added to balance because after restarting the nodes tx should be broadcastet
219
- assert_equal (self .nodes [2 ].getbalance (), Decimal ('63.99800000 ' )); #should not be
234
+ assert_equal (self .nodes [2 ].getbalance (), node_2_bal + Decimal ('4 ' )); #should not be
220
235
221
236
#send a tx with value in a string (PR#6380 +)
222
237
txId = self .nodes [0 ].sendtoaddress (self .nodes [2 ].getnewaddress (), "2" )
0 commit comments