Repository: bitcoin/bitcoin
File: test/functional/wallet_listreceivedby.py (Python)

Code before:

        self.log.info("getreceivedbyaddress Test")

        # Send from node 0 to 1
        addr = self.nodes[1].getnewaddress()
        txid = self.nodes[0].sendtoaddress(addr, 0.1)
        self.sync_all()

        # Check balance is 0 because of 0 confirmations
        balance = self.nodes[1].getreceivedbyaddress(addr)
        assert_equal(balance, Decimal("0.0"))

        # Check balance is 0.1
        balance = self.nodes[1].getreceivedbyaddress(addr, 0)
        assert_equal(balance, Decimal("0.1"))

        # Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
        self.generate(self.nodes[1], 10)
        balance = self.nodes[1].getreceivedbyaddress(addr)
        assert_equal(balance, Decimal("0.1"))

        # Trying to getreceivedby for an address the wallet doesn't own should return an error
        assert_raises_rpc_error(-4, "Address not found in wallet", self.nodes[0].getreceivedbyaddress, addr)

        # Test multiple transactions to the same address
        addr_multiple = self.nodes[1].getnewaddress()
        self.nodes[0].sendtoaddress(addr_multiple, Decimal("0.1"))
        self.nodes[0].sendtoaddress(addr_multiple, Decimal("0.2"))
        self.sync_all()
        self.generate(self.nodes[1], 10)
        balance = self.nodes[1].getreceivedbyaddress(addr_multiple)
        assert_equal(balance, Decimal("0.3"))

        # Test invalid address format error
        assert_raises_rpc_error(-5, "Invalid Bitcoin address", self.nodes[1].getreceivedbyaddress, "invalid_address")

        self.log.info("listreceivedbylabel + getreceivedbylabel Test")

        # set pre-state
        label = ''
        address = self.nodes[1].getnewaddress()
        test_address(self.nodes[1], address, labels=[label])
        received_by_label_json = [r for r in self.nodes[1].listreceivedbylabel() if r["label"] == label][0]
        balance_by_label = self.nodes[1].getreceivedbylabel(label)

        txid = self.nodes[0].sendtoaddress(addr, 0.1)
        self.sync_all()

        # getreceivedbylabel returns an error if the wallet doesn't own the label
        assert_raises_rpc_error(-4, "Label not found in wallet", self.nodes[0].getreceivedbylabel, "dummy")


Review comment (suggestion):
While it's easy to understand in this PR, this address name can be confusing for readers when it's read later. Consider:

```diff
diff --git a/test/functional/wallet_listreceivedby.py b/test/functional/wallet_listreceivedby.py
index 52a503b9d5..8169f9f28c 100755
--- a/test/functional/wallet_listreceivedby.py
+++ b/test/functional/wallet_listreceivedby.py
@@ -125,12 +125,11 @@ class ReceivedByTest(BitcoinTestFramework):
         assert_raises_rpc_error(-4, "Address not found in wallet", self.nodes[0].getreceivedbyaddress, addr)
 
         # Test multiple transactions to the same address
-        addr_multiple = self.nodes[1].getnewaddress()
-        self.nodes[0].sendtoaddress(addr_multiple, Decimal("0.1"))
-        self.nodes[0].sendtoaddress(addr_multiple, Decimal("0.2"))
+        addr_with_multiple_txs = self.nodes[1].getnewaddress()
+        self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.1"))
+        self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.2"))
         self.generate(self.nodes[1], 10)
-        balance = self.nodes[1].getreceivedbyaddress(addr_multiple)
+        balance = self.nodes[1].getreceivedbyaddress(addr_with_multiple_txs)
         assert_equal(balance, Decimal("0.3"))
 
         # Test invalid address format error
```

Code after:

        self.log.info("getreceivedbyaddress Test")

        # Send from node 0 to 1
        addr = self.nodes[1].getnewaddress()
        txid = self.nodes[0].sendtoaddress(addr, 0.1)
        self.sync_all()

        # Check balance is 0 because of 0 confirmations
        balance = self.nodes[1].getreceivedbyaddress(addr)
        assert_equal(balance, Decimal("0.0"))

        # Check balance is 0.1
        balance = self.nodes[1].getreceivedbyaddress(addr, 0)
        assert_equal(balance, Decimal("0.1"))

        # Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
        self.generate(self.nodes[1], 10)
        balance = self.nodes[1].getreceivedbyaddress(addr)
        assert_equal(balance, Decimal("0.1"))

        # Trying to getreceivedby for an address the wallet doesn't own should return an error
        assert_raises_rpc_error(-4, "Address not found in wallet", self.nodes[0].getreceivedbyaddress, addr)

        # Test multiple transactions to the same address
        addr_with_multiple_txs = self.nodes[1].getnewaddress()
        self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.1"))
        self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.2"))
        self.generate(self.nodes[0], 1)
        balance = self.nodes[1].getreceivedbyaddress(addr_with_multiple_txs)
        assert_equal(balance, Decimal("0.3"))

        # Test invalid address format error
        assert_raises_rpc_error(-5, "Invalid Bitcoin address", self.nodes[1].getreceivedbyaddress, "invalid_address")

        self.log.info("listreceivedbylabel + getreceivedbylabel Test")

        # set pre-state
        label = ''
        address = self.nodes[1].getnewaddress()
        test_address(self.nodes[1], address, labels=[label])
        received_by_label_json = [r for r in self.nodes[1].listreceivedbylabel() if r["label"] == label][0]
        balance_by_label = self.nodes[1].getreceivedbylabel(label)

        txid = self.nodes[0].sendtoaddress(addr, 0.1)
        self.sync_all()

        # getreceivedbylabel returns an error if the wallet doesn't own the label
        assert_raises_rpc_error(-4, "Label not found in wallet", self.nodes[0].getreceivedbylabel, "dummy")

        # listreceivedbylabel should return received_by_label_json because of 0 confirmations