2014-11-27 06:01:25 +01:00
|
|
|
#!/usr/bin/env python2
|
2015-12-13 14:51:43 +01:00
|
|
|
# Copyright (c) 2014-2015 The Bitcoin Core developers
|
2014-10-23 03:48:19 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2014-03-25 14:33:44 +01:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-07-08 18:07:23 +02:00
|
|
|
# Exercise the listreceivedbyaddress API
|
2014-03-25 14:33:44 +01:00
|
|
|
|
2015-05-02 12:53:35 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import *
|
2014-03-25 14:33:44 +01:00
|
|
|
|
2014-07-08 18:07:23 +02:00
|
|
|
|
2014-03-25 14:33:44 +01:00
|
|
|
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
|
|
|
|
'''
|
|
|
|
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
|
|
|
|
return item
|
|
|
|
return []
|
|
|
|
|
2014-07-08 18:07:23 +02:00
|
|
|
class ReceivedByTest(BitcoinTestFramework):
|
2014-03-25 14:33:44 +01:00
|
|
|
|
2015-12-05 18:02:02 +01:00
|
|
|
def setup_nodes(self):
|
|
|
|
#This test requires mocktime
|
|
|
|
enable_mocktime()
|
|
|
|
return start_nodes(4, self.options.tmpdir)
|
|
|
|
|
2014-10-20 14:14:04 +02:00
|
|
|
def run_test(self):
|
2014-07-08 18:07:23 +02:00
|
|
|
'''
|
|
|
|
listreceivedbyaddress Test
|
|
|
|
'''
|
|
|
|
# Send from node 0 to 1
|
2014-10-20 14:14:04 +02:00
|
|
|
addr = self.nodes[1].getnewaddress()
|
|
|
|
txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
|
|
|
self.sync_all()
|
2014-07-08 18:07:23 +02:00
|
|
|
|
|
|
|
#Check not listed in listreceivedbyaddress because has 0 confirmations
|
2016-04-19 13:22:11 +02:00
|
|
|
assert_array_result(self.nodes[1].listreceivedbyaddress(),
|
2014-07-08 18:07:23 +02:00
|
|
|
{"address":addr},
|
|
|
|
{ },
|
|
|
|
True)
|
|
|
|
#Bury Tx under 10 block so it will be returned by listreceivedbyaddress
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[1].generate(10)
|
2014-10-20 14:14:04 +02:00
|
|
|
self.sync_all()
|
2016-04-19 13:22:11 +02:00
|
|
|
assert_array_result(self.nodes[1].listreceivedbyaddress(),
|
2014-07-08 18:07:23 +02:00
|
|
|
{"address":addr},
|
|
|
|
{"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
|
|
|
|
#With min confidence < 10
|
2016-04-19 13:22:11 +02:00
|
|
|
assert_array_result(self.nodes[1].listreceivedbyaddress(5),
|
2014-07-08 18:07:23 +02:00
|
|
|
{"address":addr},
|
|
|
|
{"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
|
|
|
|
#With min confidence > 10, should not find Tx
|
2016-04-19 13:22:11 +02:00
|
|
|
assert_array_result(self.nodes[1].listreceivedbyaddress(11),{"address":addr},{ },True)
|
2014-07-08 18:07:23 +02:00
|
|
|
|
|
|
|
#Empty Tx
|
2014-10-20 14:14:04 +02:00
|
|
|
addr = self.nodes[1].getnewaddress()
|
2017-09-20 22:34:54 +02:00
|
|
|
assert_array_result(self.nodes[1].listreceivedbyaddress(0, False, True),
|
2014-07-08 18:07:23 +02:00
|
|
|
{"address":addr},
|
|
|
|
{"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
|
|
|
|
|
|
|
|
'''
|
|
|
|
getreceivedbyaddress Test
|
|
|
|
'''
|
|
|
|
# Send from node 0 to 1
|
2014-10-20 14:14:04 +02:00
|
|
|
addr = self.nodes[1].getnewaddress()
|
|
|
|
txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
|
|
|
self.sync_all()
|
2014-07-08 18:07:23 +02:00
|
|
|
|
|
|
|
#Check balance is 0 because of 0 confirmations
|
2014-10-20 14:14:04 +02:00
|
|
|
balance = self.nodes[1].getreceivedbyaddress(addr)
|
2014-07-08 18:07:23 +02:00
|
|
|
if balance != Decimal("0.0"):
|
|
|
|
raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
|
|
|
|
|
|
|
|
#Check balance is 0.1
|
2014-10-20 14:14:04 +02:00
|
|
|
balance = self.nodes[1].getreceivedbyaddress(addr,0)
|
2014-07-08 18:07:23 +02:00
|
|
|
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
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[1].generate(10)
|
2014-10-20 14:14:04 +02:00
|
|
|
self.sync_all()
|
|
|
|
balance = self.nodes[1].getreceivedbyaddress(addr)
|
2014-07-08 18:07:23 +02:00
|
|
|
if balance != Decimal("0.1"):
|
|
|
|
raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
|
|
|
|
|
|
|
|
'''
|
|
|
|
listreceivedbyaccount + getreceivedbyaccount Test
|
|
|
|
'''
|
|
|
|
#set pre-state
|
2014-10-20 14:14:04 +02: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})
|
2014-07-08 18:07:23 +02:00
|
|
|
if len(received_by_account_json) == 0:
|
|
|
|
raise AssertionError("No accounts found in node")
|
2014-10-20 14:14:04 +02:00
|
|
|
balance_by_account = rec_by_accountArr = self.nodes[1].getreceivedbyaccount(account)
|
2014-07-08 18:07:23 +02:00
|
|
|
|
2014-10-20 14:14:04 +02:00
|
|
|
txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
2014-11-20 21:49:07 +01:00
|
|
|
self.sync_all()
|
2014-07-08 18:07:23 +02:00
|
|
|
|
|
|
|
# listreceivedbyaccount should return received_by_account_json because of 0 confirmations
|
2016-04-19 13:22:11 +02:00
|
|
|
assert_array_result(self.nodes[1].listreceivedbyaccount(),
|
2014-07-08 18:07:23 +02:00
|
|
|
{"account":account},
|
|
|
|
received_by_account_json)
|
|
|
|
|
|
|
|
# getreceivedbyaddress should return same balance because of 0 confirmations
|
2014-10-20 14:14:04 +02:00
|
|
|
balance = self.nodes[1].getreceivedbyaccount(account)
|
2014-07-08 18:07:23 +02:00
|
|
|
if balance != balance_by_account:
|
|
|
|
raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
|
|
|
|
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[1].generate(10)
|
2014-10-20 14:14:04 +02:00
|
|
|
self.sync_all()
|
2014-07-08 18:07:23 +02:00
|
|
|
# listreceivedbyaccount should return updated account balance
|
2016-04-19 13:22:11 +02:00
|
|
|
assert_array_result(self.nodes[1].listreceivedbyaccount(),
|
2014-07-08 18:07:23 +02:00
|
|
|
{"account":account},
|
|
|
|
{"account":received_by_account_json["account"], "amount":(received_by_account_json["amount"] + Decimal("0.1"))})
|
|
|
|
|
|
|
|
# getreceivedbyaddress should return updates balance
|
2014-10-20 14:14:04 +02:00
|
|
|
balance = self.nodes[1].getreceivedbyaccount(account)
|
2014-07-08 18:07:23 +02:00
|
|
|
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
|
2014-10-20 14:14:04 +02:00
|
|
|
self.nodes[1].getaccountaddress("mynewaccount")
|
2017-09-20 22:34:54 +02:00
|
|
|
received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0, False, True),{"account":"mynewaccount"})
|
2014-07-08 18:07:23 +02:00
|
|
|
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
|
2014-10-20 14:14:04 +02:00
|
|
|
balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
|
2014-07-08 18:07:23 +02:00
|
|
|
if balance != Decimal("0.0"):
|
|
|
|
raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
|
2014-03-25 14:33:44 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-07-08 18:07:23 +02:00
|
|
|
ReceivedByTest().main()
|