Skip to content

Commit

Permalink
update get_crosslink_committee(...) to 0.8.0 and fix #307
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Jul 2, 2019
1 parent bf037ea commit 8672560
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion beacon_chain/beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ proc handleAttestations(node: BeaconNode, head: BlockRef, slot: Slot) =
node.blockPool.withState(node.stateCache, attestationHead):
var cache = get_empty_per_epoch_cache()
let epoch = compute_epoch_of_slot(slot)
for committee_index in 0'u64 ..< get_epoch_committee_count(state, epoch):
for committee_index in 0'u64 ..< get_committee_count(state, epoch):
## TODO verify that this is the correct mapping; it's consistent with
## other code
let
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func get_attestation_data_slot*(state: BeaconState,
func get_attestation_data_slot*(state: BeaconState,
data: AttestationData): Slot =
get_attestation_data_slot(
state, data, get_epoch_committee_count(state, data.target_epoch))
state, data, get_committee_count(state, data.target_epoch))

# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#get_block_root_at_slot
func get_block_root_at_slot*(state: BeaconState,
Expand Down
8 changes: 4 additions & 4 deletions beacon_chain/spec/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func get_active_validator_indices*(state: BeaconState, epoch: Epoch):
if is_active_validator(val, epoch):
result.add idx.ValidatorIndex

# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_epoch_committee_count
func get_epoch_committee_count*(state: BeaconState, epoch: Epoch): uint64 =
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_committee_count
func get_committee_count*(state: BeaconState, epoch: Epoch): uint64 =
# Return the number of committees at ``epoch``.
let active_validator_indices = get_active_validator_indices(state, epoch)
clamp(
Expand Down Expand Up @@ -174,8 +174,8 @@ func get_domain*(
func get_domain*(state: BeaconState, domain_type: DomainType): Domain =
get_domain(state, domain_type, get_current_epoch(state))

# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#generate_seed
func generate_seed*(state: BeaconState, epoch: Epoch): Eth2Digest =
# https://github.com/ethereum/eth2.0-specs/blob/v0.6.3/specs/core/0_beacon-chain.md#get_seed
func get_seed*(state: BeaconState, epoch: Epoch): Eth2Digest =
# Generate a seed for the given ``epoch``.

var seed_input : array[32*3, byte]
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/state_transition_block.nim
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ proc processAttestations(
committee_count = if epoch in committee_count_cache:
committee_count_cache[epoch]
else:
get_epoch_committee_count(state, epoch)
get_committee_count(state, epoch)
committee_count_cache[epoch] = committee_count

# Spec content
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/spec/state_transition_epoch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func process_crosslinks(state: var BeaconState, stateCache: var StateCache) =
# This issue comes up regularly -- iterating means an int type,
# which then needs re-conversion back to specialized type.
let epoch = epoch_int.Epoch
for offset in 0'u64 ..< get_epoch_committee_count(state, epoch):
for offset in 0'u64 ..< get_committee_count(state, epoch):
let
shard = (get_start_shard(state, epoch) + offset) mod SHARD_COUNT
crosslink_committee =
Expand Down Expand Up @@ -325,7 +325,7 @@ func get_crosslink_deltas(state: BeaconState, cache: var StateCache):
rewards = repeat(0'u64, len(state.validators))
penalties = repeat(0'u64, len(state.validators))
let epoch = get_previous_epoch(state)
for offset in 0'u64 ..< get_epoch_committee_count(state, epoch):
for offset in 0'u64 ..< get_committee_count(state, epoch):
let
shard = (get_start_shard(state, epoch) + offset) mod SHARD_COUNT
crosslink_committee =
Expand Down
13 changes: 6 additions & 7 deletions beacon_chain/spec/validator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func get_previous_epoch*(state: BeaconState): Epoch =
func get_shard_delta*(state: BeaconState, epoch: Epoch): uint64 =
## Return the number of shards to increment ``state.start_shard``
## during ``epoch``.
min(get_epoch_committee_count(state, epoch),
min(get_committee_count(state, epoch),
(SHARD_COUNT - SHARD_COUNT div SLOTS_PER_EPOCH).uint64)

# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#get_start_shard
Expand Down Expand Up @@ -136,14 +136,13 @@ func compute_committee(indices: seq[ValidatorIndex], seed: Eth2Digest,
start.int .. (endIdx.int-1),
indices[stateCache.crosslink_committee_cache[key][it]])

# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#get_crosslink_committee
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#get_crosslink_committee
func get_crosslink_committee*(state: BeaconState, epoch: Epoch, shard: Shard,
stateCache: var StateCache): seq[ValidatorIndex] =

doAssert shard >= 0'u64
# This seems to be required, basically, to be true? But I'm not entirely sure
#doAssert shard >= get_start_shard(state, epoch)
doAssert shard < SHARD_COUNT

## This is a somewhat more fragile, but high-ROI, caching setup --
## get_active_validator_indices() is slow to run in a loop and only
Expand All @@ -154,9 +153,9 @@ func get_crosslink_committee*(state: BeaconState, epoch: Epoch, shard: Shard,

compute_committee(
stateCache.active_validator_indices_cache[epoch],
generate_seed(state, epoch),
get_seed(state, epoch),
(shard + SHARD_COUNT - get_start_shard(state, epoch)) mod SHARD_COUNT,
get_epoch_committee_count(state, epoch),
get_committee_count(state, epoch),
stateCache
)

Expand All @@ -177,11 +176,11 @@ func get_beacon_proposer_index*(state: BeaconState, stateCache: var StateCache):
let
epoch = get_current_epoch(state)
committees_per_slot =
get_epoch_committee_count(state, epoch) div SLOTS_PER_EPOCH
get_committee_count(state, epoch) div SLOTS_PER_EPOCH
offset = committees_per_slot * (state.slot mod SLOTS_PER_EPOCH)
shard = (get_start_shard(state, epoch) + offset) mod SHARD_COUNT
first_committee = get_crosslink_committee(state, epoch, shard, stateCache)
seed = generate_seed(state, epoch)
seed = get_seed(state, epoch)

var
i = 0
Expand Down
2 changes: 1 addition & 1 deletion research/state_sim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ cli do(slots = 448,
epoch = compute_epoch_of_slot(state.slot)
scass = withTimerRet(timers[tShuffle]):
mapIt(
0'u64 .. (get_epoch_committee_count(state, epoch) - 1),
0'u64 .. (get_committee_count(state, epoch) - 1),
get_crosslink_committee(state, epoch,
(it + get_start_shard(state, epoch)) mod SHARD_COUNT,
cache))
Expand Down
2 changes: 1 addition & 1 deletion tests/testutil.nim
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ proc find_shard_committee(
state: BeaconState, validator_index: ValidatorIndex): auto =
let epoch = compute_epoch_of_slot(state.slot)
var cache = get_empty_per_epoch_cache()
for shard in 0'u64 ..< get_epoch_committee_count(state, epoch):
for shard in 0'u64 ..< get_committee_count(state, epoch):
let committee = get_crosslink_committee(state, epoch,
(shard + get_start_shard(state, epoch)) mod SHARD_COUNT, cache)
if validator_index in committee:
Expand Down

0 comments on commit 8672560

Please sign in to comment.