2021-08-28 10:40:01 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-08-28 10:40:01 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
import
|
2023-05-17 04:55:55 +00:00
|
|
|
std/[algorithm, sequtils, sets, tables],
|
2021-12-03 15:04:58 +00:00
|
|
|
stew/shims/hashes,
|
2022-01-24 20:40:59 +00:00
|
|
|
eth/p2p/discoveryv5/random2,
|
2021-08-28 10:40:01 +00:00
|
|
|
chronicles,
|
2023-05-17 04:55:55 +00:00
|
|
|
../spec/[crypto, digest, forks],
|
2021-10-19 14:09:26 +00:00
|
|
|
../spec/datatypes/altair
|
|
|
|
|
|
|
|
export hashes, sets, tables, altair
|
2021-08-30 01:00:37 +00:00
|
|
|
|
2023-08-05 13:12:44 +00:00
|
|
|
logScope:
|
|
|
|
topics = "syncpool"
|
|
|
|
|
2021-08-30 01:00:37 +00:00
|
|
|
const
|
|
|
|
syncCommitteeMsgsRetentionSlots = 3
|
|
|
|
## How many slots to retain sync committee
|
|
|
|
## messsages before discarding them.
|
|
|
|
|
2021-10-19 14:09:26 +00:00
|
|
|
type
|
2021-11-05 15:39:47 +00:00
|
|
|
SyncCommitteeMsgKey = object
|
2023-05-17 04:55:55 +00:00
|
|
|
originator: uint64 # ValidatorIndex to avoid invalid values
|
2021-11-05 15:39:47 +00:00
|
|
|
slot: Slot
|
2023-05-17 04:55:55 +00:00
|
|
|
subcommitteeIdx: uint64 # SyncSubcommitteeIndex to avoid invalid values
|
2021-10-19 14:09:26 +00:00
|
|
|
|
|
|
|
TrustedSyncCommitteeMsg* = object
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx*: SyncSubcommitteeIndex
|
2021-10-19 14:09:26 +00:00
|
|
|
positionInCommittee*: uint64
|
|
|
|
signature*: CookedSig
|
|
|
|
|
|
|
|
BestSyncSubcommitteeContribution* = object
|
|
|
|
totalParticipants*: int
|
|
|
|
participationBits*: SyncCommitteeAggregationBits
|
|
|
|
signature*: CookedSig
|
|
|
|
|
|
|
|
BestSyncSubcommitteeContributions* = object
|
|
|
|
subnets*: array[SYNC_COMMITTEE_SUBNET_COUNT,
|
|
|
|
BestSyncSubcommitteeContribution]
|
|
|
|
|
|
|
|
OnSyncContributionCallback* =
|
2023-08-25 09:29:07 +00:00
|
|
|
proc(data: SignedContributionAndProof) {.gcsafe, raises: [].}
|
2021-10-19 14:09:26 +00:00
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
# Messages from different slots / forks may sign the same beacon block root.
|
|
|
|
# Messages across slots are compatible, but not across forks (signing root).
|
|
|
|
# Messages from different periods have different signers, so are incompatible.
|
|
|
|
# Note that the sync committee is determined by `message.slot + 1`, the fork
|
|
|
|
# is determined by `message.slot`, and both can be different from `bid.slot`.
|
|
|
|
SyncMsgTarget = object
|
|
|
|
bid: BlockId # Based on message `beacon_block_root`
|
|
|
|
period: SyncCommitteePeriod # Based on message `slot + 1`
|
|
|
|
fork: ConsensusFork # Based on message `slot`
|
|
|
|
|
2021-10-19 14:09:26 +00:00
|
|
|
SyncCommitteeMsgPool* = object
|
2023-05-17 04:55:55 +00:00
|
|
|
seenSyncMsgByAuthor*: Table[SyncCommitteeMsgKey, Eth2Digest]
|
2021-10-19 14:09:26 +00:00
|
|
|
seenContributionByAuthor*: HashSet[SyncCommitteeMsgKey]
|
2023-05-17 04:55:55 +00:00
|
|
|
syncMessages*: Table[SyncMsgTarget, seq[TrustedSyncCommitteeMsg]]
|
|
|
|
bestContributions*: Table[SyncMsgTarget, BestSyncSubcommitteeContributions]
|
2021-10-19 14:09:26 +00:00
|
|
|
onContributionReceived*: OnSyncContributionCallback
|
|
|
|
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext
|
2023-05-17 04:55:55 +00:00
|
|
|
cfg: RuntimeConfig
|
2022-01-24 20:40:59 +00:00
|
|
|
|
2021-10-19 14:09:26 +00:00
|
|
|
func hash*(x: SyncCommitteeMsgKey): Hash =
|
2021-12-03 15:04:58 +00:00
|
|
|
hashAllFields(x)
|
2021-10-19 14:09:26 +00:00
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
func toSyncMsgTarget(
|
|
|
|
cfg: RuntimeConfig, bid: BlockId, slot: Slot): SyncMsgTarget =
|
|
|
|
SyncMsgTarget(
|
|
|
|
bid: bid,
|
|
|
|
period: (slot + 1).sync_committee_period,
|
|
|
|
fork: cfg.consensusForkAtEpoch(slot.epoch))
|
|
|
|
|
|
|
|
func hash(x: SyncMsgTarget): Hash =
|
|
|
|
hashAllFields(x)
|
|
|
|
|
|
|
|
func `<`(x, y: SyncMsgTarget): bool =
|
|
|
|
if x.bid.slot != y.bid.slot:
|
|
|
|
x.bid.slot < y.bid.slot
|
|
|
|
elif x.period != y.period:
|
|
|
|
x.period < y.period
|
|
|
|
else:
|
|
|
|
x.fork < y.fork
|
|
|
|
|
2021-09-22 12:17:15 +00:00
|
|
|
func init*(T: type SyncCommitteeMsgPool,
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2023-05-17 04:55:55 +00:00
|
|
|
cfg: RuntimeConfig,
|
2021-09-22 12:17:15 +00:00
|
|
|
onSyncContribution: OnSyncContributionCallback = nil
|
|
|
|
): SyncCommitteeMsgPool =
|
2023-05-17 04:55:55 +00:00
|
|
|
T(rng: rng, cfg: cfg, onContributionReceived: onSyncContribution)
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
func pruneData*(pool: var SyncCommitteeMsgPool, slot: Slot, force = false) =
|
2021-08-30 01:00:37 +00:00
|
|
|
## This should be called at the end of slot.
|
|
|
|
clear pool.seenContributionByAuthor
|
|
|
|
clear pool.seenSyncMsgByAuthor
|
|
|
|
|
|
|
|
if slot < syncCommitteeMsgsRetentionSlots:
|
|
|
|
return
|
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
# Messages signing a `beacon_block_root` may remain valid over multiple slots.
|
|
|
|
# Therefore, we filter by the targeted `BlockId` instead of message `slot`.
|
|
|
|
let
|
|
|
|
minSlotToRetain = slot - syncCommitteeMsgsRetentionSlots
|
|
|
|
minEntriesToKeep = if force: 0 else: syncCommitteeMsgsRetentionSlots
|
2021-08-30 01:00:37 +00:00
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
template pruneTable(table: untyped) =
|
|
|
|
if table.len > minEntriesToKeep:
|
|
|
|
var targets = table.keys().toSeq()
|
|
|
|
targets.sort(order = SortOrder.Descending)
|
|
|
|
for i in minEntriesToKeep ..< targets.len:
|
|
|
|
if targets[i].bid.slot < minSlotToRetain:
|
|
|
|
table.del targets[i]
|
2021-08-30 01:00:37 +00:00
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
pruneTable pool.syncMessages
|
|
|
|
pruneTable pool.bestContributions
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
func isSeen*(
|
|
|
|
pool: SyncCommitteeMsgPool,
|
|
|
|
msg: SyncCommitteeMessage,
|
2023-05-17 04:55:55 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
|
|
|
headBid: BlockId): bool =
|
2021-11-05 15:39:47 +00:00
|
|
|
let seenKey = SyncCommitteeMsgKey(
|
2023-05-17 04:55:55 +00:00
|
|
|
originator: msg.validator_index, # Might be unvalidated at this point
|
2021-11-05 15:39:47 +00:00
|
|
|
slot: msg.slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: subcommitteeIdx.uint64)
|
2023-05-17 04:55:55 +00:00
|
|
|
return
|
|
|
|
if seenKey notin pool.seenSyncMsgByAuthor:
|
|
|
|
false
|
|
|
|
elif msg.beacon_block_root == headBid.root:
|
|
|
|
pool.seenSyncMsgByAuthor.getOrDefault(seenKey) == headBid.root
|
|
|
|
else:
|
|
|
|
true
|
2021-11-05 15:39:47 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
proc addSyncCommitteeMessage*(
|
2021-08-28 10:40:01 +00:00
|
|
|
pool: var SyncCommitteeMsgPool,
|
|
|
|
slot: Slot,
|
2023-05-17 04:55:55 +00:00
|
|
|
bid: BlockId,
|
2021-11-05 15:39:47 +00:00
|
|
|
validatorIndex: uint64,
|
2021-08-28 10:40:01 +00:00
|
|
|
signature: CookedSig,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
2023-05-17 04:55:55 +00:00
|
|
|
positionsInCommittee: seq[uint64]) =
|
|
|
|
let seenKey = SyncCommitteeMsgKey(
|
|
|
|
originator: validatorIndex,
|
|
|
|
slot: slot,
|
|
|
|
subcommitteeIdx: subcommitteeIdx.uint64)
|
|
|
|
pool.seenSyncMsgByAuthor[seenKey] = bid.root
|
|
|
|
|
|
|
|
func registerVotes(votes: var seq[TrustedSyncCommitteeMsg]) =
|
|
|
|
for position in positionsInCommittee:
|
|
|
|
block addVote:
|
|
|
|
for vote in votes:
|
|
|
|
if vote.subcommitteeIdx == subcommitteeIdx and
|
|
|
|
vote.positionInCommittee == position:
|
|
|
|
break addVote
|
|
|
|
votes.add TrustedSyncCommitteeMsg(
|
|
|
|
subcommitteeIdx: subcommitteeIdx,
|
|
|
|
positionInCommittee: position,
|
|
|
|
signature: signature)
|
|
|
|
let target = pool.cfg.toSyncMsgTarget(bid, slot)
|
|
|
|
pool.syncMessages.mgetOrPut(target, @[]).registerVotes()
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
debug "Sync committee message resolved",
|
2023-05-17 04:55:55 +00:00
|
|
|
slot = slot, blockRoot = shortLog(target.bid.root), validatorIndex
|
2021-12-09 12:56:54 +00:00
|
|
|
|
2021-08-28 10:40:01 +00:00
|
|
|
func computeAggregateSig(votes: seq[TrustedSyncCommitteeMsg],
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
2021-08-28 10:40:01 +00:00
|
|
|
contribution: var SyncCommitteeContribution): bool =
|
|
|
|
var
|
2022-02-16 22:24:44 +00:00
|
|
|
aggregateSig {.noinit.}: AggregateSignature
|
2021-08-28 10:40:01 +00:00
|
|
|
initialized = false
|
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
contribution.aggregation_bits.reset()
|
2021-08-28 10:40:01 +00:00
|
|
|
for vote in votes:
|
2022-01-08 23:28:49 +00:00
|
|
|
if vote.subcommitteeIdx != subcommitteeIdx:
|
2021-08-28 10:40:01 +00:00
|
|
|
continue
|
|
|
|
|
2021-11-04 18:22:24 +00:00
|
|
|
if not contribution.aggregation_bits[vote.positionInCommittee]:
|
|
|
|
if not initialized:
|
|
|
|
initialized = true
|
|
|
|
aggregateSig.init(vote.signature)
|
|
|
|
else:
|
|
|
|
aggregateSig.aggregate(vote.signature)
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2021-11-04 18:22:24 +00:00
|
|
|
contribution.aggregation_bits.setBit vote.positionInCommittee
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
if initialized:
|
|
|
|
contribution.signature = aggregateSig.finish.toValidatorSig
|
2023-05-17 04:55:55 +00:00
|
|
|
else:
|
|
|
|
contribution.signature = ValidatorSig.infinity
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2022-02-21 11:55:56 +00:00
|
|
|
initialized
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
func produceContribution*(
|
|
|
|
pool: SyncCommitteeMsgPool,
|
|
|
|
slot: Slot,
|
2023-05-17 04:55:55 +00:00
|
|
|
headBid: BlockId,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
2021-08-28 10:40:01 +00:00
|
|
|
outContribution: var SyncCommitteeContribution): bool =
|
2023-05-17 04:55:55 +00:00
|
|
|
let target = pool.cfg.toSyncMsgTarget(headBid, slot)
|
|
|
|
if target in pool.syncMessages:
|
2021-08-28 10:40:01 +00:00
|
|
|
outContribution.slot = slot
|
2023-05-17 04:55:55 +00:00
|
|
|
outContribution.beacon_block_root = headBid.root
|
2022-01-08 23:28:49 +00:00
|
|
|
outContribution.subcommittee_index = subcommitteeIdx.asUInt64
|
2021-08-28 10:40:01 +00:00
|
|
|
try:
|
2023-05-17 04:55:55 +00:00
|
|
|
computeAggregateSig(pool.syncMessages[target],
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx,
|
2021-11-04 18:22:24 +00:00
|
|
|
outContribution)
|
2021-08-28 10:40:01 +00:00
|
|
|
except KeyError:
|
|
|
|
raiseAssert "We have checked for the key upfront"
|
|
|
|
else:
|
2021-11-05 15:39:47 +00:00
|
|
|
false
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2024-06-24 09:13:15 +00:00
|
|
|
func addContribution(
|
|
|
|
contributions: var BestSyncSubcommitteeContributions,
|
|
|
|
contribution: SyncCommitteeContribution) =
|
2021-08-30 01:00:37 +00:00
|
|
|
let
|
|
|
|
currentBestTotalParticipants =
|
2024-06-24 09:13:15 +00:00
|
|
|
contributions.subnets[contribution.subcommittee_index].totalParticipants
|
2021-08-30 01:00:37 +00:00
|
|
|
newBestTotalParticipants = countOnes(contribution.aggregation_bits)
|
|
|
|
|
|
|
|
if newBestTotalParticipants > currentBestTotalParticipants:
|
2024-06-24 09:13:15 +00:00
|
|
|
contributions.subnets[contribution.subcommittee_index] =
|
2021-08-28 10:40:01 +00:00
|
|
|
BestSyncSubcommitteeContribution(
|
2021-08-30 01:00:37 +00:00
|
|
|
totalParticipants: newBestTotalParticipants,
|
2021-08-28 10:40:01 +00:00
|
|
|
participationBits: contribution.aggregation_bits,
|
|
|
|
signature: contribution.signature.load.get)
|
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
func isSeen*(
|
|
|
|
pool: SyncCommitteeMsgPool,
|
|
|
|
msg: ContributionAndProof): bool =
|
|
|
|
let seenKey = SyncCommitteeMsgKey(
|
|
|
|
originator: msg.aggregator_index,
|
|
|
|
slot: msg.contribution.slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: msg.contribution.subcommittee_index)
|
2021-11-05 15:39:47 +00:00
|
|
|
seenKey in pool.seenContributionByAuthor
|
|
|
|
|
2023-01-12 14:08:08 +00:00
|
|
|
func covers(
|
2024-06-24 09:13:15 +00:00
|
|
|
contributions: BestSyncSubcommitteeContributions,
|
2023-01-12 14:08:08 +00:00
|
|
|
contribution: SyncCommitteeContribution): bool =
|
|
|
|
contribution.aggregation_bits.isSubsetOf(
|
2024-06-24 09:13:15 +00:00
|
|
|
contributions.subnets[contribution.subcommittee_index].participationBits)
|
2023-01-12 14:08:08 +00:00
|
|
|
|
|
|
|
func covers*(
|
|
|
|
pool: var SyncCommitteeMsgPool,
|
2023-05-17 04:55:55 +00:00
|
|
|
contribution: SyncCommitteeContribution,
|
|
|
|
bid: BlockId): bool =
|
2023-01-12 14:08:08 +00:00
|
|
|
## Return true iff the given contribution brings no new information compared
|
|
|
|
## to the contributions already seen in the pool, ie if the contriubution is a
|
|
|
|
## subset of the best contribution so far
|
2023-05-17 04:55:55 +00:00
|
|
|
let target = pool.cfg.toSyncMsgTarget(bid, contribution.slot)
|
|
|
|
pool.bestContributions.withValue(target, best):
|
2023-01-12 14:08:08 +00:00
|
|
|
return best[].covers(contribution)
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
2021-11-25 12:20:36 +00:00
|
|
|
proc addContribution(pool: var SyncCommitteeMsgPool,
|
2022-08-09 09:52:11 +00:00
|
|
|
aggregator_index: uint64,
|
|
|
|
contribution: SyncCommitteeContribution,
|
2023-05-17 04:55:55 +00:00
|
|
|
bid: BlockId,
|
2023-01-12 14:08:08 +00:00
|
|
|
signature: CookedSig) =
|
2021-11-05 15:39:47 +00:00
|
|
|
let seenKey = SyncCommitteeMsgKey(
|
|
|
|
originator: aggregator_index,
|
|
|
|
slot: contribution.slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: contribution.subcommittee_index)
|
2021-11-05 15:39:47 +00:00
|
|
|
pool.seenContributionByAuthor.incl seenKey
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
let target = pool.cfg.toSyncMsgTarget(bid, contribution.slot)
|
2024-06-24 09:13:15 +00:00
|
|
|
pool.bestContributions.withValue(target, contributions):
|
|
|
|
contributions[].addContribution(contribution)
|
|
|
|
do:
|
|
|
|
var contributions: BestSyncSubcommitteeContributions
|
|
|
|
contributions.addContribution(contribution)
|
|
|
|
pool.bestContributions[target] = contributions
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2021-11-25 12:20:36 +00:00
|
|
|
proc addContribution*(pool: var SyncCommitteeMsgPool,
|
2022-08-09 09:52:11 +00:00
|
|
|
scproof: SignedContributionAndProof,
|
2023-05-17 04:55:55 +00:00
|
|
|
bid: BlockId,
|
2023-01-12 14:08:08 +00:00
|
|
|
signature: CookedSig) =
|
|
|
|
pool.addContribution(
|
2023-05-17 04:55:55 +00:00
|
|
|
scproof.message.aggregator_index,
|
|
|
|
scproof.message.contribution,
|
|
|
|
bid, signature)
|
2021-11-05 15:39:47 +00:00
|
|
|
|
2021-09-22 12:17:15 +00:00
|
|
|
if not(isNil(pool.onContributionReceived)):
|
|
|
|
pool.onContributionReceived(scproof)
|
|
|
|
|
2021-08-30 01:00:37 +00:00
|
|
|
proc produceSyncAggregateAux(
|
2023-05-17 04:55:55 +00:00
|
|
|
contributions: BestSyncSubcommitteeContributions): SyncAggregate =
|
2021-08-28 10:40:01 +00:00
|
|
|
var
|
2022-02-16 22:24:44 +00:00
|
|
|
aggregateSig {.noinit.}: AggregateSignature
|
2021-08-28 10:40:01 +00:00
|
|
|
initialized = false
|
|
|
|
startTime = Moment.now
|
2023-05-17 04:55:55 +00:00
|
|
|
aggregate: SyncAggregate
|
2022-01-08 23:28:49 +00:00
|
|
|
for subcommitteeIdx in SyncSubcommitteeIndex:
|
2023-05-17 04:55:55 +00:00
|
|
|
if contributions.subnets[subcommitteeIdx].totalParticipants == 0:
|
2021-08-28 10:40:01 +00:00
|
|
|
continue
|
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
for pos, value in contributions.subnets[subcommitteeIdx].participationBits:
|
2021-08-28 10:40:01 +00:00
|
|
|
if value:
|
2022-01-08 23:28:49 +00:00
|
|
|
let globalPos = subcommitteeIdx.asInt * SYNC_SUBCOMMITTEE_SIZE + pos
|
2023-05-17 04:55:55 +00:00
|
|
|
aggregate.sync_committee_bits.setBit globalPos
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
if not initialized:
|
|
|
|
initialized = true
|
2023-05-17 04:55:55 +00:00
|
|
|
aggregateSig.init(contributions.subnets[subcommitteeIdx].signature)
|
2021-08-28 10:40:01 +00:00
|
|
|
else:
|
2023-05-17 04:55:55 +00:00
|
|
|
aggregateSig.aggregate(contributions.subnets[subcommitteeIdx].signature)
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
if initialized:
|
2023-05-17 04:55:55 +00:00
|
|
|
aggregate.sync_committee_signature = aggregateSig.finish.toValidatorSig
|
2021-08-28 10:40:01 +00:00
|
|
|
else:
|
2023-05-17 04:55:55 +00:00
|
|
|
aggregate.sync_committee_signature = ValidatorSig.infinity
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
let duration = Moment.now - startTime
|
|
|
|
debug "SyncAggregate produced", duration,
|
2023-05-17 04:55:55 +00:00
|
|
|
bits = aggregate.sync_committee_bits
|
|
|
|
|
|
|
|
aggregate
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
proc produceSyncAggregate*(
|
2024-06-24 09:13:15 +00:00
|
|
|
pool: var SyncCommitteeMsgPool,
|
2023-05-17 04:55:55 +00:00
|
|
|
bid: BlockId,
|
2023-06-16 21:30:36 +00:00
|
|
|
signatureSlot: Slot): SyncAggregate =
|
|
|
|
# Sync committee signs previous slot, relative to when new block is produced
|
2024-06-24 09:13:15 +00:00
|
|
|
let
|
|
|
|
slot = max(signatureSlot, 1.Slot) - 1
|
|
|
|
target = pool.cfg.toSyncMsgTarget(bid, slot)
|
|
|
|
|
|
|
|
var contribution {.noinit.}: SyncCommitteeContribution
|
|
|
|
pool.bestContributions.withValue(target, contributions):
|
|
|
|
for subcommitteeIdx in SyncSubcommitteeIndex:
|
|
|
|
if contributions.subnets[subcommitteeIdx].totalParticipants == 0 and
|
|
|
|
pool.produceContribution(slot, bid, subcommitteeIdx, contribution):
|
|
|
|
debug "Did not receive contribution, did aggregate locally",
|
|
|
|
target, subcommitteeIdx
|
|
|
|
contributions[].addContribution(contribution)
|
|
|
|
do:
|
|
|
|
var
|
|
|
|
contributions: BestSyncSubcommitteeContributions
|
|
|
|
didAggregate = false
|
|
|
|
for subcommitteeIdx in SyncSubcommitteeIndex:
|
|
|
|
if pool.produceContribution(slot, bid, subcommitteeIdx, contribution):
|
|
|
|
debug "Did not receive contribution, did aggregate locally",
|
|
|
|
target, subcommitteeIdx
|
|
|
|
contributions.addContribution(contribution)
|
|
|
|
didAggregate = true
|
|
|
|
if didAggregate:
|
|
|
|
pool.bestContributions[target] = contributions
|
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
if target in pool.bestContributions:
|
2021-08-28 10:40:01 +00:00
|
|
|
try:
|
2023-05-17 04:55:55 +00:00
|
|
|
produceSyncAggregateAux(pool.bestContributions[target])
|
2021-08-28 10:40:01 +00:00
|
|
|
except KeyError:
|
|
|
|
raiseAssert "We have checked for the key upfront"
|
|
|
|
else:
|
|
|
|
SyncAggregate.init()
|
2022-01-24 20:40:59 +00:00
|
|
|
|
|
|
|
proc isEpochLeadTime*(
|
|
|
|
pool: SyncCommitteeMsgPool, epochsToSyncPeriod: uint64): bool =
|
2024-09-21 01:22:41 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.6/specs/altair/validator.md#sync-committee-subnet-stability
|
2022-01-24 20:40:59 +00:00
|
|
|
# This ensures a uniform distribution without requiring additional state:
|
|
|
|
# (1/4) = 1/4, 4 slots out
|
|
|
|
# (3/4) * (1/3) = 1/4, 3 slots out
|
|
|
|
# (3/4) * (2/3) * (1/2) = 1/4, 2 slots out
|
|
|
|
# (3/4) * (2/3) * (1/2) * (1/1) = 1/4, 1 slot out
|
|
|
|
doAssert epochsToSyncPeriod > 0
|
|
|
|
epochsToSyncPeriod == 1 or pool.rng[].rand(epochsToSyncPeriod - 1) == 0
|