2424
2525class WalletTest (BitcoinTestFramework ):
2626
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+
2738 def setup_chain (self ):
2839 print ("Initializing test directory " + self .options .tmpdir )
2940 initialize_chain_clean (self .options .tmpdir , 4 )
@@ -104,33 +115,37 @@ def run_test (self):
104115
105116 # Send 10 BTC normal
106117 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 )
108120 txid = self .nodes [2 ].sendtoaddress (address , 10 , "" , "" , False )
109121 self .nodes [2 ].generate (1 )
110122 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' ))
113125
114126 # Send 10 BTC with subtract fee from amount
115127 txid = self .nodes [2 ].sendtoaddress (address , 10 , "" , "" , True )
116128 self .nodes [2 ].generate (1 )
117129 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 )))
120133
121134 # Sendmany 10 BTC
122135 txid = self .nodes [2 ].sendmany ('from1' , {address : 10 }, 0 , "" , [])
123136 self .nodes [2 ].generate (1 )
124137 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 )
127141
128142 # Sendmany 10 BTC with subtract fee from amount
129143 txid = self .nodes [2 ].sendmany ('from1' , {address : 10 }, 0 , "" , [address ])
130144 self .nodes [2 ].generate (1 )
131145 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 )))
134149
135150 # Test ResendWalletTransactions:
136151 # Create a couple of transactions, then start up a fourth
@@ -191,14 +206,14 @@ def run_test (self):
191206 txObjNotBroadcasted = self .nodes [0 ].gettransaction (txIdNotBroadcasted )
192207 self .nodes [1 ].generate (1 ) #mine a block, tx should not be in there
193208 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
195210
196211 #now broadcast from another node, mine a block, sync, and check the balance
197212 self .nodes [1 ].sendrawtransaction (txObjNotBroadcasted ['hex' ])
198213 self .nodes [1 ].generate (1 )
199214 self .sync_all ()
200215 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
202217
203218 #create another tx
204219 txIdNotBroadcasted = self .nodes [0 ].sendtoaddress (self .nodes [2 ].getnewaddress (), 2 );
@@ -216,7 +231,7 @@ def run_test (self):
216231 sync_blocks (self .nodes )
217232
218233 #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
220235
221236 #send a tx with value in a string (PR#6380 +)
222237 txId = self .nodes [0 ].sendtoaddress (self .nodes [2 ].getnewaddress (), "2" )
0 commit comments