From e4de52f24cb3501bfd10e3a377a0d646a828fbec Mon Sep 17 00:00:00 2001 From: Saolyn Date: Wed, 20 Nov 2024 15:43:56 -0800 Subject: [PATCH] Use slot to determine version --- beacon-chain/rpc/eth/beacon/handlers_pool.go | 24 ++++++++------------ beacon-chain/rpc/eth/validator/handlers.go | 14 ++++-------- runtime/version/fork.go | 24 +++++++++++++++++++- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/beacon-chain/rpc/eth/beacon/handlers_pool.go b/beacon-chain/rpc/eth/beacon/handlers_pool.go index f522e659b5a7..5241193de5a0 100644 --- a/beacon-chain/rpc/eth/beacon/handlers_pool.go +++ b/beacon-chain/rpc/eth/beacon/handlers_pool.go @@ -87,7 +87,7 @@ func (s *Server) ListAttestations(w http.ResponseWriter, r *http.Request) { // ListAttestationsV2 retrieves attestations known by the node but // not necessarily incorporated into any block. Allows filtering by committee index or slot. func (s *Server) ListAttestationsV2(w http.ResponseWriter, r *http.Request) { - ctx, span := trace.StartSpan(r.Context(), "beacon.ListAttestationsV2") + _, span := trace.StartSpan(r.Context(), "beacon.ListAttestationsV2") defer span.End() rawSlot, slot, ok := shared.UintFromQuery(w, r, "slot", false) @@ -99,12 +99,7 @@ func (s *Server) ListAttestationsV2(w http.ResponseWriter, r *http.Request) { return } - headState, err := s.ChainInfoFetcher.HeadStateReadOnly(ctx) - if err != nil { - httputil.HandleError(w, "Could not get head state: "+err.Error(), http.StatusInternalServerError) - return - } - + v := version.FromEpoch(slots.ToEpoch(primitives.Slot(slot))) attestations := s.AttestationsPool.AggregatedAttestations() unaggAtts, err := s.AttestationsPool.UnaggregatedAttestations() if err != nil { @@ -116,7 +111,7 @@ func (s *Server) ListAttestationsV2(w http.ResponseWriter, r *http.Request) { filteredAtts := make([]interface{}, 0, len(attestations)) for _, att := range attestations { var includeAttestation bool - if headState.Version() >= version.Electra { + if v >= version.Electra { attElectra, ok := att.(*eth.AttestationElectra) if !ok { httputil.HandleError(w, fmt.Sprintf("Unable to convert attestation of type %T", att), http.StatusInternalServerError) @@ -149,9 +144,9 @@ func (s *Server) ListAttestationsV2(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set(api.VersionHeader, version.String(headState.Version())) + w.Header().Set(api.VersionHeader, version.String(v)) httputil.WriteJson(w, &structs.ListAttestationsResponse{ - Version: version.String(headState.Version()), + Version: version.String(v), Data: attsData, }) } @@ -726,18 +721,19 @@ func (s *Server) GetAttesterSlashingsV2(w http.ResponseWriter, r *http.Request) ctx, span := trace.StartSpan(r.Context(), "beacon.GetAttesterSlashingsV2") defer span.End() + slot := s.HeadFetcher.HeadSlot() + v := version.FromEpoch(slots.ToEpoch(slot)) headState, err := s.ChainInfoFetcher.HeadStateReadOnly(ctx) if err != nil { httputil.HandleError(w, "Could not get head state: "+err.Error(), http.StatusInternalServerError) return } - var attStructs []interface{} sourceSlashings := s.SlashingsPool.PendingAttesterSlashings(ctx, headState, true /* return unlimited slashings */) for _, slashing := range sourceSlashings { var attStruct interface{} - if headState.Version() >= version.Electra { + if v >= version.Electra { a, ok := slashing.(*eth.AttesterSlashingElectra) if !ok { httputil.HandleError(w, fmt.Sprintf("Unable to convert slashing of type %T to an Electra slashing", slashing), http.StatusInternalServerError) @@ -762,10 +758,10 @@ func (s *Server) GetAttesterSlashingsV2(w http.ResponseWriter, r *http.Request) } resp := &structs.GetAttesterSlashingsResponse{ - Version: version.String(headState.Version()), + Version: version.String(v), Data: attBytes, } - w.Header().Set(api.VersionHeader, version.String(headState.Version())) + w.Header().Set(api.VersionHeader, version.String(v)) httputil.WriteJson(w, resp) } diff --git a/beacon-chain/rpc/eth/validator/handlers.go b/beacon-chain/rpc/eth/validator/handlers.go index 1b56aba5360a..6649142d7abf 100644 --- a/beacon-chain/rpc/eth/validator/handlers.go +++ b/beacon-chain/rpc/eth/validator/handlers.go @@ -75,7 +75,7 @@ func (s *Server) GetAggregateAttestation(w http.ResponseWriter, r *http.Request) // GetAggregateAttestationV2 aggregates all attestations matching the given attestation data root and slot, returning the aggregated result. func (s *Server) GetAggregateAttestationV2(w http.ResponseWriter, r *http.Request) { - ctx, span := trace.StartSpan(r.Context(), "validator.GetAggregateAttestationV2") + _, span := trace.StartSpan(r.Context(), "validator.GetAggregateAttestationV2") defer span.End() _, attDataRoot, ok := shared.HexFromQuery(w, r, "attestation_data_root", fieldparams.RootLength, true) @@ -91,14 +91,15 @@ func (s *Server) GetAggregateAttestationV2(w http.ResponseWriter, r *http.Reques return } + v := version.FromEpoch(slots.ToEpoch(primitives.Slot(slot))) agg := s.aggregatedAttestation(w, primitives.Slot(slot), attDataRoot, primitives.CommitteeIndex(index)) if agg == nil { return } resp := &structs.AggregateAttestationResponse{ - Version: version.String(agg.Version()), + Version: version.String(v), } - if agg.Version() >= version.Electra { + if v >= version.Electra { typedAgg, ok := agg.(*ethpbalpha.AttestationElectra) if !ok { httputil.HandleError(w, fmt.Sprintf("Attestation is not of type %T", ðpbalpha.AttestationElectra{}), http.StatusInternalServerError) @@ -123,12 +124,7 @@ func (s *Server) GetAggregateAttestationV2(w http.ResponseWriter, r *http.Reques } resp.Data = data } - headState, err := s.ChainInfoFetcher.HeadStateReadOnly(ctx) - if err != nil { - httputil.HandleError(w, "Could not get head state: "+err.Error(), http.StatusInternalServerError) - return - } - w.Header().Set(api.VersionHeader, version.String(headState.Version())) + w.Header().Set(api.VersionHeader, version.String(v)) httputil.WriteJson(w, resp) } diff --git a/runtime/version/fork.go b/runtime/version/fork.go index 902393c8bc80..e1587cffdf77 100644 --- a/runtime/version/fork.go +++ b/runtime/version/fork.go @@ -1,6 +1,10 @@ package version -import "github.com/pkg/errors" +import ( + "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/v5/config/params" + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" +) const ( Phase0 = iota @@ -36,6 +40,24 @@ func FromString(name string) (int, error) { return v, nil } +// FromEpoch translates an epoch into it's corresponding version. +func FromEpoch(epoch primitives.Epoch) int { + switch { + case epoch >= params.BeaconConfig().ElectraForkEpoch: + return Electra + case epoch >= params.BeaconConfig().DenebForkEpoch: + return Deneb + case epoch >= params.BeaconConfig().CapellaForkEpoch: + return Capella + case epoch >= params.BeaconConfig().BellatrixForkEpoch: + return Bellatrix + case epoch >= params.BeaconConfig().AltairForkEpoch: + return Altair + default: + return Phase0 + } +} + // String returns the canonical string form of a version. // Unrecognized versions won't generate an error and are represented by the string "unknown version". func String(version int) string {