mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-12 07:14:20 +00:00
spec: remove ShardCommittee remnants, add CrosslinkCommittee helper
This commit is contained in:
parent
11da02c34f
commit
2f96c4bade
@ -307,13 +307,16 @@ proc scheduleEpochActions(node: BeaconNode, epoch: uint64) =
|
|||||||
committeesIdx = get_shard_committees_index(nextState, slot)
|
committeesIdx = get_shard_committees_index(nextState, slot)
|
||||||
|
|
||||||
#for shard in node.beaconState.shard_committees_at_slots[committees_idx]:
|
#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 shard.committee:
|
||||||
for i, validatorIdx in crosslink_committee.a:
|
for i, validatorIdx in crosslink_committee.committee:
|
||||||
let validator = node.getAttachedValidator(validatorIdx)
|
let validator = node.getAttachedValidator(validatorIdx)
|
||||||
if validator != nil:
|
if validator != nil:
|
||||||
#scheduleAttestation(node, validator, slot, shard.shard, shard.committee.len, i)
|
#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
|
node.lastScheduledEpoch = epoch
|
||||||
let
|
let
|
||||||
|
@ -98,7 +98,7 @@ proc getAttestationsForBlock*(pool: AttestationPool,
|
|||||||
for slot in firstSlot .. lastSlot:
|
for slot in firstSlot .. lastSlot:
|
||||||
let slotDequeIdx = slot.int - pool.startingSlot
|
let slotDequeIdx = slot.int - pool.startingSlot
|
||||||
if slotDequeIdx >= pool.attestations.len: return
|
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:
|
for s in shardAndComittees:
|
||||||
if pool.attestations[slotDequeIdx][s.shard].isSome:
|
if pool.attestations[slotDequeIdx][s.shard].isSome:
|
||||||
result.add pool.attestations[slotDequeIdx][s.shard].get
|
result.add pool.attestations[slotDequeIdx][s.shard].get
|
||||||
|
@ -249,12 +249,12 @@ func get_attestation_participants*(state: BeaconState,
|
|||||||
let crosslink_committees = get_crosslink_committees_at_slot(state, attestation_data.slot)
|
let crosslink_committees = get_crosslink_committees_at_slot(state, attestation_data.slot)
|
||||||
|
|
||||||
# TODO investigate functional library / approach to help avoid loop bugs
|
# TODO investigate functional library / approach to help avoid loop bugs
|
||||||
assert any(
|
assert anyIt(
|
||||||
crosslink_committees,
|
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(
|
let crosslink_committee = mapIt(
|
||||||
filterIt(crosslink_committees, it.b == attestation_data.shard),
|
filterIt(crosslink_committees, it.shard == attestation_data.shard),
|
||||||
it.a)[0]
|
it.committee)[0]
|
||||||
assert len(aggregation_bitfield) == (len(crosslink_committee) + 7) div 8
|
assert len(aggregation_bitfield) == (len(crosslink_committee) + 7) div 8
|
||||||
|
|
||||||
# Find the participating attesters in the committee
|
# Find the participating attesters in the committee
|
||||||
|
@ -382,15 +382,6 @@ type
|
|||||||
shard_block_root*: Eth2Digest ##\
|
shard_block_root*: Eth2Digest ##\
|
||||||
## Shard chain block root
|
## 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
|
Eth1Data* = object
|
||||||
deposit_root*: Eth2Digest ##\
|
deposit_root*: Eth2Digest ##\
|
||||||
## Data being voted for
|
## Data being voted for
|
||||||
@ -432,6 +423,9 @@ type
|
|||||||
DOMAIN_EXIT = 3
|
DOMAIN_EXIT = 3
|
||||||
DOMAIN_RANDAO = 4
|
DOMAIN_RANDAO = 4
|
||||||
|
|
||||||
|
# TODO: not in spec
|
||||||
|
CrosslinkCommittee* = tuple[committee: seq[ValidatorIndex], shard: uint64]
|
||||||
|
|
||||||
template epoch*(slot: int|uint64): auto =
|
template epoch*(slot: int|uint64): auto =
|
||||||
slot div EPOCH_LENGTH
|
slot div EPOCH_LENGTH
|
||||||
|
|
||||||
|
@ -73,7 +73,8 @@ func get_current_epoch_committee_count_per_slot(state: BeaconState): uint64 =
|
|||||||
)
|
)
|
||||||
get_epoch_committee_count(len(current_active_validators))
|
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``.
|
## Returns the list of ``(committee, shard)`` tuples for the ``slot``.
|
||||||
|
|
||||||
let
|
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
|
(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 =
|
func get_beacon_proposer_index*(state: BeaconState, slot: uint64): ValidatorIndex =
|
||||||
## From Casper RPJ mini-spec:
|
## From Casper RPJ mini-spec:
|
||||||
## When slot i begins, validator Vidx is expected
|
## When slot i begins, validator Vidx is expected
|
||||||
|
@ -535,21 +535,21 @@ func processEpoch(state: var BeaconState) =
|
|||||||
# these closures outside this scope, but still..
|
# these closures outside this scope, but still..
|
||||||
let statePtr = state.addr
|
let statePtr = state.addr
|
||||||
func attesting_validator_indices(
|
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 =
|
let shard_block_attestations =
|
||||||
concat(current_epoch_attestations, previous_epoch_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)
|
it.data.shard_block_root == shard_block_root)
|
||||||
get_attester_indices(statePtr[], shard_block_attestations)
|
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
|
# * Let `winning_root(crosslink_committee)` be equal to the value of
|
||||||
# `shard_block_root` such that
|
# `shard_block_root` such that
|
||||||
# `sum([get_effective_balance(state, i) for i in attesting_validator_indices(crosslink_committee, shard_block_root)])`
|
# `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).
|
# is maximized (ties broken by favoring lower `shard_block_root` values).
|
||||||
let candidates =
|
let candidates =
|
||||||
concat(current_epoch_attestations, previous_epoch_attestations).
|
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)
|
mapIt(it.data.shard_block_root)
|
||||||
|
|
||||||
# TODO not covered by spec!
|
# TODO not covered by spec!
|
||||||
@ -568,18 +568,18 @@ func processEpoch(state: var BeaconState) =
|
|||||||
max_val = val
|
max_val = val
|
||||||
max_hash
|
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))
|
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))
|
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(
|
sum_effective_balances(
|
||||||
statePtr[], attesting_validator_indices(crosslink_committee))
|
statePtr[], attesting_validator_indices(crosslink_committee))
|
||||||
|
|
||||||
func total_balance_sac(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): uint64 =
|
func total_balance_sac(crosslink_committee: CrosslinkCommittee): uint64 =
|
||||||
sum_effective_balances(statePtr[], crosslink_committee.a)
|
sum_effective_balances(statePtr[], crosslink_committee.committee)
|
||||||
|
|
||||||
block: # Eth1 data
|
block: # Eth1 data
|
||||||
if state.slot mod ETH1_DATA_VOTING_PERIOD == 0:
|
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:
|
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
|
# 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
|
# 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):
|
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:
|
else:
|
||||||
# TODO underflows?
|
# TODO underflows?
|
||||||
state.validator_balances[index] -= base_reward(state, index)
|
state.validator_balances[index] -= base_reward(state, index)
|
||||||
|
@ -16,7 +16,7 @@ proc stateSize(deposits: int, maxContent = false) =
|
|||||||
let
|
let
|
||||||
crosslink_committees = get_crosslink_committees_at_slot(state, 0)
|
crosslink_committees = get_crosslink_committees_at_slot(state, 0)
|
||||||
validatorsPerCommittee =
|
validatorsPerCommittee =
|
||||||
len(crosslink_committees[0].a) # 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.participation_bitfield.setLen(validatorsPerCommittee)
|
||||||
echo "Validators: ", deposits, ", total: ", state.serialize().len
|
echo "Validators: ", deposits, ", total: ", state.serialize().len
|
||||||
|
@ -54,7 +54,7 @@ cli do(slots = 1945,
|
|||||||
# attesterRatio is the fraction of attesters that actually do their
|
# attesterRatio is the fraction of attesters that actually do their
|
||||||
# work for every slot - we'll randimize it deterministically to give
|
# work for every slot - we'll randimize it deterministically to give
|
||||||
# some variation
|
# 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:
|
for scas in scass:
|
||||||
var
|
var
|
||||||
|
@ -169,7 +169,7 @@ proc makeBlock*(
|
|||||||
addBlock(next_state, previous_block_root, body)
|
addBlock(next_state, previous_block_root, body)
|
||||||
|
|
||||||
proc find_shard_committee(
|
proc find_shard_committee(
|
||||||
sacs: openArray[ShardCommittee], validator_index: ValidatorIndex): ShardCommittee =
|
sacs: openArray[CrosslinkCommittee], validator_index: ValidatorIndex): CrosslinkCommittee =
|
||||||
for sac in sacs:
|
for sac in sacs:
|
||||||
if validator_index in sac.committee: return sac
|
if validator_index in sac.committee: return sac
|
||||||
doAssert false
|
doAssert false
|
||||||
@ -179,7 +179,7 @@ proc makeAttestation*(
|
|||||||
validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation =
|
validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation =
|
||||||
let
|
let
|
||||||
sac = find_shard_committee(
|
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]
|
validator = state.validator_registry[validator_index]
|
||||||
sac_index = sac.committee.find(validator_index)
|
sac_index = sac.committee.find(validator_index)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user