Skip to content

Commit

Permalink
Merge pull request #5008 from multiversx/rc/v1.6.0
Browse files Browse the repository at this point in the history
RC/v1.6.0
  • Loading branch information
iulianpascalau authored Jan 4, 2024
2 parents e134e53 + 2af5088 commit ea3bf1d
Show file tree
Hide file tree
Showing 1,436 changed files with 93,353 additions and 35,759 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ cmd/node/stats/**

# Local overrides for testnet scripts
scripts/testnet/local.sh

# Autogenerated test files
integrationTests/multiShard/endOfEpoch/startInEpoch/Static
18 changes: 18 additions & 0 deletions api/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,21 @@ var ErrFetchingNonceGapsCannotIncludeFields = errors.New("fetching nonce gaps ca

// ErrInvalidFields signals that invalid fields were provided
var ErrInvalidFields = errors.New("invalid fields")

// ErrGetESDTTokensWithRole signals an error in getting the esdt tokens with the given role for given address
var ErrGetESDTTokensWithRole = errors.New("getting esdt tokens with role error")

// ErrRegisteredNFTTokenIDs signals an error in getting the registered nft token ids by the given address
var ErrRegisteredNFTTokenIDs = errors.New("getting registered nft token ids error")

// ErrInvalidRole signals that an invalid role was provided
var ErrInvalidRole = errors.New("invalid role")

// ErrIsDataTrieMigrated signals that an error occurred while trying to verify the migration status of the data trie
var ErrIsDataTrieMigrated = errors.New("could not verify the migration status of the data trie")

// ErrGetEligibleManagedKeys signals that an error occurred while getting the eligible managed keys
var ErrGetEligibleManagedKeys = errors.New("error getting the eligible managed keys")

// ErrGetWaitingManagedKeys signals that an error occurred while getting the waiting managed keys
var ErrGetWaitingManagedKeys = errors.New("error getting the waiting managed keys")
6 changes: 1 addition & 5 deletions api/gin/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ func skValidator(
}

func checkArgs(args ArgsNewWebServer) error {
errHandler := func(details string) error {
return fmt.Errorf("%w: %s", apiErrors.ErrCannotCreateGinWebServer, details)
}

if check.IfNil(args.Facade) {
return errHandler("nil facade")
return fmt.Errorf("%w: %s", apiErrors.ErrCannotCreateGinWebServer, apiErrors.ErrNilFacadeHandler.Error())
}

return nil
Expand Down
1 change: 1 addition & 0 deletions api/gin/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ func TestCommon_isLogRouteEnabled(t *testing.T) {
},
}
require.True(t, isLogRouteEnabled(routesConfig))
require.False(t, isLogRouteEnabled(config.ApiRoutesConfig{}))
}
23 changes: 13 additions & 10 deletions api/gin/httpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

type httpServer struct {
server *http.Server
server server
}

// NewHttpServer returns a new instance of httpServer
func NewHttpServer(server *http.Server) (*httpServer, error) {
func NewHttpServer(server server) (*httpServer, error) {
if server == nil {
return nil, apiErrors.ErrNilHttpServer
}
Expand All @@ -27,15 +27,18 @@ func NewHttpServer(server *http.Server) (*httpServer, error) {
// called on a go routine (different from the main one)
func (h *httpServer) Start() {
err := h.server.ListenAndServe()
if err != nil {
if err != http.ErrServerClosed {
log.Error("could not start webserver",
"error", err.Error(),
)
} else {
log.Debug("ListenAndServe - webserver closed")
}
if err == nil {
return
}

if err == http.ErrServerClosed {
log.Debug("ListenAndServe - webserver closed")
return
}

log.Error("could not start webserver",
"error", err.Error(),
)
}

// Close will handle the stopping of the gin web server
Expand Down
87 changes: 81 additions & 6 deletions api/gin/httpServer_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,92 @@
package gin

import (
"context"
"errors"
"net/http"
"testing"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-go/api/errors"
apiErrors "github.com/multiversx/mx-chain-go/api/errors"
"github.com/multiversx/mx-chain-go/testscommon/api"
"github.com/stretchr/testify/require"
)

func TestNewHttpServer_NilServerShouldErr(t *testing.T) {
func TestNewHttpServer(t *testing.T) {
t.Parallel()

hs, err := NewHttpServer(nil)
require.Equal(t, errors.ErrNilHttpServer, err)
require.True(t, check.IfNil(hs))
t.Run("nil server should error", func(t *testing.T) {
t.Parallel()

hs, err := NewHttpServer(nil)
require.Equal(t, apiErrors.ErrNilHttpServer, err)
require.Nil(t, hs)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

hs, err := NewHttpServer(&api.ServerStub{})
require.NoError(t, err)
require.NotNil(t, hs)
})
}

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

t.Run("server starts", func(t *testing.T) {
t.Parallel()

wasCalled := false
serverStub := &api.ServerStub{
ListenAndServeCalled: func() error {
return nil
},
ShutdownCalled: func(ctx context.Context) error {
wasCalled = true
return nil
},
}
hs, _ := NewHttpServer(serverStub)
require.NotNil(t, hs)

hs.Start()
require.NoError(t, hs.Close())
require.True(t, wasCalled)
})
t.Run("server is closed", func(t *testing.T) {
t.Parallel()

serverStub := &api.ServerStub{
ListenAndServeCalled: func() error {
return http.ErrServerClosed
},
}
hs, _ := NewHttpServer(serverStub)
require.NotNil(t, hs)

hs.Start()
})
t.Run("server returns other error", func(t *testing.T) {
t.Parallel()

serverStub := &api.ServerStub{
ListenAndServeCalled: func() error {
return errors.New("other error")
},
}
hs, _ := NewHttpServer(serverStub)
require.NotNil(t, hs)

hs.Start()
})
}

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

var hs *httpServer
require.True(t, hs.IsInterfaceNil())

hs, _ = NewHttpServer(&api.ServerStub{})
require.False(t, hs.IsInterfaceNil())
}
7 changes: 7 additions & 0 deletions api/gin/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package gin

import "context"

type resetHandler interface {
Reset()
IsInterfaceNil() bool
}

type server interface {
ListenAndServe() error
Shutdown(ctx context.Context) error
}
17 changes: 12 additions & 5 deletions api/gin/webServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/api/errors"
"github.com/multiversx/mx-chain-go/api/groups"
"github.com/multiversx/mx-chain-go/api/middleware"
"github.com/multiversx/mx-chain-go/api/shared"
Expand Down Expand Up @@ -46,18 +47,20 @@ func NewGinWebServerHandler(args ArgsNewWebServer) (*webServer, error) {
return nil, err
}

gws := &webServer{
return &webServer{
facade: args.Facade,
antiFloodConfig: args.AntiFloodConfig,
apiConfig: args.ApiConfig,
}

return gws, nil
}, nil
}

// UpdateFacade updates the main api handler by closing the old server and starting it with the new facade. Returns the
// new web server
func (ws *webServer) UpdateFacade(facade shared.FacadeHandler) error {
if check.IfNil(facade) {
return errors.ErrNilFacadeHandler
}

ws.Lock()
defer ws.Unlock()

Expand All @@ -80,6 +83,7 @@ func (ws *webServer) StartHttpServer() error {
defer ws.Unlock()

if ws.facade.RestApiInterface() == facade.DefaultRestPortOff {
log.Debug("web server is turned off")
return nil
}

Expand Down Expand Up @@ -277,8 +281,11 @@ func (ws *webServer) Close() error {
ws.cancelFunc()
}

var err error
ws.Lock()
err := ws.httpServer.Close()
if !check.IfNil(ws.httpServer) {
err = ws.httpServer.Close()
}
ws.Unlock()

if err != nil {
Expand Down
Loading

0 comments on commit ea3bf1d

Please sign in to comment.