update get_seed(...) and get_beacon_proposer_index(...) to 0.9.0, implement compute_proposer_index(...), and render 3 more test fixtures working (#532)

* update get_seed(...) and get_beacon_proposer_index(...) to 0.9.0, implement compute_proposer_index(...), and render 3 more test fixtures working

* rm stray Crosslink reference which prevented static SSZ tests from building

* remove references to removed tests in attestations test fixture; add minimal-preset block sanity test, plus all but one of mainnet tests for block sanity to transition fixtures
This commit is contained in:
Dustin Brody 2019-11-10 00:03:41 +00:00 committed by Mamy Ratsimbazafy
parent f02aa20873
commit cb4a86756b
6 changed files with 46 additions and 34 deletions

View File

@ -415,7 +415,6 @@ template foreachSpecType*(op: untyped) =
op BeaconBlockBody
op BeaconBlockHeader
op BeaconState
op Crosslink
op Deposit
op DepositData
op Eth1Data

View File

@ -150,17 +150,19 @@ func get_domain*(
func get_domain*(state: BeaconState, domain_type: DomainType): Domain =
get_domain(state, domain_type, get_current_epoch(state))
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#get_seed
func get_seed*(state: BeaconState, epoch: Epoch): Eth2Digest =
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#get_seed
func get_seed*(state: BeaconState, epoch: Epoch, domain_type: DomainType): Eth2Digest =
# Generate a seed for the given ``epoch``.
var seed_input : array[32*3, byte]
var seed_input : array[4+8+32, byte]
# Detect potential underflow
doAssert EPOCHS_PER_HISTORICAL_VECTOR >= MIN_SEED_LOOKAHEAD
static:
doAssert EPOCHS_PER_HISTORICAL_VECTOR > MIN_SEED_LOOKAHEAD
seed_input[0..31] =
seed_input[0..3] = int_to_bytes4(domain_type.uint64)
seed_input[4..11] = int_to_bytes8(epoch.uint64)
seed_input[12..43] =
get_randao_mix(state,
epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD - 1).data
seed_input[64..95] = int_to_bytes32(epoch)
eth2hash(seed_input)

View File

@ -179,11 +179,7 @@ func get_beacon_committee*(state: BeaconState, slot: Slot, index: uint64, cache:
# TODO profiling & make sure caches populated
compute_committee(
cache.active_validator_indices_cache[epoch],
# TODO switch to 0.9 seed calculation
#get_seed(state, epoch, DOMAIN_BEACON_ATTESTER),
get_seed(state, epoch),
get_seed(state, epoch, DOMAIN_BEACON_ATTESTER),
(slot mod SLOTS_PER_EPOCH) * committees_per_slot + index,
# TODO switch to 0.9's
@ -214,35 +210,30 @@ func get_empty_per_epoch_cache*(): StateCache =
result.start_shard_cache = initTable[Epoch, Shard]()
result.committee_count_cache = initTable[Epoch, uint64]()
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#get_beacon_proposer_index
func get_beacon_proposer_index*(state: BeaconState, stateCache: var StateCache):
ValidatorIndex =
# Return the beacon proposer index at the current slot.
const
MAX_RANDOM_BYTE = 255
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#compute_proposer_index
func compute_proposer_index*(state: BeaconState, indices: seq[ValidatorIndex],
seed: Eth2Digest, stateCache: var StateCache): ValidatorIndex =
# Return from ``indices`` a random index sampled by effective balance.
const MAX_RANDOM_BYTE = 255
doAssert len(indices) > 0
# TODO fixme; should only be run once per slot and cached
# There's exactly one beacon proposer per slot.
let
epoch = get_current_epoch(state)
committees_per_slot =
get_committee_count(state, epoch) div SLOTS_PER_EPOCH
offset = committees_per_slot * (state.slot mod SLOTS_PER_EPOCH)
shard = (get_start_shard(state, epoch) + offset) mod SHARD_COUNT
first_committee = get_crosslink_committee(state, epoch, shard, stateCache)
seed = get_seed(state, epoch)
seq_len = indices.len.uint64
shuffled_seq = get_shuffled_seq(seed, seq_len)
# This mainly fails when there are no active validators for some reason
doAssert first_committee.len > 0
doAssert seq_len == shuffled_seq.len.uint64
var
i = 0
buffer: array[(32+8), byte]
buffer: array[32+8, byte]
buffer[0..31] = seed.data
while true:
# TODO update to new int_to_bytes interface
buffer[32..39] = int_to_bytes8(i.uint64 div 32)
let
candidate_index = first_committee[((epoch + i.uint64) mod
len(first_committee).uint64).int]
candidate_index = shuffled_seq[(i.uint64 mod seq_len).int]
random_byte = (eth2hash(buffer).data)[i mod 32]
effective_balance =
state.validators[candidate_index].effective_balance
@ -250,3 +241,19 @@ func get_beacon_proposer_index*(state: BeaconState, stateCache: var StateCache):
MAX_EFFECTIVE_BALANCE * random_byte:
return candidate_index
i += 1
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#get_beacon_proposer_index
func get_beacon_proposer_index*(state: BeaconState, stateCache: var StateCache):
ValidatorIndex =
# Return the beacon proposer index at the current slot.
let epoch = get_current_epoch(state)
var buffer: array[32 + 8, byte]
buffer[0..31] = get_seed(state, epoch, DOMAIN_BEACON_PROPOSER).data
buffer[32..39] = int_to_bytes8(state.slot.uint64)
let
seed = eth2hash(buffer)
indices = get_active_validator_indices(state, epoch)
compute_proposer_index(state, indices, seed, stateCache)

View File

@ -70,8 +70,6 @@ suite "Official - Operations - Attestations " & preset():
runTest("before inclusion delay", before_inclusion_delay)
runTest("after_epoch_slots", after_epoch_slots)
runTest("old source epoch", old_source_epoch)
runTest("wrong shard", wrong_shard)
runTest("invalid shard", invalid_shard)
runTest("old target epoch", old_target_epoch)
runTest("future target epoch", future_target_epoch)
runTest("new source epoch", new_source_epoch)

View File

@ -87,8 +87,10 @@ suite "Official - Sanity - Blocks " & preset():
runValidTest("Empty epoch transition", empty_epoch_transition, 1)
when const_preset=="minimal":
runValidTest("Empty epoch transition not finalizing", empty_epoch_transition_not_finalizing, 1)
# TODO investigate/fix after 0.9.0 transition broke this in mainnet
runValidTest("Attester slashing", attester_slashing, 1)
runValidTest("Proposer slashing", proposer_slashing, 1)
runValidTest("Attester slashing", attester_slashing, 1)
# TODO: Expected deposit in block

View File

@ -1,6 +1,10 @@
import
official/test_fixture_bls,
official/test_fixture_operations_attester_slashings,
official/test_fixture_operations_proposer_slashings,
official/test_fixture_shuffling,
official/test_fixture_operations_block_header,
official/test_fixture_sanity_blocks,
official/test_fixture_ssz_generic_types,
official/test_fixture_sanity_slots,
official/test_fixture_operations_voluntary_exit,