Fix #378, int to bytes32, LATEST_RANDAO_MIXES, get_seed, get_crosslink_committee, get_compact_committee_root (#380)

This commit is contained in:
Mamy Ratsimbazafy 2019-09-04 15:57:18 +02:00 committed by Dustin Brody
parent 2520646a24
commit ad240953ed
7 changed files with 15 additions and 20 deletions

Binary file not shown.

Binary file not shown.

View File

@ -7,7 +7,7 @@
# Uncategorized helper functions from the spec
import ./datatypes, ./digest, sequtils, math
import ./datatypes, ./digest, sequtils, math, endians
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#integer_squareroot
func integer_squareroot*(n: SomeInteger): SomeInteger =
@ -102,9 +102,7 @@ func get_current_epoch*(state: BeaconState): Epoch =
func get_randao_mix*(state: BeaconState,
epoch: Epoch): Eth2Digest =
## Returns the randao mix at a recent ``epoch``.
## ``epoch`` expected to be between (current_epoch -
## LATEST_RANDAO_MIXES_LENGTH, current_epoch].
state.randao_mixes[epoch mod LATEST_RANDAO_MIXES_LENGTH]
state.randao_mixes[epoch mod EPOCHS_PER_HISTORICAL_VECTOR]
func bytes_to_int*(data: openarray[byte]): uint64 =
doAssert data.len == 8
@ -118,14 +116,12 @@ func bytes_to_int*(data: openarray[byte]): uint64 =
func int_to_bytes32*(x: uint64): array[32, byte] =
## Little-endian data representation
## TODO remove uint64 when those callers fade away
for i in 0 ..< 8:
result[24 + i] = byte((x shr i*8) and 0xff)
littleEndian64(result[0].addr, x.unsafeAddr)
func int_to_bytes32*(x: Epoch): array[32, byte] {.borrow.}
func int_to_bytes8*(x: uint64): array[8, byte] =
for i in 0 ..< 8:
result[i] = byte((x shr i*8) and 0xff)
littleEndian64(result[0].addr, x.unsafeAddr)
func int_to_bytes1*(x: int): array[1, byte] =
doAssert x >= 0
@ -175,11 +171,11 @@ func get_seed*(state: BeaconState, epoch: Epoch): Eth2Digest =
var seed_input : array[32*3, byte]
# Detect potential underflow
doAssert LATEST_RANDAO_MIXES_LENGTH >= MIN_SEED_LOOKAHEAD
doAssert EPOCHS_PER_HISTORICAL_VECTOR >= MIN_SEED_LOOKAHEAD
seed_input[0..31] =
get_randao_mix(state,
epoch + LATEST_RANDAO_MIXES_LENGTH - MIN_SEED_LOOKAHEAD - 1).data
epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD - 1).data
seed_input[32..63] =
state.active_index_roots[epoch mod EPOCHS_PER_HISTORICAL_VECTOR].data
seed_input[64..95] = int_to_bytes32(epoch)

View File

@ -136,7 +136,6 @@ const
# State list lengths
# ---------------------------------------------------------------
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#state-list-lengths
LATEST_RANDAO_MIXES_LENGTH* = 8192
EPOCHS_PER_HISTORICAL_VECTOR* = 65536
EPOCHS_PER_SLASHINGS_VECTOR* = 8192
HISTORICAL_ROOTS_LIMIT* = 16777216

View File

@ -99,7 +99,6 @@ const
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#state-list-lengths
# Changed
LATEST_RANDAO_MIXES_LENGTH* = 64
EPOCHS_PER_HISTORICAL_VECTOR* = 64
EPOCHS_PER_SLASHINGS_VECTOR* = 64
HISTORICAL_ROOTS_LIMIT* = 16777216

View File

@ -90,11 +90,12 @@ proc processBlockHeader(
true
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#randao
proc processRandao(
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#randao
proc process_randao(
state: var BeaconState, body: BeaconBlockBody, flags: UpdateFlags,
stateCache: var StateCache): bool =
let
epoch = state.get_current_epoch()
proposer_index = get_beacon_proposer_index(state, stateCache)
proposer = addr state.validators[proposer_index]
@ -102,23 +103,23 @@ proc processRandao(
if skipValidation notin flags:
if not bls_verify(
proposer.pubkey,
hash_tree_root(get_current_epoch(state).uint64).data,
hash_tree_root(epoch.uint64).data,
body.randao_reveal,
get_domain(state, DOMAIN_RANDAO)):
notice "Randao mismatch", proposer_pubkey = proposer.pubkey,
message = get_current_epoch(state),
message = epoch,
signature = body.randao_reveal,
slot = state.slot
return false
# Mix it in
let
mix = get_current_epoch(state) mod LATEST_RANDAO_MIXES_LENGTH
mix = get_randao_mix(state, epoch)
rr = eth2hash(body.randao_reveal.getBytes()).data
for i, b in state.randao_mixes[mix].data:
state.randao_mixes[mix].data[i] = b xor rr[i]
for i in 0 ..< mix.data.len:
state.randao_mixes[epoch mod EPOCHS_PER_HISTORICAL_VECTOR].data[i] = mix.data[i] xor rr[i]
true

View File

@ -497,7 +497,7 @@ proc process_final_updates*(state: var BeaconState) =
state.slashings[next_epoch mod EPOCHS_PER_SLASHINGS_VECTOR] = 0.Gwei
# Set randao mix
state.randao_mixes[next_epoch mod LATEST_RANDAO_MIXES_LENGTH] =
state.randao_mixes[next_epoch mod EPOCHS_PER_HISTORICAL_VECTOR] =
get_randao_mix(state, current_epoch)
# Set historical root accumulator