Skip to content

Commit

Permalink
Merge pull request #131 from ElrondNetwork/development
Browse files Browse the repository at this point in the history
Merge Development into Main
  • Loading branch information
andreibancioiu authored Jan 15, 2021
2 parents 0f88dd9 + 047a87a commit d9e4b22
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 122 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: "CodeQL"

on:
pull_request:
branches: [development]
types: [opened, ready_for_review]
push:
workflow_dispatch:

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
language: ['go']
# Learn more...
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2


# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
32 changes: 32 additions & 0 deletions .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Go

on:
pull_request:
branches: [development]
types: [opened, ready_for_review]
push:
workflow_dispatch:

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.15.5
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build
run: cd cmd/proxy/ && go build -v .
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ For more details, go to [docs.elrond.com](https://docs.elrond.com/sdk-and-tools/
- `/v1.0/network/status/:shard` (GET) --> returns the status metrics from an observer in the given shard
- `/v1.0/network/config` (GET) --> returns the configuration of the network from any observer
- `/v1.0/network/economics` (GET) --> returns the economics data metric from the last epoch
- `/v1.0/network/total-staked` (GET) --> returns the total staked value from the validators contract

### node

Expand Down
12 changes: 12 additions & 0 deletions api/groups/baseNetworkGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewNetworkGroup(facadeHandler data.FacadeHandler) (*networkGroup, error) {
"/status/:shard": {Handler: ng.getNetworkStatusData, Method: http.MethodGet},
"/config": {Handler: ng.getNetworkConfigData, Method: http.MethodGet},
"/economics": {Handler: ng.getEconomicsData, Method: http.MethodGet},
"/total-staked": {Handler: ng.getTotalStaked, Method: http.MethodGet},
}
ng.baseGroup.endpoints = baseRoutesHandlers

Expand Down Expand Up @@ -74,3 +75,14 @@ func (group *networkGroup) getEconomicsData(c *gin.Context) {

c.JSON(http.StatusOK, economicsData)
}

// getTotalStakedValue will expose the total staked value from an observer (if any available) in json format
func (group *networkGroup) getTotalStaked(c *gin.Context) {
totalStakedData, err := group.facade.GetTotalStaked()
if err != nil {
shared.RespondWith(c, http.StatusInternalServerError, nil, err.Error(), data.ReturnCodeInternalError)
return
}

c.JSON(http.StatusOK, totalStakedData)
}
56 changes: 56 additions & 0 deletions api/groups/baseNetworkGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,62 @@ func TestGetNetworkConfigData_OkRequestShouldWork(t *testing.T) {
assert.Equal(t, value, res)
}

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

key := "totalStakedValue"
value := "2500000000000"
facade := &mock.Facade{
GetTotalStakedCalled: func() (*data.GenericAPIResponse, error) {
return &data.GenericAPIResponse{
Data: map[string]interface{}{
key: value,
},
Error: "",
}, nil
},
}
networkGroup, err := groups.NewNetworkGroup(facade)
require.NoError(t, err)
ws := startProxyServer(networkGroup, networkPath)

req, _ := http.NewRequest("GET", "/network/total-staked", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)

var result metricsResponse
loadResponse(resp.Body, &result)

res, ok := result.Data[key]
assert.True(t, ok)
assert.Equal(t, value, res)
}

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

expectedErr := errors.New("expected error")
facade := &mock.Facade{
GetTotalStakedCalled: func() (*data.GenericAPIResponse, error) {
return nil, expectedErr
},
}
networkGroup, err := groups.NewNetworkGroup(facade)
require.NoError(t, err)
ws := startProxyServer(networkGroup, networkPath)

req, _ := http.NewRequest("GET", "/network/total-staked", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)
assert.Equal(t, http.StatusInternalServerError, resp.Code)

var result metricsResponse
loadResponse(resp.Body, &result)

assert.Equal(t, expectedErr.Error(), result.Error)
}

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

Expand Down
1 change: 1 addition & 0 deletions api/groups/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type NetworkFacadeHandler interface {
GetNetworkStatusMetrics(shardID uint32) (*data.GenericAPIResponse, error)
GetNetworkConfigMetrics() (*data.GenericAPIResponse, error)
GetEconomicsDataMetrics() (*data.GenericAPIResponse, error)
GetTotalStaked() (*data.GenericAPIResponse, error)
}

