From 10e257490fc57e3fd335ac352d62abbd7a98ce68 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 26 Jun 2019 16:33:23 -0600 Subject: [PATCH 1/4] Clarify `get_randao_mix` accessor We avoid a genesis underflow by taking the randao epoch in `generate_seed` to be `+ EPOCHS_PER_HISTORICAL_VECTOR`. This conflicts with the expected epoch bounds noted in `get_randao_mix` and this PR attempts to clarify the situation by leaving a note. --- specs/core/0_beacon-chain.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 264c6d23b..31fb7c1db 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -737,7 +737,7 @@ def get_randao_mix(state: BeaconState, epoch: Epoch) -> Hash: """ Return the randao mix at a recent ``epoch``. - ``epoch`` expected to be between (current_epoch - EPOCHS_PER_HISTORICAL_VECTOR, current_epoch]. + ``epoch`` expected to be between (current_epoch - EPOCHS_PER_HISTORICAL_VECTOR, current_epoch], unless otherwise noted at a call site. """ return state.randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] ``` @@ -762,6 +762,8 @@ def generate_seed(state: BeaconState, epoch: Epoch) -> Hash: """ Generate a seed for the given ``epoch``. + + Note that avoiding the underflow on ``get_randao_mix`` here violates the epoch validity condition given in that function's comment. """ return hash( get_randao_mix(state, Epoch(epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD)) + From 23909ca72760b002c25278de9817ae26164c67d4 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 26 Jun 2019 17:06:34 -0600 Subject: [PATCH 2/4] Fix line lengths --- specs/core/0_beacon-chain.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 31fb7c1db..6f7a7a059 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -737,7 +737,8 @@ def get_randao_mix(state: BeaconState, epoch: Epoch) -> Hash: """ Return the randao mix at a recent ``epoch``. - ``epoch`` expected to be between (current_epoch - EPOCHS_PER_HISTORICAL_VECTOR, current_epoch], unless otherwise noted at a call site. + ``epoch`` expected to be between (current_epoch - EPOCHS_PER_HISTORICAL_VECTOR, current_epoch], unless + otherwise noted at a call site. """ return state.randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] ``` @@ -763,7 +764,8 @@ def generate_seed(state: BeaconState, """ Generate a seed for the given ``epoch``. - Note that avoiding the underflow on ``get_randao_mix`` here violates the epoch validity condition given in that function's comment. + Note that avoiding the underflow on ``get_randao_mix`` here violates + the epoch validity condition given in that function's comment. """ return hash( get_randao_mix(state, Epoch(epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD)) + From ded936ebad1e2486a6918ada4a955d06b3e50e55 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Sat, 29 Jun 2019 15:49:11 -0600 Subject: [PATCH 3/4] quick comment on avoiding underflow --- specs/core/0_beacon-chain.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 6f7a7a059..007079555 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -737,8 +737,7 @@ def get_randao_mix(state: BeaconState, epoch: Epoch) -> Hash: """ Return the randao mix at a recent ``epoch``. - ``epoch`` expected to be between (current_epoch - EPOCHS_PER_HISTORICAL_VECTOR, current_epoch], unless - otherwise noted at a call site. + ``epoch`` expected to be between (current_epoch - EPOCHS_PER_HISTORICAL_VECTOR, current_epoch]. """ return state.randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] ``` @@ -763,12 +762,9 @@ def generate_seed(state: BeaconState, epoch: Epoch) -> Hash: """ Generate a seed for the given ``epoch``. - - Note that avoiding the underflow on ``get_randao_mix`` here violates - the epoch validity condition given in that function's comment. """ return hash( - get_randao_mix(state, Epoch(epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD)) + + get_randao_mix(state, Epoch(epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD)) + #avoid underflow get_active_index_root(state, epoch) + int_to_bytes(epoch, length=32) ) From 2a2bd72425f564bff9dde87a6071228c9841fe65 Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 29 Jun 2019 22:52:01 +0100 Subject: [PATCH 4/4] Update 0_beacon-chain.md --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 77e9055ff..f372c457a 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -786,7 +786,7 @@ def generate_seed(state: BeaconState, Generate a seed for the given ``epoch``. """ return hash( - get_randao_mix(state, Epoch(epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD)) + # avoid underflow + get_randao_mix(state, Epoch(epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD)) + # Avoid underflow hash_tree_root(List[ValidatorIndex, VALIDATOR_REGISTRY_LIMIT](get_active_validator_indices(state, epoch))) + int_to_bytes(epoch, length=32) )