mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-22 03:30:35 +00:00
Issue 1017 taken port (#1108)
* add git ignore to avoid intellij bazel plugin files. * check if port for p2p is available * add unit test for port taken
This commit is contained in:
parent
03017d1958
commit
4f316d6ed0
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,6 +6,7 @@ bazel-*
|
||||
|
||||
# IntelliJ
|
||||
.idea
|
||||
.ijwb
|
||||
|
||||
# delve debugger output (not sure how to get rid of these)
|
||||
**/debug.test
|
||||
|
@ -3,6 +3,8 @@ package p2p
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/prysmaticlabs/prysm/shared/iputils"
|
||||
"net"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
@ -53,6 +55,10 @@ func NewServer(cfg *ServerConfig) (*Server, error) {
|
||||
if cfg.RelayNodeAddr != "" {
|
||||
opts = append(opts, libp2p.AddrsFactory(relayAddrsOnly(cfg.RelayNodeAddr)))
|
||||
}
|
||||
if !checkAvailablePort(cfg.Port) {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("error listening on p2p, port %d already taken", cfg.Port)
|
||||
}
|
||||
h, err := libp2p.New(ctx, opts...)
|
||||
if err != nil {
|
||||
cancel()
|
||||
@ -84,6 +90,22 @@ func NewServer(cfg *ServerConfig) (*Server, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func checkAvailablePort(port int) bool {
|
||||
ip, err := iputils.ExternalIPv4()
|
||||
if err != nil {
|
||||
log.Errorf("Could not get IPv4 address: %v", err)
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", ip, port))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
ln.Close()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Start the main routine for an p2p server.
|
||||
func (s *Server) Start() {
|
||||
ctx, span := trace.StartSpan(s.ctx, "p2p_server_start")
|
||||
|
@ -50,6 +50,25 @@ func TestStartDialRelayNodeFails(t *testing.T) {
|
||||
logContains(t, hook, "Could not dial relay node: invalid multiaddr, must begin with /", logrus.ErrorLevel)
|
||||
}
|
||||
|
||||
func TestP2pPortTakenError(t *testing.T) {
|
||||
thePort := 10000
|
||||
_, err := NewServer(&ServerConfig{
|
||||
Port: thePort,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create server: %s", err)
|
||||
}
|
||||
|
||||
_, err = NewServer(&ServerConfig{
|
||||
Port: thePort,
|
||||
})
|
||||
|
||||
if !strings.Contains(err.Error(), fmt.Sprintf("port %d already taken", thePort)) {
|
||||
t.Fatalf("expected fail when setting another server with same p2p port")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBroadcast(t *testing.T) {
|
||||
s, err := NewServer(&ServerConfig{})
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user