From 34d3bd2c39a6e1bd99a222318483418e8e53f608 Mon Sep 17 00:00:00 2001 From: Giulio Date: Sun, 7 Jan 2024 17:11:04 +0100 Subject: [PATCH] save --- cl/beacon/handler/builder.go | 69 ++++++++++++++++++++++++++++++++++++ cl/beacon/handler/format.go | 2 +- cl/beacon/handler/handler.go | 1 + 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 cl/beacon/handler/builder.go diff --git a/cl/beacon/handler/builder.go b/cl/beacon/handler/builder.go new file mode 100644 index 00000000000..d7174008681 --- /dev/null +++ b/cl/beacon/handler/builder.go @@ -0,0 +1,69 @@ +package handler + +import ( + "net/http" + + libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon/cl/beacon/beaconhttp" + "github.com/ledgerwatch/erigon/cl/clparams" + "github.com/ledgerwatch/erigon/cl/persistence/beacon_indicies" + "github.com/ledgerwatch/erigon/cl/phase1/core/state" +) + +func (a *ApiHandler) GetEth1V1BuilderStatesExpectedWit(w http.ResponseWriter, r *http.Request) (*beaconResponse, error) { + ctx := r.Context() + + tx, err := a.indiciesDB.BeginRo(ctx) + if err != nil { + return nil, err + } + defer tx.Rollback() + + blockId, err := stateIdFromRequest(r) + if err != nil { + return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, err.Error()) + } + root, httpStatus, err := a.blockRootFromStateId(ctx, tx, blockId) + if err != nil { + return nil, beaconhttp.NewEndpointError(httpStatus, err.Error()) + } + slot, err := beacon_indicies.ReadBlockSlotByBlockRoot(tx, root) + if err != nil { + return nil, err + } + if slot == nil { + return nil, beaconhttp.NewEndpointError(http.StatusNotFound, "state not found") + } + if a.beaconChainCfg.GetCurrentStateVersion(*slot/a.beaconChainCfg.SlotsPerEpoch) < clparams.CapellaVersion { + return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, "the specified state is not a capella state") + } + headRoot, _, err := a.forkchoiceStore.GetHead() + if err != nil { + return nil, err + } + if root == headRoot { + s, cn := a.syncedData.HeadState() + defer cn() + return newBeaconResponse(state.ExpectedWithdrawals(s)).withFinalized(false), nil + } + lookAhead := 1024 + for currSlot := *slot + 1; currSlot < *slot+uint64(lookAhead); currSlot++ { + if currSlot > a.syncedData.HeadSlot() { + return nil, beaconhttp.NewEndpointError(http.StatusNotFound, "state not found") + } + blockRoot, err := beacon_indicies.ReadCanonicalBlockRoot(tx, currSlot) + if err != nil { + return nil, err + } + if blockRoot == (libcommon.Hash{}) { + continue + } + blk, err := a.blockReader.ReadBlockByRoot(ctx, tx, blockRoot) + if err != nil { + return nil, err + } + return newBeaconResponse(blk.Block.Body.ExecutionPayload.Withdrawals).withFinalized(false), nil + } + + return nil, beaconhttp.NewEndpointError(http.StatusNotFound, "state not found") +} diff --git a/cl/beacon/handler/format.go b/cl/beacon/handler/format.go index 7baa88b42f2..3a96cf6e8c0 100644 --- a/cl/beacon/handler/format.go +++ b/cl/beacon/handler/format.go @@ -170,7 +170,7 @@ func stateIdFromRequest(r *http.Request) (*segmentID, error) { stateId := chi.URLParam(r, "state_id") if !regex.MatchString(stateId) { - return nil, fmt.Errorf("invalid path variable: {block_id}") + return nil, fmt.Errorf("invalid path variable: {state_id}") } if stateId == "head" { diff --git a/cl/beacon/handler/handler.go b/cl/beacon/handler/handler.go index eb2fc2e90f6..9ead9cb0b2f 100644 --- a/cl/beacon/handler/handler.go +++ b/cl/beacon/handler/handler.go @@ -49,6 +49,7 @@ func (a *ApiHandler) init() { // otterscn specific ones are commented as such r.Route("/eth", func(r chi.Router) { r.Route("/v1", func(r chi.Router) { + r.Get("/builder/states/{state_id}/expected_withdrawals", beaconhttp.HandleEndpointFunc(a.GetEth1V1BuilderStatesExpectedWit)) r.Get("/events", http.NotFound) r.Route("/node", func(r chi.Router) { r.Get("/health", a.GetEthV1NodeHealth)