From 8910de65a4cd07e70144dee4ddbe295213e2feea Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Mon, 27 May 2024 13:56:38 +0200 Subject: [PATCH] move `EpochParticipationFlags, SyncCommittee` to `base` Preparation for EIP-7495 SSZ `StableContainer` which can only contain immutable types in their fields. --- beacon_chain/spec/datatypes/altair.nim | 53 -------------------------- beacon_chain/spec/datatypes/base.nim | 53 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/beacon_chain/spec/datatypes/altair.nim b/beacon_chain/spec/datatypes/altair.nim index be86db285..117e94019 100644 --- a/beacon_chain/spec/datatypes/altair.nim +++ b/beacon_chain/spec/datatypes/altair.nim @@ -75,20 +75,6 @@ static: doAssert TIMELY_SOURCE_WEIGHT + TIMELY_TARGET_WEIGHT + type ### New types - # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/beacon-chain.md#custom-types - ParticipationFlags* = uint8 - - EpochParticipationFlags* = - distinct List[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT] - ## Not a HashList because the list sees significant updates every block - ## 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#synccommittee - SyncCommittee* = object - pubkeys*: HashArray[Limit SYNC_COMMITTEE_SIZE, ValidatorPubKey] - aggregate_pubkey*: ValidatorPubKey - # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.2/specs/altair/validator.md#synccommitteemessage SyncCommitteeMessage* = object slot*: Slot @@ -536,45 +522,6 @@ template `[]`*(arr: array[SYNC_COMMITTEE_SIZE, auto] | seq; makeLimitedU8(SyncSubcommitteeIndex, SYNC_COMMITTEE_SUBNET_COUNT) makeLimitedU16(IndexInSyncCommittee, SYNC_COMMITTEE_SIZE) -template asList*(epochFlags: EpochParticipationFlags): untyped = - List[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT] epochFlags -template asList*(epochFlags: var EpochParticipationFlags): untyped = - let tmp = cast[ptr List[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT]](addr epochFlags) - tmp[] - -template asSeq*(epochFlags: EpochParticipationFlags): untyped = - seq[ParticipationFlags] asList(epochFlags) - -template asSeq*(epochFlags: var EpochParticipationFlags): untyped = - let tmp = cast[ptr seq[ParticipationFlags]](addr epochFlags) - tmp[] - -template item*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex): ParticipationFlags = - asList(epochFlags)[idx] - -template `[]`*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex|uint64|int): ParticipationFlags = - asList(epochFlags)[idx] - -template `[]=`*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex, flags: ParticipationFlags) = - asList(epochFlags)[idx] = flags - -template add*(epochFlags: var EpochParticipationFlags, flags: ParticipationFlags): bool = - asList(epochFlags).add flags - -template len*(epochFlags: EpochParticipationFlags): int = - asList(epochFlags).len - -template low*(epochFlags: EpochParticipationFlags): int = - asSeq(epochFlags).low -template high*(epochFlags: EpochParticipationFlags): int = - asSeq(epochFlags).high - -template assign*(v: var EpochParticipationFlags, src: EpochParticipationFlags) = - # TODO https://github.com/nim-lang/Nim/issues/21123 - mixin assign - var tmp = cast[ptr seq[ParticipationFlags]](addr v) - assign(tmp[], distinctBase src) - func shortLog*(v: SomeBeaconBlock): auto = ( slot: shortLog(v.slot), diff --git a/beacon_chain/spec/datatypes/base.nim b/beacon_chain/spec/datatypes/base.nim index 47ea4c7a0..a7609b7cb 100644 --- a/beacon_chain/spec/datatypes/base.nim +++ b/beacon_chain/spec/datatypes/base.nim @@ -465,6 +465,20 @@ type source_index*: uint64 target_index*: uint64 + # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/beacon-chain.md#custom-types + ParticipationFlags* = uint8 + + EpochParticipationFlags* = + distinct List[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT] + ## Not a HashList because the list sees significant updates every block + ## 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#synccommittee + SyncCommittee* = object + pubkeys*: HashArray[Limit SYNC_COMMITTEE_SIZE, ValidatorPubKey] + aggregate_pubkey*: ValidatorPubKey + # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#beaconblockheader BeaconBlockHeader* = object slot*: Slot @@ -969,6 +983,45 @@ func shortLog*(v: SignedBLSToExecutionChange): auto = chronicles.formatIt AttestationData: it.shortLog chronicles.formatIt Checkpoint: it.shortLog +template asList*(epochFlags: EpochParticipationFlags): untyped = + List[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT] epochFlags +template asList*(epochFlags: var EpochParticipationFlags): untyped = + let tmp = cast[ptr List[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT]](addr epochFlags) + tmp[] + +template asSeq*(epochFlags: EpochParticipationFlags): untyped = + seq[ParticipationFlags] asList(epochFlags) + +template asSeq*(epochFlags: var EpochParticipationFlags): untyped = + let tmp = cast[ptr seq[ParticipationFlags]](addr epochFlags) + tmp[] + +template item*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex): ParticipationFlags = + asList(epochFlags)[idx] + +template `[]`*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex|uint64|int): ParticipationFlags = + asList(epochFlags)[idx] + +template `[]=`*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex, flags: ParticipationFlags) = + asList(epochFlags)[idx] = flags + +template add*(epochFlags: var EpochParticipationFlags, flags: ParticipationFlags): bool = + asList(epochFlags).add flags + +template len*(epochFlags: EpochParticipationFlags): int = + asList(epochFlags).len + +template low*(epochFlags: EpochParticipationFlags): int = + asSeq(epochFlags).low +template high*(epochFlags: EpochParticipationFlags): int = + asSeq(epochFlags).high + +template assign*(v: var EpochParticipationFlags, src: EpochParticipationFlags) = + # TODO https://github.com/nim-lang/Nim/issues/21123 + mixin assign + var tmp = cast[ptr seq[ParticipationFlags]](addr v) + assign(tmp[], distinctBase src) + const # http://facweb.cs.depaul.edu/sjost/it212/documents/ascii-pr.htm PrintableAsciiChars = {' '..'~'}