Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use middleware to handle comma-separated query params #12995

Merged
merged 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions beacon-chain/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
"node.go",
"options.go",
"prometheus.go",
"router.go",
],
importpath = "github.com/prysmaticlabs/prysm/v4/beacon-chain/node",
visibility = [
Expand Down Expand Up @@ -40,6 +41,7 @@ go_library(
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/rpc:go_default_library",
"//beacon-chain/rpc/apimiddleware:go_default_library",
"//beacon-chain/rpc/eth/helpers:go_default_library",
"//beacon-chain/slasher:go_default_library",
"//beacon-chain/startup:go_default_library",
"//beacon-chain/state:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func New(cliCtx *cli.Context, opts ...Option) (*BeaconNode, error) {

log.Debugln("Registering RPC Service")
router := mux.NewRouter()
router.Use(middleware)
if err := beacon.registerRPCService(router); err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions beacon-chain/node/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package node

import (
"net/http"

"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/helpers"
)

func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
helpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

next.ServeHTTP(w, r)
})
}
4 changes: 0 additions & 4 deletions beacon-chain/rpc/eth/beacon/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,6 @@ func (s *Server) GetBlockHeaders(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "beacon.GetBlockHeaders")
defer span.End()

query := r.URL.Query()
helpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

rawSlot := r.URL.Query().Get("slot")
rawParentRoot := r.URL.Query().Get("parent_root")

Expand Down
8 changes: 0 additions & 8 deletions beacon-chain/rpc/eth/beacon/handlers_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ func (s *Server) GetValidators(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "beacon.GetValidators")
defer span.End()

query := r.URL.Query()
helpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

