spec: remove ShardCommittee remnants, add CrosslinkCommittee helper

This commit is contained in:
Jacek Sieka 2019-02-06 20:37:25 +01:00
parent 11da02c34f
commit 2f96c4bade
No known key found for this signature in database
GPG Key ID: 6299FEB3EB6FA465
9 changed files with 34 additions and 45 deletions

View File

@ -307,13 +307,16 @@ proc scheduleEpochActions(node: BeaconNode, epoch: uint64) =
committeesIdx = get_shard_committees_index(nextState, slot)
#for shard in node.beaconState.shard_committees_at_slots[committees_idx]:
for crosslink_committee in get_crosslink_committees_at_slot(node.beaconState, committees_idx):
for crosslink_committee in get_crosslink_committees_at_slot(
node.beaconState, committees_idx):
#for i, validatorIdx in shard.committee:
for i, validatorIdx in crosslink_committee.a:
for i, validatorIdx in crosslink_committee.committee:
let validator = node.getAttachedValidator(validatorIdx)
if validator != nil:
#scheduleAttestation(node, validator, slot, shard.shard, shard.committee.len, i)
scheduleAttestation(node, validator, slot, crosslink_committee.b, crosslink_committee.a.len, i)
scheduleAttestation(
node, validator, slot, crosslink_committee.shard,
crosslink_committee.committee.len, i)
node.lastScheduledEpoch = epoch
let

View File

@ -98,7 +98,7 @@ proc getAttestationsForBlock*(pool: AttestationPool,
for slot in firstSlot .. lastSlot:
let slotDequeIdx = slot.int - pool.startingSlot
if slotDequeIdx >= pool.attestations.len: return
let shardAndComittees = get_shard_committees_at_slot(lastState, slot)
let shardAndComittees = get_crosslink_committees_at_slot(lastState, slot)
for s in shardAndComittees:
if pool.attestations[slotDequeIdx][s.shard].isSome:
result.add pool.attestations[slotDequeIdx][s.shard].get

View File

@ -249,12 +249,12 @@ func get_attestation_participants*(state: BeaconState,
let crosslink_committees = get_crosslink_committees_at_slot(state, attestation_data.slot)
# TODO investigate functional library / approach to help avoid loop bugs
assert any(
assert anyIt(
crosslink_committees,
func (x: tuple[a: seq[ValidatorIndex], b: uint64]): bool = x[1] == attestation_data.shard)
it[1] == attestation_data.shard)
let crosslink_committee = mapIt(
filterIt(crosslink_committees, it.b == attestation_data.shard),
it.a)[0]
filterIt(crosslink_committees, it.shard == attestation_data.shard),
it.committee)[0]
assert len(aggregation_bitfield) == (len(crosslink_committee) + 7) div 8
# Find the participating attesters in the committee

View File

@ -382,15 +382,6 @@ type
shard_block_root*: Eth2Digest ##\
## Shard chain block root
ShardCommittee* = object
shard*: uint64
committee*: seq[ValidatorIndex] ##\
## Committe participants that get to attest to blocks on this shard -
## indices into BeaconState.validator_registry
total_validator_count*: uint64 ##\
## Total validator count (for proofs of custody)
Eth1Data* = object
deposit_root*: Eth2Digest ##\
## Data being voted for
@ -432,6 +423,9 @@ type
DOMAIN_EXIT = 3
DOMAIN_RANDAO = 4
# TODO: not in spec
CrosslinkCommittee* = tuple[committee: seq[ValidatorIndex], shard: uint64]
template epoch*(slot: int|uint64): auto =
slot div EPOCH_LENGTH

View File

@ -73,7 +73,8 @@ func get_current_epoch_committee_count_per_slot(state: BeaconState): uint64 =
)
get_epoch_committee_count(len(current_active_validators))
func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64) : seq[tuple[a: seq[ValidatorIndex], b: uint64]] =
func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64):
seq[CrosslinkCommittee] =
## Returns the list of ``(committee, shard)`` tuples for the ``slot``.
let
@ -119,18 +120,6 @@ func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64) : seq[t
(slot_start_shard + i.uint64) mod SHARD_COUNT
)
func get_shard_committees_at_slot*(
state: BeaconState, slot: uint64): seq[ShardCommittee] =
# TODO temporary adapter; remove when all users gone
# where ShardCommittee is: shard*: uint64 / committee*: seq[ValidatorIndex]
let index = state.get_shard_committees_index(slot)
#state.shard_committees_at_slots[index]
for crosslink_committee in get_crosslink_committees_at_slot(state, slot):
var sac: ShardCommittee
sac.shard = crosslink_committee.b
sac.committee = crosslink_committee.a
result.add sac
func get_beacon_proposer_index*(state: BeaconState, slot: uint64): ValidatorIndex =
## From Casper RPJ mini-spec:
## When slot i begins, validator Vidx is expected

