Skip to content

Commit d00ab3b

Browse files
committed
Add is_active property to ValidatorRecord and refactor helpers
1 parent 8629750 commit d00ab3b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

eth/beacon/helpers.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
clamp,
2828
)
2929

30-
from eth.beacon.block_committees_info import BlockCommitteesInfo
31-
from eth.beacon.enums import (
32-
ValidatorStatusCode,
30+
from eth.beacon.block_committees_info import (
31+
BlockCommitteesInfo,
3332
)
3433
from eth.beacon.types.shard_committees import (
3534
ShardCommittee,
@@ -156,7 +155,7 @@ def get_active_validator_indices(validators: Sequence['ValidatorRecord']) -> Tup
156155
"""
157156
return tuple(
158157
i for i, v in enumerate(validators)
159-
if v.status in [ValidatorStatusCode.ACTIVE, ValidatorStatusCode.ACTIVE_PENDING_EXIT]
158+
if v.is_active
160159
)
161160

162161

eth/beacon/types/validator_records.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
)
44
import rlp
55

6+
from eth.beacon.enums import (
7+
ValidatorStatusCode,
8+
)
69
from eth.rlp.sedes import (
710
uint64,
811
uint256,
@@ -52,3 +55,13 @@ def __init__(self,
5255
latest_status_change_slot=latest_status_change_slot,
5356
exit_count=exit_count,
5457
)
58+
59+
@property
60+
def is_active(self) -> bool:
61+
"""
62+
Returns ``True`` if the validator is active.
63+
"""
64+
return self.status in [
65+
ValidatorStatusCode.ACTIVE,
66+
ValidatorStatusCode.ACTIVE_PENDING_EXIT
67+
]

tests/beacon/types/test_validator_record.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from eth.beacon.enums import (
2+
ValidatorStatusCode,
3+
)
14
from eth.beacon.types.validator_records import (
25
ValidatorRecord,
36
)
@@ -7,3 +10,27 @@ def test_defaults(sample_validator_record_params):
710
validator = ValidatorRecord(**sample_validator_record_params)
811
assert validator.pubkey == sample_validator_record_params['pubkey']
912
assert validator.withdrawal_credentials == sample_validator_record_params['withdrawal_credentials'] # noqa: E501
13+
14+
15+
def test_is_active(sample_validator_record_params):
16+
validator_record_params = {
17+
**sample_validator_record_params,
18+
'status': ValidatorStatusCode.ACTIVE
19+
}
20+
validator = ValidatorRecord(**validator_record_params)
21+
assert validator.is_active
22+
23+
validator_record_params = {
24+
**sample_validator_record_params,
25+
'status': ValidatorStatusCode.ACTIVE_PENDING_EXIT
26+
}
27+
validator = ValidatorRecord(**validator_record_params)
28+
assert validator.is_active
29+
30+
31+
validator_record_params = {
32+
**sample_validator_record_params,
33+
'status': ValidatorStatusCode.EXITED_WITHOUT_PENALTY
34+
}
35+
validator = ValidatorRecord(**validator_record_params)
36+
assert not validator.is_active

0 commit comments

Comments
 (0)