Skip to content

Commit

Permalink
begin refactoring 0.5.0 epoch transition (#185)
Browse files Browse the repository at this point in the history
* begin refactoring 0.5.0 epoch transition at justification and finalization

* update processTransfers to 0.5.0 spec

* update get_crosslink_committees_at_slot to 0.5.0 except for noted workaround for pre-0.5.0 spec bug related to epoch boundary conditions, to be flipped in more coordinated way with other epoch processing changes

* mark ejection processing as 0.5.0

* 0.5.0 epoch update finalization

* rm BEACON_CHAIN_SHARD_NUMBER to complete updating miscellaneous constants to 0.5.0

* mark verify_slashable_attestation as 0.5.0

* update process_attester_slashing to 0.5.0

* update process_slashings(...) to 0.5.0 and mark process_exit_queue as 0.5.0

* refactor epoch processing by implementing 0.5.0's update_registry_and_shuffling_data(...)
  • Loading branch information
tersec authored and arnetheduck committed Mar 20, 2019
1 parent 9930fa5 commit 13c7f7f
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 226 deletions.
1 change: 0 additions & 1 deletion beacon_chain/beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ proc proposeBlock(node: BeaconNode,

let proposal = Proposal(
slot: slot.uint64,
shard: BEACON_CHAIN_SHARD_NUMBER,
block_root: Eth2Digest(data: signed_root(newBlock)),
signature: ValidatorSig(),
)
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func get_attestation_participants*(state: BeaconState,
if aggregation_bit == 1:
result.add(validator_index)

# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#ejections
# https://github.com/ethereum/eth2.0-specs/blob/v0.5.0/specs/core/0_beacon-chain.md#ejections
func process_ejections*(state: var BeaconState) =
## Iterate through the validator registry and eject active validators with
## balance below ``EJECTION_BALANCE``
Expand Down
17 changes: 11 additions & 6 deletions beacon_chain/spec/datatypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const
## TODO: improve this scheme once we can negotiate versions in protocol

# Misc
# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#misc
# https://github.com/ethereum/eth2.0-specs/blob/v0.5.0/specs/core/0_beacon-chain.md#misc
SHARD_COUNT* {.intdefine.} = 1024 ##\
## Number of shards supported by the network - validators will jump around
## between these shards and provide attestations to their state.
Expand All @@ -72,8 +72,6 @@ const
## At most `1/MAX_BALANCE_CHURN_QUOTIENT` of the validators can change during
## each validator registry change.

BEACON_CHAIN_SHARD_NUMBER* = not 0'u64 # 2^64 - 1 in spec

MAX_INDICES_PER_SLASHABLE_VOTE* = 2^12 ##\
## votes

Expand Down Expand Up @@ -237,6 +235,8 @@ type

# FFG vote
source_epoch*: Epoch
target_root*: Eth2Digest

## TODO epoch_boundary_root and justified_block_root are creatures of new
## epoch processing and don't function quite as straightforwardly as just
## renamings, so do that as part of epoch processing change.
Expand Down Expand Up @@ -369,9 +369,6 @@ type
slot*: uint64 ##\
## Slot number

shard*: uint64 ##\
## Shard number (`BEACON_CHAIN_SHARD_NUMBER` for beacon chain)

block_root*: Eth2Digest ##\
## Block root

Expand Down Expand Up @@ -479,6 +476,14 @@ type
custody_bitfield*: seq[byte] # Custody bitfield
inclusion_slot*: Slot # Inclusion slot

# https://github.com/ethereum/eth2.0-specs/blob/v0.5.0/specs/core/0_beacon-chain.md#historicalbatch
HistoricalBatch* = object
block_roots* : array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest] ##\
## Block roots

state_roots* : array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest] ##\
## State roots

# https://github.com/ethereum/eth2.0-specs/blob/v0.5.0/specs/core/0_beacon-chain.md#fork
Fork* = object
previous_version*: array[4, byte] ##\
Expand Down
28 changes: 19 additions & 9 deletions beacon_chain/spec/validator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func get_previous_epoch*(state: BeaconState): Epoch =
## Return the previous epoch of the given ``state``.
max(get_current_epoch(state) - 1, GENESIS_EPOCH)

# https://github.com/ethereum/eth2.0-specs/blob/0.4.0/specs/core/0_beacon-chain.md#get_crosslink_committees_at_slot
# https://github.com/ethereum/eth2.0-specs/blob/v0.5.0/specs/core/0_beacon-chain.md#get_crosslink_committees_at_slot
func get_crosslink_committees_at_slot*(state: BeaconState, slot: Slot|uint64,
registry_change: bool = false):
seq[CrosslinkCommittee] =
Expand All @@ -140,6 +140,8 @@ func get_crosslink_committees_at_slot*(state: BeaconState, slot: Slot|uint64,
# TODO: the + 1 here works around a bug, remove when upgrading to
# some more recent version:
# https://github.com/ethereum/eth2.0-specs/pull/732
# TODO remove +1 along with rest of epoch reorganization, then
# remove this 0.4.0 tag.
epoch = slot_to_epoch(slot + 1)
current_epoch = get_current_epoch(state)
previous_epoch = get_previous_epoch(state)
Expand Down Expand Up @@ -174,21 +176,29 @@ func get_crosslink_committees_at_slot*(state: BeaconState, slot: Slot|uint64,
doAssert epoch == next_epoch

let
current_committees_per_epoch = get_current_epoch_committee_count(state)
committees_per_epoch = get_next_epoch_committee_count(state)
shuffling_epoch = next_epoch

epochs_since_last_registry_update = current_epoch - state.validator_registry_update_epoch
epochs_since_last_registry_update =
current_epoch - state.validator_registry_update_epoch
condition = epochs_since_last_registry_update > 1'u64 and
is_power_of_2(epochs_since_last_registry_update)
seed = if registry_change or condition:
generate_seed(state, next_epoch)
else:
state.current_shuffling_seed
use_next = registry_change or condition
committees_per_epoch =
if use_next:
get_next_epoch_committee_count(state)
else:
get_current_epoch_committee_count(state)
seed =
if use_next:
generate_seed(state, next_epoch)
else:
state.current_shuffling_seed
shuffling_epoch =
if use_next: next_epoch else: state.current_shuffling_epoch
shuffling_start_shard =
if registry_change:
(state.current_shuffling_start_shard +
current_committees_per_epoch) mod SHARD_COUNT
get_current_epoch_committee_count(state)) mod SHARD_COUNT
else:
state.current_shuffling_start_shard
(committees_per_epoch, seed, shuffling_epoch, shuffling_start_shard)
Expand Down
Loading

0 comments on commit 13c7f7f

Please sign in to comment.