diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -757,7 +757,7 @@ } CScript scriptPubKey = GetScriptForDestination(dest); if (!IsMine(*pwallet, scriptPubKey)) { - return ValueFromAmount(Amount::zero()); + throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet"); } // Minimum confirmations diff --git a/test/functional/receivedby.py b/test/functional/receivedby.py --- a/test/functional/receivedby.py +++ b/test/functional/receivedby.py @@ -7,7 +7,10 @@ from decimal import Decimal from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import assert_array_result, assert_equal +from test_framework.util import (assert_array_result, + assert_equal, + assert_raises_rpc_error, + ) class ReceivedByTest(BitcoinTestFramework): @@ -42,7 +45,8 @@ {"address": addr}, {"address": addr, "account": "", "amount": Decimal("0.1"), "confirmations": 10, "txids": [txid, ]}) # With min confidence > 10, should not find Tx - assert_array_result(self.nodes[1].listreceivedbyaddress(11), {"address": addr}, {}, True) + assert_array_result(self.nodes[1].listreceivedbyaddress(11), { + "address": addr}, {}, True) # Empty Tx addr = self.nodes[1].getnewaddress() @@ -71,12 +75,17 @@ 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) + self.log.info("listreceivedbyaccount + getreceivedbyaccount Test") # set pre-state addrArr = self.nodes[1].getnewaddress() account = self.nodes[1].getaccount(addrArr) - received_by_account_json = [r for r in self.nodes[1].listreceivedbyaccount() if r["account"] == account][0] + received_by_account_json = [ + r for r in self.nodes[1].listreceivedbyaccount() if r["account"] == account][0] balance_by_account = self.nodes[1].getreceivedbyaccount(account) txid = self.nodes[0].sendtoaddress(addr, 0.1) @@ -106,7 +115,8 @@ # Create a new account named "mynewaccount" that has a 0 balance self.nodes[1].getaccountaddress("mynewaccount") - received_by_account_json = [r for r in self.nodes[1].listreceivedbyaccount(0, True) if r["account"] == "mynewaccount"][0] + received_by_account_json = [r for r in self.nodes[1].listreceivedbyaccount( + 0, True) if r["account"] == "mynewaccount"][0] # Test includeempty of listreceivedbyaccount assert_equal(received_by_account_json["amount"], Decimal("0.0")) @@ -115,5 +125,6 @@ balance = self.nodes[1].getreceivedbyaccount("mynewaccount") assert_equal(balance, Decimal("0.0")) + if __name__ == '__main__': ReceivedByTest().main()