Fix a few bugs in the API (#9099)

This commit is contained in:
Radosław Kapka 2021-06-26 20:17:45 +02:00 committed by GitHub
parent b114247836
commit 9eb3ff6bdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 5 deletions

View File

@ -21,6 +21,7 @@ func (f *BeaconEndpointFactory) Paths() []string {
"/eth/v1/beacon/states/{state_id}/fork",
"/eth/v1/beacon/states/{state_id}/finality_checkpoints",
"/eth/v1/beacon/states/{state_id}/validators",
"/eth/v1/beacon/states/{state_id}/validators/{validator_id}",
"/eth/v1/beacon/states/{state_id}/validator_balances",
"/eth/v1/beacon/states/{state_id}/committees",
"/eth/v1/beacon/headers",
@ -74,6 +75,12 @@ func (f *BeaconEndpointFactory) Create(path string) (*gateway.Endpoint, error) {
Err: &gateway.DefaultErrorJson{},
}
case "/eth/v1/beacon/states/{state_id}/validators":
endpoint = gateway.Endpoint{
GetRequestQueryParams: []gateway.QueryParam{{Name: "id", Hex: true}, {Name: "status", Enum: true}},
GetResponse: &stateValidatorsResponseJson{},
Err: &gateway.DefaultErrorJson{},
}
case "/eth/v1/beacon/states/{state_id}/validators/{validator_id}":
endpoint = gateway.Endpoint{
GetResponse: &stateValidatorResponseJson{},
Err: &gateway.DefaultErrorJson{},
@ -162,6 +169,7 @@ func (f *BeaconEndpointFactory) Create(path string) (*gateway.Endpoint, error) {
}
case "/eth/v1/node/peers":
endpoint = gateway.Endpoint{
GetRequestQueryParams: []gateway.QueryParam{{Name: "state", Enum: true}, {Name: "direction", Enum: true}},
GetResponse: &peersResponseJson{},
Err: &gateway.DefaultErrorJson{},
}

View File

@ -41,6 +41,11 @@ type stateFinalityCheckpointResponse_StateFinalityCheckpointJson struct {
Finalized *checkpointJson `json:"finalized"`
}
// stateValidatorResponseJson is used in /beacon/states/{state_id}/validators API endpoint.
type stateValidatorsResponseJson struct {
Data []*validatorContainerJson `json:"data"`
}
// stateValidatorResponseJson is used in /beacon/states/{state_id}/validators/{validator_id} API endpoint.
type stateValidatorResponseJson struct {
Data *validatorContainerJson `json:"data"`

View File

@ -224,9 +224,7 @@ func (ns *Server) ListPeers(ctx context.Context, req *ethpb.PeersRequest) (*ethp
}
filteredPeers = append(filteredPeers, p)
}
if len(filteredPeers) == 0 {
return nil, status.Error(codes.NotFound, "Peers not found")
}
return &ethpb.PeersResponse{Data: filteredPeers}, nil
}

View File

@ -367,6 +367,19 @@ func TestListPeers(t *testing.T) {
}
}
func TestListPeers_NoPeersReturnsEmptyArray(t *testing.T) {
peerFetcher := &mockp2p.MockPeersProvider{}
peerFetcher.ClearPeers()
s := Server{PeersFetcher: peerFetcher}
resp, err := s.ListPeers(context.Background(), &ethpb.PeersRequest{
State: []ethpb.ConnectionState{ethpb.ConnectionState_CONNECTED},
})
require.NoError(t, err)
require.NotNil(t, resp.Data)
assert.Equal(t, 0, len(resp.Data))
}
func TestPeerCount(t *testing.T) {
ids := libp2ptest.GeneratePeerIDs(10)
peerFetcher := &mockp2p.MockPeersProvider{}