Skip to content

Commit

Permalink
feat: account pending deposits for abnormal rebase calc
Browse files Browse the repository at this point in the history
  • Loading branch information
vgorkavenko committed Dec 5, 2024
1 parent 782e22a commit 6d2f2ff
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/modules/accounting/accounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def _get_consensus_lido_state(self, blockstamp: ReferenceBlockStamp) -> tuple[Va
lido_validators = self.w3.lido_validators.get_lido_validators(blockstamp)

count = len(lido_validators)
pending_deposits_sum = self.w3.lido_validators.get_lido_validators_pending_deposits_sum(blockstamp)
pending_deposits_sum = self.w3.lido_validators.calculate_pending_deposits_sum(lido_validators)
total_balance = Gwei(sum(int(validator.balance) for validator in lido_validators) + pending_deposits_sum)

logger.info({'msg': 'Calculate lido state on CL. (Validators count, Total balance in gwei)', 'value': (count, total_balance)})
Expand Down
3 changes: 2 additions & 1 deletion src/services/bunker_cases/abnormal_cl_rebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ def _get_lido_validators_balance_with_vault(
Get Lido validator balance with withdrawals vault balance
"""
real_cl_balance = AbnormalClRebase.calculate_validators_balance_sum(lido_validators)
pending_deposits_sum = LidoValidatorsProvider.calculate_pending_deposits_sum(lido_validators)
withdrawals_vault_balance = int(
self.w3.from_wei(self.w3.lido_contracts.get_withdrawal_balance_no_cache(blockstamp), "gwei")
)
return Gwei(real_cl_balance + withdrawals_vault_balance)
return Gwei(real_cl_balance + pending_deposits_sum + withdrawals_vault_balance)

def _get_withdrawn_from_vault_between_blocks(
self, prev_blockstamp: BlockStamp, ref_blockstamp: ReferenceBlockStamp
Expand Down
19 changes: 8 additions & 11 deletions src/web3py/extensions/lido_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,6 @@ def get_lido_validators(self, blockstamp: BlockStamp) -> list[LidoValidator]:

return self.merge_validators_with_keys(lido_keys, validators)

@lru_cache(maxsize=1)
def get_lido_validators_pending_deposits_sum(self, blockstamp: BlockStamp) -> int:
lido_validators = self.get_lido_validators(blockstamp)

pending_deposits_sum = 0
for validator in lido_validators:
if int(validator.balance) == 0 and int(validator.validator.activation_epoch) == FAR_FUTURE_EPOCH:
pending_deposits_sum += MIN_ACTIVATION_BALANCE

return pending_deposits_sum

def _kapi_sanity_check(self, keys_count_received: int, blockstamp: BlockStamp):
stats = self.w3.lido_contracts.lido.get_beacon_stat(blockstamp.block_hash)

Expand All @@ -184,6 +173,14 @@ def merge_validators_with_keys(keys: list[LidoKey], validators: list[Validator])

return lido_validators

@staticmethod
def calculate_pending_deposits_sum(lido_validators: list[LidoValidator]) -> int:
return sum(
MIN_ACTIVATION_BALANCE
for validator in lido_validators
if int(validator.balance) == 0 and int(validator.validator.activation_epoch) == FAR_FUTURE_EPOCH
)

@lru_cache(maxsize=1)
def get_lido_validators_by_node_operators(self, blockstamp: BlockStamp) -> ValidatorsByNodeOperator:
merged_validators = self.get_lido_validators(blockstamp)
Expand Down
16 changes: 14 additions & 2 deletions tests/modules/accounting/bunker/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from src.services.bunker_cases.abnormal_cl_rebase import AbnormalClRebase
from src.services.bunker_cases.types import BunkerConfig
from src.types import BlockNumber, BlockStamp, ReferenceBlockStamp
from tests.modules.ejector.test_exit_order_state_service import FAR_FUTURE_EPOCH


def simple_ref_blockstamp(block_number: int) -> ReferenceBlockStamp:
Expand All @@ -25,7 +26,9 @@ def simple_key(pubkey: str) -> LidoKey:
return key


def simple_validator(index, pubkey, balance, slashed=False, withdrawable_epoch='', exit_epoch='100500') -> Validator:
def simple_validator(
index, pubkey, balance, slashed=False, withdrawable_epoch='', exit_epoch='100500', activation_epoch="0"
) -> Validator:
return Validator(
index=str(index),
balance=str(balance),
Expand All @@ -36,7 +39,7 @@ def simple_validator(index, pubkey, balance, slashed=False, withdrawable_epoch='
effective_balance=str(32 * 10**9),
slashed=slashed,
activation_eligibility_epoch='',
activation_epoch='0',
activation_epoch=activation_epoch,
exit_epoch=exit_epoch,
withdrawable_epoch=withdrawable_epoch,
),
Expand Down Expand Up @@ -134,6 +137,7 @@ def _get_withdrawal_vault_balance(blockstamp: BlockStamp):
31: 2 * 10**18,
33: 2 * 10**18,
40: 2 * 10**18,
50: 2 * 10**18,
}
return balance[blockstamp.block_number]

Expand Down Expand Up @@ -199,6 +203,14 @@ def _get_validators(state: ReferenceBlockStamp, _=None):
simple_validator(4, '0x04', 32 * 10**9),
simple_validator(5, '0x05', (32 * 10**9) + 824112),
],
50: [
simple_validator(4, '0x00', balance=0, activation_epoch=FAR_FUTURE_EPOCH),
simple_validator(1, '0x01', 32 * 10**9),
simple_validator(2, '0x02', 32 * 10**9),
simple_validator(3, '0x03', (32 * 10**9) + 333333),
simple_validator(4, '0x04', balance=0, activation_epoch=FAR_FUTURE_EPOCH),
simple_validator(5, '0x05', (32 * 10**9) + 824112),
],
1000: [
simple_validator(0, '0x00', 32 * 10**9),
simple_validator(1, '0x01', 32 * 10**9),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_is_abnormal_cl_rebase(
@pytest.mark.parametrize(
("blockstamp", "expected_rebase"),
[
(simple_ref_blockstamp(50), 512000000),
(simple_ref_blockstamp(40), 420650924),
(simple_ref_blockstamp(20), 140216974),
(simple_ref_blockstamp(123), 1120376622),
Expand Down Expand Up @@ -234,6 +235,7 @@ def test_calculate_cl_rebase_between_blocks(
@pytest.mark.parametrize(
("blockstamp", "expected_result"),
[
(simple_ref_blockstamp(50), 98001157445),
(simple_ref_blockstamp(40), 98001157445),
(simple_ref_blockstamp(20), 77999899300),
],
Expand Down
5 changes: 3 additions & 2 deletions tests/web3_extentions/test_lido_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_get_lido_validators(web3, lido_validators, contracts):


@pytest.mark.unit
def test_get_lido_validators_pending_deposits_sum(web3, lido_validators, contracts):
def test_calc_pending_deposits_sum(web3, lido_validators, contracts):
validators = [ValidatorFactory.build_pending_deposit_vals() for _ in range(5)]
validators.extend(ValidatorFactory.batch(30))
lido_keys = LidoKeyFactory.generate_for_validators(validators[:15])
Expand All @@ -51,7 +51,8 @@ def test_get_lido_validators_pending_deposits_sum(web3, lido_validators, contrac
web3.cc.get_validators = Mock(return_value=validators)
web3.kac.get_used_lido_keys = Mock(return_value=lido_keys)

pending_deposits_sum = web3.lido_validators.get_lido_validators_pending_deposits_sum(blockstamp)
lido_validators = web3.lido_validators.get_lido_validators(blockstamp)
pending_deposits_sum = web3.lido_validators.calculate_pending_deposits_sum(lido_validators)

assert pending_deposits_sum == 5 * MIN_ACTIVATION_BALANCE

Expand Down

0 comments on commit 6d2f2ff

Please sign in to comment.