harden sync sub committee selection (#2965)

* harden sync sub committee selection

also turn it into an iterator

* fix test and warning
This commit is contained in:
Jacek Sieka 2021-10-07 15:19:47 +02:00 committed by GitHub
parent dfb87f1d5b
commit fabec894dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 19 deletions

View File

@ -1027,19 +1027,16 @@ proc pruneBlocksDAG(dag: ChainDAGRef) =
prunedHeads = hlen - dag.heads.len,
dagPruneDur = Moment.now() - startTick
func syncSubcommittee*(syncCommittee: openarray[ValidatorPubKey],
committeeIdx: SyncCommitteeIndex): seq[ValidatorPubKey] =
## TODO Return a view type
## Unfortunately, this doesn't work as a template right now.
if syncCommittee.len == 0:
return @[]
iterator syncSubcommittee*(
syncCommittee: openarray[ValidatorPubKey],
committeeIdx: SyncCommitteeIndex): ValidatorPubKey =
var
i = committeeIdx.asInt * SYNC_SUBCOMMITTEE_SIZE
onePastEndIdx = min(syncCommittee.len, i + SYNC_SUBCOMMITTEE_SIZE)
let
startIdx = committeeIdx.asInt * SYNC_SUBCOMMITTEE_SIZE
onePastEndIdx = startIdx + SYNC_SUBCOMMITTEE_SIZE
doAssert startIdx < syncCommittee.len
@(toOpenArray(syncCommittee, startIdx, onePastEndIdx - 1))
while i < onePastEndIdx:
yield syncCommittee[i]
inc i
func syncCommitteeParticipants*(dagParam: ChainDAGRef,
slotParam: Slot): seq[ValidatorPubKey] =
@ -1081,7 +1078,7 @@ func getSubcommitteePositionsAux(
return @[]
let validatorPubKey = validatorKey.get().toPubKey
for pos, key in syncCommittee.syncSubcommittee(committeeIdx):
for pos, key in toSeq(syncCommittee.syncSubcommittee(committeeIdx)):
if validatorPubKey == key:
result.add uint64(pos)

View File

@ -68,7 +68,7 @@ type
of BeaconBlockFork.Merge:
mergeBlock*: merge.TrustedSignedBeaconBlock
ForkDigests* {.requiresInit.} = object
ForkDigests* = object
phase0*: ForkDigest
altair*: ForkDigest
merge*: ForkDigest

View File

@ -210,7 +210,7 @@ suite "Gossip validation - Extra": # Not based on preset config
syncCommitteeIdx = 0.SyncCommitteeIndex
syncCommittee = @(dag.syncCommitteeParticipants(state[].data.slot))
subcommittee = syncCommittee.syncSubcommittee(syncCommitteeIdx)
subcommittee = toSeq(syncCommittee.syncSubcommittee(syncCommitteeIdx))
pubkey = subcommittee[0]
expectedCount = subcommittee.count(pubkey)