From 2ba14bc9afb6e074b381db8668b6fce1a4d96197 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Mon, 1 Jul 2019 11:05:22 +0200 Subject: [PATCH] update get_previous_epoch(...) to 0.8.0 and mark compute_committee(...) as 0.8.0 --- beacon_chain/spec/beaconstate.nim | 5 +++-- beacon_chain/spec/helpers.nim | 2 +- beacon_chain/spec/validator.nim | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 7616bcc0a..8ed4dad62 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -35,10 +35,11 @@ func increase_balance*( # Increase the validator balance at index ``index`` by ``delta``. state.balances[index] += delta -# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#decrease_balance +# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#decrease_balance func decrease_balance*( state: var BeaconState, index: ValidatorIndex, delta: Gwei) = - # Decrease validator balance by ``delta`` with underflow protection. + ## Decrease the validator balance at index ``index`` by ``delta``, with + ## underflow protection. state.balances[index] = if delta > state.balances[index]: 0'u64 diff --git a/beacon_chain/spec/helpers.nim b/beacon_chain/spec/helpers.nim index e1330ee3c..58cd3d52b 100644 --- a/beacon_chain/spec/helpers.nim +++ b/beacon_chain/spec/helpers.nim @@ -67,7 +67,7 @@ func compute_start_slot_of_epoch*(epoch: Epoch): Slot = # Return the starting slot of the given ``epoch``. (epoch * SLOTS_PER_EPOCH).Slot -# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#is_active_validator +# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#is_active_validator func is_active_validator*(validator: Validator, epoch: Epoch): bool = ### Check if ``validator`` is active validator.activation_epoch <= epoch and epoch < validator.exit_epoch diff --git a/beacon_chain/spec/validator.nim b/beacon_chain/spec/validator.nim index f078e8f7e..ad7718b60 100644 --- a/beacon_chain/spec/validator.nim +++ b/beacon_chain/spec/validator.nim @@ -12,8 +12,8 @@ import ./crypto, ./datatypes, ./digest, ./helpers # TODO: Proceed to renaming and signature changes -# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_shuffled_index -# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#compute_committee +# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#compute_shuffled_index +# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#compute_committee func get_shuffled_seq*(seed: Eth2Digest, list_size: uint64, ): seq[ValidatorIndex] = @@ -79,15 +79,14 @@ func get_shuffled_seq*(seed: Eth2Digest, result = shuffled_active_validator_indices -# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_previous_epoch +# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#get_previous_epoch func get_previous_epoch*(state: BeaconState): Epoch = - ## Return the previous epoch of the given ``state``. - ## Return the current epoch if it's genesis epoch. + # Return the previous epoch (unless the current epoch is ``GENESIS_EPOCH``). let current_epoch = get_current_epoch(state) if current_epoch == GENESIS_EPOCH: current_epoch else: - current_epoch - 1 + (current_epoch - 1).Epoch # https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_shard_delta func get_shard_delta*(state: BeaconState, epoch: Epoch): uint64 = @@ -112,9 +111,11 @@ func get_start_shard*(state: BeaconState, epoch: Epoch): Shard = SHARD_COUNT return shard -# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#compute_committee +# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#compute_committee func compute_committee(indices: seq[ValidatorIndex], seed: Eth2Digest, index: uint64, count: uint64, stateCache: var StateCache): seq[ValidatorIndex] = + ## Return the committee corresponding to ``indices``, ``seed``, ``index``, + ## and committee ``count``. let start = (len(indices).uint64 * index) div count @@ -125,7 +126,7 @@ func compute_committee(indices: seq[ValidatorIndex], seed: Eth2Digest, stateCache.crosslink_committee_cache[key] = get_shuffled_seq(seed, len(indices).uint64) - # These assertions from get_shuffled_index(...) + # These assertions from compute_shuffled_index(...) let index_count = indices.len().uint64 doAssert endIdx <= index_count doAssert index_count <= 2'u64^40 @@ -133,8 +134,7 @@ func compute_committee(indices: seq[ValidatorIndex], seed: Eth2Digest, # In spec, this calls get_shuffled_index() every time, but that's wasteful mapIt( start.int .. (endIdx.int-1), - indices[ - stateCache.crosslink_committee_cache[key][it]]) + indices[stateCache.crosslink_committee_cache[key][it]]) # https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_crosslink_committee func get_crosslink_committee*(state: BeaconState, epoch: Epoch, shard: Shard,