From 70b7b0220aa54b935432ad0b2e761b6a199d9b75 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Wed, 7 Feb 2024 12:51:11 +0200 Subject: [PATCH] fixes after review --- multiversx_sdk_cli/cli_delegation.py | 2 +- .../delegation/staking_provider.py | 43 ++++++------------- .../validators/validators_file.py | 22 +++++----- 3 files changed, 24 insertions(+), 43 deletions(-) diff --git a/multiversx_sdk_cli/cli_delegation.py b/multiversx_sdk_cli/cli_delegation.py index b308fb83..7d936c18 100644 --- a/multiversx_sdk_cli/cli_delegation.py +++ b/multiversx_sdk_cli/cli_delegation.py @@ -221,7 +221,7 @@ def _check_if_either_bls_keys_or_validators_file_are_provided(args: Any): validators_file = args.validators_file if not bls_keys and not validators_file: - raise errors.BadUsage("No bls keys or validators file provided") + raise errors.BadUsage("No bls keys or validators file provided. Use either `--bls-keys` or `--validators-file`") def unbond_nodes(args: Any): diff --git a/multiversx_sdk_cli/delegation/staking_provider.py b/multiversx_sdk_cli/delegation/staking_provider.py index dea7b9c1..6c75bf68 100644 --- a/multiversx_sdk_cli/delegation/staking_provider.py +++ b/multiversx_sdk_cli/delegation/staking_provider.py @@ -83,12 +83,7 @@ def prepare_transaction_for_adding_nodes(self, owner: IAccount, args: Any) -> IT def prepare_transaction_for_removing_nodes(self, owner: IAccount, args: Any) -> ITransaction: delegation_contract = Address.new_from_bech32(args.delegation_contract) - if args.bls_keys: - public_keys = self._parse_public_bls_keys(args.bls_keys) - else: - validators_file_path = Path(args.validators_file).expanduser() - validators_file = ValidatorsFile(validators_file_path) - public_keys = validators_file.load_public_keys() + public_keys = self._load_validators_public_keys(args) tx = self._factory.create_transaction_for_removing_nodes( sender=owner.address, @@ -109,12 +104,7 @@ def prepare_transaction_for_removing_nodes(self, owner: IAccount, args: Any) -> def prepare_transaction_for_staking_nodes(self, owner: IAccount, args: Any) -> ITransaction: delegation_contract = Address.new_from_bech32(args.delegation_contract) - if args.bls_keys: - public_keys = self._parse_public_bls_keys(args.bls_keys) - else: - validators_file_path = Path(args.validators_file).expanduser() - validators_file = ValidatorsFile(validators_file_path) - public_keys = validators_file.load_public_keys() + public_keys = self._load_validators_public_keys(args) tx = self._factory.create_transaction_for_staking_nodes( sender=owner.address, @@ -135,12 +125,7 @@ def prepare_transaction_for_staking_nodes(self, owner: IAccount, args: Any) -> I def prepare_transaction_for_unbonding_nodes(self, owner: IAccount, args: Any) -> ITransaction: delegation_contract = Address.new_from_bech32(args.delegation_contract) - if args.bls_keys: - public_keys = self._parse_public_bls_keys(args.bls_keys) - else: - validators_file_path = Path(args.validators_file).expanduser() - validators_file = ValidatorsFile(validators_file_path) - public_keys = validators_file.load_public_keys() + public_keys = self._load_validators_public_keys(args) tx = self._factory.create_transaction_for_unbonding_nodes( sender=owner.address, @@ -161,12 +146,7 @@ def prepare_transaction_for_unbonding_nodes(self, owner: IAccount, args: Any) -> def prepare_transaction_for_unstaking_nodes(self, owner: IAccount, args: Any) -> ITransaction: delegation_contract = Address.new_from_bech32(args.delegation_contract) - if args.bls_keys: - public_keys = self._parse_public_bls_keys(args.bls_keys) - else: - validators_file_path = Path(args.validators_file).expanduser() - validators_file = ValidatorsFile(validators_file_path) - public_keys = validators_file.load_public_keys() + public_keys = self._load_validators_public_keys(args) tx = self._factory.create_transaction_for_unstaking_nodes( sender=owner.address, @@ -187,12 +167,7 @@ def prepare_transaction_for_unstaking_nodes(self, owner: IAccount, args: Any) -> def prepare_transaction_for_unjailing_nodes(self, owner: IAccount, args: Any) -> ITransaction: delegation_contract = Address.new_from_bech32(args.delegation_contract) - if args.bls_keys: - public_keys = self._parse_public_bls_keys(args.bls_keys) - else: - validators_file_path = Path(args.validators_file).expanduser() - validators_file = ValidatorsFile(validators_file_path) - public_keys = validators_file.load_public_keys() + public_keys = self._load_validators_public_keys(args) tx = self._factory.create_transaction_for_unjailing_nodes( sender=owner.address, @@ -323,6 +298,14 @@ def prepare_transaction_for_setting_metadata(self, owner: IAccount, args: Any) - return tx + def _load_validators_public_keys(self, args: Any) -> List[ValidatorPublicKey]: + if args.bls_keys: + return self._parse_public_bls_keys(args.bls_keys) + + validators_file_path = Path(args.validators_file).expanduser() + validators_file = ValidatorsFile(validators_file_path) + return validators_file.load_public_keys() + def _parse_public_bls_keys(self, public_bls_keys: str) -> List[ValidatorPublicKey]: keys = public_bls_keys.split(",") validator_public_keys: List[ValidatorPublicKey] = [] diff --git a/multiversx_sdk_cli/validators/validators_file.py b/multiversx_sdk_cli/validators/validators_file.py index 7cc5615c..4a664aac 100644 --- a/multiversx_sdk_cli/validators/validators_file.py +++ b/multiversx_sdk_cli/validators/validators_file.py @@ -1,6 +1,6 @@ import json from pathlib import Path -from typing import List +from typing import Dict, List from multiversx_sdk_wallet import ValidatorSigner from multiversx_sdk_wallet.validator_keys import ValidatorPublicKey @@ -24,12 +24,7 @@ def get_validators_list(self): def load_signers(self) -> List[ValidatorSigner]: signers: List[ValidatorSigner] = [] for validator in self.get_validators_list(): - # Get path of "pemFile", make it absolute - validator_pem = Path(validator.get("pemFile")).expanduser() - validator_pem = validator_pem if validator_pem.is_absolute() else self.validators_file_path.parent / validator_pem - - pem_file = ValidatorPEM.from_file(validator_pem) - + pem_file = self._load_validator_pem(validator) validator_signer = ValidatorSigner(pem_file.secret_key) signers.append(validator_signer) @@ -39,15 +34,18 @@ def load_public_keys(self) -> List[ValidatorPublicKey]: public_keys: List[ValidatorPublicKey] = [] for validator in self.get_validators_list(): - # Get path of "pemFile", make it absolute - validator_pem = Path(validator.get("pemFile")).expanduser() - validator_pem = validator_pem if validator_pem.is_absolute() else self.validators_file_path.parent / validator_pem - - pem_file = ValidatorPEM.from_file(validator_pem) + pem_file = self._load_validator_pem(validator) public_keys.append(pem_file.secret_key.generate_public_key()) return public_keys + def _load_validator_pem(self, validator: Dict[str, str]) -> ValidatorPEM: + # Get path of "pemFile", make it absolute + validator_pem = Path(validator.get("pemFile", "")).expanduser() + validator_pem = validator_pem if validator_pem.is_absolute() else self.validators_file_path.parent / validator_pem + + return ValidatorPEM.from_file(validator_pem) + def _read_json_file_validators(self): val_file = self.validators_file_path.expanduser() guards.is_file(val_file)