mirror of https://github.com/bitcoin/bitcoin
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
6.3 KiB
148 lines
6.3 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
||
10 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
11 years ago
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""Test the listreceivedbyaddress RPC."""
|
||
11 years ago
|
|
||
10 years ago
|
from test_framework.test_framework import BitcoinTestFramework
|
||
|
from test_framework.util import *
|
||
11 years ago
|
|
||
|
def get_sub_array_from_array(object_array, to_match):
|
||
|
'''
|
||
|
Finds and returns a sub array from an array of arrays.
|
||
|
to_match should be a unique idetifier of a sub array
|
||
|
'''
|
||
|
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 []
|
||
|
|
||
11 years ago
|
class ReceivedByTest(BitcoinTestFramework):
|
||
11 years ago
|
|
||
9 years ago
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
self.num_nodes = 4
|
||
|
self.setup_clean_chain = False
|
||
|
|
||
9 years ago
|
def setup_nodes(self):
|
||
|
#This test requires mocktime
|
||
|
enable_mocktime()
|
||
9 years ago
|
return start_nodes(self.num_nodes, self.options.tmpdir)
|
||
9 years ago
|
|
||
10 years ago
|
def run_test(self):
|
||
11 years ago
|
'''
|
||
|
listreceivedbyaddress Test
|
||
|
'''
|
||
|
# Send from node 0 to 1
|
||
10 years ago
|
addr = self.nodes[1].getnewaddress()
|
||
|
txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
||
|
self.sync_all()
|
||
11 years ago
|
|
||
|
#Check not listed in listreceivedbyaddress because has 0 confirmations
|
||
9 years ago
|
assert_array_result(self.nodes[1].listreceivedbyaddress(),
|
||
11 years ago
|
{"address":addr},
|
||
|
{ },
|
||
|
True)
|
||
|
#Bury Tx under 10 block so it will be returned by listreceivedbyaddress
|
||
10 years ago
|
self.nodes[1].generate(10)
|
||
10 years ago
|
self.sync_all()
|
||
9 years ago
|
assert_array_result(self.nodes[1].listreceivedbyaddress(),
|
||
11 years ago
|
{"address":addr},
|
||
|
{"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
|
||
|
#With min confidence < 10
|
||
9 years ago
|
assert_array_result(self.nodes[1].listreceivedbyaddress(5),
|
||
11 years ago
|
{"address":addr},
|
||
|
{"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
|
||
|
#With min confidence > 10, should not find Tx
|
||
9 years ago
|
assert_array_result(self.nodes[1].listreceivedbyaddress(11),{"address":addr},{ },True)
|
||
11 years ago
|
|
||
|
#Empty Tx
|
||
10 years ago
|
addr = self.nodes[1].getnewaddress()
|
||
9 years ago
|
assert_array_result(self.nodes[1].listreceivedbyaddress(0,True),
|
||
11 years ago
|
{"address":addr},
|
||
|
{"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
|
||
|
|
||
|
'''
|
||
|
getreceivedbyaddress Test
|
||
|
'''
|
||
|
# Send from node 0 to 1
|
||
10 years ago
|
addr = self.nodes[1].getnewaddress()
|
||
|
txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
||
|
self.sync_all()
|
||
11 years ago
|
|
||
|
#Check balance is 0 because of 0 confirmations
|
||
10 years ago
|
balance = self.nodes[1].getreceivedbyaddress(addr)
|
||
11 years ago
|
if balance != Decimal("0.0"):
|
||
|
raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
|
||
|
|
||
|
#Check balance is 0.1
|
||
10 years ago
|
balance = self.nodes[1].getreceivedbyaddress(addr,0)
|
||
11 years ago
|
if balance != Decimal("0.1"):
|
||
|
raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
|
||
|
|
||
|
#Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
|
||
10 years ago
|
self.nodes[1].generate(10)
|
||
10 years ago
|
self.sync_all()
|
||
|
balance = self.nodes[1].getreceivedbyaddress(addr)
|
||
11 years ago
|
if balance != Decimal("0.1"):
|
||
|
raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
|
||
|
|
||
|
'''
|
||
|
listreceivedbyaccount + getreceivedbyaccount Test
|
||
|
'''
|
||
|
#set pre-state
|
||
10 years ago
|
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})
|
||
11 years ago
|
if len(received_by_account_json) == 0:
|
||
|
raise AssertionError("No accounts found in node")
|
||
8 years ago
|
balance_by_account = self.nodes[1].getreceivedbyaccount(account)
|
||
11 years ago
|
|
||
10 years ago
|
txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
||
10 years ago
|
self.sync_all()
|
||
11 years ago
|
|
||
|
# listreceivedbyaccount should return received_by_account_json because of 0 confirmations
|
||
9 years ago
|
assert_array_result(self.nodes[1].listreceivedbyaccount(),
|
||
11 years ago
|
{"account":account},
|
||
|
received_by_account_json)
|
||
|
|
||
|
# getreceivedbyaddress should return same balance because of 0 confirmations
|
||
10 years ago
|
balance = self.nodes[1].getreceivedbyaccount(account)
|
||
11 years ago
|
if balance != balance_by_account:
|
||
|
raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
|
||
|
|
||
10 years ago
|
self.nodes[1].generate(10)
|
||
10 years ago
|
self.sync_all()
|
||
11 years ago
|
# listreceivedbyaccount should return updated account balance
|
||
9 years ago
|
assert_array_result(self.nodes[1].listreceivedbyaccount(),
|
||
11 years ago
|
{"account":account},
|
||
|
{"account":received_by_account_json["account"], "amount":(received_by_account_json["amount"] + Decimal("0.1"))})
|
||
|
|
||
|
# getreceivedbyaddress should return updates balance
|
||
10 years ago
|
balance = self.nodes[1].getreceivedbyaccount(account)
|
||
11 years ago
|
if balance != balance_by_account + Decimal("0.1"):
|
||
|
raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
|
||
|
|
||
|
#Create a new account named "mynewaccount" that has a 0 balance
|
||
10 years ago
|
self.nodes[1].getaccountaddress("mynewaccount")
|
||
|
received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0,True),{"account":"mynewaccount"})
|
||
11 years ago
|
if len(received_by_account_json) == 0:
|
||
|
raise AssertionError("No accounts found in node")
|
||
|
|
||
|
# Test includeempty of listreceivedbyaccount
|
||
|
if received_by_account_json["amount"] != Decimal("0.0"):
|
||
|
raise AssertionError("Wrong balance returned by listreceivedbyaccount, %0.2f"%(received_by_account_json["amount"]))
|
||
|
|
||
|
# Test getreceivedbyaccount for 0 amount accounts
|
||
10 years ago
|
balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
|
||
11 years ago
|
if balance != Decimal("0.0"):
|
||
|
raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
|
||
11 years ago
|
|
||
|
if __name__ == '__main__':
|
||
11 years ago
|
ReceivedByTest().main()
|