moved show_election_status to planetmint

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2022-09-29 16:40:09 +02:00
parent 789ca8476d
commit a4a08c64ae
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
4 changed files with 40 additions and 37 deletions

View File

@ -226,7 +226,7 @@ def run_election_show(args, planet):
logger.error(f"No election found with election_id {args.election_id}") logger.error(f"No election found with election_id {args.election_id}")
return return
response = election.show_election(planet) response = planet.show_election_status(election)
logger.info(response) logger.info(response)

View File

@ -8,6 +8,7 @@ MongoDB.
""" """
import logging import logging
import json
from collections import namedtuple from collections import namedtuple
from uuid import uuid4 from uuid import uuid4
@ -20,7 +21,7 @@ import requests
import planetmint import planetmint
from planetmint.config import Config from planetmint.config import Config
from planetmint import backend, config_utils, fastquery 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 ( from planetmint.transactions.common.exceptions import (
DuplicateTransaction, DuplicateTransaction,
InvalidSignature, InvalidSignature,
@ -42,7 +43,7 @@ from planetmint.transactions.common.transaction_mode_types import (
BROADCAST_TX_ASYNC, BROADCAST_TX_ASYNC,
BROADCAST_TX_SYNC, 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 import exceptions as core_exceptions
from planetmint.transactions.types.elections.election import Election from planetmint.transactions.types.elections.election import Election
from planetmint.validation import BaseValidationRules from planetmint.validation import BaseValidationRules
@ -741,4 +742,40 @@ class Planetmint(object):
return recipients 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")) Block = namedtuple("Block", ("app_hash", "height", "transactions"))

View File

@ -24,27 +24,5 @@ class ChainMigrationElection(Election):
def on_approval(self, planet, *args, **kwargs): # TODO: move somewhere else def on_approval(self, planet, *args, **kwargs): # TODO: move somewhere else
planet.migrate_abci_chain() 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 def on_rollback(self, planet, new_height): # TODO: move somewhere else
planet.delete_abci_chain(new_height) planet.delete_abci_chain(new_height)

View File

@ -134,18 +134,6 @@ class Election(Transaction):
return False 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 @classmethod
def _get_initiated_elections(cls, height, txns): # TODO: move somewhere else def _get_initiated_elections(cls, height, txns): # TODO: move somewhere else
elections = [] elections = []