test: add rpc getaddressinfo labels test coverage

This commit is contained in:
Jon Atack 2019-10-28 11:11:20 +01:00
parent 0f3539ac6d
commit 33f5fc32e5
No known key found for this signature in database
GPG key ID: 4F5721B3D0E3921D
6 changed files with 58 additions and 22 deletions

View file

@ -88,6 +88,11 @@ def get_multisig(node):
p2sh_p2wsh_script=CScript([OP_HASH160, witness_script, OP_EQUAL]).hex(),
p2sh_p2wsh_addr=script_to_p2sh_p2wsh(script_code))
def labels_value(name="", purpose="receive"):
"""Generate a getaddressinfo labels array from a name and purpose.
Often used as the value of a labels kwarg for calling test_address below."""
return [{"name": name, "purpose": purpose}]
def test_address(node, address, **kwargs):
"""Get address info for `address` and test whether the returned values are as expected."""
addr_info = node.getaddressinfo(address)

View file

@ -15,6 +15,10 @@ from test_framework.util import (
connect_nodes,
wait_until,
)
from test_framework.wallet_util import (
labels_value,
test_address,
)
class WalletTest(BitcoinTestFramework):
@ -390,7 +394,7 @@ class WalletTest(BitcoinTestFramework):
for label in [u'рыба', u'𝅘𝅥𝅯']:
addr = self.nodes[0].getnewaddress()
self.nodes[0].setlabel(addr, label)
assert_equal(self.nodes[0].getaddressinfo(addr)['label'], label)
test_address(self.nodes[0], addr, label=label, labels=labels_value(name=label))
assert label in self.nodes[0].listlabels()
self.nodes[0].rpc.ensure_ascii = True # restore to default

View file

