Files
prysm/testing/endtoend/evaluators/execution_engine.go
james-prysm 45fd3eb1bf gRPC Gateway Removal (#14089)
* wip passing e2e

* reverting temp comment

* remove unneeded comments

* fixing merge errors

* fixing more bugs from merge

* fixing test

* WIP moving code around and fixing tests

* unused linting

* gaz

* temp removing these tests as we need placeholder/wrapper APIs for them with the removal of the gateway

* attempting to remove dependencies to gRPC gateway , 1 mroe left in deps.bzl

* renaming flags and other gateway services to http

* goimport

* fixing deepsource

* git mv

* Update validator/package/validator.yaml

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update validator/package/validator.yaml

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update cmd/beacon-chain/flags/base.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update cmd/beacon-chain/flags/base.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update cmd/beacon-chain/flags/base.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* addressing feedback

* missed lint

* renaming import

* reversal based on feedback

* fixing web ui registration

* don't require mux handler

* gaz

* removing gRPC service from validator completely, merged with http service, renames are a work in progress

* updating go.sum

* linting

* trailing white space

* realized there was more cleanup i could do with code reuse

* adding wrapper for routes

* reverting version

* fixing dependencies from merging develop

* gaz

* fixing unit test

* fixing dependencies

* reverting unit test

* fixing conflict

* updating change log

* Update log.go

Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>

* gaz

* Update api/server/httprest/server.go

Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>

* addressing some feedback

* forgot to remove deprecated flag in usage

* gofmt

* fixing test

* fixing deepsource issue

* moving deprecated flag and adding timeout handler

* missed removal of a flag

* fixing test:

* Update CHANGELOG.md

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* addressing feedback

* updating comments based on feedback

* removing unused field for now, we can add it back in if we need to use the option

* removing unused struct

* changing api-timeout flag based on feedback

---------

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>
2024-09-04 15:40:31 +00:00

138 lines
4.2 KiB
Go

package evaluators
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/network/httputil"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
"github.com/prysmaticlabs/prysm/v5/testing/endtoend/params"
"github.com/prysmaticlabs/prysm/v5/testing/endtoend/policies"
"github.com/prysmaticlabs/prysm/v5/testing/endtoend/types"
"github.com/prysmaticlabs/prysm/v5/time/slots"
"google.golang.org/grpc"
)
// OptimisticSyncEnabled checks that the node is in an optimistic state.
var OptimisticSyncEnabled = types.Evaluator{
Name: "optimistic_sync_at_epoch_%d",
Policy: policies.AllEpochs,
Evaluation: optimisticSyncEnabled,
}
func optimisticSyncEnabled(_ *types.EvaluationContext, conns ...*grpc.ClientConn) error {
for nodeIndex := range conns {
path := fmt.Sprintf("http://localhost:%d/eth/v1/beacon/blinded_blocks/head", params.TestParams.Ports.PrysmBeaconNodeHTTPPort+nodeIndex)
resp := structs.GetBlockV2Response{}
httpResp, err := http.Get(path) // #nosec G107 -- path can't be constant because it depends on port param and node index
if err != nil {
return err
}
if httpResp.StatusCode != http.StatusOK {
e := httputil.DefaultJsonError{}
if err = json.NewDecoder(httpResp.Body).Decode(&e); err != nil {
return err
}
return fmt.Errorf("%s (status code %d)", e.Message, e.Code)
}
if err = json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return err
}
headSlot, err := retrieveHeadSlot(&resp)
if err != nil {
return err
}
currEpoch := slots.ToEpoch(primitives.Slot(headSlot))
startSlot, err := slots.EpochStart(currEpoch)
if err != nil {
return err
}
for i := startSlot; i <= primitives.Slot(headSlot); i++ {
path = fmt.Sprintf("http://localhost:%d/eth/v1/beacon/blinded_blocks/%d", params.TestParams.Ports.PrysmBeaconNodeHTTPPort+nodeIndex, i)
resp = structs.GetBlockV2Response{}
httpResp, err = http.Get(path) // #nosec G107 -- path can't be constant because it depends on port param and node index
if err != nil {
return err
}
if httpResp.StatusCode == http.StatusNotFound {
// Continue in the event of non-existent blocks.
continue
}
if httpResp.StatusCode != http.StatusOK {
e := httputil.DefaultJsonError{}
if err = json.NewDecoder(httpResp.Body).Decode(&e); err != nil {
return err
}
return fmt.Errorf("%s (status code %d)", e.Message, e.Code)
}
if err = json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return err
}
if !resp.ExecutionOptimistic {
return errors.New("expected block to be optimistic, but it is not")
}
}
}
return nil
}
func retrieveHeadSlot(resp *structs.GetBlockV2Response) (uint64, error) {
var headSlot uint64
var err error
switch resp.Version {
case version.String(version.Phase0):
b := &structs.BeaconBlock{}
if err := json.Unmarshal(resp.Data.Message, b); err != nil {
return 0, err
}
headSlot, err = strconv.ParseUint(b.Slot, 10, 64)
if err != nil {
return 0, err
}
case version.String(version.Altair):
b := &structs.BeaconBlockAltair{}
if err := json.Unmarshal(resp.Data.Message, b); err != nil {
return 0, err
}
headSlot, err = strconv.ParseUint(b.Slot, 10, 64)
if err != nil {
return 0, err
}
case version.String(version.Bellatrix):
b := &structs.BeaconBlockBellatrix{}
if err := json.Unmarshal(resp.Data.Message, b); err != nil {
return 0, err
}
headSlot, err = strconv.ParseUint(b.Slot, 10, 64)
if err != nil {
return 0, err
}
case version.String(version.Capella):
b := &structs.BeaconBlockCapella{}
if err := json.Unmarshal(resp.Data.Message, b); err != nil {
return 0, err
}
headSlot, err = strconv.ParseUint(b.Slot, 10, 64)
if err != nil {
return 0, err
}
case version.String(version.Deneb):
b := &structs.BeaconBlockDeneb{}
if err := json.Unmarshal(resp.Data.Message, b); err != nil {
return 0, err
}
headSlot, err = strconv.ParseUint(b.Slot, 10, 64)
if err != nil {
return 0, err
}
default:
return 0, errors.New("no valid block type retrieved")
}
return headSlot, nil
}