Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Feb 7, 2024
1 parent 48f3e43 commit 70b7b02
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 43 deletions.
2 changes: 1 addition & 1 deletion multiversx_sdk_cli/cli_delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
43 changes: 13 additions & 30 deletions multiversx_sdk_cli/delegation/staking_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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] = []
Expand Down
22 changes: 10 additions & 12 deletions multiversx_sdk_cli/validators/validators_file.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit 70b7b02

Please sign in to comment.