Skip to content

Commit

Permalink
fix: active validators count
Browse files Browse the repository at this point in the history
  • Loading branch information
F4ever committed Oct 16, 2024
1 parent 4c750d4 commit 4bb78a1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/modules/ejector/ejector.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,11 @@ def _get_churn_limit(self, blockstamp: ReferenceBlockStamp) -> int:
return churn_limit

def _get_total_active_validators(self, blockstamp: ReferenceBlockStamp) -> int:
total_active_validators = len([
is_active_validator(val, blockstamp.ref_epoch)
for val in self.w3.cc.get_validators(blockstamp)
])
total_active_validators = reduce(
lambda total, validator: total + int(is_active_validator(validator, blockstamp.ref_epoch)),
self.w3.cc.get_validators(blockstamp),
0,
)
logger.info({'msg': 'Calculate total active validators.', 'value': total_active_validators})
return total_active_validators

Expand Down
30 changes: 30 additions & 0 deletions tests/factory/no_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ def build_with_activation_epoch_bound(cls, max_value: int, **kwargs: Any):
validator=ValidatorStateFactory.build(activation_epoch=str(faker.pyint(max_value=max_value - 1))), **kwargs
)

@classmethod
def build_not_active_vals(cls, epoch, **kwargs: Any):
return cls.build(
validator=ValidatorStateFactory.build(
activation_epoch=str(faker.pyint(min_value=epoch + 1, max_value=FAR_FUTURE_EPOCH)),
exit_epoch=str(FAR_FUTURE_EPOCH),
),
**kwargs
)

@classmethod
def build_active_vals(cls, epoch, **kwargs: Any):
return cls.build(
validator=ValidatorStateFactory.build(
activation_epoch=str(faker.pyint(min_value=0, max_value=epoch - 1)),
exit_epoch=str(faker.pyint(min_value=epoch + 1, max_value=FAR_FUTURE_EPOCH)),
),
**kwargs
)

@classmethod
def build_exit_vals(cls, epoch, **kwargs: Any):
return cls.build(
validator=ValidatorStateFactory.build(
activation_epoch='0',
exit_epoch=str(faker.pyint(min_value=1, max_value=epoch)),
),
**kwargs
)


class NodeOperatorFactory(Web3Factory):
__model__ = NodeOperator
Expand Down
15 changes: 15 additions & 0 deletions tests/modules/ejector/test_ejector.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ def test_get_predicted_withdrawable_epoch(ejector: Ejector) -> None:
assert result == 3809, "Unexpected predicted withdrawable epoch"


@pytest.mark.unit
def test_get_total_active_validators(ejector: Ejector) -> None:
ref_blockstamp = ReferenceBlockStampFactory.build(ref_epoch=3546)
ejector.w3 = Mock()
ejector.w3.cc.get_validators = Mock(
return_value=[
*[LidoValidatorFactory.build_not_active_vals(ref_blockstamp.ref_epoch) for _ in range(150)],
*[LidoValidatorFactory.build_active_vals(ref_blockstamp.ref_epoch) for _ in range(100)],
*[LidoValidatorFactory.build_exit_vals(ref_blockstamp.ref_epoch) for _ in range(50)],
]
)

assert ejector._get_total_active_validators(ref_blockstamp) == 100


@pytest.mark.unit
@pytest.mark.usefixtures("consensus_client", "lido_validators")
def test_get_withdrawable_lido_validators(
Expand Down

0 comments on commit 4bb78a1

Please sign in to comment.