Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Mar 22, 2021
2 parents fa51499 + 3f1f8fb commit c06475c
Show file tree
Hide file tree
Showing 53 changed files with 2,567 additions and 1,106 deletions.
4 changes: 2 additions & 2 deletions api/mock/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Facade struct {
GetAllESDTTokensCalled func(address string) ([]string, error)
GetBlockByHashCalled func(hash string, withTxs bool) (*api.Block, error)
GetBlockByNonceCalled func(nonce uint64, withTxs bool) (*api.Block, error)
GetTotalStakedValueHandler func() (*big.Int, error)
GetTotalStakedValueHandler func() (*api.StakeValues, error)
}

// GetUsername -
Expand Down Expand Up @@ -214,7 +214,7 @@ func (f *Facade) StatusMetrics() external.StatusMetricsHandler {
}

// GetTotalStakedValue -
func (f *Facade) GetTotalStakedValue() (*big.Int, error) {
func (f *Facade) GetTotalStakedValue() (*api.StakeValues, error) {
return f.GetTotalStakedValueHandler()
}

Expand Down
42 changes: 11 additions & 31 deletions api/network/routes.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package network

import (
"math/big"
"net/http"

"github.com/ElrondNetwork/elrond-go/api/errors"
"github.com/ElrondNetwork/elrond-go/api/shared"
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/data/api"
"github.com/ElrondNetwork/elrond-go/node/external"
"github.com/gin-gonic/gin"
)

const (
getConfigPath = "/config"
getStatusPath = "/status"
economicsPath = "/economics"
totalStakedPath = "/total-staked"
getConfigPath = "/config"
getStatusPath = "/status"
economicsPath = "/economics"
)

// FacadeHandler interface defines methods that can be used by the gin webserver
type FacadeHandler interface {
GetTotalStakedValue() (*big.Int, error)
GetTotalStakedValue() (*api.StakeValues, error)
StatusMetrics() external.StatusMetricsHandler
IsInterfaceNil() bool
}
Expand All @@ -30,7 +30,6 @@ func Routes(router *wrapper.RouterWrapper) {
router.RegisterHandler(http.MethodGet, getConfigPath, GetNetworkConfig)
router.RegisterHandler(http.MethodGet, getStatusPath, GetNetworkStatus)
router.RegisterHandler(http.MethodGet, economicsPath, EconomicsMetrics)
router.RegisterHandler(http.MethodGet, totalStakedPath, GetTotalStaked)
}

func getFacade(c *gin.Context) (FacadeHandler, bool) {
Expand Down Expand Up @@ -106,25 +105,7 @@ func EconomicsMetrics(c *gin.Context) {
return
}

metrics := facade.StatusMetrics().EconomicsMetrics()
c.JSON(
http.StatusOK,
shared.GenericAPIResponse{
Data: gin.H{"metrics": metrics},
Error: "",
Code: shared.ReturnCodeSuccess,
},
)
}

// GetTotalStaked is the endpoint that will return the total staked value
func GetTotalStaked(c *gin.Context) {
facade, ok := getFacade(c)
if !ok {
return
}

totalStakedValue, err := facade.GetTotalStakedValue()
stakeValues, err := facade.GetTotalStakedValue()
if err != nil {
c.JSON(
http.StatusInternalServerError,
Expand All @@ -137,15 +118,14 @@ func GetTotalStaked(c *gin.Context) {
return
}

totalStakedValueStr := "0"
if totalStakedValue != nil {
totalStakedValueStr = totalStakedValue.String()
}
metrics := facade.StatusMetrics().EconomicsMetrics()
metrics[core.MetricTotalStakedValue] = stakeValues.TotalStaked.String()
metrics[core.MetricTopUpValue] = stakeValues.TopUp.String()

c.JSON(
http.StatusOK,
shared.GenericAPIResponse{
Data: gin.H{"totalStakedValue": totalStakedValueStr},
Data: gin.H{"metrics": metrics},
Error: "",
Code: shared.ReturnCodeSuccess,
},
Expand Down
54 changes: 25 additions & 29 deletions api/network/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/data/api"
"github.com/ElrondNetwork/elrond-go/node/external"
"github.com/ElrondNetwork/elrond-go/statusHandler"
"github.com/gin-contrib/cors"
Expand Down Expand Up @@ -156,7 +157,14 @@ func TestEconomicsMetrics_ShouldWork(t *testing.T) {
value := "12345"
statusMetricsProvider.SetStringValue(key, value)

facade := mock.Facade{}
facade := mock.Facade{
GetTotalStakedValueHandler: func() (*api.StakeValues, error) {
return &api.StakeValues{
TotalStaked: big.NewInt(100),
TopUp: big.NewInt(20),
}, nil
},
}
facade.StatusMetricsHandler = func() external.StatusMetricsHandler {
return statusMetricsProvider
}
Expand All @@ -174,40 +182,28 @@ func TestEconomicsMetrics_ShouldWork(t *testing.T) {
assert.True(t, keyAndValueFoundInResponse)
}

func TestTotalStaked_NilContextShouldErr(t *testing.T) {
ws := startNodeServer(nil)
req, _ := http.NewRequest(http.MethodGet, "/network/total-staked", 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 TestEconomicsMetrics_CannotGetStakeValues(t *testing.T) {
statusMetricsProvider := statusHandler.NewStatusMetrics()
key := core.MetricTotalSupply
value := "12345"
statusMetricsProvider.SetStringValue(key, value)

func TestTotalStaked_ShouldWork(t *testing.T) {
totalStaked := big.NewInt(250000000)
facade := &mock.Facade{}
facade.GetTotalStakedValueHandler = func() (*big.Int, error) {
return totalStaked, nil
localErr := fmt.Errorf("%s", "local error")
facade := mock.Facade{
GetTotalStakedValueHandler: func() (*api.StakeValues, error) {
return nil, localErr
},
}
facade.StatusMetricsHandler = func() external.StatusMetricsHandler {
return statusMetricsProvider
}

ws := startNodeServer(facade)
req, err := http.NewRequest(http.MethodGet, "/network/total-staked", nil)
fmt.Println(err)
ws := startNodeServer(&facade)
req, _ := http.NewRequest("GET", "/network/economics", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

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

key := "totalStakedValue"
keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, totalStaked.String())
assert.True(t, keyAndValueFoundInResponse)
assert.Equal(t, resp.Code, http.StatusInternalServerError)
}

func loadResponse(rsp io.Reader, destination interface{}) {
Expand Down
15 changes: 11 additions & 4 deletions cmd/assessment/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io/ioutil"
"os"
"runtime"
"strings"
"time"

logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/cmd/assessment/benchmarks"
Expand All @@ -18,6 +20,8 @@ import (
)

const maxMachineIDLen = 10
const hostPlaceholder = "%host"
const timestampPlaceholder = "%time"

var (
nodeHelpTemplate = `NAME:
Expand All @@ -39,8 +43,8 @@ VERSION:
// outputFile defines a flag for the benchmarks output file. Data will be written in csv format.
outputFile = cli.StringFlag{
Name: "output-file",
Usage: "The output file where benchmarks will be written in csv format.",
Value: "./output.csv",
Usage: "The output file format where benchmarks will be written in csv format.",
Value: "./output-" + hostPlaceholder + "-" + timestampPlaceholder + ".csv",
}

log = logger.GetOrCreate("main")
Expand Down Expand Up @@ -75,7 +79,7 @@ func main() {
}

app.Action = func(c *cli.Context) error {
return startAssessment(c, app.Version)
return startAssessment(c, app.Version, machineID)
}

err = app.Run(os.Args)
Expand All @@ -85,8 +89,11 @@ func main() {
}
}

func startAssessment(c *cli.Context, version string) error {
func startAssessment(c *cli.Context, version string, machineID string) error {
outputFileName := c.GlobalString(outputFile.Name)
outputFileName = strings.Replace(outputFileName, hostPlaceholder, machineID, 1)
outputFileName = strings.Replace(outputFileName, timestampPlaceholder, fmt.Sprintf("%d", time.Now().Unix()), 1)

log.Info("Saving benchmarks result", "file", outputFileName)
log.Info("Starting host assessment process...")
sw := core.NewStopWatch()
Expand Down
3 changes: 0 additions & 3 deletions cmd/node/config/api.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
# /network/status will return metrics related to current status of the chain (epoch, nonce, round)
{ Name = "/status", Open = true },

# /network/total-staked will return total staked value
{ Name = "/total-staked", Open = true },

# /network/economics will return all economics related metrics
{ Name = "/economics", Open = true },

Expand Down
1 change: 1 addition & 0 deletions cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
GasScheduleByEpochs = [
{ StartEpoch = 0, FileName = "gasScheduleV1.toml" },
{ StartEpoch = 3, FileName = "gasScheduleV2.toml" },
{ StartEpoch = 4, FileName = "gasScheduleV3.toml" },
]

[StoragePruning]
Expand Down
Loading

0 comments on commit c06475c

Please sign in to comment.