Skip to content

Commit

Permalink
Merge branch 'master' into merge-master-feat-esdt-2021.04.09
Browse files Browse the repository at this point in the history
# Conflicts:
#	api/mock/facade.go
#	api/network/routes.go
#	api/network/routes_test.go
#	cmd/node/config/api.toml
#	facade/nodeFacade_test.go
  • Loading branch information
iulianpascalau committed Apr 9, 2021
2 parents b9dd408 + daf18b8 commit c330246
Show file tree
Hide file tree
Showing 100 changed files with 3,614 additions and 695 deletions.
16 changes: 14 additions & 2 deletions api/mock/facade.go
Expand Up @@ -53,6 +53,8 @@ type Facade struct {
GetBlockByNonceCalled func(nonce uint64, withTxs bool) (*api.Block, error)
GetTotalStakedValueHandler func() (*api.StakeValues, error)
GetAllIssuedESDTsCalled func() ([]string, error)
GetDirectStakedListHandler func() ([]*api.DirectStakedValue, error)
GetDelegatorsListHandler func() ([]*api.Delegator, error)
}

// GetUsername -
Expand Down Expand Up @@ -198,7 +200,7 @@ func (f *Facade) SendBulkTransactions(txs []*transaction.Transaction) (uint64, e
return f.SendBulkTransactionsHandler(txs)
}

//ValidateTransaction --
// ValidateTransaction -
func (f *Facade) ValidateTransaction(tx *transaction.Transaction) error {
return f.ValidateTransactionHandler(tx)
}
Expand Down Expand Up @@ -228,7 +230,17 @@ func (f *Facade) GetTotalStakedValue() (*api.StakeValues, error) {
return f.GetTotalStakedValueHandler()
}

// ComputeTransactionGasLimit --
// GetDirectStakedList -
func (f *Facade) GetDirectStakedList() ([]*api.DirectStakedValue, error) {
return f.GetDirectStakedListHandler()
}

// GetDelegatorsList -
func (f *Facade) GetDelegatorsList() ([]*api.Delegator, error) {
return f.GetDelegatorsListHandler()
}

// ComputeTransactionGasLimit -
func (f *Facade) ComputeTransactionGasLimit(tx *transaction.Transaction) (*transaction.CostResponse, error) {
return f.ComputeTransactionGasLimitHandler(tx)
}
Expand Down
74 changes: 70 additions & 4 deletions api/network/routes.go
Expand Up @@ -13,15 +13,19 @@ import (
)

const (
getConfigPath = "/config"
getStatusPath = "/status"
economicsPath = "/economics"
getESDTsPath = "/esdts"
getConfigPath = "/config"
getStatusPath = "/status"
economicsPath = "/economics"
getESDTsPath = "/esdts"
directStakedInfoPath = "/direct-staked-info"
delegatedInfoPath = "/delegated-info"
)

// FacadeHandler interface defines methods that can be used by the gin webserver
type FacadeHandler interface {
GetTotalStakedValue() (*api.StakeValues, error)
GetDirectStakedList() ([]*api.DirectStakedValue, error)
GetDelegatorsList() ([]*api.Delegator, error)
StatusMetrics() external.StatusMetricsHandler
GetAllIssuedESDTs() ([]string, error)
IsInterfaceNil() bool
Expand All @@ -33,6 +37,8 @@ func Routes(router *wrapper.RouterWrapper) {
router.RegisterHandler(http.MethodGet, getStatusPath, GetNetworkStatus)
router.RegisterHandler(http.MethodGet, economicsPath, EconomicsMetrics)
router.RegisterHandler(http.MethodGet, getESDTsPath, GetAllIssuedESDTs)
router.RegisterHandler(http.MethodGet, directStakedInfoPath, DirectStakedInfo)
router.RegisterHandler(http.MethodGet, delegatedInfoPath, DelegatedInfo)
}

func getFacade(c *gin.Context) (FacadeHandler, bool) {
Expand Down Expand Up @@ -164,3 +170,63 @@ func GetAllIssuedESDTs(c *gin.Context) {
},
)
}