@ -11,7 +11,10 @@ with and without a label.
"""
from test_framework.test_framework import BitcoinTestFramework
from test_framework.wallet_util import test_address
from test_framework.wallet_util import (
labels_value,
test_address,
)
class ImportWithLabel(BitcoinTestFramework):
@ -36,7 +39,8 @@ class ImportWithLabel(BitcoinTestFramework):
address,
iswatchonly=True,
ismine=False,
label=label)
label=label,
labels=labels_value(name=label))
self.log.info(
"Import the watch-only address's private key without a "
@ -47,7 +51,8 @@ class ImportWithLabel(BitcoinTestFramework):
test_address(self.nodes[1],
address,
label=label)
label=label,
labels=labels_value(name=label))
self.log.info(
"Test importaddress without label and importprivkey with label."
@ -59,7 +64,8 @@ class ImportWithLabel(BitcoinTestFramework):
address2,
iswatchonly=True,
ismine=False,
label="")
label="",
labels=labels_value())
self.log.info(
"Import the watch-only address's private key with a "
@ -71,7 +77,8 @@ class ImportWithLabel(BitcoinTestFramework):
test_address(self.nodes[1],
address2,
label=label2)
label=label2,
labels=labels_value(name=label2))
self.log.info("Test importaddress with label and importprivkey with label.")
self.log.info("Import a watch-only address with a label.")
@ -82,7 +89,8 @@ class ImportWithLabel(BitcoinTestFramework):
address3,
iswatchonly=True,
ismine=False,
label=label3_addr)
label=label3_addr,
labels=labels_value(name=label3_addr))
self.log.info(
"Import the watch-only address's private key with a "
@ -94,7 +102,8 @@ class ImportWithLabel(BitcoinTestFramework):
test_address(self.nodes[1],
address3,
label=label3_priv)
label=label3_priv,
labels=labels_value(name=label3_priv))
self.log.info(
"Test importprivkey won't label new dests with the same "
@ -109,6 +118,7 @@ class ImportWithLabel(BitcoinTestFramework):
iswatchonly=True,
ismine=False,
label=label4_addr,
labels=labels_value(name=label4_addr),
embedded=None)
self.log.info(
@ -123,10 +133,13 @@ class ImportWithLabel(BitcoinTestFramework):
test_address(self.nodes[1],
embedded_addr,
label="")
label="",
labels=labels_value())
test_address(self.nodes[1],
address4,
label=label4_addr)
label=label4_addr,
labels=labels_value(name=label4_addr))
self.stop_nodes()

View file

@ -29,6 +29,7 @@ from test_framework.util import (
from test_framework.wallet_util import (
get_key,
get_multisig,
labels_value,
test_address,
)
@ -121,7 +122,7 @@ class ImportMultiTest(BitcoinTestFramework):
self.test_importmulti({"scriptPubKey": key.p2pkh_script,
"timestamp": "now",
"internal": True,
"label": "Example label"},
"label": "Unsuccessful labelling for internal addresses"},
success=False,
error_code=-8,
error_message='Internal addresses should not have a label')
@ -550,7 +551,7 @@ class ImportMultiTest(BitcoinTestFramework):
self.log.info("Should not import a p2sh-p2wpkh address from descriptor without checksum and private key")
self.test_importmulti({"desc": "sh(wpkh(" + key.pubkey + "))",
"timestamp": "now",
"label": "Descriptor import test",
"label": "Unsuccessful P2SH-P2WPKH descriptor import",
"keys": [key.privkey]},
success=False,
error_code=-5,
@ -558,17 +559,19 @@ class ImportMultiTest(BitcoinTestFramework):
# Test importing of a P2SH-P2WPKH address via descriptor + private key
key = get_key(self.nodes[0])
p2sh_p2wpkh_label = "Successful P2SH-P2WPKH descriptor import"
self.log.info("Should import a p2sh-p2wpkh address from descriptor and private key")
self.test_importmulti({"desc": descsum_create("sh(wpkh(" + key.pubkey + "))"),
"timestamp": "now",
"label": "Descriptor import test",
"label": p2sh_p2wpkh_label,
"keys": [key.privkey]},
success=True)
test_address(self.nodes[1],
key.p2sh_p2wpkh_addr,
solvable=True,
ismine=True,
label="Descriptor import test")
label=p2sh_p2wpkh_label,
labels=labels_value(name=p2sh_p2wpkh_label))
# Test ranged descriptor fails if range is not specified
xpriv = "tprv8ZgxMBicQKsPeuVhWwi6wuMQGfPKi9Li5GtX35jVNknACgqe3CY4g5xgkfDDJcmtF7o1QnxWDRYw4H5P26PXq7sbcUkEqeR4fg3Kxp2tigg"
@ -628,17 +631,19 @@ class ImportMultiTest(BitcoinTestFramework):
# Test importing of a P2PKH address via descriptor
key = get_key(self.nodes[0])
p2pkh_label = "P2PKH descriptor import"
self.log.info("Should import a p2pkh address from descriptor")
self.test_importmulti({"desc": descsum_create("pkh(" + key.pubkey + ")"),
"timestamp": "now",
"label": "Descriptor import test"},
"label": p2pkh_label},
True,
warnings=["Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."])
test_address(self.nodes[1],
key.p2pkh_addr,
solvable=True,
ismine=False,
label="Descriptor import test")
label=p2pkh_label,
labels=labels_value(name=p2pkh_label))
# Test import fails if both desc and scriptPubKey are provided
key = get_key(self.nodes[0])

View file

@ -13,6 +13,10 @@ from collections import defaultdict
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error
from test_framework.wallet_util import (
labels_value,
test_address,
)
class WalletLabelsTest(BitcoinTestFramework):
def set_test_params(self):
@ -153,11 +157,12 @@ class Label:
if self.receive_address is not None:
assert self.receive_address in self.addresses
for address in self.addresses:
assert_equal(
node.getaddressinfo(address)['labels'][0],
{"name": self.name,
"purpose": self.purpose[address]})
assert_equal(node.getaddressinfo(address)['label'], self.name)
test_address(
node,
address,
label=self.name,
labels=labels_value(name=self.name, purpose=self.purpose[address])
)
assert self.name in node.listlabels()
assert_equal(
node.getaddressesbylabel(self.name),

View file

@ -11,6 +11,10 @@ from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)
from test_framework.wallet_util import (
labels_value,
test_address,
)
class ReceivedByTest(BitcoinTestFramework):
@ -127,7 +131,7 @@ class ReceivedByTest(BitcoinTestFramework):
# set pre-state
label = ''
address = self.nodes[1].getnewaddress()
assert_equal(self.nodes[1].getaddressinfo(address)['label'], label)
test_address(self.nodes[1], address, label=label, labels=labels_value(name=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)