Skip to content

Commit

Permalink
Merge pull request #610 from lidofinance/feat/final-refactoring
Browse files Browse the repository at this point in the history
Final round of refactoring
  • Loading branch information
F4ever authored Jan 31, 2025
2 parents b76ce13 + 810ee8b commit 79c4b7a
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 991 deletions.
18 changes: 7 additions & 11 deletions src/modules/accounting/accounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class Accounting(BaseModule, ConsensusModule):
Report goes in tree phases:
- Send report hash
- Send report data (extra data hash inside)
Contains information about lido states, last withdrawal request to finalize and exited validators count by module.
- Send report data (with extra data hash inside)
Contains information about lido state, withdrawal requests to finalize and exited validators count by module.
- Send extra data
Contains stuck and exited validators count by each node operator.
Contains stuck and exited validator's updates count by each node operator.
"""
COMPATIBLE_ONCHAIN_VERSIONS = [(2, 2), (2, 3)]

Expand All @@ -67,7 +67,7 @@ def __init__(self, w3: Web3):
self.bunker_service = BunkerService(self.w3)

def refresh_contracts(self):
self.report_contract = self.w3.lido_contracts.accounting_oracle
self.report_contract = self.w3.lido_contracts.accounting_oracle # type: ignore

def execute_module(self, last_finalized_blockstamp: BlockStamp) -> ModuleExecuteDelay:
report_blockstamp = self.get_blockstamp_for_report(last_finalized_blockstamp)
Expand Down Expand Up @@ -220,7 +220,8 @@ def _get_consensus_lido_state(self, blockstamp: ReferenceBlockStamp) -> tuple[Va
if self.w3.cc.is_electra_activated(blockstamp.ref_epoch):
state = self.w3.cc.get_state_view(blockstamp)
total_lido_eth1_bridge_deposits_amount = self.w3.lido_validators.calculate_total_eth1_bridge_deposits_amount(
lido_validators, state.pending_deposits
lido_validators,
state.pending_deposits,
)
logger.info({'msg': 'Calculate Lido eth1 bridge deposits (in Gwei)', 'value': total_lido_eth1_bridge_deposits_amount})
total_lido_balance += total_lido_eth1_bridge_deposits_amount
Expand Down Expand Up @@ -330,12 +331,7 @@ def _is_bunker(self, blockstamp: ReferenceBlockStamp) -> BunkerMode:

@lru_cache(maxsize=1)
def get_extra_data(self, blockstamp: ReferenceBlockStamp) -> ExtraData:
chain_config = self.get_chain_config(blockstamp)
stuck_validators = self.lido_validator_state_service.get_lido_newly_stuck_validators(blockstamp, chain_config)
logger.info({'msg': 'Calculate stuck validators.', 'value': stuck_validators})
exited_validators = self.lido_validator_state_service.get_lido_newly_exited_validators(blockstamp)
logger.info({'msg': 'Calculate exited validators.', 'value': exited_validators})
orl = self.w3.lido_contracts.oracle_report_sanity_checker.get_oracle_report_limits(blockstamp.block_hash)
stuck_validators, exited_validators, orl = self._get_generic_extra_data(blockstamp)

return ExtraDataService.collect(
stuck_validators,
Expand Down
32 changes: 9 additions & 23 deletions src/modules/ejector/ejector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from src.modules.submodules.types import ZERO_HASH
from src.providers.consensus.types import Validator, BeaconStateView
from src.providers.execution.contracts.exit_bus_oracle import ExitBusOracleContract
from src.services.exit_order.iterator import ExitOrderIterator
from src.services.exit_order_v2.iterator import ValidatorExitIteratorV2
from src.services.exit_order_iterator import ValidatorExitIterator
from src.services.prediction import RewardsPredictionService
from src.services.validator_state import LidoValidatorStateService
from src.types import BlockStamp, EpochNumber, Gwei, NodeOperatorGlobalIndex, ReferenceBlockStamp
Expand Down Expand Up @@ -121,8 +120,12 @@ def get_validators_to_eject(self, blockstamp: ReferenceBlockStamp) -> list[tuple

expected_balance = self._get_total_expected_balance([], blockstamp)

consensus_version = self.get_consensus_version(blockstamp)
validators_iterator = iter(self.get_validators_iterator(consensus_version, blockstamp))
chain_config = self.get_chain_config(blockstamp)
validators_iterator = iter(ValidatorExitIterator(
w3=self.w3,
blockstamp=blockstamp,
seconds_per_slot=chain_config.seconds_per_slot
))

validators_to_eject: list[tuple[NodeOperatorGlobalIndex, LidoValidator]] = []
total_balance_to_eject_wei = 0
Expand Down Expand Up @@ -182,22 +185,6 @@ def _get_total_expected_balance(self, vals_to_exit: list[Validator], blockstamp:

return Wei(future_rewards + future_withdrawals + total_available_balance + going_to_withdraw_balance)

def get_validators_iterator(self, consensus_version: int, blockstamp: ReferenceBlockStamp):
chain_config = self.get_chain_config(blockstamp)

if consensus_version == 1:
return ExitOrderIterator(
web3=self.w3,
blockstamp=blockstamp,
chain_config=chain_config
)

return ValidatorExitIteratorV2(
w3=self.w3,
blockstamp=blockstamp,
seconds_per_slot=chain_config.seconds_per_slot
)

def is_reporting_allowed(self, blockstamp: ReferenceBlockStamp) -> bool:
on_pause = self.report_contract.is_paused('latest')
CONTRACT_ON_PAUSE.labels('vebo').set(on_pause)
Expand Down Expand Up @@ -235,8 +222,7 @@ def _get_predicted_withdrawable_epoch(
"""
Returns epoch when all validators in queue and validators_to_eject will be withdrawn.
"""

if self.w3.cc.is_electra_activated(blockstamp.ref_epoch):
if self.w3.cc.is_electra_activated(blockstamp.ref_epoch) and self.get_consensus_version(blockstamp) > 2:
return self._get_predicted_withdrawable_epoch_post_electra(blockstamp, validators_to_eject)

return self._get_predicted_withdrawable_epoch_pre_electra(blockstamp, validators_to_eject)
Expand Down Expand Up @@ -296,7 +282,7 @@ def compute_exit_epoch_and_update_churn(
additional_epochs = (balance_to_process - 1) // per_epoch_churn + 1
earliest_exit_epoch += additional_epochs

return earliest_exit_epoch
return EpochNumber(earliest_exit_epoch)

@lru_cache(maxsize=1)
def _get_latest_exit_epoch(self, blockstamp: ReferenceBlockStamp) -> tuple[EpochNumber, int]:
Expand Down
1 change: 1 addition & 0 deletions src/providers/consensus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ConsensusClient(HTTPProvider):
API_GET_SPEC = 'eth/v1/config/spec'
API_GET_GENESIS = 'eth/v1/beacon/genesis'

@lru_cache(maxsize=1)
def is_electra_activated(self, epoch: EpochNumber) -> bool:
spec = self.get_config_spec()
return epoch >= spec.ELECTRA_FORK_EPOCH
Expand Down
2 changes: 1 addition & 1 deletion src/providers/consensus/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class BeaconStateView(Nested, FromResponse):
validators: list[ValidatorState]
balances: list[Gwei]

# This fields are new in Electra, so here are default values for backward compatibility.
# These fields are new in Electra, so here are default values for backward compatibility.
exit_balance_to_consume: Gwei = Gwei(0)
earliest_exit_epoch: EpochNumber = EpochNumber(0)
pending_deposits: list[PendingDeposit] = field(default_factory=list)
Expand Down
Empty file.
176 changes: 0 additions & 176 deletions src/services/exit_order/iterator.py

This file was deleted.

Loading

0 comments on commit 79c4b7a

Please sign in to comment.