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.
This commit is contained in:
Roman Khimov 2020-01-22 11:17:51 +03:00
parent 9befd8de99
commit ea3b76ded1
4 changed files with 12 additions and 7 deletions

View file

@ -321,7 +321,10 @@ func startServer(ctx *cli.Context) error {
return err 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) rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server, log)
errChan := make(chan error) errChan := make(chan error)

View file

@ -32,7 +32,8 @@ func BenchmarkTXPerformanceTest(t *testing.B) {
go chain.Run() go chain.Run()
serverConfig := network.NewServerConfig(cfg) 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) data := prepareData(t)
t.ResetTimer() t.ResetTimer()

View file

@ -86,9 +86,9 @@ func randomID() uint32 {
} }
// NewServer returns a new Server, initialized with the given configuration. // 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 { if log == nil {
return nil return nil, errors.New("logger is a required parameter")
} }
s := &Server{ s := &Server{
@ -117,7 +117,7 @@ func NewServer(config ServerConfig, chain core.Blockchainer, log *zap.Logger) *S
TimePerBlock: config.TimePerBlock, TimePerBlock: config.TimePerBlock,
}) })
if err != nil { if err != nil {
return nil return nil, err
} }
s.consensus = srv s.consensus = srv
@ -149,7 +149,7 @@ func NewServer(config ServerConfig, chain core.Blockchainer, log *zap.Logger) *S
s.transport, s.transport,
) )
return s return s, nil
} }
// ID returns the servers ID. // ID returns the servers ID.

View file

@ -201,7 +201,8 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
} }
serverConfig := network.NewServerConfig(cfg) 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) rpcServer := NewServer(chain, cfg.ApplicationConfiguration.RPC, server, logger)
handler := http.HandlerFunc(rpcServer.requestHandler) handler := http.HandlerFunc(rpcServer.requestHandler)