scaffolding to remove last vestiges of get_committee_count(...), SHARD_COUNT, and rest of that 0.8.x infrastructure; this demonstrates equivalence between two, before discarding old code

This commit is contained in:
Dustin Brody 2019-11-14 18:37:51 +01:00
parent f98dd9b1bf
commit c08698bffe
No known key found for this signature in database
GPG Key ID: 3D7A11A0156519DC
6 changed files with 36 additions and 22 deletions

View File

@ -547,20 +547,28 @@ proc handleAttestations(node: BeaconNode, head: BlockRef, slot: Slot) =
let let
epoch = compute_epoch_at_slot(slot) epoch = compute_epoch_at_slot(slot)
committees_per_slot = committees_per_slot =
# get_committee_count_at_slot(state, slot)
get_committee_count_at_slot(state, epoch.compute_start_slot_at_epoch) get_committee_count_at_slot(state, epoch.compute_start_slot_at_epoch)
offset = committees_per_slot * (slot mod SLOTS_PER_EPOCH) offset = committees_per_slot * (slot mod SLOTS_PER_EPOCH)
slot_start_shard = (get_start_shard(state, epoch) + offset) mod SHARD_COUNT slot_start_shard = (get_start_shard(state, epoch) + offset) mod SHARD_COUNT
for i in 0'u64..<committees_per_slot: for committee_index in 0'u64..<committees_per_slot:
let let
shard = Shard((slot_start_shard + i) mod SHARD_COUNT) shard = Shard((slot_start_shard + committee_index) mod SHARD_COUNT)
committee = get_crosslink_committee(state, epoch, shard, cache) shard_committee = get_crosslink_committee(state, epoch, shard, cache)
committee = get_beacon_committee(state, slot, committee_index, cache)
for i, validatorIdx in committee: for index_in_committee, validatorIdx in committee:
let validator = node.getAttachedValidator(state, validatorIdx) let validator = node.getAttachedValidator(state, validatorIdx)
if validator != nil: if validator != nil:
let ad = makeAttestationData(state, shard, blck.root) let ad = makeAttestationData(state, slot, committee_index, blck.root)
attestations.add((ad, committee.len, i, validator)) let
(hm_slot, hm_index) = get_slot_and_index(state, epoch, shard)
shard_ad = makeAttestationData(state, hm_slot, hm_index, blck.root)
doAssert slot == hm_slot
doAssert hm_index == committee_index
attestations.add((ad, committee.len, index_in_committee, validator))
#attestations.add((shard_ad, committee.len, index_in_committee, validator))
for a in attestations: for a in attestations:
traceAsyncErrors sendAttestation( traceAsyncErrors sendAttestation(

View File

@ -487,7 +487,7 @@ proc process_attestation*(
false false
proc makeAttestationData*( proc makeAttestationData*(
state: BeaconState, shard: uint64, state: BeaconState, slot: Slot, committee_index: uint64,
beacon_block_root: Eth2Digest): AttestationData = beacon_block_root: Eth2Digest): AttestationData =
## Create an attestation / vote for the block `beacon_block_root` using the ## Create an attestation / vote for the block `beacon_block_root` using the
## data in `state` to fill in the rest of the fields. ## data in `state` to fill in the rest of the fields.
@ -502,11 +502,12 @@ proc makeAttestationData*(
epoch_boundary_block_root = epoch_boundary_block_root =
if start_slot == state.slot: beacon_block_root if start_slot == state.slot: beacon_block_root
else: get_block_root_at_slot(state, start_slot) else: get_block_root_at_slot(state, start_slot)
(a_slot, a_index) = get_slot_and_index(state, current_epoch, shard)
doAssert slot.compute_epoch_at_slot == current_epoch
AttestationData( AttestationData(
slot: a_slot, slot: slot,
index: a_index, index: committee_index,
beacon_block_root: beacon_block_root, beacon_block_root: beacon_block_root,
source: state.current_justified_checkpoint, source: state.current_justified_checkpoint,
target: Checkpoint( target: Checkpoint(

View File

@ -6,9 +6,11 @@ NODE_ID=${1}
shift shift
# Read in variables # Read in variables
# shellcheck source=/dev/null
source "$(dirname "$0")/vars.sh" source "$(dirname "$0")/vars.sh"
# set up the environment # set up the environment
# shellcheck source=/dev/null
source "${SIM_ROOT}/../../env.sh" source "${SIM_ROOT}/../../env.sh"
cd "$GIT_ROOT" cd "$GIT_ROOT"

View File

@ -3,9 +3,11 @@
set -eo pipefail set -eo pipefail
# Read in variables # Read in variables
# shellcheck source=/dev/null
source "$(dirname "$0")/vars.sh" source "$(dirname "$0")/vars.sh"
# set up the environment # set up the environment
# shellcheck source=/dev/null
source "${SIM_ROOT}/../../env.sh" source "${SIM_ROOT}/../../env.sh"
cd "$SIM_ROOT" cd "$SIM_ROOT"

View File

@ -31,21 +31,21 @@ import
suite "[Unit - Spec - Genesis] Genesis block checks " & preset(): suite "[Unit - Spec - Genesis] Genesis block checks " & preset():
test "is_valid_genesis_state for a valid state": test "is_valid_genesis_state for a valid state":
let state = initGenesisState( discard initGenesisState(
num_validators = MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, num_validators = MIN_GENESIS_ACTIVE_VALIDATOR_COUNT,
genesis_time = MIN_GENESIS_TIME genesis_time = MIN_GENESIS_TIME
) )
discard "TODO" discard "TODO"
test "Invalid genesis time": test "Invalid genesis time":
let state = initGenesisState( discard initGenesisState(
num_validators = MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, num_validators = MIN_GENESIS_ACTIVE_VALIDATOR_COUNT,
genesis_time = MIN_GENESIS_TIME.uint64 - 1 genesis_time = MIN_GENESIS_TIME.uint64 - 1
) )
discard "TODO" discard "TODO"
test "Not enough validators": test "Not enough validators":
let state = initGenesisState( discard initGenesisState(
num_validators = MIN_GENESIS_ACTIVE_VALIDATOR_COUNT.uint64 - 1, num_validators = MIN_GENESIS_ACTIVE_VALIDATOR_COUNT.uint64 - 1,
genesis_time = MIN_GENESIS_TIME.uint64 - 1 genesis_time = MIN_GENESIS_TIME.uint64 - 1
) )

View File

@ -157,28 +157,29 @@ proc makeBlock*(
var next_state = state var next_state = state
addBlock(next_state, previous_block_root, body) addBlock(next_state, previous_block_root, body)
proc find_shard_committee( proc find_beacon_committee(
state: BeaconState, validator_index: ValidatorIndex): auto = state: BeaconState, validator_index: ValidatorIndex): auto =
let epoch = compute_epoch_at_slot(state.slot) let epoch = compute_epoch_at_slot(state.slot)
var cache = get_empty_per_epoch_cache() var cache = get_empty_per_epoch_cache()
for shard in 0'u64 ..< get_committee_count_at_slot( for epoch_committee_index in 0'u64 ..< get_committee_count_at_slot(
state, epoch.compute_start_slot_at_epoch) * SLOTS_PER_EPOCH: state, epoch.compute_start_slot_at_epoch) * SLOTS_PER_EPOCH:
let committee = get_crosslink_committee(state, epoch, let
(shard + get_start_shard(state, epoch)) mod SHARD_COUNT, cache) slot = ((epoch_committee_index mod SLOTS_PER_EPOCH) +
epoch.compute_start_slot_at_epoch.uint64).Slot
index = epoch_committee_index div SLOTS_PER_EPOCH
committee = get_beacon_committee(state, slot, index, cache)
if validator_index in committee: if validator_index in committee:
return (committee, shard) return (committee, slot, index)
doAssert false doAssert false
proc makeAttestation*( proc makeAttestation*(
state: BeaconState, beacon_block_root: Eth2Digest, state: BeaconState, beacon_block_root: Eth2Digest,
validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation = validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation =
let let
(committee, shard) = find_shard_committee(state, validator_index) (committee, slot, index) = find_beacon_committee(state, validator_index)
validator = state.validators[validator_index] validator = state.validators[validator_index]
sac_index = committee.find(validator_index) sac_index = committee.find(validator_index)
data = makeAttestationData(state, data = makeAttestationData(state, slot, index, beacon_block_root)
(shard + get_start_shard(state, compute_epoch_at_slot(state.slot))) mod
SHARD_COUNT, beacon_block_root)
doAssert sac_index != -1, "find_shard_committee should guarantee this" doAssert sac_index != -1, "find_shard_committee should guarantee this"