From ea3b76ded1e42569ec9e10f8e4690c482797056d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 22 Jan 2020 11:17:51 +0300 Subject: [PATCH] network: make NewServer return an error, fix #612 It can return nil in two cases, so we're better return an error and handle it. --- cli/server/server.go | 5 ++++- integration/performance_test.go | 3 ++- pkg/network/server.go | 8 ++++---- pkg/rpc/server_helper_test.go | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cli/server/server.go b/cli/server/server.go index f560a245c..0f1aa80b9 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -321,7 +321,10 @@ func startServer(ctx *cli.Context) error { return err } - server := network.NewServer(serverConfig, chain, log) + server, err := network.NewServer(serverConfig, chain, log) + if err != nil { + return cli.NewExitError(fmt.Errorf("failed to create network server: %v", err), 1) + } rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server, log) errChan := make(chan error) diff --git a/integration/performance_test.go b/integration/performance_test.go index 738f660b5..42c5e2011 100644 --- a/integration/performance_test.go +++ b/integration/performance_test.go @@ -32,7 +32,8 @@ func BenchmarkTXPerformanceTest(t *testing.B) { go chain.Run() serverConfig := network.NewServerConfig(cfg) - server := network.NewServer(serverConfig, chain, logger) + server, err := network.NewServer(serverConfig, chain, logger) + require.NoError(t, err, "could not create server") data := prepareData(t) t.ResetTimer() diff --git a/pkg/network/server.go b/pkg/network/server.go index 3bf0fa249..f96ba8383 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -86,9 +86,9 @@ func randomID() uint32 { } // NewServer returns a new Server, initialized with the given configuration. -func NewServer(config ServerConfig, chain core.Blockchainer, log *zap.Logger) *Server { +func NewServer(config ServerConfig, chain core.Blockchainer, log *zap.Logger) (*Server, error) { if log == nil { - return nil + return nil, errors.New("logger is a required parameter") } s := &Server{ @@ -117,7 +117,7 @@ func NewServer(config ServerConfig, chain core.Blockchainer, log *zap.Logger) *S TimePerBlock: config.TimePerBlock, }) if err != nil { - return nil + return nil, err } s.consensus = srv @@ -149,7 +149,7 @@ func NewServer(config ServerConfig, chain core.Blockchainer, log *zap.Logger) *S s.transport, ) - return s + return s, nil } // ID returns the servers ID. diff --git a/pkg/rpc/server_helper_test.go b/pkg/rpc/server_helper_test.go index 135e96f8a..d2a25f792 100644 --- a/pkg/rpc/server_helper_test.go +++ b/pkg/rpc/server_helper_test.go @@ -201,7 +201,8 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu } serverConfig := network.NewServerConfig(cfg) - server := network.NewServer(serverConfig, chain, logger) + server, err := network.NewServer(serverConfig, chain, logger) + require.NoError(t, err) rpcServer := NewServer(chain, cfg.ApplicationConfiguration.RPC, server, logger) handler := http.HandlerFunc(rpcServer.requestHandler)