mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-05-02 03:02:54 -04:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
216 lines
6.0 KiB
Go
216 lines
6.0 KiB
Go
package beaconapi
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/OffchainLabs/prysm/v7/api"
|
|
"github.com/OffchainLabs/prysm/v7/testing/endtoend/params"
|
|
"github.com/pkg/errors"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
errEmptyPrysmData = errors.New("Prysm data is empty")
|
|
errEmptyLighthouseData = errors.New("Lighthouse data is empty")
|
|
)
|
|
|
|
const (
|
|
msgWrongJSON = "JSON response has wrong structure, expected %T, got %T"
|
|
msgRequestFailed = "%s request failed with response code %d with response body %s"
|
|
msgUnknownNode = "unknown node type %s"
|
|
msgSSZUnmarshalFailed = "failed to unmarshal SSZ"
|
|
)
|
|
|
|
func doJSONGETRequest(template, requestPath string, beaconNodeIdx int, resp any, bnType ...string) error {
|
|
if len(bnType) == 0 {
|
|
bnType = []string{"Prysm"}
|
|
}
|
|
|
|
var port int
|
|
switch bnType[0] {
|
|
case "Prysm":
|
|
port = params.TestParams.Ports.PrysmBeaconNodeHTTPPort
|
|
case "Lighthouse":
|
|
port = params.TestParams.Ports.LighthouseBeaconNodeHTTPPort
|
|
default:
|
|
return fmt.Errorf(msgUnknownNode, bnType[0])
|
|
}
|
|
|
|
basePath := fmt.Sprintf(template, port+beaconNodeIdx)
|
|
httpResp, err := http.Get(basePath + requestPath)
|
|
if err != nil {
|
|
return errors.Wrap(err, "request failed")
|
|
}
|
|
|
|
var body any
|
|
if httpResp.StatusCode != http.StatusOK {
|
|
if httpResp.Header.Get("Content-Type") == api.JsonMediaType {
|
|
if err = json.NewDecoder(httpResp.Body).Decode(&body); err != nil {
|
|
return errors.Wrap(err, "failed to decode response body")
|
|
}
|
|
} else {
|
|
defer closeBody(httpResp.Body)
|
|
body, err = io.ReadAll(httpResp.Body)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to read response body")
|
|
}
|
|
}
|
|
return fmt.Errorf(msgRequestFailed, bnType[0], httpResp.StatusCode, body)
|
|
}
|
|
|
|
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
|
|
return errors.Wrap(err, "failed to decode response body")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func doSSZGETRequest(template, requestPath string, beaconNodeIdx int, bnType ...string) ([]byte, error) {
|
|
if len(bnType) == 0 {
|
|
bnType = []string{"Prysm"}
|
|
}
|
|
|
|
var port int
|
|
switch bnType[0] {
|
|
case "Prysm":
|
|
port = params.TestParams.Ports.PrysmBeaconNodeHTTPPort
|
|
case "Lighthouse":
|
|
port = params.TestParams.Ports.LighthouseBeaconNodeHTTPPort
|
|
default:
|
|
return nil, fmt.Errorf(msgUnknownNode, bnType[0])
|
|
}
|
|
|
|
basePath := fmt.Sprintf(template, port+beaconNodeIdx)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, basePath+requestPath, http.NoBody)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to build request")
|
|
}
|
|
req.Header.Set("Accept", "application/octet-stream")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "request failed")
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
var body any
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
return nil, errors.Wrap(err, "failed to decode response body")
|
|
}
|
|
return nil, fmt.Errorf(msgRequestFailed, bnType[0], resp.StatusCode, body)
|
|
}
|
|
defer closeBody(resp.Body)
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to read response body")
|
|
}
|
|
|
|
return body, nil
|
|
}
|
|
|
|
func doJSONPOSTRequest(template, requestPath string, beaconNodeIdx int, postObj, resp any, bnType ...string) error {
|
|
if len(bnType) == 0 {
|
|
bnType = []string{"Prysm"}
|
|
}
|
|
|
|
var port int
|
|
switch bnType[0] {
|
|
case "Prysm":
|
|
port = params.TestParams.Ports.PrysmBeaconNodeHTTPPort
|
|
case "Lighthouse":
|
|
port = params.TestParams.Ports.LighthouseBeaconNodeHTTPPort
|
|
default:
|
|
return fmt.Errorf(msgUnknownNode, bnType[0])
|
|
}
|
|
|
|
basePath := fmt.Sprintf(template, port+beaconNodeIdx)
|
|
b, err := json.Marshal(postObj)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to marshal POST object")
|
|
}
|
|
httpResp, err := http.Post(
|
|
basePath+requestPath,
|
|
"application/json",
|
|
bytes.NewBuffer(b),
|
|
)
|
|
if err != nil {
|
|
return errors.Wrap(err, "request failed")
|
|
}
|
|
|
|
var body any
|
|
if httpResp.StatusCode != http.StatusOK {
|
|
if httpResp.Header.Get("Content-Type") == api.JsonMediaType {
|
|
if err = json.NewDecoder(httpResp.Body).Decode(&body); err != nil {
|
|
return errors.Wrap(err, "failed to decode response body")
|
|
}
|
|
} else {
|
|
defer closeBody(httpResp.Body)
|
|
body, err = io.ReadAll(httpResp.Body)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to read response body")
|
|
}
|
|
}
|
|
return fmt.Errorf(msgRequestFailed, bnType[0], httpResp.StatusCode, body)
|
|
}
|
|
|
|
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
|
|
return errors.Wrap(err, "failed to decode response body")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func doSSZPOSTRequest(template, requestPath string, beaconNodeIdx int, postObj any, bnType ...string) ([]byte, error) {
|
|
if len(bnType) == 0 {
|
|
bnType = []string{"Prysm"}
|
|
}
|
|
|
|
var port int
|
|
switch bnType[0] {
|
|
case "Prysm":
|
|
port = params.TestParams.Ports.PrysmBeaconNodeHTTPPort
|
|
case "Lighthouse":
|
|
port = params.TestParams.Ports.LighthouseBeaconNodeHTTPPort
|
|
default:
|
|
return nil, fmt.Errorf(msgUnknownNode, bnType[0])
|
|
}
|
|
|
|
basePath := fmt.Sprintf(template, port+beaconNodeIdx)
|
|
b, err := json.Marshal(postObj)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to marshal POST object")
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, basePath+requestPath, bytes.NewBuffer(b))
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to build request")
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/octet-stream")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "request failed")
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
var body any
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
return nil, errors.Wrap(err, "failed to decode response body")
|
|
}
|
|
return nil, fmt.Errorf(msgRequestFailed, bnType[0], resp.StatusCode, body)
|
|
}
|
|
defer closeBody(resp.Body)
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to read response body")
|
|
}
|
|
|
|
return body, nil
|
|
}
|
|
|
|
func closeBody(body io.Closer) {
|
|
if err := body.Close(); err != nil {
|
|
log.WithError(err).Error("Could not close response body")
|
|
}
|
|
}
|