Skip to content

Instantly share code, notes, and snippets.

@jtraglia
Created April 8, 2025 14:29
Show Gist options
  • Save jtraglia/457fd9ae7d2080fef1e4034a39b80c46 to your computer and use it in GitHub Desktop.
Save jtraglia/457fd9ae7d2080fef1e4034a39b80c46 to your computer and use it in GitHub Desktop.
"""
An updated version of the Eth2 Weak Subjectivity Period Calculation:
https://gist.github.com/adiasg/3aceab409b36aa9a9d9156c1baa3c248
"""
from remerkleable.basic import uint64
CHURN_LIMIT_QUOTIENT = uint64(65536)
EFFECTIVE_BALANCE_INCREMENT = uint64(1000000000)
ETH_TO_GWEI = uint64(10**9)
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA = uint64(128000000000)
MIN_VALIDATOR_WITHDRAWABILITY_DELAY = uint64(256)
SAFETY_DECAY = uint64(10)
def get_balance_churn_limit(total_active_balance) -> uint64:
"""
Return the churn limit for the current epoch.
"""
churn = max(
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA,
total_active_balance // CHURN_LIMIT_QUOTIENT
)
return churn - churn % EFFECTIVE_BALANCE_INCREMENT
def compute_weak_subjectivity_period(total_active_balance: uint64, balance_churn_limit: uint64) -> uint64:
"""
Returns the weak subjectivity period for the current ``state``.
This computation takes into account the effect of:
- validator set churn (bounded by ``get_balance_churn_limit()`` per epoch)
A detailed calculation can be found at:
https://notes.ethereum.org/@CarlBeek/electra_weak_subjectivity
"""
t = total_active_balance
delta = balance_churn_limit
epochs_for_validator_set_churn = SAFETY_DECAY * t // (2 * delta * 100)
return MIN_VALIDATOR_WITHDRAWABILITY_DELAY + epochs_for_validator_set_churn
print("| Safety Decay | Total Active Balance (ETH) | Weak Sub. Period (Epochs) |")
print("| ----: | ----: | ----: |")
for exp in range(20, 26):
total_active_balance_eth = uint64(2**exp)
total_active_balance_gwei = total_active_balance_eth * ETH_TO_GWEI
balance_churn_limit = get_balance_churn_limit(total_active_balance_gwei)
weak_subjectivity_period = compute_weak_subjectivity_period(total_active_balance_gwei, balance_churn_limit)
print(f"| {SAFETY_DECAY} | {total_active_balance_eth:,} | {weak_subjectivity_period:,} |")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment