2021-08-28 10:40:01 +00:00
|
|
|
# beacon_chain
|
2023-01-09 22:44:44 +00:00
|
|
|
# Copyright (c) 2018-2023 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
|
2021-12-03 15:04:58 +00:00
|
|
|
std/[sets, tables],
|
|
|
|
stew/shims/hashes,
|
2022-01-24 20:40:59 +00:00
|
|
|
eth/p2p/discoveryv5/random2,
|
2021-08-28 10:40:01 +00:00
|
|
|
chronicles,
|
2021-12-09 12:56:54 +00:00
|
|
|
../spec/[crypto, digest],
|
2021-10-19 14:09:26 +00:00
|
|
|
../spec/datatypes/altair
|
|
|
|
|
|
|
|
export hashes, sets, tables, altair
|
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
|
|
|
|
originator: uint64 # ValidatorIndex avoiding mess with invalid values
|
|
|
|
slot: Slot
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: uint64 # SyncSubcommitteeIndex avoiding mess with invalid values
|
2021-10-19 14:09:26 +00:00
|
|
|
|
|
|
|
TrustedSyncCommitteeMsg* = object
|
|
|
|
slot*: Slot
|
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
|
|
|
|
slot*: Slot
|
|
|
|
subnets*: array[SYNC_COMMITTEE_SUBNET_COUNT,
|
|
|
|
BestSyncSubcommitteeContribution]
|
|
|
|
|
|
|
|
OnSyncContributionCallback* =
|
|
|
|
proc(data: SignedContributionAndProof) {.gcsafe, raises: [Defect].}
|
|
|
|
|
|
|
|
SyncCommitteeMsgPool* = object
|
|
|
|
seenSyncMsgByAuthor*: HashSet[SyncCommitteeMsgKey]
|
|
|
|
seenContributionByAuthor*: HashSet[SyncCommitteeMsgKey]
|
|
|
|
syncMessages*: Table[Eth2Digest, seq[TrustedSyncCommitteeMsg]]
|
|
|
|
bestContributions*: Table[Eth2Digest, BestSyncSubcommitteeContributions]
|
|
|
|
onContributionReceived*: OnSyncContributionCallback
|
|
|
|
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext
|
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
|
|
|
|
2021-09-22 12:17:15 +00:00
|
|
|
func init*(T: type SyncCommitteeMsgPool,
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2021-09-22 12:17:15 +00:00
|
|
|
onSyncContribution: OnSyncContributionCallback = nil
|
|
|
|
): SyncCommitteeMsgPool =
|
2022-01-24 20:40:59 +00:00
|
|
|
T(rng: rng, onContributionReceived: onSyncContribution)
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2021-08-30 01:00:37 +00:00
|
|
|
func pruneData*(pool: var SyncCommitteeMsgPool, slot: Slot) =
|
|
|
|
## This should be called at the end of slot.
|
|
|
|
clear pool.seenContributionByAuthor
|
|
|
|
clear pool.seenSyncMsgByAuthor
|
|
|
|
|
|
|
|
if slot < syncCommitteeMsgsRetentionSlots:
|
|
|
|
return
|
|
|
|
|
|
|
|
let minSlotToRetain = slot - syncCommitteeMsgsRetentionSlots
|
|
|
|
var syncMsgsToDelete: seq[Eth2Digest]
|
|
|
|
var contributionsToDelete: seq[Eth2Digest]
|
|
|
|
|
|
|
|
for blockRoot, msgs in pool.syncMessages:
|
|
|
|
if msgs[0].slot < minSlotToRetain:
|
|
|
|
syncMsgsToDelete.add blockRoot
|
|
|
|
|
|
|
|
for blockRoot in syncMsgsToDelete:
|
|
|
|
pool.syncMessages.del blockRoot
|
|
|
|
|
|
|
|
for blockRoot, bestContributions in pool.bestContributions:
|
|
|
|
if bestContributions.slot < minSlotToRetain:
|
|
|
|
contributionsToDelete.add blockRoot
|
|
|
|
|
|
|
|
for blockRoot in contributionsToDelete:
|
|
|
|
pool.bestContributions.del blockRoot
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
func isSeen*(
|
|
|
|
pool: SyncCommitteeMsgPool,
|
|
|
|
msg: SyncCommitteeMessage,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex): bool =
|
2021-11-05 15:39:47 +00:00
|
|
|
let seenKey = SyncCommitteeMsgKey(
|
|
|
|
originator: msg.validator_index, # Might be unvalidated at this point
|
|
|
|
slot: msg.slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: subcommitteeIdx.uint64)
|
2021-11-05 15:39:47 +00:00
|
|
|
seenKey in pool.seenSyncMsgByAuthor
|
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
proc addSyncCommitteeMessage*(
|
2021-08-28 10:40:01 +00:00
|
|
|
pool: var SyncCommitteeMsgPool,
|
|
|
|
slot: Slot,
|
2021-08-30 01:00:37 +00:00
|
|
|
blockRoot: Eth2Digest,
|
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,
|
2021-11-05 15:39:47 +00:00
|
|
|
positionsInCommittee: openArray[uint64]) =
|
|
|
|
|
|
|
|
let
|
|
|
|
seenKey = SyncCommitteeMsgKey(
|
|
|
|
originator: validatorIndex,
|
|
|
|
slot: slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: subcommitteeIdx.uint64)
|
2021-11-05 15:39:47 +00:00
|
|
|
|
|
|
|
pool.seenSyncMsgByAuthor.incl seenKey
|
|
|
|
|
|
|
|
for position in positionsInCommittee:
|
|
|
|
pool.syncMessages.mgetOrPut(blockRoot, @[]).add TrustedSyncCommitteeMsg(
|
|
|
|
slot: slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: subcommitteeIdx,
|
2021-11-05 15:39:47 +00:00
|
|
|
positionInCommittee: position,
|
|
|
|
signature: signature)
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
debug "Sync committee message resolved",
|
|
|
|
slot = slot, blockRoot = shortLog(blockRoot), validatorIndex
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-02-21 11:55:56 +00:00
|
|
|
initialized
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
func produceContribution*(
|
|
|
|
pool: SyncCommitteeMsgPool,
|
|
|
|
slot: Slot,
|
2021-08-30 01:00:37 +00:00
|
|
|
headRoot: Eth2Digest,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
2021-08-28 10:40:01 +00:00
|
|
|
outContribution: var SyncCommitteeContribution): bool =
|
2021-08-30 01:00:37 +00:00
|
|
|
if headRoot in pool.syncMessages:
|
2021-08-28 10:40:01 +00:00
|
|
|
outContribution.slot = slot
|
2021-08-30 01:00:37 +00:00
|
|
|
outContribution.beacon_block_root = headRoot
|
2022-01-08 23:28:49 +00:00
|
|
|
outContribution.subcommittee_index = subcommitteeIdx.asUInt64
|
2021-08-28 10:40:01 +00:00
|
|
|
try:
|
2021-11-05 15:39:47 +00:00
|
|
|
computeAggregateSig(pool.syncMessages[headRoot],
|
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
|
|
|
|
|
|
|
func addAggregateAux(bestVotes: var BestSyncSubcommitteeContributions,
|
2023-01-12 14:08:08 +00:00
|
|
|
contribution: SyncCommitteeContribution) =
|
2021-08-30 01:00:37 +00:00
|
|
|
let
|
|
|
|
currentBestTotalParticipants =
|
|
|
|
bestVotes.subnets[contribution.subcommittee_index].totalParticipants
|
|
|
|
newBestTotalParticipants = countOnes(contribution.aggregation_bits)
|
|
|
|
|
|
|
|
if newBestTotalParticipants > currentBestTotalParticipants:
|
|
|
|
bestVotes.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(
|
|
|
|
bestVotes: BestSyncSubcommitteeContributions,
|
|
|
|
contribution: SyncCommitteeContribution): bool =
|
|
|
|
contribution.aggregation_bits.isSubsetOf(
|
|
|
|
bestVotes.subnets[contribution.subcommittee_index].participationBits)
|
|
|
|
|
|
|
|
func covers*(
|
|
|
|
pool: var SyncCommitteeMsgPool,
|
|
|
|
contribution: SyncCommitteeContribution): bool =
|
|
|
|
## 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
|
|
|
|
pool.bestContributions.withValue(contribution.beacon_block_root, best):
|
|
|
|
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-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
|
|
|
|
|
|
|
template blockRoot: auto = contribution.beacon_block_root
|
|
|
|
|
2021-08-30 01:00:37 +00:00
|
|
|
if blockRoot notin pool.bestContributions:
|
2021-08-28 10:40:01 +00:00
|
|
|
let totalParticipants = countOnes(contribution.aggregation_bits)
|
2021-08-30 01:00:37 +00:00
|
|
|
var initialBestContributions = BestSyncSubcommitteeContributions(
|
|
|
|
slot: contribution.slot)
|
2021-08-28 10:40:01 +00:00
|
|
|
|
2021-08-30 01:00:37 +00:00
|
|
|
initialBestContributions.subnets[contribution.subcommittee_index] =
|
2021-08-28 10:40:01 +00:00
|
|
|
BestSyncSubcommitteeContribution(
|
|
|
|
totalParticipants: totalParticipants,
|
|
|
|
participationBits: contribution.aggregation_bits,
|
|
|
|
signature: signature)
|
|
|
|
|
2021-08-30 01:00:37 +00:00
|
|
|
pool.bestContributions[blockRoot] = initialBestContributions
|
2021-08-28 10:40:01 +00:00
|
|
|
else:
|
|
|
|
try:
|
2021-08-30 01:00:37 +00:00
|
|
|
addAggregateAux(pool.bestContributions[blockRoot], contribution)
|
2021-08-28 10:40:01 +00:00
|
|
|
except KeyError:
|
|
|
|
raiseAssert "We have checked for the key upfront"
|
|
|
|
|
2021-11-25 12:20:36 +00:00
|
|
|
proc addContribution*(pool: var SyncCommitteeMsgPool,
|
2022-08-09 09:52:11 +00:00
|
|
|
scproof: SignedContributionAndProof,
|
2023-01-12 14:08:08 +00:00
|
|
|
signature: CookedSig) =
|
|
|
|
pool.addContribution(
|
2021-11-05 15:39:47 +00:00
|
|
|
scproof.message.aggregator_index, scproof.message.contribution, signature)
|
|
|
|
|
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(
|
|
|
|
bestContributions: 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
|
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
for subcommitteeIdx in SyncSubcommitteeIndex:
|
|
|
|
if bestContributions.subnets[subcommitteeIdx].totalParticipants == 0:
|
2021-08-28 10:40:01 +00:00
|
|
|
continue
|
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
for pos, value in bestContributions.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
|
2021-08-28 10:40:01 +00:00
|
|
|
result.sync_committee_bits.setBit globalPos
|
|
|
|
|
|
|
|
if not initialized:
|
|
|
|
initialized = true
|
2022-01-08 23:28:49 +00:00
|
|
|
aggregateSig.init(bestContributions.subnets[subcommitteeIdx].signature)
|
2021-08-28 10:40:01 +00:00
|
|
|
else:
|
2022-01-08 23:28:49 +00:00
|
|
|
aggregateSig.aggregate(bestContributions.subnets[subcommitteeIdx].signature)
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
if initialized:
|
|
|
|
result.sync_committee_signature = aggregateSig.finish.toValidatorSig
|
|
|
|
else:
|
|
|
|
result.sync_committee_signature = ValidatorSig.infinity
|
|
|
|
|
|
|
|
let duration = Moment.now - startTime
|
|
|
|
debug "SyncAggregate produced", duration,
|
|
|
|
bits = result.sync_committee_bits
|
|
|
|
|
|
|
|
proc produceSyncAggregate*(
|
|
|
|
pool: SyncCommitteeMsgPool,
|
2021-08-30 01:00:37 +00:00
|
|
|
targetRoot: Eth2Digest): SyncAggregate =
|
|
|
|
if targetRoot in pool.bestContributions:
|
2021-08-28 10:40:01 +00:00
|
|
|
try:
|
2021-08-30 01:00:37 +00:00
|
|
|
produceSyncAggregateAux(pool.bestContributions[targetRoot])
|
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 =
|
2023-01-30 23:26:57 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.2/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
|