// NodeFacadeHandler interface defines methods that can be used from facade context variable
Expand Down
10 changes: 10 additions & 0 deletions api/mock/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Facade struct {
GetBlockByNonceCalled func(shardID uint32, nonce uint64, withTxs bool) (*data.BlockApiResponse, error)
GetHyperBlockByHashCalled func(hash string) (*data.HyperblockApiResponse, error)
GetHyperBlockByNonceCalled func(nonce uint64) (*data.HyperblockApiResponse, error)
GetTotalStakedCalled func() (*data.GenericAPIResponse, error)
}

// IsFaucetEnabled -
Expand Down Expand Up @@ -197,6 +198,15 @@ func (f *Facade) GetHyperBlockByNonce(nonce uint64) (*data.HyperblockApiResponse
return f.GetHyperBlockByNonceCalled(nonce)
}

// GetTotalStaked -
func (f *Facade) GetTotalStaked() (*data.GenericAPIResponse, error) {
if f.GetTotalStakedCalled != nil {
return f.GetTotalStakedCalled()
}

return nil, nil
}

// WrongFacade is a struct that can be used as a wrong implementation of the node router handler
type WrongFacade struct {
}
21 changes: 12 additions & 9 deletions cmd/proxy/config/economics.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Economics config of the node
[GlobalSettings]
GenesisTotalSupply = "20000000000000000000000000" #20MILERD
GenesisTotalSupply = "20000000000000000000000000" #20MIL eGLD
MinimumInflation = 0.0
YearSettings = [
{Year = 1, MaximumInflation = 0.10845130},
Expand All @@ -15,18 +15,21 @@
{Year = 10, MaximumInflation = 0.00570796},
{Year = 11, MaximumInflation = 0.0},
]
Denomination = 18 # represents the smallest erd subdivision (10^-X ERD for a denomination of X)
Denomination = 18 # represents the smallest eGLD subdivision (10^-X eGLD for a denomination of X)

[RewardsSettings]
LeaderPercentage = 0.1 #fraction of value 1 - 10%
DeveloperPercentage = 0.3 #fraction of value 1 - 30%
LeaderPercentage = 0.1 #fraction of value 0.1 - 10%
DeveloperPercentage = 0.3 #fraction of value 0.3 - 30%
ProtocolSustainabilityPercentage = 0.1 #fraction of value 0.1 - 10%
ProtocolSustainabilityAddress = "erd1932eft30w753xyvme8d49qejgkjc09n5e49w4mwdjtm0neld797su0dlxp"
ProtocolSustainabilityAddress = "erd1j25xk97yf820rgdp3mj5scavhjkn6tjyn0t63pmv5qyjj7wxlcfqqe2rw5"
TopUpGradientPoint = "3000000000000000000000000" # 3MIL eGLD
TopUpFactor = 0.25 # fraction of value 0.25 - 25%

