Fix #378, int to bytes32, LATEST_RANDAO_MIXES, get_seed, get_crosslink_committee, get_compact_committee_root (#380)
This commit is contained in:
parent
2520646a24
commit
ad240953ed
Binary file not shown.
Binary file not shown.
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
# Uncategorized helper functions from the spec
|
# 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
|
# 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 =
|
func integer_squareroot*(n: SomeInteger): SomeInteger =
|
||||||
|
@ -102,9 +102,7 @@ func get_current_epoch*(state: BeaconState): Epoch =
|
||||||
func get_randao_mix*(state: BeaconState,
|
func get_randao_mix*(state: BeaconState,
|
||||||
epoch: Epoch): Eth2Digest =
|
epoch: Epoch): Eth2Digest =
|
||||||
## Returns the randao mix at a recent ``epoch``.
|
## Returns the randao mix at a recent ``epoch``.
|
||||||
## ``epoch`` expected to be between (current_epoch -
|
state.randao_mixes[epoch mod EPOCHS_PER_HISTORICAL_VECTOR]
|
||||||
## LATEST_RANDAO_MIXES_LENGTH, current_epoch].
|
|
||||||
state.randao_mixes[epoch mod LATEST_RANDAO_MIXES_LENGTH]
|
|
||||||
|
|
||||||
func bytes_to_int*(data: openarray[byte]): uint64 =
|
func bytes_to_int*(data: openarray[byte]): uint64 =
|
||||||
doAssert data.len == 8
|
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] =
|
func int_to_bytes32*(x: uint64): array[32, byte] =
|
||||||
## Little-endian data representation
|
## Little-endian data representation
|
||||||
## TODO remove uint64 when those callers fade away
|
## TODO remove uint64 when those callers fade away
|
||||||
for i in 0 ..< 8:
|
littleEndian64(result[0].addr, x.unsafeAddr)
|
||||||
result[24 + i] = byte((x shr i*8) and 0xff)
|
|
||||||
|
|
||||||
func int_to_bytes32*(x: Epoch): array[32, byte] {.borrow.}
|
func int_to_bytes32*(x: Epoch): array[32, byte] {.borrow.}
|
||||||
|
|
||||||
func int_to_bytes8*(x: uint64): array[8, byte] =
|
func int_to_bytes8*(x: uint64): array[8, byte] =
|
||||||
for i in 0 ..< 8:
|
littleEndian64(result[0].addr, x.unsafeAddr)
|
||||||
result[i] = byte((x shr i*8) and 0xff)
|
|
||||||
|
|
||||||
func int_to_bytes1*(x: int): array[1, byte] =
|
func int_to_bytes1*(x: int): array[1, byte] =
|
||||||
doAssert x >= 0
|
doAssert x >= 0
|
||||||
|
@ -175,11 +171,11 @@ func get_seed*(state: BeaconState, epoch: Epoch): Eth2Digest =
|
||||||
var seed_input : array[32*3, byte]
|
var seed_input : array[32*3, byte]
|
||||||
|
|
||||||
# Detect potential underflow
|
# Detect potential underflow
|
||||||
doAssert LATEST_RANDAO_MIXES_LENGTH >= MIN_SEED_LOOKAHEAD
|
doAssert EPOCHS_PER_HISTORICAL_VECTOR >= MIN_SEED_LOOKAHEAD
|
||||||
|
|
||||||
seed_input[0..31] =
|
seed_input[0..31] =
|
||||||
get_randao_mix(state,
|
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] =
|
seed_input[32..63] =
|
||||||
state.active_index_roots[epoch mod EPOCHS_PER_HISTORICAL_VECTOR].data
|
state.active_index_roots[epoch mod EPOCHS_PER_HISTORICAL_VECTOR].data
|
||||||
seed_input[64..95] = int_to_bytes32(epoch)
|
seed_input[64..95] = int_to_bytes32(epoch)
|
||||||
|
|
|
@ -136,7 +136,6 @@ const
|
||||||
# State list lengths
|
# State list lengths
|
||||||
# ---------------------------------------------------------------
|
# ---------------------------------------------------------------
|
||||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#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_HISTORICAL_VECTOR* = 65536
|
||||||
EPOCHS_PER_SLASHINGS_VECTOR* = 8192
|
EPOCHS_PER_SLASHINGS_VECTOR* = 8192
|
||||||
HISTORICAL_ROOTS_LIMIT* = 16777216
|
HISTORICAL_ROOTS_LIMIT* = 16777216
|
||||||
|
|
|
@ -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
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#state-list-lengths
|
||||||
|
|
||||||
# Changed
|
# Changed
|
||||||
LATEST_RANDAO_MIXES_LENGTH* = 64
|
|
||||||
EPOCHS_PER_HISTORICAL_VECTOR* = 64
|
EPOCHS_PER_HISTORICAL_VECTOR* = 64
|
||||||
EPOCHS_PER_SLASHINGS_VECTOR* = 64
|
EPOCHS_PER_SLASHINGS_VECTOR* = 64
|
||||||
HISTORICAL_ROOTS_LIMIT* = 16777216
|
HISTORICAL_ROOTS_LIMIT* = 16777216
|
||||||
|
|
|
@ -90,11 +90,12 @@ proc processBlockHeader(
|
||||||
|
|
||||||
true
|
true
|
||||||
|
|
||||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#randao
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#randao
|
||||||
proc processRandao(
|
proc process_randao(
|
||||||
state: var BeaconState, body: BeaconBlockBody, flags: UpdateFlags,
|
state: var BeaconState, body: BeaconBlockBody, flags: UpdateFlags,
|
||||||
stateCache: var StateCache): bool =
|
stateCache: var StateCache): bool =
|
||||||
let
|
let
|
||||||
|
epoch = state.get_current_epoch()
|
||||||
proposer_index = get_beacon_proposer_index(state, stateCache)
|
proposer_index = get_beacon_proposer_index(state, stateCache)
|
||||||
proposer = addr state.validators[proposer_index]
|
proposer = addr state.validators[proposer_index]
|
||||||
|
|
||||||
|
@ -102,23 +103,23 @@ proc processRandao(
|
||||||
if skipValidation notin flags:
|
if skipValidation notin flags:
|
||||||
if not bls_verify(
|
if not bls_verify(
|
||||||
proposer.pubkey,
|
proposer.pubkey,
|
||||||
hash_tree_root(get_current_epoch(state).uint64).data,
|
hash_tree_root(epoch.uint64).data,
|
||||||
body.randao_reveal,
|
body.randao_reveal,
|
||||||
get_domain(state, DOMAIN_RANDAO)):
|
get_domain(state, DOMAIN_RANDAO)):
|
||||||
|
|
||||||
notice "Randao mismatch", proposer_pubkey = proposer.pubkey,
|
notice "Randao mismatch", proposer_pubkey = proposer.pubkey,
|
||||||
message = get_current_epoch(state),
|
message = epoch,
|
||||||
signature = body.randao_reveal,
|
signature = body.randao_reveal,
|
||||||
slot = state.slot
|
slot = state.slot
|
||||||
return false
|
return false
|
||||||
|
|
||||||
# Mix it in
|
# Mix it in
|
||||||
let
|
let
|
||||||
mix = get_current_epoch(state) mod LATEST_RANDAO_MIXES_LENGTH
|
mix = get_randao_mix(state, epoch)
|
||||||
rr = eth2hash(body.randao_reveal.getBytes()).data
|
rr = eth2hash(body.randao_reveal.getBytes()).data
|
||||||
|
|
||||||
for i, b in state.randao_mixes[mix].data:
|
for i in 0 ..< mix.data.len:
|
||||||
state.randao_mixes[mix].data[i] = b xor rr[i]
|
state.randao_mixes[epoch mod EPOCHS_PER_HISTORICAL_VECTOR].data[i] = mix.data[i] xor rr[i]
|
||||||
|
|
||||||
true
|
true
|
||||||
|
|
||||||
|
|
|
@ -497,7 +497,7 @@ proc process_final_updates*(state: var BeaconState) =
|
||||||
state.slashings[next_epoch mod EPOCHS_PER_SLASHINGS_VECTOR] = 0.Gwei
|
state.slashings[next_epoch mod EPOCHS_PER_SLASHINGS_VECTOR] = 0.Gwei
|
||||||
|
|
||||||
# Set randao mix
|
# 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)
|
get_randao_mix(state, current_epoch)
|
||||||
|
|
||||||
# Set historical root accumulator
|
# Set historical root accumulator
|
||||||
|
|
Loading…
Reference in New Issue