make get_validator_from_deposit for better code reuse across phase 0 and 1

This commit is contained in:
Danny Ryan 2020-05-19 08:14:04 -06:00
parent 7fc9dbf297
commit 1623086088
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
2 changed files with 35 additions and 57 deletions

View File

@ -1632,6 +1632,22 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None:
##### Deposits ##### Deposits
```python
def get_validator_from_deposit(state: BeaconState, deposit: Deposit) -> Validator:
amount = deposit.data.amount
effective_balance = min(amount - amount % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE)
return Validator(
pubkey=deposit.data.pubkey,
withdrawal_credentials=deposit.data.withdrawal_credentials,
activation_eligibility_epoch=FAR_FUTURE_EPOCH,
activation_epoch=FAR_FUTURE_EPOCH,
exit_epoch=FAR_FUTURE_EPOCH,
withdrawable_epoch=FAR_FUTURE_EPOCH,
effective_balance=effective_balance,
)
```
```python ```python
def process_deposit(state: BeaconState, deposit: Deposit) -> None: def process_deposit(state: BeaconState, deposit: Deposit) -> None:
# Verify the Merkle branch # Verify the Merkle branch
@ -1662,15 +1678,7 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None:
return return
# Add validator and balance entries # Add validator and balance entries
state.validators.append(Validator( state.validators.append(get_validator_from_deposit(state, deposit))
pubkey=pubkey,
withdrawal_credentials=deposit.data.withdrawal_credentials,
activation_eligibility_epoch=FAR_FUTURE_EPOCH,
activation_epoch=FAR_FUTURE_EPOCH,
exit_epoch=FAR_FUTURE_EPOCH,
withdrawable_epoch=FAR_FUTURE_EPOCH,
effective_balance=min(amount - amount % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE),
))
state.balances.append(amount) state.balances.append(amount)
else: else:
# Increase balance by deposit amount # Increase balance by deposit amount

View File

@ -883,58 +883,28 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None:
state.previous_epoch_attestations.append(pending_attestation) state.previous_epoch_attestations.append(pending_attestation)
``` ```
##### New deposits ##### New default validator for deposits
```python ```python
def process_deposit(state: BeaconState, deposit: Deposit) -> None: def get_validator_from_deposit(state: BeaconState, deposit: Deposit) -> Validator:
# Verify the Merkle branch amount = deposit.data.amount
assert is_valid_merkle_branch( effective_balance = min(amount - amount % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE)
leaf=hash_tree_root(deposit.data), next_custody_secret_to_reveal = get_custody_period_for_validator(
branch=deposit.proof, ValidatorIndex(len(state.validators)),
depth=DEPOSIT_CONTRACT_TREE_DEPTH + 1, # Add 1 for the List length mix-in get_current_epoch(state),
index=state.eth1_deposit_index,
root=state.eth1_data.deposit_root,
) )
# Deposits must be processed in order return Validator(
state.eth1_deposit_index += 1 pubkey=deposit.data.pubkey,
withdrawal_credentials=deposit.data.withdrawal_credentials,
pubkey = deposit.data.pubkey activation_eligibility_epoch=FAR_FUTURE_EPOCH,
amount = deposit.data.amount activation_epoch=FAR_FUTURE_EPOCH,
validator_pubkeys = [v.pubkey for v in state.validators] exit_epoch=FAR_FUTURE_EPOCH,
if pubkey not in validator_pubkeys: withdrawable_epoch=FAR_FUTURE_EPOCH,
# Verify the deposit signature (proof of possession) which is not checked by the deposit contract effective_balance=effective_balance,
deposit_message = DepositMessage( next_custody_secret_to_reveal=next_custody_secret_to_reveal,
pubkey=deposit.data.pubkey, all_custody_secrets_revealed_epoch=FAR_FUTURE_EPOCH,
withdrawal_credentials=deposit.data.withdrawal_credentials, )
amount=deposit.data.amount,
)
domain = compute_domain(DOMAIN_DEPOSIT) # Fork-agnostic domain since deposits are valid across forks
signing_root = compute_signing_root(deposit_message, domain)
if not bls.Verify(pubkey, signing_root, deposit.data.signature):
return
# Add validator and balance entries
# TODO: This function is duplicated from phase 1 just because the validator definition
# has changed and we need to initialize it properly. Is there a better solution for
# this?
state.validators.append(Validator(
pubkey=pubkey,
withdrawal_credentials=deposit.data.withdrawal_credentials,
activation_eligibility_epoch=FAR_FUTURE_EPOCH,
activation_epoch=FAR_FUTURE_EPOCH,
exit_epoch=FAR_FUTURE_EPOCH,
withdrawable_epoch=FAR_FUTURE_EPOCH,
effective_balance=min(amount - amount % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE),
next_custody_secret_to_reveal=get_custody_period_for_validator(ValidatorIndex(len(state.validators)),
get_current_epoch(state)),
all_custody_secrets_revealed_epoch=FAR_FUTURE_EPOCH,
))
state.balances.append(amount)
else:
# Increase balance by deposit amount
index = ValidatorIndex(validator_pubkeys.index(pubkey))
increase_balance(state, index, amount)
``` ```
##### New Attester slashing processing ##### New Attester slashing processing