From 12ae6df8c77ce5d2d95f4723864af69ad7b94cee Mon Sep 17 00:00:00 2001 From: maaktweluit <10008353+maaktweluit@users.noreply.github.com> Date: Fri, 7 Jun 2019 12:35:04 +0200 Subject: [PATCH] review comments - fixed redunant if and print --- scripts/node_integration_tests/key_reuse.py | 41 +++++++++++++-------- tests/factories/granary.py | 34 +++++++---------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/scripts/node_integration_tests/key_reuse.py b/scripts/node_integration_tests/key_reuse.py index 27fc6e82c5..441e2e6857 100644 --- a/scripts/node_integration_tests/key_reuse.py +++ b/scripts/node_integration_tests/key_reuse.py @@ -20,10 +20,10 @@ class NodeKeyReuse: - instance = None - provider = None - enabled = True - granary_hostname = None + instance: Optional[NodeKeyReuse] = None + provider: Optional[NodeKeyReuseBase] = None + enabled: bool = True + granary_hostname: Optional[str] = None @classmethod def get(cls, test_dir: Path): @@ -64,8 +64,15 @@ def enable(cls): def set_granary(cls, hostname): cls.granary_hostname = hostname +class NodeKeyReuseBase(): + def begin_test(self, datadirs: Dict[NodeId, Path]) -> None: + raise Exception("Not implemented") + + def end_test(self) -> None: + raise Exception("Not implemented") -class NodeKeyReuseLocalFolder(): + +class NodeKeyReuseLocalFolder(NodeKeyReuseBase): def __init__(self, test_dir: Path): self.dir: Path = test_dir / 'key_reuse' self.datadirs: Dict[NodeId, Path] = {} @@ -81,7 +88,8 @@ def begin_test(self, datadirs: Dict[NodeId, Path]) -> None: self._recycle_keys() def end_test(self) -> None: - print("NodeKeyReuseLocalFolder.end_test() called.") + if _logging: + print("NodeKeyReuseLocalFolder.end_test() called.") try: if _logging: print("Moving keys from data-dirs to reuse-dirs") @@ -143,7 +151,7 @@ def _copy_keystore(datadir: Path, reuse_dir: Path) -> None: shutil.copyfile(src, dst) -class NodeKeyReuseGranary(): +class NodeKeyReuseGranary(NodeKeyReuseBase): def __init__(self, hostname: str): self.datadirs: Dict[NodeId, Path] = {} self.granary = Granary(hostname) @@ -152,12 +160,12 @@ def begin_test(self, datadirs: Dict[NodeId, Path]) -> None: self.datadirs = datadirs if _logging: print("NodeKeyReuseGranary.begin_test() called.") - if _logging: print("Moving keys from granary to data-dirs") - self._recycle_keys() + self._recycle_keys() def end_test(self) -> None: - print("NodeKeyReuseGranary.end_test() called.") + if _logging: + print("NodeKeyReuseGranary.end_test() called.") try: if _logging: print("Moving keys from data-dirs to granary") @@ -167,7 +175,8 @@ def end_test(self) -> None: return def _recycle_keys(self) -> None: - print("Recycle keys") + if _logging: + print("Recycle keys") # this is run before running second and later tests for i, node_id in enumerate(self.datadirs): account = self.granary.request_account() @@ -207,12 +216,14 @@ def _copy_keystore(datadir: Path) -> Optional[Account]: with open(src_ts_file, 'r') as f: ts = f.read() except FileNotFoundError: - print('No tx.json, continue') + if _logging: + print('No tx.json, continue') try: # read keystore.json with open(src_key_file, 'r') as f: keystore = f.read() except FileNotFoundError: - print('No File, no key') + if _logging: + print('No File, no key') return None keystore = json.loads(keystore) @@ -225,8 +236,8 @@ def _copy_keystore(datadir: Path) -> Optional[Account]: @staticmethod def _save_private_key(key, key_path: Path, password: str) -> None: - print("_save_private_key") - print(password) + if _logging: + print("_save_private_key() password=", password) keystore = create_keyfile_json( key, password.encode('utf-8'), diff --git a/tests/factories/granary.py b/tests/factories/granary.py index 014c7eb694..3085be3949 100644 --- a/tests/factories/granary.py +++ b/tests/factories/granary.py @@ -15,7 +15,6 @@ class Account: def __init__(self, raw_key, ts): self.key = ECCx(raw_key) - random.seed() assert self.key.raw_privkey == raw_key self.raw_key = self.key.raw_privkey if _logging: @@ -39,35 +38,28 @@ def request_account(self) -> Optional[Account]: universal_newlines=True) if _logging: print('returncode:', completed.returncode) - - if _logging: if completed.stderr: print('stderr:', completed.stderr) else: print('stderr: EMPTY') - if completed.stdout: - if _logging: - print('stdout:', completed.stdout) - out_lines = completed.stdout.split('\n') - raw_key = out_lines[0].strip() - - if _logging: - print('raw_key:', raw_key) - key = decode_hex(raw_key) - if _logging: - print('key:', key) - return Account(key, out_lines[1] or None) - elif _logging: + if not completed.stdout: print('stdout: EMPTY') - return None + return None + elif _logging: + print('stdout:', completed.stdout) + + out_lines = completed.stdout.split('\n') + raw_key = out_lines[0].strip() + + key = decode_hex(raw_key) + if _logging: + print('raw_key:', raw_key, 'key:', key) + return Account(key, out_lines[1] or None) def return_account(self, account): if _logging: - print("Granary called, account returned") - print(account) - print(account.raw_key) - print(account.transaction_store) + print("Granary called, account returned. account=", account) key_pub_addr = encode_hex(sha3(account.key.raw_pubkey)[12:])