Skip to content

Commit

Permalink
Merge pull request #81 from valefar-on-discord/file-timestamp-sync
Browse files Browse the repository at this point in the history
Share timestamp across generated files for an action
  • Loading branch information
remyroy authored Jul 25, 2024
2 parents ec866e4 + c2bc027 commit 7993d3d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
3 changes: 2 additions & 1 deletion ethstaker_deposit/cli/exit_transaction_keystore.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import click
import os
import time

from typing import Any
from ethstaker_deposit.exit_transaction import exit_transaction_generation, export_exit_transaction_json
Expand Down Expand Up @@ -118,7 +119,7 @@ def exit_transaction_keystore(
os.mkdir(folder)

click.echo(load_text(['msg_exit_transaction_creation']))
saved_folder = export_exit_transaction_json(folder=folder, signed_exit=signed_exit)
saved_folder = export_exit_transaction_json(folder=folder, signed_exit=signed_exit, timestamp=time.time())

click.echo(load_text(['msg_verify_exit_transaction']))
if (not verify_signed_exit_json(saved_folder, keystore.pubkey, chain_settings)):
Expand Down
4 changes: 3 additions & 1 deletion ethstaker_deposit/cli/exit_transaction_mnemonic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
import os
import concurrent.futures
import os
import time

from typing import Any, Sequence, Dict
from ethstaker_deposit.cli.existing_mnemonic import load_mnemonic_arguments_decorator
Expand Down Expand Up @@ -141,6 +142,7 @@ def exit_transaction_mnemonic(
'validator_index': validator_index,
'epoch': epoch,
'folder': folder,
'timestamp': time.time(),
} for credential, validator_index in zip(credentials, validator_indices)]

with concurrent.futures.ProcessPoolExecutor() as executor:
Expand Down
10 changes: 7 additions & 3 deletions ethstaker_deposit/cli/generate_keys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import click
import os
import time
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -142,8 +143,11 @@ def generate_keys(ctx: click.Context, validator_start_index: int,
hex_withdrawal_address=withdrawal_address,
use_pbkdf2=pbkdf2
)
keystore_filefolders = credentials.export_keystores(password=keystore_password, folder=folder)
deposits_file = credentials.export_deposit_data_json(folder=folder)

timestamp = time.time()

keystore_filefolders = credentials.export_keystores(password=keystore_password, folder=folder, timestamp=timestamp)
deposits_file = credentials.export_deposit_data_json(folder=folder, timestamp=timestamp)
if not credentials.verify_keystores(keystore_filefolders=keystore_filefolders, password=keystore_password):
raise ValidationError(load_text(['err_verify_keystores']))
if not verify_deposit_data_json(deposits_file, credentials.credentials):
Expand Down
15 changes: 8 additions & 7 deletions ethstaker_deposit/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def signing_keystore(self, password: str) -> Keystore:
else:
return ScryptKeystore.encrypt(secret=secret, password=password, path=self.signing_key_path)

def save_signing_keystore(self, password: str, folder: str) -> str:
def save_signing_keystore(self, password: str, folder: str, timestamp: float) -> str:
keystore = self.signing_keystore(password)
filefolder = os.path.join(folder, 'keystore-%s-%i.json' % (keystore.path.replace('/', '_'), time.time()))
filefolder = os.path.join(folder, 'keystore-%s-%i.json' % (keystore.path.replace('/', '_'), timestamp))
keystore.save(filefolder)
return filefolder

