From 489b34a01f47211e39705af6fe7621dcb4803ad4 Mon Sep 17 00:00:00 2001 From: Potuz Date: Fri, 22 Sep 2023 16:48:24 -0300 Subject: [PATCH] Use headstate when requesting current epochs (#12940) * Use headstate when requesting current epochs * gazelle * review requests --- beacon-chain/rpc/eth/validator/BUILD.bazel | 1 + beacon-chain/rpc/eth/validator/handlers.go | 32 +++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/beacon-chain/rpc/eth/validator/BUILD.bazel b/beacon-chain/rpc/eth/validator/BUILD.bazel index 32054be99..18903e880 100644 --- a/beacon-chain/rpc/eth/validator/BUILD.bazel +++ b/beacon-chain/rpc/eth/validator/BUILD.bazel @@ -18,6 +18,7 @@ go_library( "//beacon-chain/cache:go_default_library", "//beacon-chain/core/feed/operation:go_default_library", "//beacon-chain/core/helpers:go_default_library", + "//beacon-chain/core/transition:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/kv:go_default_library", "//beacon-chain/operations/attestations:go_default_library", diff --git a/beacon-chain/rpc/eth/validator/handlers.go b/beacon-chain/rpc/eth/validator/handlers.go index 0b0d4215e..ffb9b6522 100644 --- a/beacon-chain/rpc/eth/validator/handlers.go +++ b/beacon-chain/rpc/eth/validator/handlers.go @@ -19,6 +19,7 @@ import ( "github.com/prysmaticlabs/prysm/v4/beacon-chain/builder" "github.com/prysmaticlabs/prysm/v4/beacon-chain/cache" "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/helpers" + "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/transition" "github.com/prysmaticlabs/prysm/v4/beacon-chain/db/kv" "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/core" rpchelpers "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/helpers" @@ -804,10 +805,33 @@ func (s *Server) GetProposerDuties(w http.ResponseWriter, r *http.Request) { http2.HandleError(w, fmt.Sprintf("Could not get start slot of epoch %d: %v", requestedEpoch, err), http.StatusInternalServerError) return } - st, err := s.Stater.StateBySlot(ctx, epochStartSlot) - if err != nil { - http2.HandleError(w, fmt.Sprintf("Could not get state for slot %d: %v ", epochStartSlot, err), http.StatusInternalServerError) - return + var st state.BeaconState + // if the requested epoch is new, use the head state and the next slot cache + if requestedEpoch < currentEpoch { + st, err = s.Stater.StateBySlot(ctx, epochStartSlot) + if err != nil { + http2.HandleError(w, fmt.Sprintf("Could not get state for slot %d: %v ", epochStartSlot, err), http.StatusInternalServerError) + return + } + } else { + st, err = s.HeadFetcher.HeadState(ctx) + if err != nil { + http2.HandleError(w, fmt.Sprintf("Could not get head state: %v ", err), http.StatusInternalServerError) + return + } + // Advance state with empty transitions up to the requested epoch start slot. + if st.Slot() < epochStartSlot { + headRoot, err := s.HeadFetcher.HeadRoot(ctx) + if err != nil { + http2.HandleError(w, fmt.Sprintf("Could not get head root: %v ", err), http.StatusInternalServerError) + return + } + st, err = transition.ProcessSlotsUsingNextSlotCache(ctx, st, headRoot, epochStartSlot) + if err != nil { + http2.HandleError(w, fmt.Sprintf("Could not process slots up to %d: %v ", epochStartSlot, err), http.StatusInternalServerError) + return + } + } } var proposals map[primitives.ValidatorIndex][]primitives.Slot