[FeeSettings]
MaxGasLimitPerBlock = "1500000000"
MaxGasLimitPerMetaBlock = "15000000000"
MinGasPrice = "1000000000" #will yield min tx fee of 0.00005ERD
MinGasLimit = "50000"
GasPerDataByte = "1500"
DataLimitForBaseCalc = "10000"
MinGasPrice = "1000000000" #will yield min tx fee of 0.00005 eGLD
GasPriceModifier = 0.01
MinGasLimit = "50000"
GasPerDataByte = "1500"
DataLimitForBaseCalc = "10000"
28 changes: 16 additions & 12 deletions data/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ import (
// Transaction represents the structure that maps and validates user input for publishing a new transaction
type Transaction struct {
// This field is used to tag transactions for send-multiple route
Index int `json:"-"`
Nonce uint64 `form:"nonce" json:"nonce"`
Value string `form:"value" json:"value"`
Receiver string `form:"receiver" json:"receiver"`
Sender string `form:"sender" json:"sender"`
GasPrice uint64 `form:"gasPrice" json:"gasPrice,omitempty"`
GasLimit uint64 `form:"gasLimit" json:"gasLimit,omitempty"`
Data []byte `form:"data" json:"data,omitempty"`
Signature string `form:"signature" json:"signature,omitempty"`
ChainID string `form:"chainID" json:"chainID"`
Version uint32 `form:"version" json:"version"`
Options uint32 `form:"options" json:"options,omitempty"`
Index int `json:"-"`
Nonce uint64 `json:"nonce"`
Value string `json:"value"`
Receiver string `json:"receiver"`
Sender string `json:"sender"`
SenderUsername []byte `json:"senderUsername,omitempty"`
ReceiverUsername []byte `json:"receiverUsername,omitempty"`
GasPrice uint64 `json:"gasPrice"`
GasLimit uint64 `json:"gasLimit"`
Data []byte `json:"data,omitempty"`
Signature string `json:"signature,omitempty"`
ChainID string `json:"chainID"`
Version uint32 `json:"version"`
Options uint32 `json:"options,omitempty"`
}

// FullTransaction is a transaction featuring all data saved in the full history
Expand All @@ -34,6 +36,8 @@ type FullTransaction struct {
Value string `json:"value,omitempty"`
Receiver string `json:"receiver,omitempty"`
Sender string `json:"sender,omitempty"`
SenderUsername []byte `json:"senderUsername,omitempty"`
ReceiverUsername []byte `json:"receiverUsername,omitempty"`
GasPrice uint64 `json:"gasPrice,omitempty"`
GasLimit uint64 `json:"gasLimit,omitempty"`
Data []byte `json:"data,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions facade/baseFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ func (epf *ElrondProxyFacade) GetNetworkStatusMetrics(shardID uint32) (*data.Gen
return epf.nodeStatusProc.GetNetworkStatusMetrics(shardID)
}

// GetTotalStaked retrieves the total staked value
func (epf *ElrondProxyFacade) GetTotalStaked() (*data.GenericAPIResponse, error) {
return epf.nodeStatusProc.GetTotalStaked()
}

// GetNetworkStatusMetrics retrieves the node's network metrics for a given shard
func (epf *ElrondProxyFacade) GetEconomicsDataMetrics() (*data.GenericAPIResponse, error) {
return epf.nodeStatusProc.GetEconomicsDataMetrics()
Expand Down
1 change: 1 addition & 0 deletions facade/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type ValidatorStatisticsProcessor interface {

// NodeStatusProcessor defines what a node status processor should do
type NodeStatusProcessor interface {
GetTotalStaked() (*data.GenericAPIResponse, error)
GetNetworkConfigMetrics() (*data.GenericAPIResponse, error)
GetNetworkStatusMetrics(shardID uint32) (*data.GenericAPIResponse, error)
GetEconomicsDataMetrics() (*data.GenericAPIResponse, error)
Expand Down
5 changes: 5 additions & 0 deletions facade/mock/nodeStatusProcessorStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type NodeStatusProcessorStub struct {
GetNetworkMetricsCalled func(shardID uint32) (*data.GenericAPIResponse, error)
GetLatestBlockNonceCalled func() (uint64, error)
GetEconomicsDataMetricsCalled func() (*data.GenericAPIResponse, error)
GetTotalStakedCalled func() (*data.GenericAPIResponse, error)
}

// GetNetworkConfigMetrics --
Expand All @@ -29,3 +30,7 @@ func (nsps *NodeStatusProcessorStub) GetEconomicsDataMetrics() (*data.GenericAPI
func (nsps *NodeStatusProcessorStub) GetLatestFullySynchronizedHyperblockNonce() (uint64, error) {
return nsps.GetLatestBlockNonceCalled()
}

func (nsps *NodeStatusProcessorStub) GetTotalStaked() (*data.GenericAPIResponse, error) {
return nsps.GetTotalStakedCalled()
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module github.com/ElrondNetwork/elrond-proxy-go

go 1.12
go 1.13

require (
github.com/ElrondNetwork/elrond-go v1.1.6-0.20201126175543-3e5a9696c46f
github.com/ElrondNetwork/elrond-go v1.1.16
github.com/ElrondNetwork/elrond-go-logger v1.0.4
github.com/coinbase/rosetta-sdk-go v0.6.1
github.com/elastic/go-elasticsearch/v7 v7.1.0
github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a
github.com/gin-gonic/gin v1.6.3
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.3.0
github.com/pkg/profile v1.5.0
github.com/stretchr/testify v1.6.1
github.com/urfave/cli v1.22.4
gopkg.in/go-playground/validator.v8 v8.18.2
Expand Down
Loading

0 comments on commit d9e4b22

Please sign in to comment.