Expand Down Expand Up @@ -218,7 +218,7 @@ def get_bls_to_execution_change_dict(self, validator_index: int) -> Dict[str, by
result_dict.update({'metadata': metadata})
return result_dict

def save_exit_transaction(self, validator_index: int, epoch: int, folder: str) -> str:
def save_exit_transaction(self, validator_index: int, epoch: int, folder: str, timestamp: float) -> str:
signing_key = self.signing_sk

signed_voluntary_exit = exit_transaction_generation(
Expand All @@ -228,7 +228,7 @@ def save_exit_transaction(self, validator_index: int, epoch: int, folder: str) -
epoch=epoch
)

return export_exit_transaction_json(folder=folder, signed_exit=signed_voluntary_exit)
return export_exit_transaction_json(folder=folder, signed_exit=signed_voluntary_exit, timestamp=timestamp)


def _credential_builder(kwargs: Dict[str, Any]) -> Credential:
Expand Down Expand Up @@ -297,14 +297,15 @@ def from_mnemonic(cls,
bar.update(1)
return cls(credentials)

def export_keystores(self, password: str, folder: str) -> List[str]:
def export_keystores(self, password: str, folder: str, timestamp: float) -> List[str]:
filefolders: List[str] = []
with click.progressbar(length=len(self.credentials), label=load_text(['msg_keystore_creation']),
show_percent=False, show_pos=True) as bar:
executor_kwargs = [{
'credential': credential,
'password': password,
'folder': folder,
'timestamp': timestamp,
} for credential in self.credentials]

with concurrent.futures.ProcessPoolExecutor() as executor:
Expand All @@ -313,7 +314,7 @@ def export_keystores(self, password: str, folder: str) -> List[str]:
bar.update(1)
return filefolders

def export_deposit_data_json(self, folder: str) -> str:
def export_deposit_data_json(self, folder: str, timestamp: float) -> str:
deposit_data = []
with click.progressbar(length=len(self.credentials), label=load_text(['msg_depositdata_creation']),
show_percent=False, show_pos=True) as bar:
Expand All @@ -323,7 +324,7 @@ def export_deposit_data_json(self, folder: str) -> str:
deposit_data.append(datum_dict)
bar.update(1)

filefolder = os.path.join(folder, 'deposit_data-%i.json' % time.time())
filefolder = os.path.join(folder, 'deposit_data-%i.json' % timestamp)
with open(filefolder, 'w') as f:
json.dump(deposit_data, f, default=lambda x: x.hex())
if os.name == 'posix':
Expand Down
6 changes: 3 additions & 3 deletions ethstaker_deposit/exit_transaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import os
import time
from typing import Any, Dict
from py_ecc.bls import G2ProofOfPossession as bls

Expand Down Expand Up @@ -39,7 +38,7 @@ def exit_transaction_generation(
return signed_exit


def export_exit_transaction_json(folder: str, signed_exit: SignedVoluntaryExit) -> str:
def export_exit_transaction_json(folder: str, signed_exit: SignedVoluntaryExit, timestamp: float) -> str:
signed_exit_json: Dict[str, Any] = {}
message = {
'epoch': str(signed_exit.message.epoch), # type: ignore[attr-defined]
Expand All @@ -51,7 +50,8 @@ def export_exit_transaction_json(folder: str, signed_exit: SignedVoluntaryExit)
filefolder = os.path.join(
folder,
'signed_exit_transaction-%s-%i.json' % (
signed_exit.message.validator_index, time.time() # type: ignore[attr-defined]
signed_exit.message.validator_index, # type: ignore[attr-defined]
timestamp,
)
)

Expand Down
7 changes: 5 additions & 2 deletions tests/test_cli/test_exit_transaction_keystore.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time

from click.testing import CliRunner

Expand Down Expand Up @@ -41,7 +42,7 @@ def test_exit_transaction_keystore() -> None:
)

# Save keystore file
keystore_filepath = credential.save_signing_keystore(keystore_password, exit_transaction_folder_path)
keystore_filepath = credential.save_signing_keystore(keystore_password, exit_transaction_folder_path, time.time())

runner = CliRunner()
arguments = [
Expand Down Expand Up @@ -126,10 +127,12 @@ def test_exit_transaction_with_pbkdf2() -> None:
pbkdf2_keystore_filepath = pbkdf2_credential.save_signing_keystore(
keystore_password,
pbkdf2_exit_transaction_folder_path,
time.time(),
)
scrypt_keystore_filepath = scrypt_credential.save_signing_keystore(
keystore_password,
scrypt_exit_transaction_folder_path,
time.time(),
)

runner = CliRunner()
Expand Down Expand Up @@ -260,7 +263,7 @@ def test_invalid_keystore_password() -> None:
)

# Save keystore file
keystore_filepath = credential.save_signing_keystore(keystore_password, exit_transaction_folder_path)
keystore_filepath = credential.save_signing_keystore(keystore_password, exit_transaction_folder_path, time.time())
runner = CliRunner()
inputs = []
data = '\n'.join(inputs)
Expand Down

0 comments on commit 7993d3d

Please sign in to comment.