moved rollback_eleciton to planetmint

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2022-10-10 13:01:40 +02:00
parent c3d5b7ef82
commit 7dec1b16ab
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
4 changed files with 20 additions and 21 deletions

View File

@ -265,5 +265,5 @@ def rollback(b):
# NOTE: the pre-commit state is always at most 1 block ahead of the commited state # NOTE: the pre-commit state is always at most 1 block ahead of the commited state
if latest_block["height"] < pre_commit["height"]: if latest_block["height"] < pre_commit["height"]:
Election.rollback(b, pre_commit["height"], pre_commit["transactions"]) b.rollback_election(pre_commit["height"], pre_commit["transactions"])
b.delete_transactions(pre_commit["transactions"]) b.delete_transactions(pre_commit["transactions"])

View File

@ -931,4 +931,22 @@ class Planetmint(object):
return True return True
def rollback_election(self, new_height, txn_ids): # TODO: move somewhere else
"""Looks for election and vote transactions inside the block and
cleans up the database artifacts possibly created in `process_blocks`.
Part of the `end_block`/`commit` crash recovery.
"""
# delete election records for elections initiated at this height and
# elections concluded at this height
self.delete_elections(new_height)
txns = [self.get_transaction(tx_id) for tx_id in txn_ids]
elections = self._get_votes(txns)
for election_id in elections:
election = self.get_transaction(election_id)
election.on_rollback(self, new_height)
Block = namedtuple("Block", ("app_hash", "height", "transactions")) Block = namedtuple("Block", ("app_hash", "height", "transactions"))

View File

@ -67,25 +67,6 @@ class Election(Transaction):
if cls.TX_SCHEMA_CUSTOM: if cls.TX_SCHEMA_CUSTOM:
_validate_schema(cls.TX_SCHEMA_CUSTOM, tx) _validate_schema(cls.TX_SCHEMA_CUSTOM, tx)
@classmethod
def rollback(cls, planet, new_height, txn_ids): # TODO: move somewhere else
"""Looks for election and vote transactions inside the block and
cleans up the database artifacts possibly created in `process_blocks`.
Part of the `end_block`/`commit` crash recovery.
"""
# delete election records for elections initiated at this height and
# elections concluded at this height
planet.delete_elections(new_height)
txns = [planet.get_transaction(tx_id) for tx_id in txn_ids]
elections = planet._get_votes(txns)
for election_id in elections:
election = planet.get_transaction(election_id)
election.on_rollback(planet, new_height)
def on_approval(self, planet, new_height): def on_approval(self, planet, new_height):
"""Override to update the database state according to the """Override to update the database state according to the
election rules. Consider the current database state to account for election rules. Consider the current database state to account for

View File

@ -15,7 +15,7 @@ from planetmint.backend.schema import TABLES, SPACE_NAMES
from planetmint.transactions.common import crypto from planetmint.transactions.common import crypto
from planetmint.transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT from planetmint.transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT
from planetmint.transactions.types.assets.create import Create from planetmint.transactions.types.assets.create import Create
from planetmint.transactions.types.elections.election import Election, Vote from planetmint.transactions.types.elections.vote import Vote
from planetmint.transactions.types.elections.validator_utils import election_id_to_public_key from planetmint.transactions.types.elections.validator_utils import election_id_to_public_key
from planetmint.tendermint_utils import key_to_base64 from planetmint.tendermint_utils import key_to_base64