rm min_empty_validator_index, ZERO_BALANCE_VALIDATOR_TTL, Validator.latest_status_change_slot; switch Fork.fork_slot to Fork.epoch; begin removing Validator.randao_layers; now that https://github.com/ethereum/eth2.0-specs/issues/352 is fixed, test out for more slots; get process_deposit consistent with current spec

This commit is contained in:
Dustin Brody 2019-01-30 00:41:07 -08:00
parent 33dd0da1de
commit 68ca7850f4
5 changed files with 19 additions and 55 deletions

View File

@ -47,10 +47,10 @@ func validate_proof_of_possession(state: BeaconState,
func process_deposit(state: var BeaconState,
pubkey: ValidatorPubKey,
deposit: uint64,
amount: uint64,
proof_of_possession: ValidatorSig,
withdrawal_credentials: Eth2Digest,
randao_commitment: Eth2Digest) : ValidatorIndex =
randao_commitment: Eth2Digest) =
## Process a deposit from Ethereum 1.0.
if false:
@ -77,16 +77,9 @@ func process_deposit(state: var BeaconState,
status_flags: 0,
)
let index = min_empty_validator_index(
state.validator_registry, state.validator_balances, state.slot)
if index.isNone():
state.validator_registry.add(validator)
state.validator_balances.add(deposit)
(len(state.validator_registry) - 1).ValidatorIndex
else:
state.validator_registry[index.get()] = validator
state.validator_balances[index.get()] = deposit
index.get().ValidatorIndex
# Note: In phase 2 registry indices that have been withdrawn for a long time will be recycled.
state.validator_registry.add(validator)
state.validator_balances.add(amount)
else:
# Increase balance by deposit amount
let index = validator_pubkeys.find(pubkey)
@ -94,8 +87,7 @@ func process_deposit(state: var BeaconState,
assert state.validator_registry[index].withdrawal_credentials ==
withdrawal_credentials
state.validator_balances[index] += deposit
index.ValidatorIndex
state.validator_balances[index] += amount
func get_entry_exit_effect_epoch*(epoch: EpochNumber): EpochNumber =
## An entry or exit triggered in the ``epoch`` given by the input takes effect at
@ -189,11 +181,10 @@ func get_initial_beacon_state*(
# Misc
slot: GENESIS_SLOT,
genesis_time: genesis_time,
# rm fork_slot init in favor of epoch
fork: Fork(
previous_version: GENESIS_FORK_VERSION,
current_version: GENESIS_FORK_VERSION,
fork_slot: GENESIS_SLOT,
epoch: GENESIS_EPOCH,
),
validator_registry_update_epoch: GENESIS_EPOCH,
@ -220,7 +211,7 @@ func get_initial_beacon_state*(
# Process initial deposits
for deposit in initial_validator_deposits:
let validator_index = process_deposit(
process_deposit(
state,
deposit.deposit_data.deposit_input.pubkey,
deposit.deposit_data.amount,
@ -229,13 +220,10 @@ func get_initial_beacon_state*(
deposit.deposit_data.deposit_input.randao_commitment,
)
if state.validator_balances[validator_index] >= MAX_DEPOSIT_AMOUNT:
activate_validator(state, validator_index, true)
# Process initial activations
for validator_index in 0 ..< state.validator_registry.len:
let vi = validator_index.ValidatorIndex
if get_effective_balance(state, vi) > MAX_DEPOSIT_AMOUNT:
if get_effective_balance(state, vi) >= MAX_DEPOSIT_AMOUNT:
activate_validator(state, vi, true)
state

View File

@ -128,9 +128,6 @@ const
ENTRY_EXIT_DELAY* = 256 ##\
## slots (~25.6 minutes)
ZERO_BALANCE_VALIDATOR_TTL* = 2'u64^22 ##\
## slots (~291 days)
DEPOSIT_ROOT_VOTING_PERIOD* = 2'u64^10 ##\
## slots (~1.7 hours)
@ -347,7 +344,7 @@ type
pubkey*: ValidatorPubKey
withdrawal_credentials*: Eth2Digest
# TODO remove randao_commitment, randao_layers, latest_status_change_slot
# TODO remove randao_commitment, randao_layers
randao_commitment*: Eth2Digest ##\
## RANDAO commitment created by repeatedly taking the hash of a secret value
## so as to create "onion layers" around it. For every block that a
@ -360,9 +357,6 @@ type
## Number of proposals the proposer missed, and thus the number of times to
## apply hash function to randao reveal
latest_status_change_slot*: uint64 ##\
## Slot when validator last changed status (or 0)
activation_epoch*: EpochNumber ##\
## Slot when validator activated
@ -413,8 +407,8 @@ type
Fork* = object
previous_version*: uint64 # Previous fork version
current_version*: uint64 # Current fork version
fork_slot*: uint64 # Fork slot number TODO should be epoch
current_version*: uint64 # Current fork version
epoch*: uint64 # Fork epoch number
ValidatorRegistryDeltaBlock* = object
latest_registry_delta_root*: Eth2Digest

View File

@ -119,9 +119,11 @@ func integer_squareroot*(n: SomeInteger): SomeInteger =
y = (x + n div x) div 2
x
func get_fork_version*(fork: Fork, slot: uint64): uint64 =
if slot < fork.fork_slot: fork.previous_version
else: fork.current_version
func get_fork_version*(fork: Fork, epoch: EpochNumber): uint64 =
if epoch < fork.epoch:
fork.previous_version
else:
fork.current_version
func get_domain*(
fork: Fork, slot: uint64, domain_type: SignatureDomain): uint64 =

View File

@ -12,16 +12,6 @@ import
../ssz,
./crypto, ./datatypes, ./digest, ./helpers
func min_empty_validator_index*(
validators: seq[Validator],
validator_balances: seq[uint64],
current_slot: uint64): Option[int] =
for i, v in validators:
if validator_balances[i] == 0 and
v.latest_status_change_slot +
ZERO_BALANCE_VALIDATOR_TTL.uint64 <= current_slot:
return some(i)
func xorSeed(seed: Eth2Digest, x: uint64): Eth2Digest =
## Integers are all encoded as bigendian
## Helper for get_shuffling in lieu of generally better bitwise handling
@ -154,5 +144,5 @@ func get_beacon_proposer_index*(state: BeaconState, slot: uint64): ValidatorInde
# TODO this index is invalid outside of the block state transition function
# because presently, `state.slot += 1` happens before this function
# is called - see also testutil.getNextBeaconProposerIndex
let first_committee = get_crosslink_committees_at_slot(state, slot)[0][0]
let (first_committee, _) = get_crosslink_committees_at_slot(state, slot)[0]
first_committee[slot.int mod len(first_committee)]

View File

@ -34,10 +34,6 @@ suite "Block processing":
state.slot == genesisState.slot + 1
# When proposer skips their proposal, randao layer is still peeled!
state.validator_registry[proposer_index].randao_layers ==
genesisState.validator_registry[proposer_index].randao_layers + 1
test "Passes from genesis state, empty block":
var
state = genesisState
@ -53,10 +49,6 @@ suite "Block processing":
state.slot == genesisState.slot + 1
# Proposer proposed, no need for additional peeling
state.validator_registry[proposer_index].randao_layers ==
genesisState.validator_registry[proposer_index].randao_layers
test "Passes through epoch update, no block":
var
state = genesisState
@ -122,9 +114,7 @@ suite "Block processing":
check:
state.latest_attestations.len == 1
# TODO Can't run more than 127 for now:
# https://github.com/ethereum/eth2.0-specs/issues/352
while state.slot < 127:
while state.slot < 191:
discard updateState(
state, previous_block_root, none(BeaconBlock), {})