Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
review comments - fixed redunant if and print
Browse files Browse the repository at this point in the history
  • Loading branch information
maaktweluit committed Jun 7, 2019
1 parent 8456a78 commit 12ae6df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
41 changes: 26 additions & 15 deletions scripts/node_integration_tests/key_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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] = {}
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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'),
Expand Down
34 changes: 13 additions & 21 deletions tests/factories/granary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:])

Expand Down

0 comments on commit 12ae6df

Please sign in to comment.