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

Code before:
        # 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.sync_all()
        self.generate(self.nodes[1], 10)
        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
        assert_array_result(self.nodes[1].listreceivedbylabel(),
                            {"label": label},

Review comment (suggestion):
You probably wouldn't need it if you would generate the blocks with `self.nodes[0]` because that node is also sending the funds. Alternatively you could also do this. But either way seems fine.

```
self.generate(self.nodes[1], 10, sync_fun=lambda: self.sync_all(self.nodes[0:1]))
```

Code after:
        # 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
        assert_array_result(self.nodes[1].listreceivedbylabel(),
                            {"label": label},
                            received_by_label_json)