From f07b44ec4127d4bf2254c3d70c38ef6dd4b230b6 Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Thu, 13 Jun 2024 16:46:09 +0200 Subject: [PATCH] fix: restake after eviction --- operate/services/manage.py | 19 ++++++++++++++++--- operate/services/protocol.py | 4 ++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/operate/services/manage.py b/operate/services/manage.py index a03b5aafa..8f31b916f 100644 --- a/operate/services/manage.py +++ b/operate/services/manage.py @@ -675,6 +675,12 @@ def stake_service_on_chain(self, hash: str) -> None: service.store() return + if state == StakingState.EVICTED: + self.logger.info(f"{service.chain_data.token} has been evicted") + service.chain_data.staked = True + service.store() + self.unstake_service_on_chain(hash=hash) + self.logger.info(f"Staking service: {service.chain_data.token}") ocm.stake( service_id=service.chain_data.token, @@ -714,6 +720,12 @@ def stake_service_on_chain_from_safe(self, hash: str) -> None: service.store() return + if state == StakingState.EVICTED: + self.logger.info(f"{service.chain_data.token} has been evicted") + service.chain_data.staked = True + service.store() + self.unstake_service_on_chain_from_safe(hash=hash) + self.logger.info(f"Approving staking: {service.chain_data.token}") sftxb.new_tx().add( sftxb.get_staking_approval_data( @@ -751,7 +763,8 @@ def unstake_service_on_chain(self, hash: str) -> None: service_id=service.chain_data.token, staking_contract=STAKING[service.ledger_config.chain], ) - if state != StakingState.STAKED: + self.logger.info(f"Staking status for service {service.chain_data.token}: {state}") + if state not in {StakingState.STAKED, StakingState.EVICTED}: self.logger.info("Cannot unstake service, it's not staked") service.chain_data.staked = False service.store() @@ -781,8 +794,8 @@ def unstake_service_on_chain_from_safe(self, hash: str) -> None: service_id=service.chain_data.token, staking_contract=STAKING[service.ledger_config.chain], ) - self.logger.info(f"Checking staking status for: {service.chain_data.token}") - if state != StakingState.STAKED: + self.logger.info(f"Staking status for service {service.chain_data.token}: {state}") + if state not in {StakingState.STAKED, StakingState.EVICTED}: self.logger.info("Cannot unstake service, it's not staked") service.chain_data.staked = False service.store() diff --git a/operate/services/protocol.py b/operate/services/protocol.py index 5853f8516..401afbc1a 100644 --- a/operate/services/protocol.py +++ b/operate/services/protocol.py @@ -248,7 +248,7 @@ def check_staking_compatibility( """Check if service can be staked.""" status = self.status(service_id, staking_contract) if status == StakingState.STAKED: - raise ValueError("Service already stacked") + raise ValueError("Service already staked") if status == StakingState.EVICTED: raise ValueError("Service is evicted") @@ -330,7 +330,7 @@ def check_if_unstaking_possible( """Check unstaking availability""" if ( self.status(service_id=service_id, staking_contract=staking_contract) - != StakingState.STAKED + not in {StakingState.STAKED, StakingState.EVICTED} ): raise ValueError("Service not staked.")