2021-08-28 10:40:01 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2021-12-03 15:04:58 +00:00
|
|
|
std/[sets, tables],
|
|
|
|
stew/shims/hashes,
|
2021-08-28 10:40:01 +00:00
|
|
|
chronicles,
|
|
|
|
../spec/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
|
|
|
|
subcommitteeIndex: uint64 # SyncSubcommitteeIndex avoiding mess with invalid values
|
2021-10-19 14:09:26 +00:00
|
|
|
|
|
|
|
TrustedSyncCommitteeMsg* = object
|
|
|
|
slot*: Slot
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIndex*: 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
|
|
|
|
|
|
|
|
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,
|
|
|
|
onSyncContribution: OnSyncContributionCallback = nil
|
|
|
|
): SyncCommitteeMsgPool =
|
|
|
|
T(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,
|
|
|
|
subcommitteeIndex: SyncSubcommitteeIndex): bool =
|
|
|
|
let seenKey = SyncCommitteeMsgKey(
|
|
|
|
originator: msg.validator_index, # Might be unvalidated at this point
|
|
|
|
slot: msg.slot,
|
|
|
|
subcommitteeIndex: subcommitteeIndex.uint64)
|
|
|
|
seenKey in pool.seenSyncMsgByAuthor
|
|
|
|
|
2021-11-25 12:20:36 +00:00
|
|
|
func 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,
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIndex: SyncSubcommitteeIndex,
|
|
|
|
positionsInCommittee: openArray[uint64]) =
|
|
|
|
|
|
|
|
let
|
|
|
|
seenKey = SyncCommitteeMsgKey(
|
|
|
|
originator: validatorIndex,
|
|
|
|
slot: slot,
|
|
|
|
subcommitteeIndex: subcommitteeIndex.uint64)
|
|
|
|
|
|
|
|
pool.seenSyncMsgByAuthor.incl seenKey
|
|
|
|
|
|
|
|
for position in positionsInCommittee:
|
|
|
|
pool.syncMessages.mgetOrPut(blockRoot, @[]).add TrustedSyncCommitteeMsg(
|
|
|
|
slot: slot,
|
|
|
|
subcommitteeIndex: subcommitteeIndex,
|
|
|
|
positionInCommittee: position,
|
|
|
|
signature: signature)
|
2021-08-28 10:40:01 +00:00
|
|
|
|
|
|
|
func computeAggregateSig(votes: seq[TrustedSyncCommitteeMsg],
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIndex: SyncSubcommitteeIndex,
|
2021-08-28 10:40:01 +00:00
|
|
|
contribution: var SyncCommitteeContribution): bool =
|
|
|
|
var
|
|
|
|
aggregateSig {.noInit.}: AggregateSignature
|
|
|
|
initialized = false
|
|
|
|
|
|
|
|
for vote in votes:
|
2021-11-05 15:39:47 +00:00
|
|
|
if vote.subcommitteeIndex != subcommitteeIndex:
|
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
|
|
|
|
|
|
|
|
return initialized
|
|
|
|
|
|
|
|
func produceContribution*(
|
|
|
|
pool: SyncCommitteeMsgPool,
|
|
|
|
slot: Slot,
|
2021-08-30 01:00:37 +00:00
|
|
|
headRoot: Eth2Digest,
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIndex: 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
|
2021-11-05 15:39:47 +00:00
|
|
|
outContribution.subcommittee_index = subcommitteeIndex.asUInt64
|
2021-08-28 10:40:01 +00:00
|
|
|
try:
|
2021-11-05 15:39:47 +00:00
|
|
|
computeAggregateSig(pool.syncMessages[headRoot],
|
2021-11-04 18:22:24 +00:00
|
|
|
subcommitteeIndex,
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
subcommitteeIndex: msg.contribution.subcommittee_index)
|
|
|
|
seenKey in pool.seenContributionByAuthor
|
|
|
|
|
2021-11-25 12:20:36 +00:00
|
|
|
proc addContribution(pool: var SyncCommitteeMsgPool,
|
2021-11-05 15:39:47 +00:00
|
|
|
aggregator_index: uint64,
|
|
|
|
contribution: SyncCommitteeContribution,
|
|
|
|
signature: CookedSig) =
|
|
|
|
let seenKey = SyncCommitteeMsgKey(
|
|
|
|
originator: aggregator_index,
|
|
|
|
slot: contribution.slot,
|
|
|
|
subcommitteeIndex: contribution.subcommittee_index)
|
|
|
|
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,
|
2021-09-22 12:17:15 +00:00
|
|
|
scproof: SignedContributionAndProof,
|
|
|
|
signature: CookedSig) =
|
2021-11-25 12:20:36 +00:00
|
|
|
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
|
|
|
|
aggregateSig {.noInit.}: AggregateSignature
|
|
|
|
initialized = false
|
|
|
|
startTime = Moment.now
|
|
|
|
|
2021-10-21 13:09:19 +00:00
|
|
|
for subnetId in allSyncSubcommittees():
|
2021-08-30 01:00:37 +00:00
|
|
|
if bestContributions.subnets[subnetId].totalParticipants == 0:
|
2021-08-28 10:40:01 +00:00
|
|
|
continue
|
|
|
|
|
2021-08-30 01:00:37 +00:00
|
|
|
for pos, value in bestContributions.subnets[subnetId].participationBits:
|
2021-08-28 10:40:01 +00:00
|
|
|
if value:
|
2021-09-28 18:02:01 +00:00
|
|
|
let globalPos = subnetId.asInt * SYNC_SUBCOMMITTEE_SIZE + pos
|
2021-08-28 10:40:01 +00:00
|
|
|
result.sync_committee_bits.setBit globalPos
|
|
|
|
|
|
|
|
if not initialized:
|
|
|
|
initialized = true
|
2021-08-30 01:00:37 +00:00
|
|
|
aggregateSig.init(bestContributions.subnets[subnetId].signature)
|
2021-08-28 10:40:01 +00:00
|
|
|
else:
|
2021-08-30 01:00:37 +00:00
|
|
|
aggregateSig.aggregate(bestContributions.subnets[subnetId].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()
|