From 2f96c4bade431de57c9e163fdfc9564a8ec7f21d Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Wed, 6 Feb 2019 20:37:25 +0100 Subject: [PATCH] spec: remove ShardCommittee remnants, add CrosslinkCommittee helper --- beacon_chain/beacon_node.nim | 9 ++++++--- beacon_chain/fork_choice.nim | 2 +- beacon_chain/spec/beaconstate.nim | 8 ++++---- beacon_chain/spec/datatypes.nim | 12 +++--------- beacon_chain/spec/validator.nim | 15 ++------------- beacon_chain/state_transition.nim | 25 ++++++++++++++----------- research/serialized_sizes.nim | 2 +- research/state_sim.nim | 2 +- tests/testutil.nim | 4 ++-- 9 files changed, 34 insertions(+), 45 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 1914a529c..5cdcd1c08 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -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 diff --git a/beacon_chain/fork_choice.nim b/beacon_chain/fork_choice.nim index 2156eaba3..c2a5592c9 100644 --- a/beacon_chain/fork_choice.nim +++ b/beacon_chain/fork_choice.nim @@ -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 diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index ed0652ef7..3e453b121 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -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 diff --git a/beacon_chain/spec/datatypes.nim b/beacon_chain/spec/datatypes.nim index 1fa16c91f..d8a5582fd 100644 --- a/beacon_chain/spec/datatypes.nim +++ b/beacon_chain/spec/datatypes.nim @@ -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 diff --git a/beacon_chain/spec/validator.nim b/beacon_chain/spec/validator.nim index cfdca9cc0..79a433eea 100644 --- a/beacon_chain/spec/validator.nim +++ b/beacon_chain/spec/validator.nim @@ -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 diff --git a/beacon_chain/state_transition.nim b/beacon_chain/state_transition.nim index 45a051723..0f67711e2 100644 --- a/beacon_chain/state_transition.nim +++ b/beacon_chain/state_transition.nim @@ -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) diff --git a/research/serialized_sizes.nim b/research/serialized_sizes.nim index d1c73bd2f..154aa59b0 100644 --- a/research/serialized_sizes.nim +++ b/research/serialized_sizes.nim @@ -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 diff --git a/research/state_sim.nim b/research/state_sim.nim index 9b2c80c88..59307d438 100644 --- a/research/state_sim.nim +++ b/research/state_sim.nim @@ -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 diff --git a/tests/testutil.nim b/tests/testutil.nim index 13c6ab72d..0617076f6 100644 --- a/tests/testutil.nim +++ b/tests/testutil.nim @@ -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)