2019-11-23 18:47:54 -08:00
#!/usr/bin/env python3
2016-03-19 12:58:06 -07:00
# Copyright (c) 2014-2016 The Bitcoin Core developers
2014-10-22 18:48:19 -07:00
# Distributed under the MIT software license, see the accompanying
2019-07-18 07:16:09 -07:00
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
2014-03-25 06:33:44 -07:00
2014-07-08 09:07:23 -07:00
# Exercise the listreceivedbyaddress API
2014-03-25 06:33:44 -07:00
2015-05-02 03:53:35 -07:00
from test_framework . test_framework import BitcoinTestFramework
2020-04-22 05:13:15 -07:00
from test_framework . util import assert_equal
2014-03-25 06:33:44 -07:00
2017-06-20 13:09:33 -07:00
from decimal import Decimal
2014-07-08 09:07:23 -07:00
2014-03-25 06:33:44 -07:00
def get_sub_array_from_array ( object_array , to_match ) :
'''
2017-06-20 13:23:24 -07:00
Finds and returns a sub array from an array of arrays .
to_match should be a unique idetifier of a sub array
2014-03-25 06:33:44 -07:00
'''
for item in object_array :
all_match = True
for key , value in to_match . items ( ) :
if item [ key ] != value :
all_match = False
if not all_match :
continue
return item
return [ ]
def check_array_result ( object_array , to_match , expected , should_not_find = False ) :
2017-06-20 13:23:24 -07:00
'''
Pass in array of JSON objects , a dictionary with key / value pairs
to match against , and another dictionary with expected key / value
pairs .
If the should_not_find flag is true , to_match should not be found in object_array
'''
2014-03-25 06:33:44 -07:00
if should_not_find == True :
expected = { }
num_matched = 0
for item in object_array :
all_match = True
for key , value in to_match . items ( ) :
if item [ key ] != value :
all_match = False
if not all_match :
continue
for key , value in expected . items ( ) :
if item [ key ] != value :
raise AssertionError ( " %s : expected %s = %s " % ( str ( item ) , str ( key ) , str ( value ) ) )
num_matched = num_matched + 1
if num_matched == 0 and should_not_find != True :
raise AssertionError ( " No objects matched %s " % ( str ( to_match ) ) )
if num_matched > 0 and should_not_find == True :
raise AssertionError ( " Objects was matched %s " % ( str ( to_match ) ) )
2014-07-08 09:07:23 -07:00
class ReceivedByTest ( BitcoinTestFramework ) :
2014-03-25 06:33:44 -07:00
2016-05-14 04:01:31 -07:00
def __init__ ( self ) :
super ( ) . __init__ ( )
self . num_nodes = 4
self . setup_clean_chain = False
2014-10-20 05:14:04 -07:00
def run_test ( self ) :
2014-07-08 09:07:23 -07:00
'''
listreceivedbyaddress Test
'''
# Send from node 0 to 1
2014-10-20 05:14:04 -07:00
addr = self . nodes [ 1 ] . getnewaddress ( )
txid = self . nodes [ 0 ] . sendtoaddress ( addr , 0.1 )
self . sync_all ( )
2014-07-08 09:07:23 -07:00
2017-06-20 13:23:24 -07:00
# Check not listed in listreceivedbyaddress because has 0 confirmations
2014-10-20 05:14:04 -07:00
check_array_result ( self . nodes [ 1 ] . listreceivedbyaddress ( ) ,
2014-07-08 09:07:23 -07:00
{ " address " : addr } ,
{ } ,
True )
2017-06-20 13:23:24 -07:00
# Bury Tx under 10 block so it will be returned by listreceivedbyaddress
2015-03-31 20:28:28 -07:00
self . nodes [ 1 ] . generate ( 10 )
2014-10-20 05:14:04 -07:00
self . sync_all ( )
check_array_result ( self . nodes [ 1 ] . listreceivedbyaddress ( ) ,
2014-07-08 09:07:23 -07:00
{ " address " : addr } ,
2020-05-18 14:02:22 -07:00
{ " address " : addr , " account " : " " , " amount " : Decimal ( " 0.1 " ) , " amountZat " : 10000000 , " confirmations " : 10 , " txids " : [ txid , ] } )
2017-06-20 13:23:24 -07:00
# With min confidence < 10
2014-10-20 05:14:04 -07:00
check_array_result ( self . nodes [ 1 ] . listreceivedbyaddress ( 5 ) ,
2014-07-08 09:07:23 -07:00
{ " address " : addr } ,
2020-05-18 14:02:22 -07:00
{ " address " : addr , " account " : " " , " amount " : Decimal ( " 0.1 " ) , " amountZat " : 10000000 , " confirmations " : 10 , " txids " : [ txid , ] } )
2017-06-20 13:23:24 -07:00
# With min confidence > 10, should not find Tx
2014-10-20 05:14:04 -07:00
check_array_result ( self . nodes [ 1 ] . listreceivedbyaddress ( 11 ) , { " address " : addr } , { } , True )
2014-07-08 09:07:23 -07:00
2017-06-20 13:23:24 -07:00
# Empty Tx
2014-10-20 05:14:04 -07:00
addr = self . nodes [ 1 ] . getnewaddress ( )
check_array_result ( self . nodes [ 1 ] . listreceivedbyaddress ( 0 , True ) ,
2014-07-08 09:07:23 -07:00
{ " address " : addr } ,
2020-05-30 06:34:03 -07:00
{ " address " : addr , " account " : " " , " amount " : 0 , " confirmations " : 0 , " amountZat " : 0 , " txids " : [ ] } )
2014-07-08 09:07:23 -07:00
'''
2017-06-20 13:23:24 -07:00
getreceivedbyaddress Test
2014-07-08 09:07:23 -07:00
'''
# Send from node 0 to 1
2014-10-20 05:14:04 -07:00
addr = self . nodes [ 1 ] . getnewaddress ( )
txid = self . nodes [ 0 ] . sendtoaddress ( addr , 0.1 )
self . sync_all ( )
2014-07-08 09:07:23 -07:00
2017-06-20 13:23:24 -07:00
# Check balance is 0 because of 0 confirmations
2014-10-20 05:14:04 -07:00
balance = self . nodes [ 1 ] . getreceivedbyaddress ( addr )
2020-04-22 05:13:15 -07:00
assert_equal ( balance , Decimal ( " 0.0 " ) , " Wrong balance returned by getreceivedbyaddress, %0.2f " % ( balance ) )
2014-07-08 09:07:23 -07:00
2017-06-20 13:23:24 -07:00
# Check balance is 0.1
2014-10-20 05:14:04 -07:00
balance = self . nodes [ 1 ] . getreceivedbyaddress ( addr , 0 )
2020-04-22 05:13:15 -07:00
assert_equal ( balance , Decimal ( " 0.1 " ) , " Wrong balance returned by getreceivedbyaddress, %0.2f " % ( balance ) )
2014-07-08 09:07:23 -07:00
2017-06-20 13:23:24 -07:00
# Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
2015-03-31 20:28:28 -07:00
self . nodes [ 1 ] . generate ( 10 )
2014-10-20 05:14:04 -07:00
self . sync_all ( )
balance = self . nodes [ 1 ] . getreceivedbyaddress ( addr )
2020-04-22 05:13:15 -07:00
assert_equal ( balance , Decimal ( " 0.1 " ) , " Wrong balance returned by getreceivedbyaddress, %0.2f " % ( balance ) )
2014-07-08 09:07:23 -07:00
2020-01-02 06:04:47 -08:00
# Get balance as integer
2020-01-28 07:48:40 -08:00
balance = self . nodes [ 1 ] . getreceivedbyaddress ( addr , 1 , True )
2020-04-22 05:13:15 -07:00
assert_equal ( balance , 10000000 , " Wrong balance returned by getreceivedbyaddress, %i " % ( balance ) )
2020-01-02 06:04:47 -08:00
2014-07-08 09:07:23 -07:00
'''
2017-06-20 13:23:24 -07:00
listreceivedbyaccount + getreceivedbyaccount Test
2014-07-08 09:07:23 -07:00
'''
2017-06-20 13:23:24 -07:00
# set pre-state
2014-10-20 05:14:04 -07:00
addrArr = self . nodes [ 1 ] . getnewaddress ( )
account = self . nodes [ 1 ] . getaccount ( addrArr )
received_by_account_json = get_sub_array_from_array ( self . nodes [ 1 ] . listreceivedbyaccount ( ) , { " account " : account } )
2020-04-22 05:13:15 -07:00
2014-07-08 09:07:23 -07:00
if len ( received_by_account_json ) == 0 :
raise AssertionError ( " No accounts found in node " )
2017-06-20 13:20:50 -07:00
balance_by_account = self . nodes [ 1 ] . getreceivedbyaccount ( account )
2014-07-08 09:07:23 -07:00
2014-10-20 05:14:04 -07:00
txid = self . nodes [ 0 ] . sendtoaddress ( addr , 0.1 )
2014-11-20 12:49:07 -08:00
self . sync_all ( )
2014-07-08 09:07:23 -07:00
# listreceivedbyaccount should return received_by_account_json because of 0 confirmations
2014-10-20 05:14:04 -07:00
check_array_result ( self . nodes [ 1 ] . listreceivedbyaccount ( ) ,
2014-07-08 09:07:23 -07:00
{ " account " : account } ,
received_by_account_json )
2020-01-28 07:48:40 -08:00
# getreceivedbyaccount should return same balance because of 0 confirmations
2014-10-20 05:14:04 -07:00
balance = self . nodes [ 1 ] . getreceivedbyaccount ( account )
2020-04-22 05:13:15 -07:00
assert_equal ( balance , balance_by_account , " Wrong balance returned by getreceivedbyaccount, %0.2f " % ( balance ) )
2014-07-08 09:07:23 -07:00
2015-03-31 20:28:28 -07:00
self . nodes [ 1 ] . generate ( 10 )
2014-10-20 05:14:04 -07:00
self . sync_all ( )
2014-07-08 09:07:23 -07:00
# listreceivedbyaccount should return updated account balance
2014-10-20 05:14:04 -07:00
check_array_result ( self . nodes [ 1 ] . listreceivedbyaccount ( ) ,
2014-07-08 09:07:23 -07:00
{ " account " : account } ,
2020-04-22 05:13:15 -07:00
{ " account " : received_by_account_json [ " account " ] , " amount " : ( received_by_account_json [ " amount " ] + Decimal ( " 0.1 " ) ) , " amountZat " : 30000000 } )
2014-07-08 09:07:23 -07:00
2020-01-28 07:48:40 -08:00
# getreceivedbyaccount should return updates balance
2014-10-20 05:14:04 -07:00
balance = self . nodes [ 1 ] . getreceivedbyaccount ( account )
2020-04-22 05:13:15 -07:00
assert_equal ( balance , balance_by_account + Decimal ( " 0.1 " ) , " Wrong balance returned by getreceivedbyaccount, %0.2f " % ( balance ) )
2014-07-08 09:07:23 -07:00
2020-01-28 07:48:40 -08:00
# Get balance as integer
balance = self . nodes [ 1 ] . getreceivedbyaccount ( account , 1 , True )
2020-04-22 05:13:15 -07:00
assert_equal ( balance , 30000000 , " Wrong balance returned by getreceivedbyaccount, %i " % ( balance ) )
2020-01-28 07:48:40 -08:00
2017-06-20 13:23:24 -07:00
# Create a new account named "mynewaccount" that has a 0 balance
2014-10-20 05:14:04 -07:00
self . nodes [ 1 ] . getaccountaddress ( " mynewaccount " )
received_by_account_json = get_sub_array_from_array ( self . nodes [ 1 ] . listreceivedbyaccount ( 0 , True ) , { " account " : " mynewaccount " } )
2014-07-08 09:07:23 -07:00
if len ( received_by_account_json ) == 0 :
raise AssertionError ( " No accounts found in node " )
2020-04-22 05:13:15 -07:00
# Test listreceivedbyaccount for 0 amount accounts
assert_equal ( received_by_account_json [ " amount " ] , Decimal ( " 0.0 " ) , " Wrong balance returned by listreceivedbyaccount, %0.2f " % ( received_by_account_json [ " amount " ] ) )
2014-07-08 09:07:23 -07:00
# Test getreceivedbyaccount for 0 amount accounts
2014-10-20 05:14:04 -07:00
balance = self . nodes [ 1 ] . getreceivedbyaccount ( " mynewaccount " )
2020-04-22 05:13:15 -07:00
assert_equal ( balance , Decimal ( " 0.0 " ) , " Wrong balance returned by getreceivedbyaccount, %0.2f " % ( balance ) )
2014-03-25 06:33:44 -07:00
if __name__ == ' __main__ ' :
2014-07-08 09:07:23 -07:00
ReceivedByTest ( ) . main ( )