[tests] add test_address method to wallet_import.py

Adds a new test_address method for testing the
imported addresses.
This commit is contained in:
John Newbery 2018-12-06 10:20:11 -05:00
parent fd3a02c381
commit fbdba40594

View file

@ -116,6 +116,16 @@ class ImportMultiTest(BitcoinTestFramework):
assert_equal(result[0]['error']['code'], error_code) assert_equal(result[0]['error']['code'], error_code)
assert_equal(result[0]['error']['message'], error_message) assert_equal(result[0]['error']['message'], error_message)
def test_address(self, address, **kwargs):
"""Get address info for `address` and test whether the returned values are as expected."""
addr_info = self.nodes[1].getaddressinfo(address)
for key, value in kwargs.items():
if value is None:
if key in addr_info.keys():
raise AssertionError("key {} unexpectedly returned in getaddressinfo.".format(key))
elif addr_info[key] != value:
raise AssertionError("key {} value {} did not match expected value {}".format(key, addr_info[key], value))
def run_test(self): def run_test(self):
self.log.info("Mining blocks...") self.log.info("Mining blocks...")
self.nodes[0].generate(1) self.nodes[0].generate(1)
@ -144,11 +154,11 @@ class ImportMultiTest(BitcoinTestFramework):
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now"}, "timestamp": "now"},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], True) iswatchonly=True,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp,
assert_equal(address_assert['ischange'], False) ischange=False)
watchonly_address = address watchonly_address = address
watchonly_timestamp = timestamp watchonly_timestamp = timestamp
@ -166,11 +176,11 @@ class ImportMultiTest(BitcoinTestFramework):
"timestamp": "now", "timestamp": "now",
"internal": True}, "internal": True},
True) True)
address_assert = self.nodes[1].getaddressinfo(key.p2pkh_addr) self.test_address(key.p2pkh_addr,
assert_equal(address_assert['iswatchonly'], True) iswatchonly=True,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp,
assert_equal(address_assert['ischange'], True) ischange=True)
# ScriptPubKey + internal + label # ScriptPubKey + internal + label
self.log.info("Should not allow a label to be specified when internal is true") self.log.info("Should not allow a label to be specified when internal is true")
@ -193,10 +203,10 @@ class ImportMultiTest(BitcoinTestFramework):
False, False,
error_code=-8, error_code=-8,
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') error_message='Internal must be set to true for nonstandard scriptPubKey imports.')
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal('timestamp' in address_assert, False) timestamp=None)
# Address + Public key + !Internal(explicit) # Address + Public key + !Internal(explicit)
self.log.info("Should import an address with public key") self.log.info("Should import an address with public key")
@ -207,10 +217,10 @@ class ImportMultiTest(BitcoinTestFramework):
"pubkeys": [key.pubkey], "pubkeys": [key.pubkey],
"internal": False}, "internal": False},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], True) iswatchonly=True,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp)
# ScriptPubKey + Public key + internal # ScriptPubKey + Public key + internal
self.log.info("Should import a scriptPubKey with internal and with public key") self.log.info("Should import a scriptPubKey with internal and with public key")
@ -221,10 +231,10 @@ class ImportMultiTest(BitcoinTestFramework):
"pubkeys": [key.pubkey], "pubkeys": [key.pubkey],
"internal": True}, "internal": True},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], True) iswatchonly=True,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp)
# Nonstandard scriptPubKey + Public key + !internal # Nonstandard scriptPubKey + Public key + !internal
self.log.info("Should not import a nonstandard scriptPubKey without internal and with public key") self.log.info("Should not import a nonstandard scriptPubKey without internal and with public key")
@ -236,10 +246,10 @@ class ImportMultiTest(BitcoinTestFramework):
False, False,
error_code=-8, error_code=-8,
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') error_message='Internal must be set to true for nonstandard scriptPubKey imports.')
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal('timestamp' in address_assert, False) timestamp=None)
# Address + Private key + !watchonly # Address + Private key + !watchonly
self.log.info("Should import an address with private key") self.log.info("Should import an address with private key")
@ -249,10 +259,10 @@ class ImportMultiTest(BitcoinTestFramework):
"timestamp": "now", "timestamp": "now",
"keys": [key.privkey]}, "keys": [key.privkey]},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], True) ismine=True,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp)
self.log.info("Should not import an address with private key if is already imported") self.log.info("Should not import an address with private key if is already imported")
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": address},
@ -273,10 +283,10 @@ class ImportMultiTest(BitcoinTestFramework):
False, False,
error_code=-8, error_code=-8,
error_message='Watch-only addresses should not include private keys') error_message='Watch-only addresses should not include private keys')
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal('timestamp' in address_assert, False) timestamp=None)
# ScriptPubKey + Private key + internal # ScriptPubKey + Private key + internal
self.log.info("Should import a scriptPubKey with internal and with private key") self.log.info("Should import a scriptPubKey with internal and with private key")
@ -287,10 +297,10 @@ class ImportMultiTest(BitcoinTestFramework):
"keys": [key.privkey], "keys": [key.privkey],
"internal": True}, "internal": True},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], True) ismine=True,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp)
# Nonstandard scriptPubKey + Private key + !internal # Nonstandard scriptPubKey + Private key + !internal
self.log.info("Should not import a nonstandard scriptPubKey without internal and with private key") self.log.info("Should not import a nonstandard scriptPubKey without internal and with private key")
@ -302,10 +312,10 @@ class ImportMultiTest(BitcoinTestFramework):
False, False,
error_code=-8, error_code=-8,
error_message='Internal must be set to true for nonstandard scriptPubKey imports.') error_message='Internal must be set to true for nonstandard scriptPubKey imports.')
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal('timestamp' in address_assert, False) timestamp=None)
# P2SH address # P2SH address
multisig = self.get_multisig() multisig = self.get_multisig()
@ -318,10 +328,10 @@ class ImportMultiTest(BitcoinTestFramework):
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr}, self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr},
"timestamp": "now"}, "timestamp": "now"},
True) True)
address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) self.test_address(multisig.p2sh_addr,
assert_equal(address_assert['isscript'], True) isscript=True,
assert_equal(address_assert['iswatchonly'], True) iswatchonly=True,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp)
p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0] p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0]
assert_equal(p2shunspent['spendable'], False) assert_equal(p2shunspent['spendable'], False)
assert_equal(p2shunspent['solvable'], False) assert_equal(p2shunspent['solvable'], False)
@ -338,8 +348,7 @@ class ImportMultiTest(BitcoinTestFramework):
"timestamp": "now", "timestamp": "now",
"redeemscript": multisig.redeem_script}, "redeemscript": multisig.redeem_script},
True) True)
address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) self.test_address(multisig.p2sh_addr, timestamp=timestamp)
assert_equal(address_assert['timestamp'], timestamp)
p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0] p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0]
assert_equal(p2shunspent['spendable'], False) assert_equal(p2shunspent['spendable'], False)
@ -358,8 +367,8 @@ class ImportMultiTest(BitcoinTestFramework):
"redeemscript": multisig.redeem_script, "redeemscript": multisig.redeem_script,
"keys": multisig.privkeys[0:2]}, "keys": multisig.privkeys[0:2]},
True) True)
address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) self.test_address(multisig.p2sh_addr,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp)
p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0] p2shunspent = self.nodes[1].listunspent(0, 999999, [multisig.p2sh_addr])[0]
assert_equal(p2shunspent['spendable'], False) assert_equal(p2shunspent['spendable'], False)
@ -393,10 +402,10 @@ class ImportMultiTest(BitcoinTestFramework):
False, False,
error_code=-5, error_code=-5,
error_message='Key does not match address destination') error_message='Key does not match address destination')
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal('timestamp' in address_assert, False) timestamp=None)
# ScriptPubKey + Public key + internal + Wrong pubkey # ScriptPubKey + Public key + internal + Wrong pubkey
self.log.info("Should not import a scriptPubKey with internal and with a wrong public key") self.log.info("Should not import a scriptPubKey with internal and with a wrong public key")
@ -410,10 +419,10 @@ class ImportMultiTest(BitcoinTestFramework):
False, False,
error_code=-5, error_code=-5,
error_message='Key does not match address destination') error_message='Key does not match address destination')
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal('timestamp' in address_assert, False) timestamp=None)
# Address + Private key + !watchonly + Wrong private key # Address + Private key + !watchonly + Wrong private key
self.log.info("Should not import an address with a wrong private key") self.log.info("Should not import an address with a wrong private key")
@ -426,10 +435,10 @@ class ImportMultiTest(BitcoinTestFramework):
False, False,
error_code=-5, error_code=-5,
error_message='Key does not match address destination') error_message='Key does not match address destination')
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal('timestamp' in address_assert, False) timestamp=None)
# ScriptPubKey + Private key + internal + Wrong private key # ScriptPubKey + Private key + internal + Wrong private key
self.log.info("Should not import a scriptPubKey with internal and with a wrong private key") self.log.info("Should not import a scriptPubKey with internal and with a wrong private key")
@ -443,10 +452,10 @@ class ImportMultiTest(BitcoinTestFramework):
False, False,
error_code=-5, error_code=-5,
error_message='Key does not match address destination') error_message='Key does not match address destination')
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal('timestamp' in address_assert, False) timestamp=None)
# Importing existing watch only address with new timestamp should replace saved timestamp. # Importing existing watch only address with new timestamp should replace saved timestamp.
assert_greater_than(timestamp, watchonly_timestamp) assert_greater_than(timestamp, watchonly_timestamp)
@ -454,19 +463,19 @@ class ImportMultiTest(BitcoinTestFramework):
self.test_importmulti({"scriptPubKey": {"address": watchonly_address}, self.test_importmulti({"scriptPubKey": {"address": watchonly_address},
"timestamp": "now"}, "timestamp": "now"},
True) True)
address_assert = self.nodes[1].getaddressinfo(watchonly_address) self.test_address(watchonly_address,
assert_equal(address_assert['iswatchonly'], True) iswatchonly=True,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal(address_assert['timestamp'], timestamp) timestamp=timestamp)
watchonly_timestamp = timestamp watchonly_timestamp = timestamp
# restart nodes to check for proper serialization/deserialization of watch only address # restart nodes to check for proper serialization/deserialization of watch only address
self.stop_nodes() self.stop_nodes()
self.start_nodes() self.start_nodes()
address_assert = self.nodes[1].getaddressinfo(watchonly_address) self.test_address(watchonly_address,
assert_equal(address_assert['iswatchonly'], True) iswatchonly=True,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal(address_assert['timestamp'], watchonly_timestamp) timestamp=watchonly_timestamp)
# Bad or missing timestamps # Bad or missing timestamps
self.log.info("Should throw on invalid or missing timestamp values") self.log.info("Should throw on invalid or missing timestamp values")
@ -485,9 +494,9 @@ class ImportMultiTest(BitcoinTestFramework):
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now"}, "timestamp": "now"},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], True) iswatchonly=True,
assert_equal(address_assert['solvable'], False) solvable=False)
# Import P2WPKH address with public key but no private key # Import P2WPKH address with public key but no private key
self.log.info("Should import a P2WPKH address and public key as solvable but not spendable") self.log.info("Should import a P2WPKH address and public key as solvable but not spendable")
@ -497,9 +506,9 @@ class ImportMultiTest(BitcoinTestFramework):
"timestamp": "now", "timestamp": "now",
"pubkeys": [key.pubkey]}, "pubkeys": [key.pubkey]},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['ismine'], False) ismine=False,
assert_equal(address_assert['solvable'], True) solvable=True)
# Import P2WPKH address with key and check it is spendable # Import P2WPKH address with key and check it is spendable
self.log.info("Should import a P2WPKH address with key") self.log.info("Should import a P2WPKH address with key")
@ -509,9 +518,9 @@ class ImportMultiTest(BitcoinTestFramework):
"timestamp": "now", "timestamp": "now",
"keys": [key.privkey]}, "keys": [key.privkey]},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['iswatchonly'], False) iswatchonly=False,
assert_equal(address_assert['ismine'], True) ismine=True)
# P2WSH multisig address without scripts or keys # P2WSH multisig address without scripts or keys
multisig = self.get_multisig() multisig = self.get_multisig()
@ -519,8 +528,8 @@ class ImportMultiTest(BitcoinTestFramework):
self.test_importmulti({"scriptPubKey": {"address": multisig.p2wsh_addr}, self.test_importmulti({"scriptPubKey": {"address": multisig.p2wsh_addr},
"timestamp": "now"}, "timestamp": "now"},
True) True)
address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) self.test_address(multisig.p2sh_addr,
assert_equal(address_assert['solvable'], False) solvable=False)
# Same P2WSH multisig address as above, but now with witnessscript + private keys # Same P2WSH multisig address as above, but now with witnessscript + private keys
self.log.info("Should import a p2wsh with respective witness script and private keys") self.log.info("Should import a p2wsh with respective witness script and private keys")
@ -529,10 +538,10 @@ class ImportMultiTest(BitcoinTestFramework):
"witnessscript": multisig.redeem_script, "witnessscript": multisig.redeem_script,
"keys": multisig.privkeys}, "keys": multisig.privkeys},
True) True)
address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) self.test_address(multisig.p2sh_addr,
assert_equal(address_assert['solvable'], True) solvable=True,
assert_equal(address_assert['ismine'], True) ismine=True,
assert_equal(address_assert['sigsrequired'], 2) sigsrequired=2)
# P2SH-P2WPKH address with no redeemscript or public or private key # P2SH-P2WPKH address with no redeemscript or public or private key
key = self.get_key() key = self.get_key()
@ -541,9 +550,9 @@ class ImportMultiTest(BitcoinTestFramework):
self.test_importmulti({"scriptPubKey": {"address": address}, self.test_importmulti({"scriptPubKey": {"address": address},
"timestamp": "now"}, "timestamp": "now"},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['solvable'], False) solvable=False,
assert_equal(address_assert['ismine'], False) ismine=False)
# P2SH-P2WPKH address + redeemscript + public key with no private key # P2SH-P2WPKH address + redeemscript + public key with no private key
self.log.info("Should import a p2sh-p2wpkh with respective redeem script and pubkey as solvable") self.log.info("Should import a p2sh-p2wpkh with respective redeem script and pubkey as solvable")
@ -552,9 +561,9 @@ class ImportMultiTest(BitcoinTestFramework):
"redeemscript": key.p2sh_p2wpkh_redeem_script, "redeemscript": key.p2sh_p2wpkh_redeem_script,
"pubkeys": [key.pubkey]}, "pubkeys": [key.pubkey]},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['solvable'], True) solvable=True,
assert_equal(address_assert['ismine'], False) ismine=False)
# P2SH-P2WPKH address + redeemscript + private key # P2SH-P2WPKH address + redeemscript + private key
key = self.get_key() key = self.get_key()
@ -565,9 +574,9 @@ class ImportMultiTest(BitcoinTestFramework):
"redeemscript": key.p2sh_p2wpkh_redeem_script, "redeemscript": key.p2sh_p2wpkh_redeem_script,
"keys": [key.privkey]}, "keys": [key.privkey]},
True) True)
address_assert = self.nodes[1].getaddressinfo(address) self.test_address(address,
assert_equal(address_assert['solvable'], True) solvable=True,
assert_equal(address_assert['ismine'], True) ismine=True)
# P2SH-P2WSH multisig + redeemscript with no private key # P2SH-P2WSH multisig + redeemscript with no private key
multisig = self.get_multisig() multisig = self.get_multisig()
@ -577,8 +586,8 @@ class ImportMultiTest(BitcoinTestFramework):
"redeemscript": multisig.p2wsh_script, "redeemscript": multisig.p2wsh_script,
"witnessscript": multisig.redeem_script}, "witnessscript": multisig.redeem_script},
True) True)
address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) self.test_address(address,
assert_equal(address_assert['solvable'], True) solvable=True)
if __name__ == '__main__': if __name__ == '__main__':
ImportMultiTest().main() ImportMultiTest().main()