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

@ -178,13 +178,13 @@ suite "Gossip validation " & preset():
suite "Gossip validation - Extra": # Not based on preset config
test "validateSyncCommitteeMessage":
const num_validators = SLOTS_PER_EPOCH
let
let
cfg = block:
var cfg = defaultRuntimeConfig
cfg.ALTAIR_FORK_EPOCH = (GENESIS_EPOCH + 1).Epoch
cfg
dag = block:
let
let
dag = ChainDAGRef.init(cfg, makeTestDB(num_validators), {})
taskpool = Taskpool.new()
quarantine = QuarantineRef.init(keys.newRng(), taskpool)
@ -192,7 +192,7 @@ suite "Gossip validation - Extra": # Not based on preset config
for blck in makeTestBlocks(
dag.headState.data, dag.head.root, cache,
int(SLOTS_PER_EPOCH), false, cfg = cfg):
let added =
let added =
case blck.kind
of BeaconBlockFork.Phase0:
const nilCallback = OnPhase0BlockAdded(nil)
@ -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)
@ -225,7 +225,7 @@ suite "Gossip validation - Extra": # Not based on preset config
syncCommitteeMsgPool = newClone(SyncCommitteeMsgPool.init())
res = validateSyncCommitteeMessage(
dag, syncCommitteeMsgPool, msg, syncCommitteeIdx,
dag, syncCommitteeMsgPool, msg, syncCommitteeIdx,
state[].data.slot.toBeaconTime(), true)
contribution = block:
var contribution: SyncCommitteeContribution