// DirectStakedInfo is the endpoint that will return the directed staked info list
func DirectStakedInfo(c *gin.Context) {
facade, ok := getFacade(c)
if !ok {
return
}

directStakedList, err := facade.GetDirectStakedList()
if err != nil {
c.JSON(
http.StatusInternalServerError,
shared.GenericAPIResponse{
Data: nil,
Error: err.Error(),
Code: shared.ReturnCodeInternalError,
},
)
return
}

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

// DelegatedInfo is the endpoint that will return the delegated list
func DelegatedInfo(c *gin.Context) {
facade, ok := getFacade(c)
if !ok {
return
}

delegatedList, err := facade.GetDelegatorsList()
if err != nil {
c.JSON(
http.StatusInternalServerError,
shared.GenericAPIResponse{
Data: nil,
Error: err.Error(),
Code: shared.ReturnCodeInternalError,
},
)
return
}

c.JSON(
http.StatusOK,
shared.GenericAPIResponse{
Data: gin.H{"list": delegatedList},
Error: "",
Code: shared.ReturnCodeSuccess,
},
)
}
173 changes: 173 additions & 0 deletions api/network/routes_test.go
Expand Up @@ -252,6 +252,176 @@ func TestGetAllIssuedESDTs_Error(t *testing.T) {
assert.Equal(t, resp.Code, http.StatusInternalServerError)
}

func TestDirectStakedInfo_NilContextShouldErr(t *testing.T) {
ws := startNodeServer(nil)
req, _ := http.NewRequest("GET", "/network/direct-staked-info", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

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

assert.Equal(t, shared.ReturnCodeInternalError, response.Code)
assert.True(t, strings.Contains(response.Error, errors.ErrNilAppContext.Error()))
}

func TestDirectStakedInfo_ShouldWork(t *testing.T) {
stakedData1 := api.DirectStakedValue{
Address: "addr1",
Staked: "1111",
TopUp: "2222",
Total: "3333",
}
stakedData2 := api.DirectStakedValue{
Address: "addr2",
Staked: "4444",
TopUp: "5555",
Total: "9999",
}

facade := mock.Facade{
GetDirectStakedListHandler: func() ([]*api.DirectStakedValue, error) {
return []*api.DirectStakedValue{&stakedData1, &stakedData2}, nil
},
}

ws := startNodeServer(&facade)
req, _ := http.NewRequest("GET", "/network/direct-staked-info", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

valuesFoundInResponse := directStakedFoundInResponse(respStr, stakedData1) && directStakedFoundInResponse(respStr, stakedData2)
assert.True(t, valuesFoundInResponse)
}

func directStakedFoundInResponse(response string, stakedData api.DirectStakedValue) bool {
return strings.Contains(response, stakedData.Address) && strings.Contains(response, stakedData.Total) &&
strings.Contains(response, stakedData.Staked) && strings.Contains(response, stakedData.TopUp)
}

func TestDirectStakedInfo_CannotGetDirectStakedList(t *testing.T) {
expectedError := fmt.Errorf("%s", "expected error")
facade := mock.Facade{
GetDirectStakedListHandler: func() ([]*api.DirectStakedValue, error) {
return nil, expectedError
},
}

ws := startNodeServer(&facade)
req, _ := http.NewRequest("GET", "/network/direct-staked-info", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respStr := string(respBytes)

assert.Equal(t, resp.Code, http.StatusInternalServerError)
assert.True(t, strings.Contains(respStr, expectedError.Error()))
}

func TestDelegatedInfo_NilContextShouldErr(t *testing.T) {
ws := startNodeServer(nil)
req, _ := http.NewRequest("GET", "/network/delegated-info", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

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

assert.Equal(t, shared.ReturnCodeInternalError, response.Code)
assert.True(t, strings.Contains(response.Error, errors.ErrNilAppContext.Error()))
}

func TestDelegatedInfo_ShouldWork(t *testing.T) {
delegator1 := api.Delegator{
DelegatorAddress: "addr1",
DelegatedTo: []*api.DelegatedValue{
{
DelegationScAddress: "addr2",
Value: "1111",
},
{
DelegationScAddress: "addr3",
Value: "2222",
},
},
Total: "3333",
TotalAsBigInt: big.NewInt(4444),
}

delegator2 := api.Delegator{
DelegatorAddress: "addr4",
DelegatedTo: []*api.DelegatedValue{
{
DelegationScAddress: "addr5",
Value: "5555",
},
{
DelegationScAddress: "addr6",
Value: "6666",
},
},
Total: "12221",
TotalAsBigInt: big.NewInt(13331),
}

facade := mock.Facade{
GetDelegatorsListHandler: func() ([]*api.Delegator, error) {
return []*api.Delegator{&delegator1, &delegator2}, nil
},
}

ws := startNodeServer(&facade)
req, _ := http.NewRequest("GET", "/network/delegated-info", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

valuesFoundInResponse := delegatorFoundInResponse(respStr, delegator1) && delegatorFoundInResponse(respStr, delegator2)
assert.True(t, valuesFoundInResponse)
}

func delegatorFoundInResponse(response string, delegator api.Delegator) bool {
if strings.Contains(response, delegator.TotalAsBigInt.String()) {
//we should have not encoded the total as big int
return false
}

found := strings.Contains(response, delegator.DelegatorAddress) && strings.Contains(response, delegator.Total)
for _, delegatedTo := range delegator.DelegatedTo {
found = found && strings.Contains(response, delegatedTo.Value) && strings.Contains(response, delegatedTo.DelegationScAddress)
}

return found
}

func TestDelegatedInfo_CannotGetDelegatedList(t *testing.T) {
expectedError := fmt.Errorf("%s", "expected error")
facade := mock.Facade{
GetDelegatorsListHandler: func() ([]*api.Delegator, error) {
return nil, expectedError
},
}

ws := startNodeServer(&facade)
req, _ := http.NewRequest("GET", "/network/delegated-info", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respStr := string(respBytes)

assert.Equal(t, resp.Code, http.StatusInternalServerError)
assert.True(t, strings.Contains(respStr, expectedError.Error()))
}

func loadResponse(rsp io.Reader, destination interface{}) {
jsonParser := json.NewDecoder(rsp)
err := jsonParser.Decode(destination)
Expand Down Expand Up @@ -302,6 +472,9 @@ func getRoutesConfig() config.ApiRoutesConfig {
{Name: "/status", Open: true},
{Name: "/economics", Open: true},
{Name: "/esdts", Open: true},
{Name: "/total-staked", Open: true},
{Name: "/direct-staked-info", Open: true},
{Name: "/delegated-info", Open: true},
},
},
},
Expand Down
10 changes: 9 additions & 1 deletion cmd/node/config/api.toml
Expand Up @@ -71,7 +71,15 @@
{ Name = "/config", Open = true },

# /network/esdts will return all the issued esdts on the protocol
{ Name = "/esdts", Open = true }
{ Name = "/esdts", Open = true },

# /network/direct-staked-info will return a list containing direct staked list of addresses
# and their staked values
{Name = "/direct-staked-info", Open = false},

# /network/delegated-info will return a list containing delegated list of addresses
# and their staked values on the system delegation smart contracts
{Name = "/delegated-info", Open = false}
]

[APIPackages.log]
Expand Down
10 changes: 6 additions & 4 deletions cmd/node/config/config.toml
Expand Up @@ -381,9 +381,9 @@

[TrieNodesDataPool]
Name = "TrieNodesDataPool"
Capacity = 900000
Capacity = 300000
Type = "SizeLRU"
SizeInBytes = 314572800 #300MB
SizeInBytes = 104857600 #100MB

[SmartContractDataPool]
Name = "SmartContractDataPool"
Expand Down Expand Up @@ -788,5 +788,7 @@
LogFileLifeSpanInSec = 86400

[TrieSync]
NumConcurrentTrieSyncers = 2000
MaxHardCapForMissingNodes = 500
NumConcurrentTrieSyncers = 200
MaxHardCapForMissingNodes = 5000
#available versions: 1 and 2. 1 is the initial version, 2 is updated, more efficient version
TrieSyncerVersion = 2

0 comments on commit c330246

Please sign in to comment.