stateId := mux.Vars(r)["state_id"]
if stateId == "" {
http2.HandleError(w, "state_id is required in URL params", http.StatusBadRequest)
Expand Down Expand Up @@ -215,10 +211,6 @@ func (bs *Server) GetValidatorBalances(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "beacon.GetValidatorBalances")
defer span.End()

query := r.URL.Query()
helpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

stateId := mux.Vars(r)["state_id"]
if stateId == "" {
http2.HandleError(w, "state_id is required in URL params", http.StatusBadRequest)
Expand Down
30 changes: 0 additions & 30 deletions beacon-chain/rpc/eth/beacon/handlers_validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,36 +154,6 @@ func TestGetValidators(t *testing.T) {
assert.Equal(t, "20", resp.Data[0].Index)
assert.Equal(t, "60", resp.Data[1].Index)
})
t.Run("multiple comma-separated", func(t *testing.T) {
chainService := &chainMock.ChainService{}
s := Server{
Stater: &testutil.MockStater{
BeaconState: st,
},
HeadFetcher: chainService,
OptimisticModeFetcher: chainService,
FinalizationFetcher: chainService,
}

pubkey := st.PubkeyAtIndex(primitives.ValidatorIndex(20))
hexPubkey := hexutil.Encode(pubkey[:])
request := httptest.NewRequest(
http.MethodGet,
fmt.Sprintf("http://example.com/eth/v1/beacon/states/{state_id}/validators?id=%s,60", hexPubkey),
nil,
)
request = mux.SetURLVars(request, map[string]string{"state_id": "head"})
writer := httptest.NewRecorder()
writer.Body = &bytes.Buffer{}

s.GetValidators(writer, request)
assert.Equal(t, http.StatusOK, writer.Code)
resp := &GetValidatorsResponse{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
require.Equal(t, 2, len(resp.Data))
assert.Equal(t, "20", resp.Data[0].Index)
assert.Equal(t, "60", resp.Data[1].Index)
})
t.Run("state ID required", func(t *testing.T) {
s := Server{
Stater: &testutil.MockStater{
Expand Down
1 change: 0 additions & 1 deletion beacon-chain/rpc/eth/blob/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ go_library(
deps = [
"//beacon-chain/blockchain:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/rpc/eth/helpers:go_default_library",
"//beacon-chain/rpc/lookup:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
Expand Down
5 changes: 1 addition & 4 deletions beacon-chain/rpc/eth/blob/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/helpers"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/lookup"
field_params "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/config/params"
Expand Down Expand Up @@ -122,9 +121,7 @@ func (s *Server) Blobs(w http.ResponseWriter, r *http.Request) {

// parseIndices filters out invalid and duplicate blob indices
func parseIndices(url *url.URL) []uint64 {
query := url.Query()
helpers.NormalizeQueryValues(query)
rawIndices := query["indices"]
rawIndices := url.Query()["indices"]
indices := make([]uint64, 0, field_params.MaxBlobsPerBlock)
loop:
for _, raw := range rawIndices {
Expand Down
12 changes: 0 additions & 12 deletions beacon-chain/rpc/eth/validator/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ func (s *Server) GetAggregateAttestation(w http.ResponseWriter, r *http.Request)
ctx, span := trace.StartSpan(r.Context(), "validator.GetAggregateAttestation")
defer span.End()

query := r.URL.Query()
rpchelpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

attDataRoot := r.URL.Query().Get("attestation_data_root")
attDataRootBytes, valid := shared.ValidateHex(w, "Attestation data root", attDataRoot, fieldparams.RootLength)
if !valid {
Expand Down Expand Up @@ -402,10 +398,6 @@ func (s *Server) GetAttestationData(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "validator.GetAttestationData")
defer span.End()

query := r.URL.Query()
rpchelpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

if shared.IsSyncing(ctx, w, s.SyncChecker, s.HeadFetcher, s.TimeFetcher, s.OptimisticModeFetcher) {
return
}
Expand Down Expand Up @@ -458,10 +450,6 @@ func (s *Server) ProduceSyncCommitteeContribution(w http.ResponseWriter, r *http
ctx, span := trace.StartSpan(r.Context(), "validator.ProduceSyncCommitteeContribution")
defer span.End()

query := r.URL.Query()
rpchelpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

subIndex := r.URL.Query().Get("subcommittee_index")
index, valid := shared.ValidateUint(w, "Subcommittee Index", subIndex)
if !valid {
Expand Down
5 changes: 0 additions & 5 deletions beacon-chain/rpc/eth/validator/handlers_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/api"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/helpers"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared"
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
Expand All @@ -32,10 +31,6 @@ func (s *Server) ProduceBlockV3(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "validator.ProduceBlockV3")
defer span.End()

query := r.URL.Query()
helpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

if shared.IsSyncing(r.Context(), w, s.SyncChecker, s.HeadFetcher, s.TimeFetcher, s.OptimisticModeFetcher) {
return
}
Expand Down
4 changes: 0 additions & 4 deletions beacon-chain/rpc/prysm/validator/validator_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func (vs *Server) GetValidatorCount(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "beacon.GetValidatorCount")
defer span.End()

query := r.URL.Query()
helpers.NormalizeQueryValues(query)
r.URL.RawQuery = query.Encode()

stateID := mux.Vars(r)["state_id"]

isOptimistic, err := helpers.IsOptimistic(ctx, []byte(stateID), vs.OptimisticModeFetcher, vs.Stater, vs.ChainInfoFetcher, vs.BeaconDB)
Expand Down
13 changes: 13 additions & 0 deletions testing/endtoend/evaluators/beaconapi_evaluators/beacon_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@
"json": &beacon.GetBlockHeaderResponse{},
},
},
// we want to test comma-seperated query params

Check failure on line 191 in testing/endtoend/evaluators/beaconapi_evaluators/beacon_api.go

View workflow job for this annotation

GitHub Actions / Lint

`seperated` is a misspelling of `separated` (misspell)
"/beacon/states/{param1}/validators?id=0,1": {
basepath: v1MiddlewarePathTemplate,
params: func(_ string, e primitives.Epoch) []string {
return []string{"head"}
},
prysmResps: map[string]interface{}{
"json": &beacon.GetValidatorsResponse{},
},
lighthouseResps: map[string]interface{}{
"json": &beacon.GetValidatorsResponse{},
},
},
"/node/identity": {
basepath: v1MiddlewarePathTemplate,
params: func(_ string, _ primitives.Epoch) []string {
Expand Down
Loading