diff --git a/test/functional/wallet_importmulti.py b/test/functional/wallet_importmulti.py --- a/test/functional/wallet_importmulti.py +++ b/test/functional/wallet_importmulti.py @@ -11,7 +11,9 @@ - `get_key()` and `get_multisig()` are called to generate keys on node0 and return the privkeys, pubkeys and all variants of scriptPubKey and address. - `test_importmulti()` is called to send an importmulti call to node1, test - success, and (if unsuccessful) test the error code and error message returned.""" + success, and (if unsuccessful) test the error code and error message returned. +- `test_address()` is called to call getaddressinfo for an address on node1 + and test the values returned.""" from collections import namedtuple from test_framework.address import ( @@ -108,6 +110,19 @@ assert_equal(result[0]['error']['code'], error_code) 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): self.log.info("Mining blocks...") self.nodes[0].generate(1) @@ -138,11 +153,11 @@ self.test_importmulti({"scriptPubKey": {"address": address}, "timestamp": "now"}, True) - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], True) - assert_equal(address_assert['ismine'], False) - assert_equal(address_assert['timestamp'], timestamp) - assert_equal(address_assert['ischange'], False) + self.test_address(address, + iswatchonly=True, + ismine=False, + timestamp=timestamp, + ischange=False) watchonly_address = address watchonly_timestamp = timestamp @@ -160,11 +175,11 @@ "timestamp": "now", "internal": True}, True) - address_assert = self.nodes[1].getaddressinfo(key.p2pkh_addr) - assert_equal(address_assert['iswatchonly'], True) - assert_equal(address_assert['ismine'], False) - assert_equal(address_assert['timestamp'], timestamp) - assert_equal(address_assert['ischange'], True) + self.test_address(key.p2pkh_addr, + iswatchonly=True, + ismine=False, + timestamp=timestamp, + ischange=True) # ScriptPubKey + internal + label self.log.info( @@ -189,10 +204,10 @@ False, error_code=-8, error_message='Internal must be set to true for nonstandard scriptPubKey imports.') - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], False) - assert_equal('timestamp' in address_assert, False) + self.test_address(address, + iswatchonly=False, + ismine=False, + timestamp=None) # Address + Public key + !Internal(explicit) self.log.info("Should import an address with public key") @@ -203,10 +218,10 @@ "pubkeys": [key.pubkey], "internal": False}, True) - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], True) - assert_equal(address_assert['ismine'], False) - assert_equal(address_assert['timestamp'], timestamp) + self.test_address(address, + iswatchonly=True, + ismine=False, + timestamp=timestamp) # ScriptPubKey + Public key + internal self.log.info( @@ -218,10 +233,10 @@ "pubkeys": [key.pubkey], "internal": True}, True) - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], True) - assert_equal(address_assert['ismine'], False) - assert_equal(address_assert['timestamp'], timestamp) + self.test_address(address, + iswatchonly=True, + ismine=False, + timestamp=timestamp) # Nonstandard scriptPubKey + Public key + !internal self.log.info( @@ -234,10 +249,10 @@ False, error_code=-8, error_message='Internal must be set to true for nonstandard scriptPubKey imports.') - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], False) - assert_equal('timestamp' in address_assert, False) + self.test_address(address, + iswatchonly=False, + ismine=False, + timestamp=None) # Address + Private key + !watchonly self.log.info("Should import an address with private key") @@ -247,10 +262,10 @@ "timestamp": "now", "keys": [key.privkey]}, True) - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], True) - assert_equal(address_assert['timestamp'], timestamp) + self.test_address(address, + iswatchonly=False, + ismine=True, + timestamp=timestamp) self.log.info( "Should not import an address with private key if is already imported") @@ -273,10 +288,10 @@ False, error_code=-8, error_message='Watch-only addresses should not include private keys') - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], False) - assert_equal('timestamp' in address_assert, False) + self.test_address(address, + iswatchonly=False, + ismine=False, + timestamp=None) # ScriptPubKey + Private key + internal self.log.info( @@ -288,10 +303,10 @@ "keys": [key.privkey], "internal": True}, True) - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], True) - assert_equal(address_assert['timestamp'], timestamp) + self.test_address(address, + iswatchonly=False, + ismine=True, + timestamp=timestamp) # Nonstandard scriptPubKey + Private key + !internal self.log.info( @@ -304,10 +319,10 @@ False, error_code=-8, error_message='Internal must be set to true for nonstandard scriptPubKey imports.') - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], False) - assert_equal('timestamp' in address_assert, False) + self.test_address(address, + iswatchonly=False, + ismine=False, + timestamp=None) # P2SH address multisig = self.get_multisig() @@ -321,10 +336,10 @@ self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr}, "timestamp": "now"}, True) - address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) - assert_equal(address_assert['isscript'], True) - assert_equal(address_assert['iswatchonly'], True) - assert_equal(address_assert['timestamp'], timestamp) + self.test_address(multisig.p2sh_addr, + isscript=True, + iswatchonly=True, + timestamp=timestamp) p2shunspent = self.nodes[1].listunspent( 0, 999999, [multisig.p2sh_addr])[0] assert_equal(p2shunspent['spendable'], False) @@ -343,8 +358,7 @@ "timestamp": "now", "redeemscript": multisig.redeem_script}, True) - address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) - assert_equal(address_assert['timestamp'], timestamp) + self.test_address(multisig.p2sh_addr, timestamp=timestamp) p2shunspent = self.nodes[1].listunspent( 0, 999999, [multisig.p2sh_addr])[0] @@ -366,8 +380,8 @@ "redeemscript": multisig.redeem_script, "keys": multisig.privkeys[0:2]}, True) - address_assert = self.nodes[1].getaddressinfo(multisig.p2sh_addr) - assert_equal(address_assert['timestamp'], timestamp) + self.test_address(multisig.p2sh_addr, + timestamp=timestamp) p2shunspent = self.nodes[1].listunspent( 0, 999999, [multisig.p2sh_addr])[0] @@ -404,10 +418,10 @@ False, error_code=-5, error_message='Key does not match address destination') - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], False) - assert_equal('timestamp' in address_assert, False) + self.test_address(address, + iswatchonly=False, + ismine=False, + timestamp=None) # ScriptPubKey + Public key + internal + Wrong pubkey self.log.info( @@ -422,10 +436,10 @@ False, error_code=-5, error_message='Key does not match address destination') - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], False) - assert_equal('timestamp' in address_assert, False) + self.test_address(address, + iswatchonly=False, + ismine=False, + timestamp=None) # Address + Private key + !watchonly + Wrong private key self.log.info("Should not import an address with a wrong private key") @@ -438,10 +452,10 @@ False, error_code=-5, error_message='Key does not match address destination') - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], False) - assert_equal('timestamp' in address_assert, False) + self.test_address(address, + iswatchonly=False, + ismine=False, + timestamp=None) # ScriptPubKey + Private key + internal + Wrong private key self.log.info( @@ -456,10 +470,10 @@ False, error_code=-5, error_message='Key does not match address destination') - address_assert = self.nodes[1].getaddressinfo(address) - assert_equal(address_assert['iswatchonly'], False) - assert_equal(address_assert['ismine'], False) - assert_equal('timestamp' in address_assert, False) + self.test_address(address, + iswatchonly=False, + ismine=False, + timestamp=None) # Importing existing watch only address with new timestamp should # replace saved timestamp. @@ -468,20 +482,20 @@ self.test_importmulti({"scriptPubKey": {"address": watchonly_address}, "timestamp": "now"}, True) - address_assert = self.nodes[1].getaddressinfo(watchonly_address) - assert_equal(address_assert['iswatchonly'], True) - assert_equal(address_assert['ismine'], False) - assert_equal(address_assert['timestamp'], timestamp) + self.test_address(watchonly_address, + iswatchonly=True, + ismine=False, + timestamp=timestamp) watchonly_timestamp = timestamp # restart nodes to check for proper serialization/deserialization of # watch only address self.stop_nodes() self.start_nodes() - address_assert = self.nodes[1].getaddressinfo(watchonly_address) - assert_equal(address_assert['iswatchonly'], True) - assert_equal(address_assert['ismine'], False) - assert_equal(address_assert['timestamp'], watchonly_timestamp) + self.test_address(watchonly_address, + iswatchonly=True, + ismine=False, + timestamp=watchonly_timestamp) # Bad or missing timestamps self.log.info("Should throw on invalid or missing timestamp values")