Skip to content

Commit 3e6f737

Browse files
committed
Improve getbalances coverage in wallet_balance tests
and shift some getunconfirmedbalance calls to getbalances, as the former is deprecated, while leaving essential coverage for it in test_balances().
1 parent 96a30b9 commit 3e6f737

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

test/functional/wallet_balance.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def run_test(self):
107107
# First argument of getbalance must be set to "*"
108108
assert_raises_rpc_error(-32, "dummy first argument must be excluded or set to \"*\"", self.nodes[1].getbalance, "")
109109

110-
self.log.info("Test getbalance and getunconfirmedbalance with unconfirmed inputs")
110+
self.log.info("Test balances with unconfirmed inputs")
111111

112112
# Before `test_balance()`, we have had two nodes with a balance of 50
113113
# each and then we:
@@ -148,6 +148,18 @@ def run_test(self):
148148

149149

150150
def test_balances(*, fee_node_1=0):
151+
# getbalances
152+
expected_balances_0 = {'mine': {'immature': Decimal('0E-8'),
153+
'trusted': Decimal('9.99'), # change from node 0's send
154+
'untrusted_pending': Decimal('60.0')},
155+
'watchonly': {'immature': Decimal('5000'),
156+
'trusted': Decimal('50.0'),
157+
'untrusted_pending': Decimal('0E-8')}}
158+
expected_balances_1 = {'mine': {'immature': Decimal('0E-8'),
159+
'trusted': Decimal('0E-8'), # node 1's send had an unsafe input
160+
'untrusted_pending': Decimal('30.0') - fee_node_1}} # Doesn't include output of node 0's send since it was spent
161+
assert_equal(self.nodes[0].getbalances(), expected_balances_0)
162+
assert_equal(self.nodes[1].getbalances(), expected_balances_1)
151163
# getbalance without any arguments includes unconfirmed transactions, but not untrusted transactions
152164
assert_equal(self.nodes[0].getbalance(), Decimal('9.99')) # change from node 0's send
153165
assert_equal(self.nodes[1].getbalance(), Decimal('0')) # node 1's send had an unsafe input
@@ -160,11 +172,9 @@ def test_balances(*, fee_node_1=0):
160172
assert_equal(self.nodes[1].getbalance(minconf=1), Decimal('0'))
161173
# getunconfirmedbalance
162174
assert_equal(self.nodes[0].getunconfirmedbalance(), Decimal('60')) # output of node 1's spend
163-
assert_equal(self.nodes[0].getbalances()['mine']['untrusted_pending'], Decimal('60'))
164-
assert_equal(self.nodes[0].getwalletinfo()["unconfirmed_balance"], Decimal('60'))
165-
166175
assert_equal(self.nodes[1].getunconfirmedbalance(), Decimal('30') - fee_node_1) # Doesn't include output of node 0's send since it was spent
167-
assert_equal(self.nodes[1].getbalances()['mine']['untrusted_pending'], Decimal('30') - fee_node_1)
176+
# getwalletinfo.unconfirmed_balance
177+
assert_equal(self.nodes[0].getwalletinfo()["unconfirmed_balance"], Decimal('60'))
168178
assert_equal(self.nodes[1].getwalletinfo()["unconfirmed_balance"], Decimal('30') - fee_node_1)
169179

170180
test_balances(fee_node_1=Decimal('0.01'))
@@ -174,15 +184,19 @@ def test_balances(*, fee_node_1=0):
174184
self.nodes[0].sendrawtransaction(txs[1]['hex']) # sending on both nodes is faster than waiting for propagation
175185
self.sync_all()
176186

177-
self.log.info("Test getbalance and getunconfirmedbalance with conflicted unconfirmed inputs")
187+
self.log.info("Test getbalance and getbalances.mine.untrusted_pending with conflicted unconfirmed inputs")
178188
test_balances(fee_node_1=Decimal('0.02'))
179189

180190
self.nodes[1].generatetoaddress(1, ADDRESS_WATCHONLY)
181191
self.sync_all()
182192

183193
# balances are correct after the transactions are confirmed
184-
assert_equal(self.nodes[0].getbalance(), Decimal('69.99')) # node 1's send plus change from node 0's send
185-
assert_equal(self.nodes[1].getbalance(), Decimal('29.98')) # change from node 0's send
194+
balance_node0 = Decimal('69.99') # node 1's send plus change from node 0's send
195+
balance_node1 = Decimal('29.98') # change from node 0's send
196+
assert_equal(self.nodes[0].getbalances()['mine']['trusted'], balance_node0)
197+
assert_equal(self.nodes[1].getbalances()['mine']['trusted'], balance_node1)
198+
assert_equal(self.nodes[0].getbalance(), balance_node0)
199+
assert_equal(self.nodes[1].getbalance(), balance_node1)
186200

187201
# Send total balance away from node 1
188202
txs = create_transactions(self.nodes[1], self.nodes[0].getnewaddress(), Decimal('29.97'), [Decimal('0.01')])
@@ -200,13 +214,13 @@ def test_balances(*, fee_node_1=0):
200214

201215
# check mempool transactions count for wallet unconfirmed balance after
202216
# dynamically loading the wallet.
203-
before = self.nodes[1].getunconfirmedbalance()
217+
before = self.nodes[1].getbalances()['mine']['untrusted_pending']
204218
dst = self.nodes[1].getnewaddress()
205219
self.nodes[1].unloadwallet('')
206220
self.nodes[0].sendtoaddress(dst, 0.1)
207221
self.sync_all()
208222
self.nodes[1].loadwallet('')
209-
after = self.nodes[1].getunconfirmedbalance()
223+
after = self.nodes[1].getbalances()['mine']['untrusted_pending']
210224
assert_equal(before + Decimal('0.1'), after)
211225

212226
# Create 3 more wallet txs, where the last is not accepted to the

0 commit comments

Comments
 (0)