diff --git a/planetmint/commands/planetmint.py b/planetmint/commands/planetmint.py index 0815061..d5c3892 100644 --- a/planetmint/commands/planetmint.py +++ b/planetmint/commands/planetmint.py @@ -226,7 +226,7 @@ def run_election_show(args, planet): logger.error(f"No election found with election_id {args.election_id}") return - response = election.show_election(planet) + response = planet.show_election_status(election) logger.info(response) diff --git a/planetmint/lib.py b/planetmint/lib.py index d67a00a..2e59cca 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -8,6 +8,7 @@ MongoDB. """ import logging +import json from collections import namedtuple from uuid import uuid4 @@ -20,7 +21,7 @@ import requests import planetmint from planetmint.config import Config from planetmint import backend, config_utils, fastquery -from planetmint.transactions.common.transaction import VALIDATOR_ELECTION, Transaction +from planetmint.transactions.common.transaction import CHAIN_MIGRATION_ELECTION, VALIDATOR_ELECTION, Transaction from planetmint.transactions.common.exceptions import ( DuplicateTransaction, InvalidSignature, @@ -42,7 +43,7 @@ from planetmint.transactions.common.transaction_mode_types import ( BROADCAST_TX_ASYNC, BROADCAST_TX_SYNC, ) -from planetmint.tendermint_utils import encode_transaction, merkleroot, key_from_base64 +from planetmint.tendermint_utils import encode_transaction, merkleroot, key_from_base64, public_key_to_base64 from planetmint import exceptions as core_exceptions from planetmint.transactions.types.elections.election import Election from planetmint.validation import BaseValidationRules @@ -741,4 +742,40 @@ class Planetmint(object): return recipients + def show_election_status(self, transaction): + data = transaction.asset["data"] + if "public_key" in data.keys(): + data["public_key"] = public_key_to_base64(data["public_key"]["value"]) + response = "" + for k, v in data.items(): + if k != "seed": + response += f"{k}={v}\n" + response += f"status={self.get_election_status(transaction)}" + + if transaction.operation == CHAIN_MIGRATION_ELECTION: + response = self.append_chain_migration_status(response) + + return response + + def append_chain_migration_status(self, status): + chain = self.get_latest_abci_chain() + if chain is None or chain["is_synced"]: + return status + + status += f'\nchain_id={chain["chain_id"]}' + block = self.get_latest_block() + status += f'\napp_hash={block["app_hash"]}' + validators = [ + { + "pub_key": { + "type": "tendermint/PubKeyEd25519", + "value": k, + }, + "power": v, + } + for k, v in self.get_validator_dict().items() + ] + status += f"\nvalidators={json.dumps(validators, indent=4)}" + return status + Block = namedtuple("Block", ("app_hash", "height", "transactions")) diff --git a/planetmint/transactions/types/elections/chain_migration_election.py b/planetmint/transactions/types/elections/chain_migration_election.py index 9d46bfd..4cf4fc4 100644 --- a/planetmint/transactions/types/elections/chain_migration_election.py +++ b/planetmint/transactions/types/elections/chain_migration_election.py @@ -24,27 +24,5 @@ class ChainMigrationElection(Election): def on_approval(self, planet, *args, **kwargs): # TODO: move somewhere else planet.migrate_abci_chain() - def show_election(self, planet): # TODO: move somewhere else - output = super().show_election(planet) - chain = planet.get_latest_abci_chain() - if chain is None or chain["is_synced"]: - return output - - output += f'\nchain_id={chain["chain_id"]}' - block = planet.get_latest_block() - output += f'\napp_hash={block["app_hash"]}' - validators = [ - { - "pub_key": { - "type": "tendermint/PubKeyEd25519", - "value": k, - }, - "power": v, - } - for k, v in planet.get_validator_dict().items() - ] - output += f"\nvalidators={json.dumps(validators, indent=4)}" - return output - def on_rollback(self, planet, new_height): # TODO: move somewhere else planet.delete_abci_chain(new_height) diff --git a/planetmint/transactions/types/elections/election.py b/planetmint/transactions/types/elections/election.py index b3a4329..17b0fba 100644 --- a/planetmint/transactions/types/elections/election.py +++ b/planetmint/transactions/types/elections/election.py @@ -134,18 +134,6 @@ class Election(Transaction): return False - def show_election(self, planet): # TODO: move somewhere else - data = self.asset["data"] - if "public_key" in data.keys(): - data["public_key"] = public_key_to_base64(data["public_key"]["value"]) - response = "" - for k, v in data.items(): - if k != "seed": - response += f"{k}={v}\n" - response += f"status={planet.get_election_status(self)}" - - return response - @classmethod def _get_initiated_elections(cls, height, txns): # TODO: move somewhere else elections = []