Spec updates (#94)

* s/slot_included/inclusion_slot/; s/participation_bitfield/aggregation_bitfield/; rearrange some type definitions to match spec order

* a couple more Eth1Data-related spec syncs
This commit is contained in:
Dustin Brody 2019-02-07 16:55:48 +00:00 committed by Mamy Ratsimbazafy
parent 31abd31d8a
commit 2794181889
7 changed files with 56 additions and 38 deletions

View File

@ -179,13 +179,13 @@ proc get_ancestor(store: BeaconChainDB, blck: Eth2Digest, slot: uint64): Eth2Dig
store.get_ancestor(blk.parent_root, slot) # TODO: Eliminate recursion store.get_ancestor(blk.parent_root, slot) # TODO: Eliminate recursion
# TODO: what if the slot was never observed/verified? # TODO: what if the slot was never observed/verified?
func getVoteCount(participation_bitfield: openarray[byte]): int = func getVoteCount(aggregation_bitfield: openarray[byte]): int =
## Get the number of votes ## Get the number of votes
# TODO: A bitfield type that tracks that information # TODO: A bitfield type that tracks that information
# https://github.com/status-im/nim-beacon-chain/issues/19 # https://github.com/status-im/nim-beacon-chain/issues/19
for validatorIdx in 0 ..< participation_bitfield.len * 8: for validatorIdx in 0 ..< aggregation_bitfield.len * 8:
result += int participation_bitfield.get_bitfield_bit(validatorIdx) result += int aggregation_bitfield.get_bitfield_bit(validatorIdx)
func getAttestationVoteCount(pool: AttestationPool, current_slot: int): CountTable[Eth2Digest] = func getAttestationVoteCount(pool: AttestationPool, current_slot: int): CountTable[Eth2Digest] =
## Returns all blocks more recent that the current slot ## Returns all blocks more recent that the current slot

View File

@ -313,7 +313,9 @@ func update_validator_registry*(state: var BeaconState) =
process_penalties_and_exits(state) process_penalties_and_exits(state)
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_epoch_start_slot
func get_epoch_start_slot*(epoch: EpochNumber): SlotNumber = func get_epoch_start_slot*(epoch: EpochNumber): SlotNumber =
# Return the starting slot of the given ``epoch``.
epoch * EPOCH_LENGTH epoch * EPOCH_LENGTH
proc checkAttestation*( proc checkAttestation*(

View File

@ -12,9 +12,6 @@
# specification and that follows the spec as closely as possible, so as to make # specification and that follows the spec as closely as possible, so as to make
# it easy to keep up-to-date. # it easy to keep up-to-date.
# #
# The latest version can be seen here:
# https://github.com/ethereum/eth2.0-specs/blob/master/specs/beacon-chain.md
#
# These datatypes are used as specifications for serialization - thus should not # These datatypes are used as specifications for serialization - thus should not
# be altered outside of what the spec says. Likewise, they should not be made # be altered outside of what the spec says. Likewise, they should not be made
# `ref` - this can be achieved by wrapping them in higher-level # `ref` - this can be achieved by wrapping them in higher-level
@ -356,51 +353,62 @@ type
latest_eth1_data*: Eth1Data latest_eth1_data*: Eth1Data
eth1_data_votes*: seq[Eth1DataVote] eth1_data_votes*: seq[Eth1DataVote]
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#validator
Validator* = object Validator* = object
pubkey*: ValidatorPubKey pubkey*: ValidatorPubKey ##\
withdrawal_credentials*: Eth2Digest ## BLS public key
withdrawal_credentials*: Eth2Digest ##\
## Withdrawal credentials
activation_epoch*: EpochNumber ##\ activation_epoch*: EpochNumber ##\
## Slot when validator activated ## Epoch when validator activated
exit_epoch*: EpochNumber ##\ exit_epoch*: EpochNumber ##\
## Slot when validator exited ## Epoch when validator exited
withdrawal_epoch*: EpochNumber ##\ withdrawal_epoch*: EpochNumber ##\
## Slot when validator withdrew ## Epoch when validator withdrew
penalized_epoch*: EpochNumber ##\ penalized_epoch*: EpochNumber ##\
## Slot when validator penalized ## Epoch when validator penalized
status_flags*: uint64 status_flags*: uint64
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#crosslink
Crosslink* = object Crosslink* = object
epoch*: uint64 epoch*: uint64
shard_block_root*: Eth2Digest ##\ shard_block_root*: Eth2Digest ##\
## Shard chain block root ## Shard chain block root
Eth1Data* = object # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#pendingattestation
deposit_root*: Eth2Digest ##\
## Data being voted for
vote_count*: Eth2Digest ##\
## Vote count
Eth1DataVote* = object
eth1_data*: Eth1Data
vote_count*: uint64 # Vote count
PendingAttestation* = object PendingAttestation* = object
data*: AttestationData # Signed data aggregation_bitfield*: seq[byte] # Attester participation bitfield
participation_bitfield*: seq[byte] # Attester participation bitfield data*: AttestationData # Attestation data
custody_bitfield*: seq[byte] # Proof of custody bitfield custody_bitfield*: seq[byte] # Custody bitfield
slot_included*: uint64 # Slot in which it was included inclusion_slot*: uint64 # Inclusion slot
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#fork
Fork* = object Fork* = object
previous_version*: uint64 # Previous fork version previous_version*: uint64 # Previous fork version
current_version*: uint64 # Current fork version current_version*: uint64 # Current fork version
epoch*: uint64 # Fork epoch number epoch*: uint64 # Fork epoch number
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#eth1data
Eth1Data* = object
deposit_root*: Eth2Digest ##\
## Data being voted for
block_hash*: Eth2Digest ##\
## Block hash
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#eth1datavote
Eth1DataVote* = object
eth1_data*: Eth1Data
vote_count*: uint64 # Vote count
## TODO remove or otherwise conditional-compile this, since it's for light
## client but not in spec
ValidatorRegistryDeltaBlock* = object ValidatorRegistryDeltaBlock* = object
latest_registry_delta_root*: Eth2Digest latest_registry_delta_root*: Eth2Digest
validator_index*: ValidatorIndex validator_index*: ValidatorIndex
@ -408,6 +416,8 @@ type
slot*: uint64 slot*: uint64
flag*: ValidatorSetDeltaFlags flag*: ValidatorSetDeltaFlags
## TODO remove or otherwise conditional-compile this, since it's for light
## client but not in spec
ValidatorSetDeltaFlags* {.pure.} = enum ValidatorSetDeltaFlags* {.pure.} = enum
Activation = 0 Activation = 0
Exit = 1 Exit = 1

View File

@ -31,6 +31,7 @@ func verify_bitfield*(bitfield: openarray[byte], committee_size: int): bool =
true true
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#shuffle
func shuffle*[T](values: seq[T], seed: Eth2Digest): seq[T] = func shuffle*[T](values: seq[T], seed: Eth2Digest): seq[T] =
## Returns the shuffled ``values`` with seed as entropy. ## Returns the shuffled ``values`` with seed as entropy.
## TODO: this calls out for tests, but I odn't particularly trust spec ## TODO: this calls out for tests, but I odn't particularly trust spec
@ -79,6 +80,7 @@ func shuffle*[T](values: seq[T], seed: Eth2Digest): seq[T] =
swap result[index], result[replacement_position] swap result[index], result[replacement_position]
inc index inc index
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#split
func split*[T](lst: openArray[T], N: Positive): seq[seq[T]] = func split*[T](lst: openArray[T], N: Positive): seq[seq[T]] =
## split lst in N pieces, with each piece having `len(lst) div N` or ## split lst in N pieces, with each piece having `len(lst) div N` or
## `len(lst) div N + 1` pieces ## `len(lst) div N + 1` pieces
@ -152,6 +154,7 @@ proc is_double_vote*(attestation_data_1: AttestationData,
## same slot - doing so means risking getting slashed. ## same slot - doing so means risking getting slashed.
attestation_data_1.slot == attestation_data_2.slot attestation_data_1.slot == attestation_data_2.slot
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#slot_to_epoch
func slot_to_epoch*(slot: SlotNumber): EpochNumber = func slot_to_epoch*(slot: SlotNumber): EpochNumber =
slot div EPOCH_LENGTH slot div EPOCH_LENGTH
@ -174,10 +177,12 @@ proc is_surround_vote*(attestation_data_1: AttestationData,
(target_epoch_2 < target_epoch_1) (target_epoch_2 < target_epoch_1)
) )
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#is_active_validator
func is_active_validator*(validator: Validator, epoch: EpochNumber): bool = func is_active_validator*(validator: Validator, epoch: EpochNumber): bool =
### Checks if validator is active ### Checks if validator is active
validator.activation_epoch <= epoch and epoch < validator.exit_epoch validator.activation_epoch <= epoch and epoch < validator.exit_epoch
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_active_validator_indices
func get_active_validator_indices*(validators: openArray[Validator], epoch: EpochNumber): seq[ValidatorIndex] = func get_active_validator_indices*(validators: openArray[Validator], epoch: EpochNumber): seq[ValidatorIndex] =
## Gets indices of active validators from validators ## Gets indices of active validators from validators
for idx, val in validators: for idx, val in validators:
@ -196,6 +201,7 @@ func get_current_epoch_committee_count*(state: BeaconState): uint64 =
) )
return get_epoch_committee_count(len(current_active_validators)) return get_epoch_committee_count(len(current_active_validators))
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_current_epoch
func get_current_epoch*(state: BeaconState): EpochNumber = func get_current_epoch*(state: BeaconState): EpochNumber =
slot_to_epoch(state.slot) slot_to_epoch(state.slot)

View File

@ -275,9 +275,9 @@ proc processAttestations(
state.latest_attestations.add blck.body.attestations.mapIt( state.latest_attestations.add blck.body.attestations.mapIt(
PendingAttestation( PendingAttestation(
data: it.data, data: it.data,
participation_bitfield: it.aggregation_bitfield, aggregation_bitfield: it.aggregation_bitfield,
custody_bitfield: it.custody_bitfield, custody_bitfield: it.custody_bitfield,
slot_included: state.slot, inclusion_slot: state.slot,
) )
) )
@ -411,7 +411,7 @@ func get_attester_indices(
# TODO spec - add as helper? # TODO spec - add as helper?
attestations. attestations.
mapIt( mapIt(
get_attestation_participants(state, it.data, it.participation_bitfield)). get_attestation_participants(state, it.data, it.aggregation_bitfield)).
flatten(). flatten().
deduplicate() deduplicate()
@ -634,14 +634,14 @@ func processEpoch(state: var BeaconState) =
func inclusion_slot(state: BeaconState, v: ValidatorIndex): uint64 = func inclusion_slot(state: BeaconState, v: ValidatorIndex): uint64 =
for a in previous_epoch_attestations: for a in previous_epoch_attestations:
if v in get_attestation_participants(state, a.data, a.participation_bitfield): if v in get_attestation_participants(state, a.data, a.aggregation_bitfield):
return a.slot_included return a.inclusion_slot
doAssert false # shouldn't happen.. doAssert false # shouldn't happen..
func inclusion_distance(state: BeaconState, v: ValidatorIndex): uint64 = func inclusion_distance(state: BeaconState, v: ValidatorIndex): uint64 =
for a in previous_epoch_attestations: for a in previous_epoch_attestations:
if v in get_attestation_participants(state, a.data, a.participation_bitfield): if v in get_attestation_participants(state, a.data, a.aggregation_bitfield):
return a.slot_included - a.data.slot return a.inclusion_slot - a.data.slot
doAssert false # shouldn't happen.. doAssert false # shouldn't happen..
block: # Justification and finalization block: # Justification and finalization

View File

@ -18,7 +18,7 @@ proc stateSize(deposits: int, maxContent = false) =
validatorsPerCommittee = validatorsPerCommittee =
len(crosslink_committees[0].committee) # close enough.. len(crosslink_committees[0].committee) # close enough..
for a in state.latest_attestations.mitems(): for a in state.latest_attestations.mitems():
a.participation_bitfield.setLen(validatorsPerCommittee) a.aggregation_bitfield.setLen(validatorsPerCommittee)
echo "Validators: ", deposits, ", total: ", state.serialize().len echo "Validators: ", deposits, ", total: ", state.serialize().len
dispatch(stateSize) dispatch(stateSize)

View File

@ -188,8 +188,8 @@ proc makeAttestation*(
assert sac_index != -1, "find_shard_committe should guarantee this" assert sac_index != -1, "find_shard_committe should guarantee this"
var var
participation_bitfield = repeat(0'u8, ceil_div8(sac.committee.len)) aggregation_bitfield = repeat(0'u8, ceil_div8(sac.committee.len))
bitSet(participation_bitfield, sac_index) bitSet(aggregation_bitfield, sac_index)
let let
msg = hash_tree_root_final(data) msg = hash_tree_root_final(data)
@ -203,6 +203,6 @@ proc makeAttestation*(
Attestation( Attestation(
data: data, data: data,
aggregation_bitfield: participation_bitfield, aggregation_bitfield: aggregation_bitfield,
aggregate_signature: sig aggregate_signature: sig
) )