Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
PR feedback, thanks to ralexstokes
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Feb 28, 2019
1 parent 855c937 commit 78ed252
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions eth2/beacon/state_machines/forks/serenity/epoch_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,15 +876,14 @@ def _update_shuffling_seed(state: BeaconState,


def is_ready_to_active(state: BeaconState,
config: BeaconConfig,
index: ValidatorIndex) -> bool:
index: ValidatorIndex,
max_deposit_amount: Gwei) -> bool:
validator = state.validator_registry[index]
balance = state.validator_balances[index]
return validator.activation_epoch == FAR_FUTURE_EPOCH and balance >= config.MAX_DEPOSIT_AMOUNT
return validator.activation_epoch == FAR_FUTURE_EPOCH and balance >= max_deposit_amount


def is_ready_to_exit(state: BeaconState,
config: BeaconConfig,
index: ValidatorIndex) -> bool:
validator = state.validator_registry[index]
return validator.exit_epoch == FAR_FUTURE_EPOCH and validator.initiated_exit
Expand All @@ -894,14 +893,12 @@ def churn_validators(state: BeaconState,
config: BeaconConfig,
check_should_churn_fn: Callable[..., Any],
churn_fn: Callable[..., Any],
max_balance_churn: int,
**churn_fn_kwargs: Any) -> BeaconState:
max_balance_churn: int) -> BeaconState:
balance_churn = 0
for index in range(len(state.validator_registry)):
index = ValidatorIndex(index)
should_churn = check_should_churn_fn(
state,
config,
index,
)
if should_churn:
Expand All @@ -914,11 +911,7 @@ def churn_validators(state: BeaconState,
if balance_churn > max_balance_churn:
break

state = churn_fn(
state,
index,
**churn_fn_kwargs,
)
state = churn_fn(state, index)
return state


Expand All @@ -944,30 +937,42 @@ def update_validator_registry(state: BeaconState, config: BeaconConfig) -> Beaco
)

# Activate validators within the allowable balance churn
state = churn_validators(
state=state,
config=config,
check_should_churn_fn=is_ready_to_active,
churn_fn=activate_validator,
max_balance_churn=max_balance_churn,
# **churn_fn_kwargs
check_should_churn_fn = lambda state, index: is_ready_to_active(
state,
index,
config.MAX_DEPOSIT_AMOUNT,
)
churn_fn = lambda state, index: activate_validator(
state,
index,
is_genesis=False,
genesis_epoch=config.GENESIS_EPOCH,
slots_per_epoch=config.SLOTS_PER_EPOCH,
activation_exit_delay=config.ACTIVATION_EXIT_DELAY,
)

# Exit validators within the allowable balance churn
state = churn_validators(
state=state,
config=config,
check_should_churn_fn=is_ready_to_exit,
churn_fn=exit_validator,
check_should_churn_fn=check_should_churn_fn,
churn_fn=churn_fn,
max_balance_churn=max_balance_churn,
# **churn_fn_kwargs
)

# Exit validators within the allowable balance churn
check_should_churn_fn = lambda state, index: is_ready_to_exit(state, index)
churn_fn = lambda state, index: exit_validator(
state,
index,
slots_per_epoch=config.SLOTS_PER_EPOCH,
activation_exit_delay=config.ACTIVATION_EXIT_DELAY,
)
state = churn_validators(
state=state,
config=config,
check_should_churn_fn=check_should_churn_fn,
churn_fn=churn_fn,
max_balance_churn=max_balance_churn,
)

state = state.copy(
validator_registry_update_epoch=current_epoch,
Expand Down Expand Up @@ -1062,7 +1067,7 @@ def update_validator_registry_2(state: BeaconState, config: BeaconConfig) -> Bea
def _process_validator_registry_with_update(current_epoch_committee_count: int,
state: BeaconState,
config: BeaconConfig) -> StateUpdaterForConfig:
state = update_validator_registry(state)
state = update_validator_registry(state, config)

# Update step-by-step since updated `state.current_shuffling_epoch`
# is used to calculate other value). Follow the spec tightly now.
Expand Down

0 comments on commit 78ed252

Please sign in to comment.