Skip to content

Commit

Permalink
Merge pull request #101 from ElrondNetwork/development
Browse files Browse the repository at this point in the history
Merge Development into Main (beta Rosetta API, other new endpoints)
  • Loading branch information
andreibancioiu authored Oct 19, 2020
2 parents 8fbf423 + 4375b57 commit 3212ac6
Show file tree
Hide file tree
Showing 61 changed files with 5,812 additions and 85 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ For more details, go to [docs.elrond.com](https://docs.elrond.com/tools/proxy).
- `/address/:address` (GET) --> returns the account's data in JSON format for the given :address.
- `/address/:address/balance` (GET) --> returns the balance of a given :address.
- `/address/:address/nonce` (GET) --> returns the nonce of an :address.
- `/address/:address/shard` (GET) --> returns the shard of an :address based on current proxy's configuration.
- `/address/:address/storage/:key` (GET) --> returns the value for a given key for an account.
- `/address/:address/transactions` (GET) --> returns the transactions stored in indexer for a given :address.

### transaction

- `/transaction/send` (POST) --> receives a single transaction in JSON format and forwards it to an observer in the same shard as the sender's shard ID. Returns the transaction's hash if successful or the interceptor error otherwise.
- `/transaction/simulate` (POST) --> same as /transaction/send but does not execute it. will output simulation results
- `/transaction/send-multiple` (POST) --> receives a bulk of transactions in JSON format and will forward them to observers in the rights shards. Will return the number of transactions which were accepted by the interceptor and forwarded on the p2p topic.
- `/transaction/send-user-funds` (POST) --> receives a request containing `address`, `numOfTxs` and `value` and will select a random account from the PEM file in the same shard as the address received. Will return the transaction's hash if successful or the interceptor error otherwise.
- `/transaction/cost` (POST) --> receives a single transaction in JSON format and returns it's cost
Expand All @@ -38,6 +40,7 @@ For more details, go to [docs.elrond.com](https://docs.elrond.com/tools/proxy).

- `/network/status/:shard` (GET) --> returns the status metrics from an observer in the given shard
- `/network/config` (GET) --> returns the configuration of the network from any observer
- `/network/economics` (GET) --> returns the economics data metric from the last epoch

### node

Expand Down
15 changes: 11 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"net/http"
"reflect"

"github.com/ElrondNetwork/elrond-proxy-go/api/address"
Expand All @@ -24,18 +25,24 @@ type validatorInput struct {
Validator validator.Func
}

// Start will boot up the api and appropriate routes, handlers and validators
func Start(elrondProxyFacade ElrondProxyHandler, port int) error {
// CreateServer creates a HTTP server
func CreateServer(elrondProxyFacade ElrondProxyHandler, port int) (*http.Server, error) {
ws := gin.Default()
ws.Use(cors.Default())

err := registerValidators()
if err != nil {
return err
return nil, err
}

registerRoutes(ws, elrondProxyFacade)

return ws.Run(fmt.Sprintf(":%d", port))
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: ws,
}

return httpServer, nil
}

func registerRoutes(ws *gin.Engine, elrondProxyFacade ElrondProxyHandler) {
Expand Down
16 changes: 16 additions & 0 deletions api/mock/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mock
import (
"math/big"

"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/data/vm"
"github.com/ElrondNetwork/elrond-proxy-go/data"
)
Expand All @@ -26,6 +27,7 @@ type Facade struct {
GetTransactionStatusHandler func(txHash string, sender string) (string, error)
GetConfigMetricsHandler func() (*data.GenericAPIResponse, error)
GetNetworkMetricsHandler func(shardID uint32) (*data.GenericAPIResponse, error)
GetEconomicsDataMetricsHandler func() (*data.GenericAPIResponse, error)
GetBlockByShardIDAndNonceHandler func(shardID uint32, nonce uint64) (data.AtlasBlock, error)
GetTransactionByHashAndSenderAddressHandler func(txHash string, sndAddr string) (*data.FullTransaction, int, error)
GetBlockByHashCalled func(shardID uint32, hash string, withTxs bool) (*data.BlockApiResponse, error)
Expand Down Expand Up @@ -61,6 +63,15 @@ func (f *Facade) GetNetworkConfigMetrics() (*data.GenericAPIResponse, error) {
return nil, nil
}

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

return &data.GenericAPIResponse{}, nil
}

// ValidatorStatistics -
func (f *Facade) ValidatorStatistics() (map[string]*data.ValidatorApiResponse, error) {
return f.ValidatorStatisticsHandler()
Expand Down Expand Up @@ -106,6 +117,11 @@ func (f *Facade) SimulateTransaction(tx *data.Transaction) (*data.ResponseTransa
return f.SimulateTransactionHandler(tx)
}

// GetAddressConverter -
func (f *Facade) GetAddressConverter() (core.PubkeyConverter, error) {
return nil, nil
}

// SendMultipleTransactions -
func (f *Facade) SendMultipleTransactions(txs []*data.Transaction) (data.MultipleTransactionsResponseData, error) {
return f.SendMultipleTransactionsHandler(txs)
Expand Down
1 change: 1 addition & 0 deletions api/network/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import "github.com/ElrondNetwork/elrond-proxy-go/data"
type FacadeHandler interface {
GetNetworkStatusMetrics(shardID uint32) (*data.GenericAPIResponse, error)
GetNetworkConfigMetrics() (*data.GenericAPIResponse, error)
GetEconomicsDataMetrics() (*data.GenericAPIResponse, error)
}
20 changes: 19 additions & 1 deletion api/network/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func Routes(router *gin.RouterGroup) {
router.GET("/status/:shard", GetNetworkStatusData)
router.GET("/config", GetNetworkConfigData)
router.GET("/economics", GetEconomicsData)
}

// GetNetworkStatusData will expose the node network metrics for the given shard
Expand All @@ -29,7 +30,7 @@ func GetNetworkStatusData(c *gin.Context) {
return
}

networkStatusResults, err := ef.GetNetworkStatusMetrics(uint32(shardIDUint))
networkStatusResults, err := ef.GetNetworkStatusMetrics(shardIDUint)
if err != nil {
shared.RespondWith(c, http.StatusInternalServerError, nil, err.Error(), data.ReturnCodeInternalError)
return
Expand All @@ -54,3 +55,20 @@ func GetNetworkConfigData(c *gin.Context) {

c.JSON(http.StatusOK, networkConfigResults)
}

// GetEconomicsData will expose the economics data metrics from an observer (if any available) in json format
func GetEconomicsData(c *gin.Context) {
ef, ok := c.MustGet("elrondProxyFacade").(FacadeHandler)
if !ok {
shared.RespondWithInvalidAppContext(c)
return
}

economicsData, err := ef.GetEconomicsDataMetrics()
if err != nil {
shared.RespondWith(c, http.StatusInternalServerError, nil, err.Error(), data.ReturnCodeInternalError)
return
}

c.JSON(http.StatusOK, economicsData)
}
60 changes: 60 additions & 0 deletions api/network/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,63 @@ func TestGetNetworkConfigData_OkRequestShouldWork(t *testing.T) {
assert.True(t, ok)
assert.Equal(t, value, res)
}

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

ws := startNodeServerWrongFacade()
req, _ := http.NewRequest("GET", "/network/economics", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

ecResp := data.GenericAPIResponse{}
loadResponse(resp.Body, &ecResp)

assert.Equal(t, resp.Code, http.StatusInternalServerError)
assert.Equal(t, ecResp.Error, apiErrors.ErrInvalidAppContext.Error())
}

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

expectedErr := errors.New("internal error")
facade := mock.Facade{
GetEconomicsDataMetricsHandler: func() (*data.GenericAPIResponse, error) {
return nil, expectedErr
},
}
ws := startNodeServer(&facade)

req, _ := http.NewRequest("GET", "/network/economics", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

ecDataResp := data.GenericAPIResponse{}
loadResponse(resp.Body, &ecDataResp)

assert.Equal(t, http.StatusInternalServerError, resp.Code)
assert.Equal(t, expectedErr.Error(), ecDataResp.Error)
}

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

expectedResp := data.GenericAPIResponse{Data: map[string]interface{}{"erd_total_supply": "12345"}}
facade := mock.Facade{
GetEconomicsDataMetricsHandler: func() (*data.GenericAPIResponse, error) {
return &expectedResp, nil
},
}
ws := startNodeServer(&facade)

req, _ := http.NewRequest("GET", "/network/economics", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

ecDataResp := data.GenericAPIResponse{}
loadResponse(resp.Body, &ecDataResp)

assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, expectedResp, ecDataResp)
assert.Equal(t, expectedResp.Data, ecDataResp.Data) //extra safe
}
2 changes: 1 addition & 1 deletion api/node/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func loadResponse(rsp io.Reader, destination interface{}) {
logError(err)
}

func TestGetAccount_FailsWithWrongFacadeTypeConversion(t *testing.T) {
func TestHeartbeat_FailsWithWrongFacadeTypeConversion(t *testing.T) {
t.Parallel()

ws := startNodeServerWrongFacade()
Expand Down
6 changes: 6 additions & 0 deletions cmd/proxy/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
# Type specifies the type of public keys: hex or bech32
Type = "bech32"

[Marshalizer]
Type = "gogo protobuf"

[Hasher]
Type = "blake2b"

# List of Observers. If you want to define a metachain observer (needed for validator statistics route) use
# shard id 4294967295
[[Observers]]
Expand Down
Loading

0 comments on commit 3212ac6

Please sign in to comment.