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

Staking V4: auction list api endpoint #4068

Merged
merged 15 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 36 additions & 1 deletion api/groups/validatorGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import (
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go/api/errors"
"github.com/ElrondNetwork/elrond-go/api/shared"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/state"
"github.com/gin-gonic/gin"
)

const statisticsPath = "/statistics"
const (
statisticsPath = "/statistics"
auctionPath = "/auction"
)

// validatorFacadeHandler defines the methods to be implemented by a facade for validator requests
type validatorFacadeHandler interface {
ValidatorStatisticsApi() (map[string]*state.ValidatorApiResponse, error)
AuctionListApi() ([]*common.AuctionListValidatorAPIResponse, error)
IsInterfaceNil() bool
}

Expand All @@ -43,6 +48,11 @@ func NewValidatorGroup(facade validatorFacadeHandler) (*validatorGroup, error) {
Method: http.MethodGet,
Handler: ng.statistics,
},
{
Path: auctionPath,
Method: http.MethodGet,
Handler: ng.auction,
},
}
ng.endpoints = endpoints

Expand Down Expand Up @@ -74,6 +84,31 @@ func (vg *validatorGroup) statistics(c *gin.Context) {
)
}

// auction will return the list of the validators in the auction list
func (vg *validatorGroup) auction(c *gin.Context) {
valStats, err := vg.getFacade().AuctionListApi()
if err != nil {
c.JSON(
http.StatusBadRequest,
shared.GenericAPIResponse{
Data: nil,
Error: err.Error(),
Code: shared.ReturnCodeRequestError,
},
)
return
}

c.JSON(
http.StatusOK,
shared.GenericAPIResponse{
Data: gin.H{"auctionList": valStats},
Error: "",
Code: shared.ReturnCodeSuccess,
},
)
}

func (vg *validatorGroup) getFacade() validatorFacadeHandler {
vg.mutFacade.RLock()
defer vg.mutFacade.RUnlock()
Expand Down
74 changes: 68 additions & 6 deletions api/groups/validatorGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ElrondNetwork/elrond-go/api/groups"
"github.com/ElrondNetwork/elrond-go/api/mock"
"github.com/ElrondNetwork/elrond-go/api/shared"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/state"
"github.com/stretchr/testify/assert"
Expand All @@ -33,11 +34,18 @@ func TestNewValidatorGroup(t *testing.T) {
})
}

type ValidatorStatisticsResponse struct {
type validatorStatisticsResponse struct {
Result map[string]*state.ValidatorApiResponse `json:"statistics"`
Error string `json:"error"`
}

type auctionListReponse struct {
Data struct {
Result []*common.AuctionListValidatorAPIResponse `json:"auctionList"`
} `json:"data"`
Error string
}

func TestValidatorStatistics_ErrorWhenFacadeFails(t *testing.T) {
t.Parallel()

Expand All @@ -59,7 +67,7 @@ func TestValidatorStatistics_ErrorWhenFacadeFails(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

response := ValidatorStatisticsResponse{}
response := validatorStatisticsResponse{}
loadResponse(resp.Body, &response)

assert.Equal(t, http.StatusBadRequest, resp.Code)
Expand Down Expand Up @@ -87,31 +95,85 @@ func TestValidatorStatistics_ReturnsSuccessfully(t *testing.T) {
require.NoError(t, err)

ws := startWebServer(validatorGroup, "validator", getValidatorRoutesConfig())

req, _ := http.NewRequest("GET", "/validator/statistics", nil)

resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

response := shared.GenericAPIResponse{}
loadResponse(resp.Body, &response)

validatorStatistics := ValidatorStatisticsResponse{}
validatorStatistics := validatorStatisticsResponse{}
mapResponseData := response.Data.(map[string]interface{})
mapResponseDataBytes, _ := json.Marshal(mapResponseData)
_ = json.Unmarshal(mapResponseDataBytes, &validatorStatistics)

assert.Equal(t, http.StatusOK, resp.Code)

assert.Equal(t, validatorStatistics.Result, mapToReturn)
}

func TestAuctionList_ErrorWhenFacadeFails(t *testing.T) {
t.Parallel()

errStr := "error in facade"
facade := mock.FacadeStub{
AuctionListHandler: func() ([]*common.AuctionListValidatorAPIResponse, error) {
return nil, errors.New(errStr)
},
}

validatorGroup, err := groups.NewValidatorGroup(&facade)
require.NoError(t, err)

ws := startWebServer(validatorGroup, "validator", getValidatorRoutesConfig())
req, _ := http.NewRequest("GET", "/validator/auction", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

response := auctionListReponse{}
loadResponse(resp.Body, &response)

assert.Equal(t, http.StatusBadRequest, resp.Code)
assert.Contains(t, response.Error, errStr)
}

func TestAuctionList_ReturnsSuccessfully(t *testing.T) {
t.Parallel()

auctionListToReturn := []*common.AuctionListValidatorAPIResponse{
{
Owner: "owner",
NodeKey: "nodeKey",
TopUp: "112233",
},
}
facade := mock.FacadeStub{
AuctionListHandler: func() ([]*common.AuctionListValidatorAPIResponse, error) {
return auctionListToReturn, nil
},
}

validatorGroup, err := groups.NewValidatorGroup(&facade)
require.NoError(t, err)

ws := startWebServer(validatorGroup, "validator", getValidatorRoutesConfig())
req, _ := http.NewRequest("GET", "/validator/auction", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

response := auctionListReponse{}
loadResponse(resp.Body, &response)

assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, response.Data.Result, auctionListToReturn)
}

func getValidatorRoutesConfig() config.ApiRoutesConfig {
return config.ApiRoutesConfig{
APIPackages: map[string]config.APIPackageConfig{
"validator": {
Routes: []config.RouteConfig{
{Name: "/statistics", Open: true},
{Name: "/auction", Open: true},
},
},
},
Expand Down