-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #371 from multiversx/MX-13877-auction-list-api
Auction list api
- Loading branch information
Showing
16 changed files
with
306 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package data | ||
|
||
// AuctionNode holds data needed for a node in auction to respond to API calls | ||
type AuctionNode struct { | ||
BlsKey string `json:"blsKey"` | ||
Qualified bool `json:"qualified"` | ||
} | ||
|
||
// AuctionListValidatorAPIResponse holds the data needed for an auction node validator for responding to API calls | ||
type AuctionListValidatorAPIResponse struct { | ||
Owner string `json:"owner"` | ||
NumStakedNodes int64 `json:"numStakedNodes"` | ||
TotalTopUp string `json:"totalTopUp"` | ||
TopUpPerNode string `json:"topUpPerNode"` | ||
QualifiedTopUp string `json:"qualifiedTopUp"` | ||
AuctionList []*AuctionNode `json:"auctionList"` | ||
} | ||
|
||
// AuctionListResponse respects the format the auction list api response received from the observers | ||
type AuctionListResponse struct { | ||
AuctionListValidators []*AuctionListValidatorAPIResponse `json:"auctionList"` | ||
} | ||
|
||
// AuctionListAPIResponse respects the format the auction list received from the observers | ||
type AuctionListAPIResponse struct { | ||
Data AuctionListResponse `json:"data"` | ||
Error string `json:"error"` | ||
Code string `json:"code"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package process | ||
|
||
import ( | ||
"github.com/multiversx/mx-chain-core-go/core" | ||
"github.com/multiversx/mx-chain-proxy-go/data" | ||
) | ||
|
||
// GetAuctionList returns the auction list from a metachain observer node | ||
func (vsp *ValidatorStatisticsProcessor) GetAuctionList() (*data.AuctionListResponse, error) { | ||
observers, errFetchObs := vsp.proc.GetObservers(core.MetachainShardId) | ||
if errFetchObs != nil { | ||
return nil, errFetchObs | ||
} | ||
|
||
var valStatsResponse data.AuctionListAPIResponse | ||
for _, observer := range observers { | ||
_, err := vsp.proc.CallGetRestEndPoint(observer.Address, auctionListPath, &valStatsResponse) | ||
if err == nil { | ||
log.Info("auction list fetched from API", "observer", observer.Address) | ||
return &valStatsResponse.Data, nil | ||
} | ||
|
||
log.Error("getAuctionListFromApi", "observer", observer.Address, "error", "no response") | ||
} | ||
|
||
return nil, ErrAuctionListNotAvailable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package process | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/multiversx/mx-chain-core-go/core" | ||
"github.com/multiversx/mx-chain-proxy-go/data" | ||
"github.com/multiversx/mx-chain-proxy-go/process/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidatorStatisticsProcessor_GetAuctionList(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
node := &data.NodeData{ | ||
Address: "addr", | ||
ShardId: core.MetachainShardId, | ||
} | ||
expectedResp := &data.AuctionListAPIResponse{ | ||
Data: data.AuctionListResponse{ | ||
AuctionListValidators: []*data.AuctionListValidatorAPIResponse{ | ||
{ | ||
Owner: "owner", | ||
NumStakedNodes: 4, | ||
TotalTopUp: "100", | ||
TopUpPerNode: "50", | ||
QualifiedTopUp: "50", | ||
}, | ||
}, | ||
}, | ||
Error: "", | ||
Code: "ok", | ||
} | ||
expectedRespMarshalled, err := json.Marshal(expectedResp) | ||
require.Nil(t, err) | ||
|
||
processor := &mock.ProcessorStub{ | ||
GetObserversCalled: func(shardId uint32) ([]*data.NodeData, error) { | ||
require.Equal(t, core.MetachainShardId, shardId) | ||
|
||
return []*data.NodeData{node}, nil | ||
}, | ||
CallGetRestEndPointCalled: func(address string, path string, value interface{}) (int, error) { | ||
require.Equal(t, node.Address, address) | ||
require.Equal(t, auctionListPath, path) | ||
|
||
err = json.Unmarshal(expectedRespMarshalled, value) | ||
require.Nil(t, err) | ||
return 0, nil | ||
}, | ||
} | ||
vsp, _ := NewValidatorStatisticsProcessor(processor, &mock.ValStatsCacherMock{}, time.Second) | ||
resp, err := vsp.GetAuctionList() | ||
require.Nil(t, err) | ||
require.Equal(t, expectedResp.Data, *resp) | ||
}) | ||
|
||
t.Run("get observers failed, should return error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
errGetObservers := errors.New("err getting observers") | ||
callGetRestEndPointCalledCt := int32(0) | ||
|
||
processor := &mock.ProcessorStub{ | ||
GetObserversCalled: func(shardId uint32) ([]*data.NodeData, error) { | ||
require.Equal(t, core.MetachainShardId, shardId) | ||
return nil, errGetObservers | ||
}, | ||
CallGetRestEndPointCalled: func(address string, path string, value interface{}) (int, error) { | ||
atomic.AddInt32(&callGetRestEndPointCalledCt, 1) | ||
|
||
return 0, nil | ||
}, | ||
} | ||
vsp, _ := NewValidatorStatisticsProcessor(processor, &mock.ValStatsCacherMock{}, time.Second) | ||
|
||
resp, err := vsp.GetAuctionList() | ||
require.Equal(t, errGetObservers, err) | ||
require.Nil(t, resp) | ||
require.Equal(t, int32(0), callGetRestEndPointCalledCt) | ||
}) | ||
|
||
t.Run("could not get auction list from observer, should return error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
node := &data.NodeData{ | ||
Address: "addr", | ||
ShardId: core.MetachainShardId, | ||
} | ||
|
||
errCallEndpoint := errors.New("error call endpoint") | ||
processor := &mock.ProcessorStub{ | ||
GetObserversCalled: func(shardId uint32) ([]*data.NodeData, error) { | ||
require.Equal(t, core.MetachainShardId, shardId) | ||
|
||
return []*data.NodeData{node}, nil | ||
}, | ||
CallGetRestEndPointCalled: func(address string, path string, value interface{}) (int, error) { | ||
require.Equal(t, node.Address, address) | ||
require.Equal(t, auctionListPath, path) | ||
|
||
return 0, errCallEndpoint | ||
}, | ||
} | ||
vsp, _ := NewValidatorStatisticsProcessor(processor, &mock.ValStatsCacherMock{}, time.Second) | ||
|
||
resp, err := vsp.GetAuctionList() | ||
require.Equal(t, ErrAuctionListNotAvailable, err) | ||
require.Nil(t, resp) | ||
}) | ||
} |
Oops, something went wrong.