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
# 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
# TODO: A bitfield type that tracks that information
# https://github.com/status-im/nim-beacon-chain/issues/19
for validatorIdx in 0 ..< participation_bitfield.len * 8:
result += int participation_bitfield.get_bitfield_bit(validatorIdx)
for validatorIdx in 0 ..< aggregation_bitfield.len * 8:
result += int aggregation_bitfield.get_bitfield_bit(validatorIdx)
func getAttestationVoteCount(pool: AttestationPool, current_slot: int): CountTable[Eth2Digest] =
## 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)
# 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 =
# Return the starting slot of the given ``epoch``.
epoch * EPOCH_LENGTH
proc checkAttestation*(

View File

@ -12,9 +12,6 @@
# specification and that follows the spec as closely as possible, so as to make
# 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
# 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
@ -356,51 +353,62 @@ type
latest_eth1_data*: Eth1Data
eth1_data_votes*: seq[Eth1DataVote]
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#validator
Validator* = object
pubkey*: ValidatorPubKey
withdrawal_credentials*: Eth2Digest
pubkey*: ValidatorPubKey ##\
## BLS public key
withdrawal_credentials*: Eth2Digest ##\
## Withdrawal credentials
activation_epoch*: EpochNumber ##\
## Slot when validator activated
## Epoch when validator activated
exit_epoch*: EpochNumber ##\
## Slot when validator exited
## Epoch when validator exited
withdrawal_epoch*: EpochNumber ##\
## Slot when validator withdrew
## Epoch when validator withdrew
penalized_epoch*: EpochNumber ##\
## Slot when validator penalized
## Epoch when validator penalized
status_flags*: uint64
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#crosslink
Crosslink* = object
epoch*: uint64
shard_block_root*: Eth2Digest ##\
## Shard chain block root
Eth1Data* = object
deposit_root*: Eth2Digest ##\
## Data being voted for
vote_count*: Eth2Digest ##\
## Vote count
Eth1DataVote* = object
eth1_data*: Eth1Data
vote_count*: uint64 # Vote count
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#pendingattestation
PendingAttestation* = object
data*: AttestationData # Signed data
participation_bitfield*: seq[byte] # Attester participation bitfield
custody_bitfield*: seq[byte] # Proof of custody bitfield
slot_included*: uint64 # Slot in which it was included
aggregation_bitfield*: seq[byte] # Attester participation bitfield
data*: AttestationData # Attestation data
custody_bitfield*: seq[byte] # Custody bitfield
inclusion_slot*: uint64 # Inclusion slot
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#fork
Fork* = object
previous_version*: uint64 # Previous fork version
current_version*: uint64 # Current fork version
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
latest_registry_delta_root*: Eth2Digest
validator_index*: ValidatorIndex
@ -408,6 +416,8 @@ type
slot*: uint64
flag*: ValidatorSetDeltaFlags
## TODO remove or otherwise conditional-compile this, since it's for light
## client but not in spec
ValidatorSetDeltaFlags* {.pure.} = enum
Activation = 0
Exit = 1

View File

@ -31,6 +31,7 @@ func verify_bitfield*(bitfield: openarray[byte], committee_size: int): bool =
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] =
## Returns the shuffled ``values`` with seed as entropy.
## 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]
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]] =
## split lst in N pieces, with each piece having `len(lst) div N` or
## `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.
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 =
slot div EPOCH_LENGTH
@ -174,10 +177,12 @@ proc is_surround_vote*(attestation_data_1: AttestationData,
(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 =
### Checks if validator is active
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] =
## Gets indices of active validators from 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))
# 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 =
slot_to_epoch(state.slot)

View File

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

View File

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

View File

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