From bae84cb1e6094e70bb80b7f70877972932cb1991 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Wed, 19 Jun 2019 11:58:38 -0600 Subject: [PATCH 1/3] increase historical length and a few cleanups --- specs/core/0_beacon-chain.md | 41 +++++++++++++++--------------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 3016b52d88..6bbf4bba34 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -235,8 +235,7 @@ These configurations are updated for releases, but may be out of sync during `de | Name | Value | Unit | Duration | | - | - | :-: | :-: | -| `RANDAO_MIXES_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | -| `ACTIVE_INDEX_ROOTS_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | +| `HISTORICAL_VECTOR_LENGTH` | `2**16` (= 65,536) | epochs | ~0.8 years | | `SLASHED_EXIT_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | ### Rewards and penalties @@ -524,8 +523,8 @@ class BeaconState(Container): balances: List[Gwei] # Shuffling start_shard: Shard - randao_mixes: Vector[Hash, RANDAO_MIXES_LENGTH] - active_index_roots: Vector[Hash, ACTIVE_INDEX_ROOTS_LENGTH] # Digests of the active registry, for light clients + randao_mixes: Vector[Hash, HISTORICAL_VECTOR_LENGTH] + active_index_roots: Vector[Hash, HISTORICAL_VECTOR_LENGTH] # Digests of the active registry, for light clients # Slashings slashed_balances: Vector[Gwei, SLASHED_EXIT_LENGTH] # Sums of the effective balances of slashed validators # Attestations @@ -751,9 +750,9 @@ def get_randao_mix(state: BeaconState, epoch: Epoch) -> Hash: """ Return the randao mix at a recent ``epoch``. - ``epoch`` expected to be between (current_epoch - RANDAO_MIXES_LENGTH, current_epoch]. + ``epoch`` expected to be between (current_epoch - HISTORICAL_VECTOR_LENGTH, current_epoch]. """ - return state.randao_mixes[epoch % RANDAO_MIXES_LENGTH] + return state.randao_mixes[epoch % HISTORICAL_VECTOR_LENGTH] ``` ### `get_active_index_root` @@ -764,9 +763,9 @@ def get_active_index_root(state: BeaconState, """ Return the index root at a recent ``epoch``. ``epoch`` expected to be between - (current_epoch - ACTIVE_INDEX_ROOTS_LENGTH + ACTIVATION_EXIT_DELAY, current_epoch + ACTIVATION_EXIT_DELAY]. + (current_epoch - HISTORICAL_VECTOR_LENGTH + ACTIVATION_EXIT_DELAY, current_epoch + ACTIVATION_EXIT_DELAY]. """ - return state.active_index_roots[epoch % ACTIVE_INDEX_ROOTS_LENGTH] + return state.active_index_roots[epoch % HISTORICAL_VECTOR_LENGTH] ``` ### `generate_seed` @@ -778,7 +777,7 @@ def generate_seed(state: BeaconState, Generate a seed for the given ``epoch``. """ return hash( - get_randao_mix(state, Epoch(epoch + RANDAO_MIXES_LENGTH - MIN_SEED_LOOKAHEAD)) + + get_randao_mix(state, Epoch(epoch + HISTORICAL_VECTOR_LENGTH - MIN_SEED_LOOKAHEAD)) + get_active_index_root(state, epoch) + int_to_bytes(epoch, length=32) ) @@ -1188,7 +1187,7 @@ def get_genesis_beacon_state(deposits: List[Deposit], genesis_time: int, genesis # Populate active_index_roots genesis_active_index_root = hash_tree_root(get_active_validator_indices(state, GENESIS_EPOCH)) - for index in range(ACTIVE_INDEX_ROOTS_LENGTH): + for index in range(HISTORICAL_VECTOR_LENGTH): state.active_index_roots[index] = genesis_active_index_root return state @@ -1542,7 +1541,7 @@ def process_final_updates(state: BeaconState) -> None: # Update start shard state.start_shard = Shard((state.start_shard + get_shard_delta(state, current_epoch)) % SHARD_COUNT) # Set active index root - index_root_position = (next_epoch + ACTIVATION_EXIT_DELAY) % ACTIVE_INDEX_ROOTS_LENGTH + index_root_position = (next_epoch + ACTIVATION_EXIT_DELAY) % HISTORICAL_VECTOR_LENGTH state.active_index_roots[index_root_position] = hash_tree_root( get_active_validator_indices(state, Epoch(next_epoch + ACTIVATION_EXIT_DELAY)) ) @@ -1551,7 +1550,7 @@ def process_final_updates(state: BeaconState) -> None: state.slashed_balances[current_epoch % SLASHED_EXIT_LENGTH] ) # Set randao mix - state.randao_mixes[next_epoch % RANDAO_MIXES_LENGTH] = get_randao_mix(state, current_epoch) + state.randao_mixes[next_epoch % HISTORICAL_VECTOR_LENGTH] = get_randao_mix(state, current_epoch) # Set historical root accumulator if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: historical_batch = HistoricalBatch( @@ -1599,19 +1598,13 @@ def process_block_header(state: BeaconState, block: BeaconBlock) -> None: ```python def process_randao(state: BeaconState, body: BeaconBlockBody) -> None: + epoch = get_current_epoch(state) + # Verify RANDAO reveal proposer = state.validators[get_beacon_proposer_index(state)] - # Verify that the provided randao value is valid - assert bls_verify( - proposer.pubkey, - hash_tree_root(get_current_epoch(state)), - body.randao_reveal, - get_domain(state, DOMAIN_RANDAO), - ) - # Mix it in - state.randao_mixes[get_current_epoch(state) % RANDAO_MIXES_LENGTH] = ( - xor(get_randao_mix(state, get_current_epoch(state)), - hash(body.randao_reveal)) - ) + assert bls_verify(proposer.pubkey, hash_tree_root(epoch), body.randao_reveal, get_domain(state, DOMAIN_RANDAO)) + # Mix in RANDAO reveal + mix = xor(get_randao_mix(state, epoch), hash(body.randao_reveal)) + state.latest_randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] = mix ``` #### Eth1 data From 68b410160b33b9b67a4b846e7527434ba5b298e0 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Wed, 19 Jun 2019 12:16:03 -0600 Subject: [PATCH 2/3] fix up config params and a small bug --- configs/constant_presets/mainnet.yaml | 4 +--- configs/constant_presets/minimal.yaml | 4 +--- specs/core/0_beacon-chain.md | 2 +- test_libs/pyspec/eth2spec/test/helpers/genesis.py | 2 +- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/configs/constant_presets/mainnet.yaml b/configs/constant_presets/mainnet.yaml index ed839fc7d6..e3f6af7b7e 100644 --- a/configs/constant_presets/mainnet.yaml +++ b/configs/constant_presets/mainnet.yaml @@ -74,9 +74,7 @@ MIN_EPOCHS_TO_INACTIVITY_PENALTY: 4 # State list lengths # --------------------------------------------------------------- # 2**13 (= 8,192) epochs ~36 days -RANDAO_MIXES_LENGTH: 8192 -# 2**13 (= 8,192) epochs ~36 days -ACTIVE_INDEX_ROOTS_LENGTH: 8192 +HISTORICAL_VECTOR_LENGTH: 65536 # 2**13 (= 8,192) epochs ~36 days SLASHED_EXIT_LENGTH: 8192 diff --git a/configs/constant_presets/minimal.yaml b/configs/constant_presets/minimal.yaml index d631300e27..00053346e8 100644 --- a/configs/constant_presets/minimal.yaml +++ b/configs/constant_presets/minimal.yaml @@ -75,9 +75,7 @@ EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS: 4096 # State list lengths # --------------------------------------------------------------- # [customized] smaller state -RANDAO_MIXES_LENGTH: 64 -# [customized] smaller state -ACTIVE_INDEX_ROOTS_LENGTH: 64 +HISTORICAL_VECTOR_LENGTH: 64 # [customized] smaller state SLASHED_EXIT_LENGTH: 64 diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 6bbf4bba34..acdc75ab9d 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1604,7 +1604,7 @@ def process_randao(state: BeaconState, body: BeaconBlockBody) -> None: assert bls_verify(proposer.pubkey, hash_tree_root(epoch), body.randao_reveal, get_domain(state, DOMAIN_RANDAO)) # Mix in RANDAO reveal mix = xor(get_randao_mix(state, epoch), hash(body.randao_reveal)) - state.latest_randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] = mix + state.randao_mixes[epoch % HISTORICAL_VECTOR_LENGTH] = mix ``` #### Eth1 data diff --git a/test_libs/pyspec/eth2spec/test/helpers/genesis.py b/test_libs/pyspec/eth2spec/test/helpers/genesis.py index 87b0446afd..cb655ddeca 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/genesis.py +++ b/test_libs/pyspec/eth2spec/test/helpers/genesis.py @@ -41,7 +41,7 @@ def create_genesis_state(spec, num_validators): validator.activation_epoch = spec.GENESIS_EPOCH genesis_active_index_root = hash_tree_root(spec.get_active_validator_indices(state, spec.GENESIS_EPOCH)) - for index in range(spec.ACTIVE_INDEX_ROOTS_LENGTH): + for index in range(spec.HISTORICAL_VECTOR_LENGTH): state.active_index_roots[index] = genesis_active_index_root return state From 0070f682ba009b8bd13cd4bb025795cafa53bd31 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Wed, 19 Jun 2019 12:25:48 -0600 Subject: [PATCH 3/3] use EPOCHS_PER notation for var names --- configs/constant_presets/mainnet.yaml | 8 ++-- configs/constant_presets/minimal.yaml | 6 +-- specs/core/0_beacon-chain.md | 42 +++++++++---------- .../pyspec/eth2spec/test/helpers/genesis.py | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/configs/constant_presets/mainnet.yaml b/configs/constant_presets/mainnet.yaml index e3f6af7b7e..09ec7bdb79 100644 --- a/configs/constant_presets/mainnet.yaml +++ b/configs/constant_presets/mainnet.yaml @@ -71,12 +71,12 @@ MAX_EPOCHS_PER_CROSSLINK: 64 MIN_EPOCHS_TO_INACTIVITY_PENALTY: 4 -# State list lengths +# State vector lengths # --------------------------------------------------------------- +# 2**16 (= 65,536) epochs ~0.8 years +EPOCHS_PER_HISTORICAL_VECTOR: 65536 # 2**13 (= 8,192) epochs ~36 days -HISTORICAL_VECTOR_LENGTH: 65536 -# 2**13 (= 8,192) epochs ~36 days -SLASHED_EXIT_LENGTH: 8192 +EPOCHS_PER_SLASHED_BALANCES_VECTOR: 8192 # Reward and penalty quotients diff --git a/configs/constant_presets/minimal.yaml b/configs/constant_presets/minimal.yaml index 00053346e8..c8c2853c70 100644 --- a/configs/constant_presets/minimal.yaml +++ b/configs/constant_presets/minimal.yaml @@ -72,12 +72,12 @@ MIN_EPOCHS_TO_INACTIVITY_PENALTY: 4 EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS: 4096 -# State list lengths +# State vector lengths # --------------------------------------------------------------- # [customized] smaller state -HISTORICAL_VECTOR_LENGTH: 64 +EPOCHS_PER_HISTORICAL_VECTOR: 64 # [customized] smaller state -SLASHED_EXIT_LENGTH: 64 +EPOCHS_PER_SLASHED_BALANCES_VECTOR: 64 # Reward and penalty quotients diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index acdc75ab9d..1dcdbdef94 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -235,8 +235,8 @@ These configurations are updated for releases, but may be out of sync during `de | Name | Value | Unit | Duration | | - | - | :-: | :-: | -| `HISTORICAL_VECTOR_LENGTH` | `2**16` (= 65,536) | epochs | ~0.8 years | -| `SLASHED_EXIT_LENGTH` | `2**13` (= 8,192) | epochs | ~36 days | +| `EPOCHS_PER_HISTORICAL_VECTOR` | `2**16` (= 65,536) | epochs | ~0.8 years | +| `EPOCHS_PER_SLASHED_BALANCES_VECTOR` | `2**13` (= 8,192) | epochs | ~36 days | ### Rewards and penalties @@ -523,10 +523,10 @@ class BeaconState(Container): balances: List[Gwei] # Shuffling start_shard: Shard - randao_mixes: Vector[Hash, HISTORICAL_VECTOR_LENGTH] - active_index_roots: Vector[Hash, HISTORICAL_VECTOR_LENGTH] # Digests of the active registry, for light clients + randao_mixes: Vector[Hash, EPOCHS_PER_HISTORICAL_VECTOR] + active_index_roots: Vector[Hash, EPOCHS_PER_HISTORICAL_VECTOR] # Digests of the active registry, for light clients # Slashings - slashed_balances: Vector[Gwei, SLASHED_EXIT_LENGTH] # Sums of the effective balances of slashed validators + slashed_balances: Vector[Gwei, EPOCHS_PER_SLASHED_BALANCES_VECTOR] # Sums of the effective balances of slashed validators # Attestations previous_epoch_attestations: List[PendingAttestation] current_epoch_attestations: List[PendingAttestation] @@ -750,9 +750,9 @@ def get_randao_mix(state: BeaconState, epoch: Epoch) -> Hash: """ Return the randao mix at a recent ``epoch``. - ``epoch`` expected to be between (current_epoch - HISTORICAL_VECTOR_LENGTH, current_epoch]. + ``epoch`` expected to be between (current_epoch - EPOCHS_PER_HISTORICAL_VECTOR, current_epoch]. """ - return state.randao_mixes[epoch % HISTORICAL_VECTOR_LENGTH] + return state.randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] ``` ### `get_active_index_root` @@ -763,9 +763,9 @@ def get_active_index_root(state: BeaconState, """ Return the index root at a recent ``epoch``. ``epoch`` expected to be between - (current_epoch - HISTORICAL_VECTOR_LENGTH + ACTIVATION_EXIT_DELAY, current_epoch + ACTIVATION_EXIT_DELAY]. + (current_epoch - EPOCHS_PER_HISTORICAL_VECTOR + ACTIVATION_EXIT_DELAY, current_epoch + ACTIVATION_EXIT_DELAY]. """ - return state.active_index_roots[epoch % HISTORICAL_VECTOR_LENGTH] + return state.active_index_roots[epoch % EPOCHS_PER_HISTORICAL_VECTOR] ``` ### `generate_seed` @@ -777,7 +777,7 @@ def generate_seed(state: BeaconState, Generate a seed for the given ``epoch``. """ return hash( - get_randao_mix(state, Epoch(epoch + HISTORICAL_VECTOR_LENGTH - MIN_SEED_LOOKAHEAD)) + + get_randao_mix(state, Epoch(epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD)) + get_active_index_root(state, epoch) + int_to_bytes(epoch, length=32) ) @@ -1111,9 +1111,9 @@ def slash_validator(state: BeaconState, current_epoch = get_current_epoch(state) initiate_validator_exit(state, slashed_index) state.validators[slashed_index].slashed = True - state.validators[slashed_index].withdrawable_epoch = Epoch(current_epoch + SLASHED_EXIT_LENGTH) + state.validators[slashed_index].withdrawable_epoch = Epoch(current_epoch + EPOCHS_PER_SLASHED_BALANCES_VECTOR) slashed_balance = state.validators[slashed_index].effective_balance - state.slashed_balances[current_epoch % SLASHED_EXIT_LENGTH] += slashed_balance + state.slashed_balances[current_epoch % EPOCHS_PER_SLASHED_BALANCES_VECTOR] += slashed_balance proposer_index = get_beacon_proposer_index(state) if whistleblower_index is None: @@ -1187,7 +1187,7 @@ def get_genesis_beacon_state(deposits: List[Deposit], genesis_time: int, genesis # Populate active_index_roots genesis_active_index_root = hash_tree_root(get_active_validator_indices(state, GENESIS_EPOCH)) - for index in range(HISTORICAL_VECTOR_LENGTH): + for index in range(EPOCHS_PER_HISTORICAL_VECTOR): state.active_index_roots[index] = genesis_active_index_root return state @@ -1510,12 +1510,12 @@ def process_slashings(state: BeaconState) -> None: total_balance = get_total_active_balance(state) # Compute slashed balances in the current epoch - total_at_start = state.slashed_balances[(current_epoch + 1) % SLASHED_EXIT_LENGTH] - total_at_end = state.slashed_balances[current_epoch % SLASHED_EXIT_LENGTH] + total_at_start = state.slashed_balances[(current_epoch + 1) % EPOCHS_PER_SLASHED_BALANCES_VECTOR] + total_at_end = state.slashed_balances[current_epoch % EPOCHS_PER_SLASHED_BALANCES_VECTOR] total_penalties = total_at_end - total_at_start for index, validator in enumerate(state.validators): - if validator.slashed and current_epoch == validator.withdrawable_epoch - SLASHED_EXIT_LENGTH // 2: + if validator.slashed and current_epoch == validator.withdrawable_epoch - EPOCHS_PER_SLASHED_BALANCES_VECTOR // 2: penalty = max( validator.effective_balance * min(total_penalties * 3, total_balance) // total_balance, validator.effective_balance // MIN_SLASHING_PENALTY_QUOTIENT @@ -1541,16 +1541,16 @@ def process_final_updates(state: BeaconState) -> None: # Update start shard state.start_shard = Shard((state.start_shard + get_shard_delta(state, current_epoch)) % SHARD_COUNT) # Set active index root - index_root_position = (next_epoch + ACTIVATION_EXIT_DELAY) % HISTORICAL_VECTOR_LENGTH + index_root_position = (next_epoch + ACTIVATION_EXIT_DELAY) % EPOCHS_PER_HISTORICAL_VECTOR state.active_index_roots[index_root_position] = hash_tree_root( get_active_validator_indices(state, Epoch(next_epoch + ACTIVATION_EXIT_DELAY)) ) # Set total slashed balances - state.slashed_balances[next_epoch % SLASHED_EXIT_LENGTH] = ( - state.slashed_balances[current_epoch % SLASHED_EXIT_LENGTH] + state.slashed_balances[next_epoch % EPOCHS_PER_SLASHED_BALANCES_VECTOR] = ( + state.slashed_balances[current_epoch % EPOCHS_PER_SLASHED_BALANCES_VECTOR] ) # Set randao mix - state.randao_mixes[next_epoch % HISTORICAL_VECTOR_LENGTH] = get_randao_mix(state, current_epoch) + state.randao_mixes[next_epoch % EPOCHS_PER_HISTORICAL_VECTOR] = get_randao_mix(state, current_epoch) # Set historical root accumulator if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: historical_batch = HistoricalBatch( @@ -1604,7 +1604,7 @@ def process_randao(state: BeaconState, body: BeaconBlockBody) -> None: assert bls_verify(proposer.pubkey, hash_tree_root(epoch), body.randao_reveal, get_domain(state, DOMAIN_RANDAO)) # Mix in RANDAO reveal mix = xor(get_randao_mix(state, epoch), hash(body.randao_reveal)) - state.randao_mixes[epoch % HISTORICAL_VECTOR_LENGTH] = mix + state.randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] = mix ``` #### Eth1 data diff --git a/test_libs/pyspec/eth2spec/test/helpers/genesis.py b/test_libs/pyspec/eth2spec/test/helpers/genesis.py index cb655ddeca..d1d8189080 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/genesis.py +++ b/test_libs/pyspec/eth2spec/test/helpers/genesis.py @@ -41,7 +41,7 @@ def create_genesis_state(spec, num_validators): validator.activation_epoch = spec.GENESIS_EPOCH genesis_active_index_root = hash_tree_root(spec.get_active_validator_indices(state, spec.GENESIS_EPOCH)) - for index in range(spec.HISTORICAL_VECTOR_LENGTH): + for index in range(spec.EPOCHS_PER_HISTORICAL_VECTOR): state.active_index_roots[index] = genesis_active_index_root return state