From bb2ca747bce7164fbc49a723e8518284003ed975 Mon Sep 17 00:00:00 2001 From: tersec Date: Tue, 16 Apr 2024 21:01:30 +0200 Subject: [PATCH] move Attestation-related types/logging from base to phase0 types (#6207) --- beacon_chain/spec/datatypes/base.nim | 54 ------------------------- beacon_chain/spec/datatypes/phase0.nim | 54 +++++++++++++++++++++++++ beacon_chain/spec/mev/bellatrix_mev.nim | 1 + beacon_chain/spec/mev/capella_mev.nim | 1 + beacon_chain/spec/mev/deneb_mev.nim | 1 + beacon_chain/spec/mev/electra_mev.nim | 1 + 6 files changed, 58 insertions(+), 54 deletions(-) diff --git a/beacon_chain/spec/datatypes/base.nim b/beacon_chain/spec/datatypes/base.nim index 9cfe09827..7f38af834 100644 --- a/beacon_chain/spec/datatypes/base.nim +++ b/beacon_chain/spec/datatypes/base.nim @@ -250,20 +250,6 @@ type CommitteeValidatorsBits* = BitList[Limit MAX_VALIDATORS_PER_COMMITTEE] - # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#attestation - Attestation* = object - aggregation_bits*: CommitteeValidatorsBits - data*: AttestationData - signature*: ValidatorSig - - TrustedAttestation* = object - # The Trusted version, at the moment, implies that the cryptographic signature was checked. - # It DOES NOT imply that the state transition was verified. - # Currently the code MUST verify the state transition as soon as the signature is verified - aggregation_bits*: CommitteeValidatorsBits - data*: AttestationData - signature*: TrustedSig - ForkDigest* = distinct array[4, byte] # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#forkdata @@ -318,7 +304,6 @@ type ## Earliest epoch when voluntary exit can be processed validator_index*: uint64 # `ValidatorIndex` after validation - SomeAttestation* = Attestation | TrustedAttestation SomeIndexedAttestation* = IndexedAttestation | TrustedIndexedAttestation SomeProposerSlashing* = ProposerSlashing | TrustedProposerSlashing SomeAttesterSlashing* = AttesterSlashing | TrustedAttesterSlashing @@ -430,17 +415,6 @@ type message*: BeaconBlockHeader signature*: TrustedSig - # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/validator.md#aggregateandproof - AggregateAndProof* = object - aggregator_index*: uint64 # `ValidatorIndex` after validation - aggregate*: Attestation - selection_proof*: ValidatorSig - - # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/validator.md#signedaggregateandproof - SignedAggregateAndProof* = object - message*: AggregateAndProof - signature*: ValidatorSig - SyncCommitteeCache* = object current_sync_committee*: array[SYNC_COMMITTEE_SIZE, ValidatorIndex] next_sync_committee*: array[SYNC_COMMITTEE_SIZE, ValidatorIndex] @@ -842,16 +816,6 @@ func shortLog*(v: PendingAttestation): auto = proposer_index: v.proposer_index ) -func shortLog*(v: SomeAttestation): auto = - ( - aggregation_bits: v.aggregation_bits, - data: shortLog(v.data), - signature: shortLog(v.signature) - ) - -template asTrusted*(x: Attestation): TrustedAttestation = - isomorphicCast[TrustedAttestation](x) - func shortLog*(v: SomeIndexedAttestation): auto = ( attestating_indices: v.attesting_indices, @@ -894,7 +858,6 @@ func shortLog*(v: SomeSignedVoluntaryExit): auto = ) chronicles.formatIt AttestationData: it.shortLog -chronicles.formatIt Attestation: it.shortLog chronicles.formatIt Checkpoint: it.shortLog const @@ -926,23 +889,6 @@ func init*(T: type GraffitiBytes, input: string): GraffitiBytes raise newException(ValueError, "The graffiti value should be 32 characters or less") distinctBase(result)[0 ..< input.len] = toBytes(input) -func init*( - T: type Attestation, - indices_in_committee: openArray[uint64], - committee_len: int, - data: AttestationData, - signature: ValidatorSig): Result[T, cstring] = - var bits = CommitteeValidatorsBits.init(committee_len) - for index_in_committee in indices_in_committee: - if index_in_committee >= committee_len.uint64: return err("Invalid index for committee") - bits.setBit index_in_committee - - ok Attestation( - aggregation_bits: bits, - data: data, - signature: signature - ) - func defaultGraffitiBytes*(): GraffitiBytes = const graffitiBytes = toBytes("Nimbus/" & fullVersionStr) diff --git a/beacon_chain/spec/datatypes/phase0.nim b/beacon_chain/spec/datatypes/phase0.nim index 62978c95f..ecf165ccf 100644 --- a/beacon_chain/spec/datatypes/phase0.nim +++ b/beacon_chain/spec/datatypes/phase0.nim @@ -257,6 +257,31 @@ type root* {.dontSerialize.}: Eth2Digest # cached root of signed beacon block + # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#attestation + Attestation* = object + aggregation_bits*: CommitteeValidatorsBits + data*: AttestationData + signature*: ValidatorSig + + TrustedAttestation* = object + # The Trusted version, at the moment, implies that the cryptographic signature was checked. + # It DOES NOT imply that the state transition was verified. + # Currently the code MUST verify the state transition as soon as the signature is verified + aggregation_bits*: CommitteeValidatorsBits + data*: AttestationData + signature*: TrustedSig + + # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/validator.md#aggregateandproof + AggregateAndProof* = object + aggregator_index*: uint64 # `ValidatorIndex` after validation + aggregate*: Attestation + selection_proof*: ValidatorSig + + # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/validator.md#signedaggregateandproof + SignedAggregateAndProof* = object + message*: AggregateAndProof + signature*: ValidatorSig + SomeSignedBeaconBlock* = SignedBeaconBlock | SigVerifiedSignedBeaconBlock | @@ -270,6 +295,7 @@ type BeaconBlockBody | SigVerifiedBeaconBlockBody | TrustedBeaconBlockBody + SomeAttestation* = Attestation | TrustedAttestation EpochInfo* = object ## Information about the outcome of epoch processing @@ -277,6 +303,7 @@ type balances*: TotalBalances chronicles.formatIt BeaconBlock: it.shortLog +chronicles.formatIt Attestation: it.shortLog func clear*(info: var EpochInfo) = info.validators.setLen(0) @@ -314,6 +341,16 @@ func shortLog*(v: SomeSignedBeaconBlock): auto = signature: shortLog(v.signature) ) +func shortLog*(v: SomeAttestation): auto = + ( + aggregation_bits: v.aggregation_bits, + data: shortLog(v.data), + signature: shortLog(v.signature) + ) + +template asTrusted*(x: Attestation): TrustedAttestation = + isomorphicCast[TrustedAttestation](x) + template asSigned*( x: SigVerifiedSignedBeaconBlock | MsgTrustedSignedBeaconBlock | @@ -341,3 +378,20 @@ template asTrusted*( SigVerifiedSignedBeaconBlock | MsgTrustedSignedBeaconBlock): TrustedSignedBeaconBlock = isomorphicCast[TrustedSignedBeaconBlock](x) + +func init*( + T: type Attestation, + indices_in_committee: openArray[uint64], + committee_len: int, + data: AttestationData, + signature: ValidatorSig): Result[T, cstring] = + var bits = CommitteeValidatorsBits.init(committee_len) + for index_in_committee in indices_in_committee: + if index_in_committee >= committee_len.uint64: return err("Invalid index for committee") + bits.setBit index_in_committee + + ok Attestation( + aggregation_bits: bits, + data: data, + signature: signature + ) diff --git a/beacon_chain/spec/mev/bellatrix_mev.nim b/beacon_chain/spec/mev/bellatrix_mev.nim index e742470e8..bece01f85 100644 --- a/beacon_chain/spec/mev/bellatrix_mev.nim +++ b/beacon_chain/spec/mev/bellatrix_mev.nim @@ -8,6 +8,7 @@ {.push raises: [].} import ".."/datatypes/altair +from ".."/datatypes/phase0 import Attestation from ".."/datatypes/bellatrix import ExecutionPayloadHeader from ".."/eth2_merkleization import hash_tree_root diff --git a/beacon_chain/spec/mev/capella_mev.nim b/beacon_chain/spec/mev/capella_mev.nim index 2a69742ed..08e5066c8 100644 --- a/beacon_chain/spec/mev/capella_mev.nim +++ b/beacon_chain/spec/mev/capella_mev.nim @@ -8,6 +8,7 @@ {.push raises: [].} import ".."/datatypes/[altair, capella] +from ".."/datatypes/phase0 import Attestation from stew/byteutils import to0xHex from ../eth2_merkleization import fromSszBytes, hash_tree_root, toSszType diff --git a/beacon_chain/spec/mev/deneb_mev.nim b/beacon_chain/spec/mev/deneb_mev.nim index ca18920c2..23a289461 100644 --- a/beacon_chain/spec/mev/deneb_mev.nim +++ b/beacon_chain/spec/mev/deneb_mev.nim @@ -10,6 +10,7 @@ import ".."/datatypes/[altair, deneb] from stew/byteutils import to0xHex +from ".."/datatypes/phase0 import Attestation from ../datatypes/bellatrix import ExecutionAddress from ".."/datatypes/capella import SignedBLSToExecutionChange from ".."/eth2_merkleization import hash_tree_root diff --git a/beacon_chain/spec/mev/electra_mev.nim b/beacon_chain/spec/mev/electra_mev.nim index 6d02dc09e..e29049819 100644 --- a/beacon_chain/spec/mev/electra_mev.nim +++ b/beacon_chain/spec/mev/electra_mev.nim @@ -10,6 +10,7 @@ import ".."/datatypes/[altair, electra] from stew/byteutils import to0xHex +from ".."/datatypes/phase0 import Attestation from ../datatypes/bellatrix import ExecutionAddress from ".."/datatypes/capella import SignedBLSToExecutionChange from ".."/datatypes/deneb import BlobsBundle, KzgCommitments