-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvalidator_state.py
116 lines (88 loc) · 4.51 KB
/
validator_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from typing import Sequence
from src.constants import (
MAX_EFFECTIVE_BALANCE,
ETH1_ADDRESS_WITHDRAWAL_PREFIX,
SHARD_COMMITTEE_PERIOD,
FAR_FUTURE_EPOCH,
EFFECTIVE_BALANCE_INCREMENT,
MAX_SEED_LOOKAHEAD,
MIN_PER_EPOCH_CHURN_LIMIT,
CHURN_LIMIT_QUOTIENT,
)
from src.providers.consensus.types import Validator
from src.types import EpochNumber, Gwei
def is_active_validator(validator: Validator, epoch: EpochNumber) -> bool:
"""
Check if ``validator`` is active.
https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#is_active_validator
"""
return int(validator.validator.activation_epoch) <= epoch < int(validator.validator.exit_epoch)
def is_exited_validator(validator: Validator, epoch: EpochNumber) -> bool:
return int(validator.validator.exit_epoch) <= epoch
def is_on_exit(validator: Validator) -> bool:
"""Validator exited or is going to exit"""
return int(validator.validator.exit_epoch) != FAR_FUTURE_EPOCH
def get_validator_age(validator: Validator, ref_epoch: EpochNumber) -> int:
"""Validator age in epochs from activation to ref_epoch"""
return max(ref_epoch - int(validator.validator.activation_epoch), 0)
def is_partially_withdrawable_validator(validator: Validator) -> bool:
"""
Check if `validator` is partially withdrawable
https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/beacon-chain.md#is_partially_withdrawable_validator
"""
has_max_effective_balance = int(validator.validator.effective_balance) == MAX_EFFECTIVE_BALANCE
has_excess_balance = int(validator.balance) > MAX_EFFECTIVE_BALANCE
return (
has_eth1_withdrawal_credential(validator)
and has_max_effective_balance
and has_excess_balance
)
def has_eth1_withdrawal_credential(validator: Validator) -> bool:
"""
Check if ``validator`` has an 0x01 prefixed "eth1" withdrawal credential.
https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/beacon-chain.md#has_eth1_withdrawal_credential
"""
return validator.validator.withdrawal_credentials[:4] == ETH1_ADDRESS_WITHDRAWAL_PREFIX
def is_fully_withdrawable_validator(validator: Validator, epoch: EpochNumber) -> bool:
"""
Check if `validator` is fully withdrawable
https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/beacon-chain.md#is_fully_withdrawable_validator
"""
return (
has_eth1_withdrawal_credential(validator)
and EpochNumber(int(validator.validator.withdrawable_epoch)) <= epoch
and Gwei(int(validator.balance)) > Gwei(0)
)
def is_validator_eligible_to_exit(validator: Validator, epoch: EpochNumber) -> bool:
"""
Check if `validator` can exit.
Verify the validator has been active long enough.
https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#voluntary-exits
"""
active_long_enough = int(validator.validator.activation_epoch) + SHARD_COMMITTEE_PERIOD <= epoch
return active_long_enough and not is_on_exit(validator)
def calculate_total_active_effective_balance(all_validators: Sequence[Validator], ref_epoch: EpochNumber) -> Gwei:
"""
Return the combined effective balance of the active validators.
Note: returns ``EFFECTIVE_BALANCE_INCREMENT`` Gwei minimum to avoid divisions by zero.
https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#get_total_active_balance
"""
total_effective_balance = calculate_active_effective_balance_sum(all_validators, ref_epoch)
return Gwei(max(EFFECTIVE_BALANCE_INCREMENT, total_effective_balance))
def calculate_active_effective_balance_sum(validators: Sequence[Validator], ref_epoch: EpochNumber) -> Gwei:
"""
Return the combined effective balance of the active validators from the given list
"""
effective_balance_sum = 0
for validator in validators:
if is_active_validator(validator, ref_epoch):
effective_balance_sum += int(validator.validator.effective_balance)
return Gwei(effective_balance_sum)
def compute_activation_exit_epoch(ref_epoch: EpochNumber):
"""
Return the epoch during which validator activations and exits initiated in ``epoch`` take effect.
Spec: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#compute_activation_exit_epoch
"""
return ref_epoch + 1 + MAX_SEED_LOOKAHEAD
def compute_exit_churn_limit(active_validators_count: int):
return max(MIN_PER_EPOCH_CHURN_LIMIT, active_validators_count // CHURN_LIMIT_QUOTIENT)