From ee5af9f7ee1415c1bbd663f8dbeec78ce82b7f50 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Sun, 26 May 2024 14:39:24 +0200 Subject: [PATCH] move `SyncAggregate` to `base` Preparation for EIP-7495 SSZ `StableContainer` which can only contain immutable types in their fields. --- beacon_chain/spec/datatypes/altair.nim | 32 -------------------------- beacon_chain/spec/datatypes/base.nim | 31 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/beacon_chain/spec/datatypes/altair.nim b/beacon_chain/spec/datatypes/altair.nim index 5d866ceb4..be86db285 100644 --- a/beacon_chain/spec/datatypes/altair.nim +++ b/beacon_chain/spec/datatypes/altair.nim @@ -84,15 +84,6 @@ type ## effectively making the cost of clearing the cache higher than the typical ## gains - # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/beacon-chain.md#syncaggregate - SyncAggregate* = object - sync_committee_bits*: BitArray[SYNC_COMMITTEE_SIZE] - sync_committee_signature*: ValidatorSig - - TrustedSyncAggregate* = object - sync_committee_bits*: BitArray[SYNC_COMMITTEE_SIZE] - sync_committee_signature*: TrustedSig - # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/beacon-chain.md#synccommittee SyncCommittee* = object pubkeys*: HashArray[Limit SYNC_COMMITTEE_SIZE, ValidatorPubKey] @@ -529,8 +520,6 @@ type SigVerifiedBeaconBlockBody | TrustedBeaconBlockBody - SomeSyncAggregate* = SyncAggregate | TrustedSyncAggregate - SyncSubcommitteeIndex* = distinct uint8 IndexInSyncCommittee* = distinct uint16 @@ -630,23 +619,6 @@ func shortLog*(v: SyncCommitteeMessage): auto = signature: shortLog(v.signature) ) -func init*(T: type SyncAggregate): SyncAggregate = - SyncAggregate(sync_committee_signature: ValidatorSig.infinity) - -func num_active_participants*(v: SomeSyncAggregate): int = - countOnes(v.sync_committee_bits) - -func hasSupermajoritySyncParticipation*( - num_active_participants: uint64): bool = - const max_active_participants = SYNC_COMMITTEE_SIZE.uint64 - num_active_participants * 3 >= static(max_active_participants * 2) - -func hasSupermajoritySyncParticipation*(v: SomeSyncAggregate): bool = - hasSupermajoritySyncParticipation(v.num_active_participants.uint64) - -func shortLog*(v: SyncAggregate): auto = - $(v.sync_committee_bits) - func shortLog*(v: ContributionAndProof): auto = ( aggregator_index: v.aggregator_index, @@ -741,7 +713,3 @@ template asTrusted*( SigVerifiedSignedBeaconBlock | MsgTrustedSignedBeaconBlock): TrustedSignedBeaconBlock = isomorphicCast[TrustedSignedBeaconBlock](x) - -template asTrusted*( - x: SyncAggregate): TrustedSyncAggregate = - isomorphicCast[TrustedSyncAggregate](x) diff --git a/beacon_chain/spec/datatypes/base.nim b/beacon_chain/spec/datatypes/base.nim index 5dc1708d4..8ac1fdb62 100644 --- a/beacon_chain/spec/datatypes/base.nim +++ b/beacon_chain/spec/datatypes/base.nim @@ -281,6 +281,7 @@ type SomeProposerSlashing* = ProposerSlashing | TrustedProposerSlashing SomeSignedBeaconBlockHeader* = SignedBeaconBlockHeader | TrustedSignedBeaconBlockHeader SomeSignedVoluntaryExit* = SignedVoluntaryExit | TrustedSignedVoluntaryExit + SomeSyncAggregate* = SyncAggregate | TrustedSyncAggregate # Legacy database type, see BeaconChainDB ImmutableValidatorData* = object @@ -363,6 +364,15 @@ type message*: VoluntaryExit signature*: TrustedSig + # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/beacon-chain.md#syncaggregate + SyncAggregate* = object + sync_committee_bits*: BitArray[SYNC_COMMITTEE_SIZE] + sync_committee_signature*: ValidatorSig + + TrustedSyncAggregate* = object + sync_committee_bits*: BitArray[SYNC_COMMITTEE_SIZE] + sync_committee_signature*: TrustedSig + # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#beaconblockheader BeaconBlockHeader* = object slot*: Slot @@ -806,6 +816,27 @@ func shortLog*(v: SomeSignedVoluntaryExit): auto = signature: shortLog(v.signature) ) +func shortLog*(v: SomeSyncAggregate): auto = + $(v.sync_committee_bits) + +func init*(T: type SyncAggregate): SyncAggregate = + SyncAggregate(sync_committee_signature: ValidatorSig.infinity) + +template asTrusted*( + x: SyncAggregate): TrustedSyncAggregate = + isomorphicCast[TrustedSyncAggregate](x) + +func num_active_participants*(v: SomeSyncAggregate): int = + countOnes(v.sync_committee_bits) + +func hasSupermajoritySyncParticipation*( + num_active_participants: uint64): bool = + const max_active_participants = SYNC_COMMITTEE_SIZE.uint64 + num_active_participants * 3 >= static(max_active_participants * 2) + +func hasSupermajoritySyncParticipation*(v: SomeSyncAggregate): bool = + hasSupermajoritySyncParticipation(v.num_active_participants.uint64) + chronicles.formatIt AttestationData: it.shortLog chronicles.formatIt Checkpoint: it.shortLog