From 7dec1b16abde4b7aa27ad394cbf6e8d97296d9e0 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Mon, 10 Oct 2022 13:01:40 +0200 Subject: [PATCH] moved rollback_eleciton to planetmint Signed-off-by: Lorenz Herzberger --- planetmint/core.py | 2 +- planetmint/lib.py | 18 ++++++++++++++++++ .../transactions/types/elections/election.py | 19 ------------------- tests/utils.py | 2 +- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/planetmint/core.py b/planetmint/core.py index 9ea3452..bc123dd 100644 --- a/planetmint/core.py +++ b/planetmint/core.py @@ -265,5 +265,5 @@ def rollback(b): # NOTE: the pre-commit state is always at most 1 block ahead of the commited state 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"]) diff --git a/planetmint/lib.py b/planetmint/lib.py index 2343d90..e7805da 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -931,4 +931,22 @@ class Planetmint(object): 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")) diff --git a/planetmint/transactions/types/elections/election.py b/planetmint/transactions/types/elections/election.py index ea0b6de..b72031a 100644 --- a/planetmint/transactions/types/elections/election.py +++ b/planetmint/transactions/types/elections/election.py @@ -67,25 +67,6 @@ class Election(Transaction): if cls.TX_SCHEMA_CUSTOM: _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): """Override to update the database state according to the election rules. Consider the current database state to account for diff --git a/tests/utils.py b/tests/utils.py index e99c022..f330d75 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -15,7 +15,7 @@ from planetmint.backend.schema import TABLES, SPACE_NAMES from planetmint.transactions.common import crypto from planetmint.transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT 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.tendermint_utils import key_to_base64