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-10-19 14:09:26 +00:00
|
|
|
std/[hashes, sets, tables],
|
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
|
|
|
|
SyncCommitteeMsgKey* = object
|
|
|
|
originator*: ValidatorIndex
|
|
|
|
slot*: Slot
|
2021-10-20 16:32:46 +00:00
|
|
|
committeeIdx*: SyncSubcommitteeIndex
|
2021-10-19 14:09:26 +00:00
|
|
|
|
|
|
|
TrustedSyncCommitteeMsg* = object
|
|
|
|
slot*: Slot
|
2021-10-20 16:32:46 +00:00
|
|
|
committeeIdx*: 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 =
|
|
|
|
hashData(unsafeAddr x, sizeof(x))
|
|
|
|
|
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
|
|
|
|
|
|
|
func addSyncCommitteeMsg*(
|
|
|
|
pool: var SyncCommitteeMsgPool,
|
|
|
|
slot: Slot,
|
2021-08-30 01:00:37 +00:00
|
|
|
blockRoot: Eth2Digest,
|
2021-08-28 10:40:01 +00:00
|
|
|
signature: CookedSig,
|
2021-10-20 16:32:46 +00:00
|
|
|
committeeIdx: SyncSubcommitteeIndex,
|
2021-08-28 10:40:01 +00:00
|
|
|
positionInCommittee: uint64) =
|
2021-08-30 01:00:37 +00:00
|
|
|
pool.syncMessages.mgetOrPut(blockRoot, @[]).add TrustedSyncCommitteeMsg(
|
2021-08-28 10:40:01 +00:00
|
|
|
slot: slot,
|
|
|
|
committeeIdx: committeeIdx,
|
|
|
|
positionInCommittee: positionInCommittee,
|
|
|
|
signature: signature)
|
|
|
|
|
|
|
|
func computeAggregateSig(votes: seq[TrustedSyncCommitteeMsg],
|
2021-10-20 16:32:46 +00:00
|
|
|
committeeIdx: SyncSubcommitteeIndex,
|
2021-08-28 10:40:01 +00:00
|
|
|
contribution: var SyncCommitteeContribution): bool =
|
|
|
|
var
|
|
|
|
aggregateSig {.noInit.}: AggregateSignature
|
|
|
|
initialized = false
|
|
|
|
|
|
|
|
for vote in votes:
|
|
|
|
if vote.committeeIdx != committeeIdx:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not initialized:
|
|
|
|
initialized = true
|
|
|
|
aggregateSig.init(vote.signature)
|
|
|
|
else:
|
|
|
|
aggregateSig.aggregate(vote.signature)
|
|
|
|
|
|
|
|
contribution.aggregation_bits.setBit vote.positionInCommittee
|
|
|
|
|
|
|
|
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-10-20 16:32:46 +00:00
|
|
|
committeeIdx: 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-08-28 10:40:01 +00:00
|
|
|
outContribution.subcommittee_index = committeeIdx.asUInt64
|
|
|
|
try:
|
2021-08-30 01:00:37 +00:00
|
|
|
return computeAggregateSig(pool.syncMessages[headRoot],
|
2021-08-28 10:40:01 +00:00
|
|
|
committeeIdx,
|
|
|
|
outContribution)
|
|
|
|
except KeyError:
|
|
|
|
raiseAssert "We have checked for the key upfront"
|
|
|
|
else:
|
|
|
|
return false
|
|
|
|
|
|
|
|
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-09-22 12:17:15 +00:00
|
|
|
proc addSyncContribution*(pool: var SyncCommitteeMsgPool,
|
|
|
|
contribution: SyncCommitteeContribution,
|
|
|
|
signature: CookedSig) =
|
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-09-22 12:17:15 +00:00
|
|
|
proc addSyncContribution*(pool: var SyncCommitteeMsgPool,
|
|
|
|
scproof: SignedContributionAndProof,
|
|
|
|
signature: CookedSig) =
|
|
|
|
pool.addSyncContribution(scproof.message.contribution, signature)
|
|
|
|
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()
|