update get_previous_epoch(...) to 0.8.0 and mark compute_committee(...) as 0.8.0

This commit is contained in:
Dustin Brody 2019-07-01 11:05:22 +02:00 committed by zah
parent dc98ffb09b
commit 2ba14bc9af
3 changed files with 14 additions and 13 deletions

View File

@ -35,10 +35,11 @@ func increase_balance*(
# Increase the validator balance at index ``index`` by ``delta``. # Increase the validator balance at index ``index`` by ``delta``.
state.balances[index] += 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*( func decrease_balance*(
state: var BeaconState, index: ValidatorIndex, delta: Gwei) = 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] = state.balances[index] =
if delta > state.balances[index]: if delta > state.balances[index]:
0'u64 0'u64

View File

@ -67,7 +67,7 @@ func compute_start_slot_of_epoch*(epoch: Epoch): Slot =
# Return the starting slot of the given ``epoch``. # Return the starting slot of the given ``epoch``.
(epoch * SLOTS_PER_EPOCH).Slot (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 = func is_active_validator*(validator: Validator, epoch: Epoch): bool =
### Check if ``validator`` is active ### Check if ``validator`` is active
validator.activation_epoch <= epoch and epoch < validator.exit_epoch validator.activation_epoch <= epoch and epoch < validator.exit_epoch

View File

@ -12,8 +12,8 @@ import
./crypto, ./datatypes, ./digest, ./helpers ./crypto, ./datatypes, ./digest, ./helpers
# TODO: Proceed to renaming and signature changes # 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.8.0/specs/core/0_beacon-chain.md#compute_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_committee
func get_shuffled_seq*(seed: Eth2Digest, func get_shuffled_seq*(seed: Eth2Digest,
list_size: uint64, list_size: uint64,
): seq[ValidatorIndex] = ): seq[ValidatorIndex] =
@ -79,15 +79,14 @@ func get_shuffled_seq*(seed: Eth2Digest,
result = shuffled_active_validator_indices 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 = func get_previous_epoch*(state: BeaconState): Epoch =
## Return the previous epoch of the given ``state``. # Return the previous epoch (unless the current epoch is ``GENESIS_EPOCH``).
## Return the current epoch if it's genesis epoch.
let current_epoch = get_current_epoch(state) let current_epoch = get_current_epoch(state)
if current_epoch == GENESIS_EPOCH: if current_epoch == GENESIS_EPOCH:
current_epoch current_epoch
else: 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 # 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 = func get_shard_delta*(state: BeaconState, epoch: Epoch): uint64 =
@ -112,9 +111,11 @@ func get_start_shard*(state: BeaconState, epoch: Epoch): Shard =
SHARD_COUNT SHARD_COUNT
return shard 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, func compute_committee(indices: seq[ValidatorIndex], seed: Eth2Digest,
index: uint64, count: uint64, stateCache: var StateCache): seq[ValidatorIndex] = index: uint64, count: uint64, stateCache: var StateCache): seq[ValidatorIndex] =
## Return the committee corresponding to ``indices``, ``seed``, ``index``,
## and committee ``count``.
let let
start = (len(indices).uint64 * index) div count start = (len(indices).uint64 * index) div count
@ -125,7 +126,7 @@ func compute_committee(indices: seq[ValidatorIndex], seed: Eth2Digest,
stateCache.crosslink_committee_cache[key] = stateCache.crosslink_committee_cache[key] =
get_shuffled_seq(seed, len(indices).uint64) 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 let index_count = indices.len().uint64
doAssert endIdx <= index_count doAssert endIdx <= index_count
doAssert index_count <= 2'u64^40 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 # In spec, this calls get_shuffled_index() every time, but that's wasteful
mapIt( mapIt(
start.int .. (endIdx.int-1), start.int .. (endIdx.int-1),
indices[ indices[stateCache.crosslink_committee_cache[key][it]])
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 # 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, func get_crosslink_committee*(state: BeaconState, epoch: Epoch, shard: Shard,