Skip to content

Commit

Permalink
Merge branch 'development' into deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed May 13, 2020
2 parents ca84803 + 7eb0642 commit 40e42e6
Show file tree
Hide file tree
Showing 544 changed files with 27,607 additions and 6,361 deletions.
7 changes: 4 additions & 3 deletions api/address/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

"github.com/ElrondNetwork/elrond-go/api/errors"
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/gin-gonic/gin"
)
Expand All @@ -28,9 +29,9 @@ type accountResponse struct {
}

// Routes defines address related routes
func Routes(router *gin.RouterGroup) {
router.GET("/:address", GetAccount)
router.GET("/:address/balance", GetBalance)
func Routes(router *wrapper.RouterWrapper) {
router.RegisterHandler(http.MethodGet, "/:address", GetAccount)
router.RegisterHandler(http.MethodGet, "/:address/balance", GetBalance)
}

// GetAccount returns an accountResponse containing information
Expand Down
21 changes: 19 additions & 2 deletions api/address/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
errors2 "github.com/ElrondNetwork/elrond-go/api/errors"
"github.com/ElrondNetwork/elrond-go/api/middleware"
"github.com/ElrondNetwork/elrond-go/api/mock"
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -262,7 +264,8 @@ func startNodeServer(handler address.FacadeHandler) *gin.Engine {
if handler != nil {
addressRoutes.Use(middleware.WithElrondFacade(handler))
}
address.Routes(addressRoutes)
addressRoute, _ := wrapper.NewRouterWrapper("address", addressRoutes, getRoutesConfig())
address.Routes(addressRoute)
return ws
}

Expand All @@ -272,7 +275,21 @@ func startNodeServerWrongFacade() *gin.Engine {
ws.Use(func(c *gin.Context) {
c.Set("elrondFacade", mock.WrongFacade{})
})
addressRoute := ws.Group("/address")
ginAddressRoute := ws.Group("/address")
addressRoute, _ := wrapper.NewRouterWrapper("address", ginAddressRoute, getRoutesConfig())
address.Routes(addressRoute)
return ws
}

func getRoutesConfig() config.ApiRoutesConfig {
return config.ApiRoutesConfig{
APIPackages: map[string]config.APIPackageConfig{
"address": {
[]config.RouteConfig{
{Name: "/:address", Open: true},
{Name: "/:address/balance", Open: true},
},
},
},
}
}
67 changes: 56 additions & 11 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (
"github.com/ElrondNetwork/elrond-go/api/hardfork"
"github.com/ElrondNetwork/elrond-go/api/logs"
"github.com/ElrondNetwork/elrond-go/api/middleware"
"github.com/ElrondNetwork/elrond-go/api/network"
"github.com/ElrondNetwork/elrond-go/api/node"
"github.com/ElrondNetwork/elrond-go/api/transaction"
valStats "github.com/ElrondNetwork/elrond-go/api/validator"
"github.com/ElrondNetwork/elrond-go/api/vmValues"
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/gin-contrib/cors"
Expand Down Expand Up @@ -63,7 +66,7 @@ func (gev *ginErrorWriter) Write(p []byte) (n int, err error) {
}

// Start will boot up the api and appropriate routes, handlers and validators
func Start(elrondFacade MainApiHandler, processors ...MiddlewareProcessor) error {
func Start(elrondFacade MainApiHandler, routesConfig config.ApiRoutesConfig, processors ...MiddlewareProcessor) error {
var ws *gin.Engine
if !elrondFacade.RestAPIServerDebugMode() {
gin.DefaultWriter = &ginWriter{}
Expand All @@ -86,43 +89,85 @@ func Start(elrondFacade MainApiHandler, processors ...MiddlewareProcessor) error
return err
}

registerRoutes(ws, elrondFacade)
registerRoutes(ws, routesConfig, elrondFacade)

return ws.Run(elrondFacade.RestApiInterface())
}

func registerRoutes(ws *gin.Engine, elrondFacade middleware.ElrondHandler) {
func registerRoutes(ws *gin.Engine, routesConfig config.ApiRoutesConfig, elrondFacade middleware.ElrondHandler) {
nodeRoutes := ws.Group("/node")
nodeRoutes.Use(middleware.WithElrondFacade(elrondFacade))
node.Routes(nodeRoutes)
wrappedNodeRouter, err := wrapper.NewRouterWrapper("node", nodeRoutes, routesConfig)
if err == nil {
node.Routes(wrappedNodeRouter)
}

addressRoutes := ws.Group("/address")
addressRoutes.Use(middleware.WithElrondFacade(elrondFacade))
address.Routes(addressRoutes)
wrappedAddressRouter, err := wrapper.NewRouterWrapper("address", addressRoutes, routesConfig)
if err == nil {
address.Routes(wrappedAddressRouter)
}

networkRoutes := ws.Group("/network")
networkRoutes.Use(middleware.WithElrondFacade(elrondFacade))
wrappedNetworkRoutes, err := wrapper.NewRouterWrapper("network", networkRoutes, routesConfig)
if err == nil {
network.Routes(wrappedNetworkRoutes)
}

txRoutes := ws.Group("/transaction")
txRoutes.Use(middleware.WithElrondFacade(elrondFacade))
transaction.Routes(txRoutes)
wrappedTransactionRouter, err := wrapper.NewRouterWrapper("transaction", txRoutes, routesConfig)
if err == nil {
transaction.Routes(wrappedTransactionRouter)
}

vmValuesRoutes := ws.Group("/vm-values")
vmValuesRoutes.Use(middleware.WithElrondFacade(elrondFacade))
vmValues.Routes(vmValuesRoutes)
wrappedVmValuesRouter, err := wrapper.NewRouterWrapper("vm-values", vmValuesRoutes, routesConfig)
if err == nil {
vmValues.Routes(wrappedVmValuesRouter)
}

validatorRoutes := ws.Group("/validator")
validatorRoutes.Use(middleware.WithElrondFacade(elrondFacade))
valStats.Routes(validatorRoutes)
wrappedValidatorsRouter, err := wrapper.NewRouterWrapper("validator", validatorRoutes, routesConfig)
if err == nil {
valStats.Routes(wrappedValidatorsRouter)
}

hardforkRoutes := ws.Group("/hardfork")
hardforkRoutes.Use(middleware.WithElrondFacade(elrondFacade))
hardfork.Routes(hardforkRoutes)
wrappedHardforkRouter, err := wrapper.NewRouterWrapper("hardfork", hardforkRoutes, routesConfig)
if err == nil {
hardfork.Routes(wrappedHardforkRouter)
}

apiHandler, ok := elrondFacade.(MainApiHandler)
if ok && apiHandler.PprofEnabled() {
pprof.Register(ws)
}

marshalizerForLogs := &marshal.GogoProtoMarshalizer{}
registerLoggerWsRoute(ws, marshalizerForLogs)
if isLogRouteEnabled(routesConfig) {
marshalizerForLogs := &marshal.GogoProtoMarshalizer{}
registerLoggerWsRoute(ws, marshalizerForLogs)
}
}

func isLogRouteEnabled(routesConfig config.ApiRoutesConfig) bool {
logConfig, ok := routesConfig.APIPackages["log"]
if !ok {
return false
}

for _, cfg := range logConfig.Routes {
if cfg.Name == "/log" && cfg.Open {
return true
}
}

return false
}

func registerValidators() error {
Expand Down
5 changes: 3 additions & 2 deletions api/hardfork/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/ElrondNetwork/elrond-go/api/errors"
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/gin-gonic/gin"
)

Expand All @@ -17,8 +18,8 @@ type TriggerHardforkHandler interface {
}

// Routes defines node related routes
func Routes(router *gin.RouterGroup) {
router.POST("/trigger", Trigger)
func Routes(router *wrapper.RouterWrapper) {
router.RegisterHandler(http.MethodPost, "/trigger", Trigger)
}

// Trigger will receive a trigger request from the client and propagate it for processing
Expand Down
26 changes: 21 additions & 5 deletions api/hardfork/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/ElrondNetwork/elrond-go/api/hardfork"
"github.com/ElrondNetwork/elrond-go/api/middleware"
"github.com/ElrondNetwork/elrond-go/api/mock"
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
Expand All @@ -38,11 +40,12 @@ type TriggerResponse struct {
func startNodeServer(handler hardfork.TriggerHardforkHandler) *gin.Engine {
ws := gin.New()
ws.Use(cors.Default())
hardforkRoute := ws.Group("/hardfork")
ginHardforkRoute := ws.Group("/hardfork")
if handler != nil {
hardforkRoute.Use(middleware.WithElrondFacade(handler))
ginHardforkRoute.Use(middleware.WithElrondFacade(handler))
}
hardfork.Routes(hardforkRoute)
hardForkRoute, _ := wrapper.NewRouterWrapper("hardfork", ginHardforkRoute, getRoutesConfig())
hardfork.Routes(hardForkRoute)
return ws
}

Expand All @@ -52,8 +55,9 @@ func startNodeServerWrongFacade() *gin.Engine {
ws.Use(func(c *gin.Context) {
c.Set("elrondFacade", mock.WrongFacade{})
})
hardforkRoute := ws.Group("/hardfork")
hardfork.Routes(hardforkRoute)
ginHardforkRoute := ws.Group("/hardfork")
hardForkRoute, _ := wrapper.NewRouterWrapper("hardfork", ginHardforkRoute, getRoutesConfig())
hardfork.Routes(hardForkRoute)
return ws
}

Expand Down Expand Up @@ -145,3 +149,15 @@ func TestTrigger_BroadcastShouldWork(t *testing.T) {
assert.Equal(t, resp.Code, http.StatusOK)
assert.Equal(t, hardfork.ExecBroadcastTrigger, triggerResponse.Status)
}

func getRoutesConfig() config.ApiRoutesConfig {
return config.ApiRoutesConfig{
APIPackages: map[string]config.APIPackageConfig{
"hardfork": {
[]config.RouteConfig{
{Name: "/trigger", Open: true},
},
},
},
}
}
20 changes: 18 additions & 2 deletions api/middleware/globalThrottler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/ElrondNetwork/elrond-go/api/address"
"github.com/ElrondNetwork/elrond-go/api/middleware"
"github.com/ElrondNetwork/elrond-go/api/mock"
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand All @@ -28,10 +30,11 @@ func startNodeServerGlobalThrottler(handler address.FacadeHandler, maxConnection
ws.Use(cors.Default())
globalThrottler, _ := middleware.NewGlobalThrottler(maxConnections)
ws.Use(globalThrottler.MiddlewareHandlerFunc())
addressRoutes := ws.Group("/address")
ginAddressRoutes := ws.Group("/address")
if handler != nil {
addressRoutes.Use(middleware.WithElrondFacade(handler))
ginAddressRoutes.Use(middleware.WithElrondFacade(handler))
}
addressRoutes, _ := wrapper.NewRouterWrapper("address", ginAddressRoutes, getRoutesConfig())
address.Routes(addressRoutes)
return ws
}
Expand Down Expand Up @@ -123,3 +126,16 @@ func makeRequestGlobalThrottler(ws *gin.Engine, mutResponses *sync.Mutex, respon
responses[resp.Code]++
mutResponses.Unlock()
}

func getRoutesConfig() config.ApiRoutesConfig {
return config.ApiRoutesConfig{
APIPackages: map[string]config.APIPackageConfig{
"address": {
[]config.RouteConfig{
{Name: "/:address", Open: true},
{Name: "/:address/balance", Open: true},
},
},
},
}
}
6 changes: 4 additions & 2 deletions api/middleware/sourceThrottler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ElrondNetwork/elrond-go/api/address"
"github.com/ElrondNetwork/elrond-go/api/middleware"
"github.com/ElrondNetwork/elrond-go/api/mock"
"github.com/ElrondNetwork/elrond-go/api/wrapper"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand All @@ -26,10 +27,11 @@ func startNodeServerSourceThrottler(handler address.FacadeHandler, maxConnection
ws.Use(cors.Default())
sourceThrottler, _ := middleware.NewSourceThrottler(maxConnections)
ws.Use(sourceThrottler.MiddlewareHandlerFunc())
addressRoutes := ws.Group("/address")
ginAddressRoutes := ws.Group("/address")
if handler != nil {
addressRoutes.Use(middleware.WithElrondFacade(handler))
ginAddressRoutes.Use(middleware.WithElrondFacade(handler))
}
addressRoutes, _ := wrapper.NewRouterWrapper("address", ginAddressRoutes, getRoutesConfig())
address.Routes(addressRoutes)
return ws, sourceThrottler
}
Expand Down
10 changes: 5 additions & 5 deletions api/mock/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/data/transaction"
"github.com/ElrondNetwork/elrond-go/debug"
"github.com/ElrondNetwork/elrond-go/heartbeat/data"
"github.com/ElrondNetwork/elrond-go/node/external"
"github.com/ElrondNetwork/elrond-go/node/heartbeat"
"github.com/ElrondNetwork/elrond-go/process"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
Expand All @@ -19,12 +19,12 @@ type Facade struct {
ShouldErrorStart bool
ShouldErrorStop bool
TpsBenchmarkHandler func() *statistics.TpsBenchmark
GetHeartbeatsHandler func() ([]heartbeat.PubKeyHeartbeat, error)
GetHeartbeatsHandler func() ([]data.PubKeyHeartbeat, error)
BalanceHandler func(string) (*big.Int, error)
GetAccountHandler func(address string) (state.UserAccountHandler, error)
GenerateTransactionHandler func(sender string, receiver string, value *big.Int, code string) (*transaction.Transaction, error)
GetTransactionHandler func(hash string) (*transaction.Transaction, error)
CreateTransactionHandler func(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64, gasLimit uint64, data []byte, signatureHex string) (*transaction.Transaction, []byte, error)
CreateTransactionHandler func(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64, gasLimit uint64, data string, signatureHex string) (*transaction.Transaction, []byte, error)
ValidateTransactionHandler func(tx *transaction.Transaction) error
SendBulkTransactionsHandler func(txs []*transaction.Transaction) (uint64, error)
ExecuteSCQueryHandler func(query *process.SCQuery) (*vmcommon.VMOutput, error)
Expand Down Expand Up @@ -59,7 +59,7 @@ func (f *Facade) TpsBenchmark() *statistics.TpsBenchmark {
}

// GetHeartbeats returns the slice of heartbeat info
func (f *Facade) GetHeartbeats() ([]heartbeat.PubKeyHeartbeat, error) {
func (f *Facade) GetHeartbeats() ([]data.PubKeyHeartbeat, error) {
return f.GetHeartbeatsHandler()
}

Expand All @@ -81,7 +81,7 @@ func (f *Facade) CreateTransaction(
senderHex string,
gasPrice uint64,
gasLimit uint64,
data []byte,
data string,
signatureHex string,
) (*transaction.Transaction, []byte, error) {
return f.CreateTransactionHandler(nonce, value, receiverHex, senderHex, gasPrice, gasLimit, data, signatureHex)
Expand Down
Loading

0 comments on commit 40e42e6

Please sign in to comment.