Skip to content

Commit a775182

Browse files
committed
Merge pull request #7103
fa506c0 [wallet] Add rpc tests to verify fee calculations (MarcoFalke) 4b89f01 Default fPayAtLeastCustomFee to false (Ryan Havar)
2 parents 6fc287f + fa506c0 commit a775182

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

qa/rpc-tests/test_framework/util.py

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def check_json_precision():
6767
if satoshis != 2000000000000003:
6868
raise RuntimeError("JSON encode/decode loses precision")
6969

70+
def count_bytes(hex_string):
71+
return len(bytearray.fromhex(hex_string))
72+
7073
def sync_blocks(rpc_connections, wait=1):
7174
"""
7275
Wait until everybody has the same block count

qa/rpc-tests/wallet.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424

2525
class 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")

src/wallet/wallet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
4141
unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
4242
bool bSpendZeroConfChange = DEFAULT_SPEND_ZEROCONF_CHANGE;
4343
bool fSendFreeTransactions = DEFAULT_SEND_FREE_TRANSACTIONS;
44-
bool fPayAtLeastCustomFee = true;
44+
bool fPayAtLeastCustomFee = false;
4545

4646
/**
4747
* Fees smaller than this (in satoshi) are considered zero fee (for transaction creation)

0 commit comments

Comments
 (0)