Skip to content

Commit

Permalink
Merge pull request #4934 from multiversx/feat/staking-v4
Browse files Browse the repository at this point in the history
Feat/staking v4
  • Loading branch information
mariusmihaic authored Mar 7, 2024
2 parents 0a6277c + 8bfcd2e commit 271fd46
Show file tree
Hide file tree
Showing 324 changed files with 25,007 additions and 9,140 deletions.
37 changes: 36 additions & 1 deletion api/groups/validatorGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import (
"github.com/multiversx/mx-chain-core-go/data/validator"
"github.com/multiversx/mx-chain-go/api/errors"
"github.com/multiversx/mx-chain-go/api/shared"
"github.com/multiversx/mx-chain-go/common"
)

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]*validator.ValidatorStatistics, 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
76 changes: 71 additions & 5 deletions api/groups/validatorGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/multiversx/mx-chain-go/api/groups"
"github.com/multiversx/mx-chain-go/api/mock"
"github.com/multiversx/mx-chain-go/api/shared"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -34,11 +35,18 @@ func TestNewValidatorGroup(t *testing.T) {
}

// ValidatorStatisticsResponse is the response for the validator statistics endpoint.
type ValidatorStatisticsResponse struct {
type validatorStatisticsResponse struct {
Result map[string]*validator.ValidatorStatistics `json:"statistics"`
Error string `json:"error"`
}

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

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

Expand All @@ -60,7 +68,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 @@ -97,7 +105,7 @@ func TestValidatorStatistics_ReturnsSuccessfully(t *testing.T) {
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)
Expand Down Expand Up @@ -147,14 +155,13 @@ func TestValidatorGroup_UpdateFacade(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)
Expand Down Expand Up @@ -191,12 +198,71 @@ func TestValidatorGroup_IsInterfaceNil(t *testing.T) {
require.False(t, validatorGroup.IsInterfaceNil())
}

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 := auctionListResponse{}
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",
NumStakedNodes: 4,
TotalTopUp: "1234",
TopUpPerNode: "4321",
QualifiedTopUp: "4444",
},
}
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 := auctionListResponse{}
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
Loading

0 comments on commit 271fd46

Please sign in to comment.