Skip to content

Commit

Permalink
Merge pull request #4068 from ElrondNetwork/auction-list-api-endpoint
Browse files Browse the repository at this point in the history
Staking V4: auction list api endpoint
  • Loading branch information
mariusmihaic committed Jun 16, 2022
2 parents 5c883f5 + 160a2c4 commit ac70458
Show file tree
Hide file tree
Showing 33 changed files with 757 additions and 209 deletions.
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

0 comments on commit ac70458

Please sign in to comment.