do most of 0.8.0 BeaconState updating, in particular field renamings, re-orderings, etc; rm now-unnecessary genesis block creation aspects, towards the near-stub version in current spec as mostfields are 0/default/etc values

This commit is contained in:
Dustin Brody 2019-07-01 11:42:37 +02:00 committed by zah
parent fb601bd5b8
commit fa39eb82c4
7 changed files with 62 additions and 76 deletions

View File

@ -57,8 +57,8 @@ func process_deposit*(
hash_tree_root(deposit.data),
deposit.proof,
DEPOSIT_CONTRACT_TREE_DEPTH,
state.deposit_index,
state.latest_eth1_data.deposit_root,
state.eth1_deposit_index,
state.eth1_data.deposit_root,
):
## TODO: a notice-like mechanism which works in a func
## here and elsewhere, one minimal approach is a check-if-true
@ -69,7 +69,7 @@ func process_deposit*(
discard
# Deposits must be processed in order
state.deposit_index += 1
state.eth1_deposit_index += 1
let
pubkey = deposit.data.pubkey
@ -159,7 +159,7 @@ func slash_validator*(state: var BeaconState, slashed_index: ValidatorIndex,
current_epoch + LATEST_SLASHED_EXIT_LENGTH
let slashed_balance =
state.validators[slashed_index].effective_balance
state.latest_slashed_balances[current_epoch mod LATEST_SLASHED_EXIT_LENGTH] +=
state.slashings[current_epoch mod LATEST_SLASHED_EXIT_LENGTH] +=
slashed_balance
let
@ -213,7 +213,6 @@ func get_genesis_beacon_state*(
var state = BeaconState(
# Misc
slot: GENESIS_SLOT,
genesis_time: genesis_time,
fork: Fork(
previous_version: GENESIS_FORK_VERSION,
@ -221,30 +220,11 @@ func get_genesis_beacon_state*(
epoch: GENESIS_EPOCH,
),
# validator_registry and balances automatically initalized
# Randomness and committees
# latest_randao_mixes automatically initialized
# Finality
# previous_epoch_attestations and current_epoch_attestations automatically
# initialized
previous_justified_epoch: GENESIS_EPOCH,
current_justified_epoch: GENESIS_EPOCH,
justification_bitfield: 0,
finalized_epoch: GENESIS_EPOCH,
finalized_root: ZERO_HASH,
# Recent state
# latest_block_roots, latest_state_roots, latest_active_index_roots,
# latest_slashed_balances, and latest_slashed_balances automatically
# initialized
latest_block_header: get_temporary_block_header(get_empty_block()),
# Ethereum 1.0 chain data
# eth1_data_votes automatically initialized
latest_eth1_data: genesis_eth1_data,
deposit_index: 0,
eth1_data: genesis_eth1_data,
)
# Process genesis deposits
@ -261,7 +241,7 @@ func get_genesis_beacon_state*(
let genesis_active_index_root = hash_tree_root(
get_active_validator_indices(state, GENESIS_EPOCH))
for index in 0 ..< LATEST_ACTIVE_INDEX_ROOTS_LENGTH:
state.latest_active_index_roots[index] = genesis_active_index_root
state.active_index_roots[index] = genesis_active_index_root
state

View File

@ -235,51 +235,57 @@ type
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#beaconstate
BeaconState* = object
slot*: Slot
# Versioning
genesis_time*: uint64
fork*: Fork ##\
## For versioning hard forks
slot*: Slot
fork*: Fork
# Validator registry
# History
latest_block_header*: BeaconBlockHeader ##\
## `latest_block_header.state_root == ZERO_HASH` temporarily
block_roots*: array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest] ##\
## Needed to process attestations, older to newer
state_roots*: array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest]
historical_roots*: seq[Eth2Digest]
# Eth1
eth1_data*: Eth1Data
eth1_data_votes*: seq[Eth1Data]
eth1_deposit_index*: uint64
# Registry
validators*: seq[Validator]
balances*: seq[uint64] ##\
## Validator balances in Gwei!
# Randomness and committees
latest_randao_mixes*: array[LATEST_RANDAO_MIXES_LENGTH, Eth2Digest]
latest_start_shard*: Shard
# Shuffling
start_shard*: Shard
randao_mixes*: array[LATEST_RANDAO_MIXES_LENGTH, Eth2Digest]
active_index_roots*: array[LATEST_ACTIVE_INDEX_ROOTS_LENGTH, Eth2Digest]
# Finality
# Slashings
slashings*: array[LATEST_SLASHED_EXIT_LENGTH, uint64] ##\
## Per-epoch sums of slashed effective balances
# Attestations
previous_epoch_attestations*: seq[PendingAttestation]
current_epoch_attestations*: seq[PendingAttestation]
# Crosslinks
previous_crosslinks*: array[SHARD_COUNT, Crosslink]
current_crosslinks*: array[SHARD_COUNT, Crosslink]
# Finality
justification_bits*: uint64
previous_justified_epoch*: Epoch
current_justified_epoch*: Epoch
previous_justified_root*: Eth2Digest
current_justified_root*: Eth2Digest
justification_bitfield*: uint64
finalized_epoch*: Epoch
finalized_root*: Eth2Digest
# Recent state
current_crosslinks*: array[SHARD_COUNT, Crosslink]
previous_crosslinks*: array[SHARD_COUNT, Crosslink]
block_roots*: array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest] ##\
## Needed to process attestations, older to newer
latest_state_roots*: array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest]
latest_active_index_roots*: array[LATEST_ACTIVE_INDEX_ROOTS_LENGTH, Eth2Digest]
latest_slashed_balances*: array[LATEST_SLASHED_EXIT_LENGTH, uint64] ##\
## Balances penalized in the current withdrawal period
latest_block_header*: BeaconBlockHeader ##\
## `latest_block_header.state_root == ZERO_HASH` temporarily
historical_roots*: seq[Eth2Digest]
# Ethereum 1.0 chain data
latest_eth1_data*: Eth1Data
eth1_data_votes*: seq[Eth1Data]
deposit_index*: uint64
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#validator
Validator* = object
pubkey*: ValidatorPubKey

View File

@ -100,7 +100,7 @@ func get_randao_mix*(state: BeaconState,
## Returns the randao mix at a recent ``epoch``.
## ``epoch`` expected to be between (current_epoch -
## LATEST_RANDAO_MIXES_LENGTH, current_epoch].
state.latest_randao_mixes[epoch mod LATEST_RANDAO_MIXES_LENGTH]
state.randao_mixes[epoch mod LATEST_RANDAO_MIXES_LENGTH]
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_active_index_root
func get_active_index_root(state: BeaconState, epoch: Epoch): Eth2Digest =
@ -109,7 +109,7 @@ func get_active_index_root(state: BeaconState, epoch: Epoch): Eth2Digest =
## (current_epoch - LATEST_ACTIVE_INDEX_ROOTS_LENGTH + ACTIVATION_EXIT_DELAY, current_epoch + ACTIVATION_EXIT_DELAY].
## TODO maybe assert this, but omission of such seems conspicuously
## intentional
state.latest_active_index_roots[epoch mod LATEST_ACTIVE_INDEX_ROOTS_LENGTH]
state.active_index_roots[epoch mod LATEST_ACTIVE_INDEX_ROOTS_LENGTH]
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#bytes_to_int
func bytes_to_int*(data: openarray[byte]): uint64 =

View File

@ -112,8 +112,8 @@ proc processRandao(
mix = get_current_epoch(state) mod LATEST_RANDAO_MIXES_LENGTH
rr = eth2hash(body.randao_reveal.getBytes()).data
for i, b in state.latest_randao_mixes[mix].data:
state.latest_randao_mixes[mix].data[i] = b xor rr[i]
for i, b in state.randao_mixes[mix].data:
state.randao_mixes[mix].data[i] = b xor rr[i]
true
@ -122,7 +122,7 @@ func processEth1Data(state: var BeaconState, body: BeaconBlockBody) =
state.eth1_data_votes.add body.eth1_data
if state.eth1_data_votes.count(body.eth1_data) * 2 >
SLOTS_PER_ETH1_VOTING_PERIOD:
state.latest_eth1_data = body.eth1_data
state.eth1_data = body.eth1_data
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#is_slashable_validator
func is_slashable_validator(validator: Validator, epoch: Epoch): bool =

View File

@ -154,7 +154,7 @@ func process_justification_and_finalization(
# Process justifications
state.previous_justified_epoch = state.current_justified_epoch
state.previous_justified_root = state.current_justified_root
state.justification_bitfield = (state.justification_bitfield shl 1)
state.justification_bits = (state.justification_bits shl 1)
let previous_epoch_matching_target_balance =
get_attesting_balance(state,
get_matching_target_attestations(state, previous_epoch), stateCache)
@ -163,7 +163,7 @@ func process_justification_and_finalization(
state.current_justified_epoch = previous_epoch
state.current_justified_root =
get_block_root(state, state.current_justified_epoch)
state.justification_bitfield = state.justification_bitfield or (1 shl 1)
state.justification_bits = state.justification_bits or (1 shl 1)
let current_epoch_matching_target_balance =
get_attesting_balance(state,
get_matching_target_attestations(state, current_epoch),
@ -173,10 +173,10 @@ func process_justification_and_finalization(
state.current_justified_epoch = current_epoch
state.current_justified_root =
get_block_root(state, state.current_justified_epoch)
state.justification_bitfield = state.justification_bitfield or (1 shl 0)
state.justification_bits = state.justification_bits or (1 shl 0)
# Process finalizations
let bitfield = state.justification_bitfield
let bitfield = state.justification_bits
## The 2nd/3rd/4th most recent epochs are justified, the 2nd using the 4th
## as source
@ -366,10 +366,10 @@ func process_slashings(state: var BeaconState) =
total_balance = get_total_active_balance(state)
# Compute `total_penalties`
total_at_start = state.latest_slashed_balances[
total_at_start = state.slashings[
(current_epoch + 1) mod LATEST_SLASHED_EXIT_LENGTH]
total_at_end =
state.latest_slashed_balances[current_epoch mod
state.slashings[current_epoch mod
LATEST_SLASHED_EXIT_LENGTH]
total_penalties = total_at_end - total_at_start
@ -405,24 +405,24 @@ func process_final_updates(state: var BeaconState) =
MAX_EFFECTIVE_BALANCE)
# Update start shard
state.latest_start_shard =
(state.latest_start_shard + get_shard_delta(state, current_epoch)) mod
state.start_shard =
(state.start_shard + get_shard_delta(state, current_epoch)) mod
SHARD_COUNT
# Set total slashed balances
state.latest_slashed_balances[next_epoch mod LATEST_SLASHED_EXIT_LENGTH] = (
state.latest_slashed_balances[current_epoch mod LATEST_SLASHED_EXIT_LENGTH]
state.slashings[next_epoch mod LATEST_SLASHED_EXIT_LENGTH] = (
state.slashings[current_epoch mod LATEST_SLASHED_EXIT_LENGTH]
)
# Set randao mix
state.latest_randao_mixes[next_epoch mod LATEST_RANDAO_MIXES_LENGTH] =
state.randao_mixes[next_epoch mod LATEST_RANDAO_MIXES_LENGTH] =
get_randao_mix(state, current_epoch)
# Set historical root accumulator
if next_epoch mod (SLOTS_PER_HISTORICAL_ROOT div SLOTS_PER_EPOCH).uint64 == 0:
let historical_batch = HistoricalBatch(
block_roots: state.block_roots,
state_roots: state.latest_state_roots,
state_roots: state.state_roots,
)
state.historical_roots.add (hash_tree_root(historical_batch))

View File

@ -90,7 +90,7 @@ func get_previous_epoch*(state: BeaconState): Epoch =
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_shard_delta
func get_shard_delta*(state: BeaconState, epoch: Epoch): uint64 =
## Return the number of shards to increment ``state.latest_start_shard``
## Return the number of shards to increment ``state.start_shard``
## during ``epoch``.
min(get_epoch_committee_count(state, epoch),
(SHARD_COUNT - SHARD_COUNT div SLOTS_PER_EPOCH).uint64)
@ -103,7 +103,7 @@ func get_start_shard*(state: BeaconState, epoch: Epoch): Shard =
var
check_epoch = get_current_epoch(state) + 1
shard =
(state.latest_start_shard +
(state.start_shard +
get_shard_delta(state, get_current_epoch(state))) mod SHARD_COUNT
while check_epoch > epoch:
check_epoch -= 1.Epoch

View File

@ -49,7 +49,7 @@ func advance_slot(state: var BeaconState) =
func process_slot(state: var BeaconState) =
# Cache state root
let previous_state_root = hash_tree_root(state)
state.latest_state_roots[state.slot mod SLOTS_PER_HISTORICAL_ROOT] =
state.state_roots[state.slot mod SLOTS_PER_HISTORICAL_ROOT] =
previous_state_root
# Cache latest block header state root
@ -171,7 +171,7 @@ proc skipSlots*(state: var BeaconState, slot: Slot,
func process_slot(state: var HashedBeaconState) =
# Cache state root
let previous_slot_state_root = state.root
state.data.latest_state_roots[state.data.slot mod SLOTS_PER_HISTORICAL_ROOT] =
state.data.state_roots[state.data.slot mod SLOTS_PER_HISTORICAL_ROOT] =
previous_slot_state_root
# Cache latest block header state root