View File

@ -535,21 +535,21 @@ func processEpoch(state: var BeaconState) =
# these closures outside this scope, but still..
let statePtr = state.addr
func attesting_validator_indices(
crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64], shard_block_root: Eth2Digest): seq[ValidatorIndex] =
crosslink_committee: CrosslinkCommittee, shard_block_root: Eth2Digest): seq[ValidatorIndex] =
let shard_block_attestations =
concat(current_epoch_attestations, previous_epoch_attestations).
filterIt(it.data.shard == crosslink_committee.b and
filterIt(it.data.shard == crosslink_committee.shard and
it.data.shard_block_root == shard_block_root)
get_attester_indices(statePtr[], shard_block_attestations)
func winning_root(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): Eth2Digest =
func winning_root(crosslink_committee: CrosslinkCommittee): Eth2Digest =
# * Let `winning_root(crosslink_committee)` be equal to the value of
# `shard_block_root` such that
# `sum([get_effective_balance(state, i) for i in attesting_validator_indices(crosslink_committee, shard_block_root)])`
# is maximized (ties broken by favoring lower `shard_block_root` values).
let candidates =
concat(current_epoch_attestations, previous_epoch_attestations).
filterIt(it.data.shard == crosslink_committee.b).
filterIt(it.data.shard == crosslink_committee.shard).
mapIt(it.data.shard_block_root)
# TODO not covered by spec!
@ -568,18 +568,18 @@ func processEpoch(state: var BeaconState) =
max_val = val
max_hash
func attesting_validators(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): seq[ValidatorIndex] =
func attesting_validators(crosslink_committee: CrosslinkCommittee): seq[ValidatorIndex] =
attesting_validator_indices(crosslink_committee, winning_root(crosslink_committee))
func attesting_validator_indices(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): seq[ValidatorIndex] =
func attesting_validator_indices(crosslink_committee: CrosslinkCommittee): seq[ValidatorIndex] =
attesting_validator_indices(crosslink_committee, winning_root(crosslink_committee))
func total_attesting_balance(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): uint64 =
func total_attesting_balance(crosslink_committee: CrosslinkCommittee): uint64 =
sum_effective_balances(
statePtr[], attesting_validator_indices(crosslink_committee))
func total_balance_sac(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): uint64 =
sum_effective_balances(statePtr[], crosslink_committee.a)
func total_balance_sac(crosslink_committee: CrosslinkCommittee): uint64 =
sum_effective_balances(statePtr[], crosslink_committee.committee)
block: # Eth1 data
if state.slot mod ETH1_DATA_VOTING_PERIOD == 0:
@ -714,9 +714,12 @@ func processEpoch(state: var BeaconState) =
for crosslink_committee in crosslink_committees_at_slot:
# TODO https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#crosslinks-1
# but this is a best guess based on reasonableness of what "index" is
for index in crosslink_committee.a:
for index in crosslink_committee.committee:
if index in attesting_validators(crosslink_committee):
state.validator_balances[index.int] += base_reward(state, index) * total_attesting_balance(crosslink_committee) div total_balance_sac(crosslink_committee)
state.validator_balances[index.int] +=
base_reward(state, index) *
total_attesting_balance(crosslink_committee) div
total_balance_sac(crosslink_committee)
else:
# TODO underflows?
state.validator_balances[index] -= base_reward(state, index)

View File

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

View File

@ -54,7 +54,7 @@ cli do(slots = 1945,
# attesterRatio is the fraction of attesters that actually do their
# work for every slot - we'll randimize it deterministically to give
# some variation
let scass = get_shard_committees_at_slot(state, state.slot)
let scass = get_crosslink_committees_at_slot(state, state.slot)
for scas in scass:
var

View File

@ -169,7 +169,7 @@ proc makeBlock*(
addBlock(next_state, previous_block_root, body)
proc find_shard_committee(
sacs: openArray[ShardCommittee], validator_index: ValidatorIndex): ShardCommittee =
sacs: openArray[CrosslinkCommittee], validator_index: ValidatorIndex): CrosslinkCommittee =
for sac in sacs:
if validator_index in sac.committee: return sac
doAssert false
@ -179,7 +179,7 @@ proc makeAttestation*(
validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation =
let
sac = find_shard_committee(
get_shard_committees_at_slot(state, state.slot), validator_index)
get_crosslink_committees_at_slot(state, state.slot), validator_index)
validator = state.validator_registry[validator_index]
sac_index = sac.committee.find(validator_index)