Skip to content

Commit b569675

Browse files
committed
Test including coinbase transactions in receivedby wallet rpcs
1 parent bce20c3 commit b569675

File tree

1 file changed

+122
-1
lines changed

1 file changed

+122
-1
lines changed

test/functional/wallet_listreceivedby.py

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# Copyright (c) 2014-2021 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5-
"""Test the listreceivedbyaddress RPC."""
5+
"""Test the listreceivedbyaddress, listreceivedbylabel, getreceivedybaddress, and getreceivedbylabel RPCs."""
66
from decimal import Decimal
77

8+
from test_framework.blocktools import COINBASE_MATURITY
89
from test_framework.test_framework import BitcoinTestFramework
910
from test_framework.util import (
1011
assert_array_result,
@@ -17,6 +18,8 @@
1718
class ReceivedByTest(BitcoinTestFramework):
1819
def set_test_params(self):
1920
self.num_nodes = 2
21+
# Test deprecated exclude coinbase on second node
22+
self.extra_args = [[], ["-deprecatedrpc=exclude_coinbase"]]
2023

2124
def skip_test_if_missing_module(self):
2225
self.skip_if_no_wallet()
@@ -162,5 +165,123 @@ def run_test(self):
162165
balance = self.nodes[1].getreceivedbylabel("mynewlabel")
163166
assert_equal(balance, Decimal("0.0"))
164167

168+
self.log.info("Tests for including coinbase outputs")
169+
170+
# Generate block reward to address with label
171+
label = "label"
172+
address = self.nodes[0].getnewaddress(label)
173+
174+
reward = Decimal("25")
175+
self.generatetoaddress(self.nodes[0], 1, address, sync_fun=self.no_op)
176+
hash = self.nodes[0].getbestblockhash()
177+
178+
self.log.info("getreceivedbyaddress returns nothing with defaults")
179+
balance = self.nodes[0].getreceivedbyaddress(address)
180+
assert_equal(balance, 0)
181+
182+
self.log.info("getreceivedbyaddress returns block reward when including immature coinbase")
183+
balance = self.nodes[0].getreceivedbyaddress(address=address, include_immature_coinbase=True)
184+
assert_equal(balance, reward)
185+
186+
self.log.info("getreceivedbylabel returns nothing with defaults")
187+
balance = self.nodes[0].getreceivedbylabel("label")
188+
assert_equal(balance, 0)
189+
190+
self.log.info("getreceivedbylabel returns block reward when including immature coinbase")
191+
balance = self.nodes[0].getreceivedbylabel(label="label", include_immature_coinbase=True)
192+
assert_equal(balance, reward)
193+
194+
self.log.info("listreceivedbyaddress does not include address with defaults")
195+
assert_array_result(self.nodes[0].listreceivedbyaddress(),
196+
{"address": address},
197+
{}, True)
198+
199+
self.log.info("listreceivedbyaddress includes address when including immature coinbase")
200+
assert_array_result(self.nodes[0].listreceivedbyaddress(minconf=1, include_immature_coinbase=True),
201+
{"address": address},
202+
{"address": address, "amount": reward})
203+
204+
self.log.info("listreceivedbylabel does not include label with defaults")
205+
assert_array_result(self.nodes[0].listreceivedbylabel(),
206+
{"label": label},
207+
{}, True)
208+
209+
self.log.info("listreceivedbylabel includes label when including immature coinbase")
210+
assert_array_result(self.nodes[0].listreceivedbylabel(minconf=1, include_immature_coinbase=True),
211+
{"label": label},
212+
{"label": label, "amount": reward})
213+
214+
self.log.info("Generate 100 more blocks")
215+
self.generate(self.nodes[0], COINBASE_MATURITY, sync_fun=self.no_op)
216+
217+
self.log.info("getreceivedbyaddress returns reward with defaults")
218+
balance = self.nodes[0].getreceivedbyaddress(address)
219+
assert_equal(balance, reward)
220+
221+
self.log.info("getreceivedbylabel returns reward with defaults")
222+
balance = self.nodes[0].getreceivedbylabel("label")
223+
assert_equal(balance, reward)
224+
225+
self.log.info("listreceivedbyaddress includes address with defaults")
226+
assert_array_result(self.nodes[0].listreceivedbyaddress(),
227+
{"address": address},
228+
{"address": address, "amount": reward})
229+
230+
self.log.info("listreceivedbylabel includes label with defaults")
231+
assert_array_result(self.nodes[0].listreceivedbylabel(),
232+
{"label": label},
233+
{"label": label, "amount": reward})
234+
235+
self.log.info("Invalidate block that paid to address")
236+
self.nodes[0].invalidateblock(hash)
237+
238+
self.log.info("getreceivedbyaddress does not include invalidated block when minconf is 0 when including immature coinbase")
239+
balance = self.nodes[0].getreceivedbyaddress(address=address, minconf=0, include_immature_coinbase=True)
240+
assert_equal(balance, 0)
241+
242+
self.log.info("getreceivedbylabel does not include invalidated block when minconf is 0 when including immature coinbase")
243+
balance = self.nodes[0].getreceivedbylabel(label="label", minconf=0, include_immature_coinbase=True)
244+
assert_equal(balance, 0)
245+
246+
self.log.info("listreceivedbyaddress does not include invalidated block when minconf is 0 when including immature coinbase")
247+
assert_array_result(self.nodes[0].listreceivedbyaddress(minconf=0, include_immature_coinbase=True),
248+
{"address": address},
249+
{}, True)
250+
251+
self.log.info("listreceivedbylabel does not include invalidated block when minconf is 0 when including immature coinbase")
252+
assert_array_result(self.nodes[0].listreceivedbylabel(minconf=0, include_immature_coinbase=True),
253+
{"label": label},
254+
{}, True)
255+
256+
# Test exclude_coinbase
257+
address2 = self.nodes[1].getnewaddress(label)
258+
self.generatetoaddress(self.nodes[1], COINBASE_MATURITY + 1, address2, sync_fun=self.no_op)
259+
260+
self.log.info("getreceivedbyaddress returns nothing when excluding coinbase")
261+
balance = self.nodes[1].getreceivedbyaddress(address2)
262+
assert_equal(balance, 0)
263+
264+
self.log.info("getreceivedbylabel returns nothing when excluding coinbase")
265+
balance = self.nodes[1].getreceivedbylabel("label")
266+
assert_equal(balance, 0)
267+
268+
self.log.info("listreceivedbyaddress does not include address when excluding coinbase")
269+
assert_array_result(self.nodes[1].listreceivedbyaddress(),
270+
{"address": address2},
271+
{}, True)
272+
273+
self.log.info("listreceivedbylabel does not include label when excluding coinbase")
274+
assert_array_result(self.nodes[1].listreceivedbylabel(),
275+
{"label": label},
276+
{}, True)
277+
278+
self.log.info("getreceivedbyaddress throws when setting include_immature_coinbase with deprecated exclude_coinbase")
279+
assert_raises_rpc_error(-8, 'include_immature_coinbase is incompatible with deprecated exclude_coinbase', self.nodes[1].getreceivedbyaddress, address2, 1, True)
280+
281+
282+
self.log.info("listreceivedbyaddress throws when setting include_immature_coinbase with deprecated exclude_coinbase")
283+
assert_raises_rpc_error(-8, 'include_immature_coinbase is incompatible with deprecated exclude_coinbase', self.nodes[1].listreceivedbyaddress, 1, False, False, "", True)
284+
285+
165286
if __name__ == '__main__':
166287
ReceivedByTest().main()

0 commit comments

Comments
 (0)