forked from cerc-io/plugeth
#15685 made peer_test.go more portable by using random free port instead of hardcoded port 30303 (#15687)
Improves test portability by resolving 127.0.0.1:0 to get a random free port instead of the hard coded one. Now the test works if you have a running node on the same interface already. Fixes #15685
This commit is contained in:
parent
e33a5de454
commit
591cef17d4
@ -19,7 +19,6 @@ package whisperv5
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@ -108,8 +107,6 @@ func TestSimulation(t *testing.T) {
|
|||||||
|
|
||||||
func initialize(t *testing.T) {
|
func initialize(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
ip := net.IPv4(127, 0, 0, 1)
|
|
||||||
port0 := 30303
|
|
||||||
|
|
||||||
for i := 0; i < NumNodes; i++ {
|
for i := 0; i < NumNodes; i++ {
|
||||||
var node TestNode
|
var node TestNode
|
||||||
@ -128,29 +125,15 @@ func initialize(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed convert the key: %s.", keys[i])
|
t.Fatalf("failed convert the key: %s.", keys[i])
|
||||||
}
|
}
|
||||||
port := port0 + i
|
|
||||||
addr := fmt.Sprintf(":%d", port) // e.g. ":30303"
|
|
||||||
name := common.MakeName("whisper-go", "2.0")
|
name := common.MakeName("whisper-go", "2.0")
|
||||||
var peers []*discover.Node
|
|
||||||
if i > 0 {
|
|
||||||
peerNodeId := nodes[i-1].id
|
|
||||||
peerPort := uint16(port - 1)
|
|
||||||
peerNode := discover.PubkeyID(&peerNodeId.PublicKey)
|
|
||||||
peer := discover.NewNode(peerNode, ip, peerPort, peerPort)
|
|
||||||
peers = append(peers, peer)
|
|
||||||
}
|
|
||||||
|
|
||||||
node.server = &p2p.Server{
|
node.server = &p2p.Server{
|
||||||
Config: p2p.Config{
|
Config: p2p.Config{
|
||||||
PrivateKey: node.id,
|
PrivateKey: node.id,
|
||||||
MaxPeers: NumNodes/2 + 1,
|
MaxPeers: NumNodes/2 + 1,
|
||||||
Name: name,
|
Name: name,
|
||||||
Protocols: node.shh.Protocols(),
|
Protocols: node.shh.Protocols(),
|
||||||
ListenAddr: addr,
|
ListenAddr: "127.0.0.1:0",
|
||||||
NAT: nat.Any(),
|
NAT: nat.Any(),
|
||||||
BootstrapNodes: peers,
|
|
||||||
StaticNodes: peers,
|
|
||||||
TrustedNodes: peers,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,6 +142,15 @@ func initialize(t *testing.T) {
|
|||||||
t.Fatalf("failed to start server %d.", i)
|
t.Fatalf("failed to start server %d.", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for j := 0; j < i; j++ {
|
||||||
|
peerNodeId := nodes[j].id
|
||||||
|
address, _ := net.ResolveTCPAddr("tcp", nodes[j].server.ListenAddr)
|
||||||
|
peerPort := uint16(address.Port)
|
||||||
|
peerNode := discover.PubkeyID(&peerNodeId.PublicKey)
|
||||||
|
peer := discover.NewNode(peerNode, address.IP, peerPort, peerPort)
|
||||||
|
node.server.AddPeer(peer)
|
||||||
|
}
|
||||||
|
|
||||||
nodes[i] = &node
|
nodes[i] = &node
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,12 +21,13 @@ import (
|
|||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"fmt"
|
"fmt"
|
||||||
mrand "math/rand"
|
mrand "math/rand"
|
||||||
"net"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
@ -173,8 +174,6 @@ func initialize(t *testing.T) {
|
|||||||
initBloom(t)
|
initBloom(t)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
ip := net.IPv4(127, 0, 0, 1)
|
|
||||||
port0 := 30303
|
|
||||||
|
|
||||||
for i := 0; i < NumNodes; i++ {
|
for i := 0; i < NumNodes; i++ {
|
||||||
var node TestNode
|
var node TestNode
|
||||||
@ -199,40 +198,36 @@ func initialize(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed convert the key: %s.", keys[i])
|
t.Fatalf("failed convert the key: %s.", keys[i])
|
||||||
}
|
}
|
||||||
port := port0 + i
|
|
||||||
addr := fmt.Sprintf(":%d", port) // e.g. ":30303"
|
|
||||||
name := common.MakeName("whisper-go", "2.0")
|
name := common.MakeName("whisper-go", "2.0")
|
||||||
var peers []*discover.Node
|
|
||||||
if i > 0 {
|
|
||||||
peerNodeID := nodes[i-1].id
|
|
||||||
peerPort := uint16(port - 1)
|
|
||||||
peerNode := discover.PubkeyID(&peerNodeID.PublicKey)
|
|
||||||
peer := discover.NewNode(peerNode, ip, peerPort, peerPort)
|
|
||||||
peers = append(peers, peer)
|
|
||||||
}
|
|
||||||
|
|
||||||
node.server = &p2p.Server{
|
node.server = &p2p.Server{
|
||||||
Config: p2p.Config{
|
Config: p2p.Config{
|
||||||
PrivateKey: node.id,
|
PrivateKey: node.id,
|
||||||
MaxPeers: NumNodes/2 + 1,
|
MaxPeers: NumNodes/2 + 1,
|
||||||
Name: name,
|
Name: name,
|
||||||
Protocols: node.shh.Protocols(),
|
Protocols: node.shh.Protocols(),
|
||||||
ListenAddr: addr,
|
ListenAddr: "127.0.0.1:0",
|
||||||
NAT: nat.Any(),
|
NAT: nat.Any(),
|
||||||
BootstrapNodes: peers,
|
|
||||||
StaticNodes: peers,
|
|
||||||
TrustedNodes: peers,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go startServer(t, node.server)
|
||||||
|
|
||||||
nodes[i] = &node
|
nodes[i] = &node
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < NumNodes; i++ {
|
|
||||||
go startServer(t, nodes[i].server)
|
|
||||||
}
|
|
||||||
|
|
||||||
waitForServersToStart(t)
|
waitForServersToStart(t)
|
||||||
|
|
||||||
|
for i := 0; i < NumNodes; i++ {
|
||||||
|
for j := 0; j < i; j++ {
|
||||||
|
peerNodeId := nodes[j].id
|
||||||
|
address, _ := net.ResolveTCPAddr("tcp", nodes[j].server.ListenAddr)
|
||||||
|
peerPort := uint16(address.Port)
|
||||||
|
peerNode := discover.PubkeyID(&peerNodeId.PublicKey)
|
||||||
|
peer := discover.NewNode(peerNode, address.IP, peerPort, peerPort)
|
||||||
|
nodes[i].server.AddPeer(peer)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startServer(t *testing.T, s *p2p.Server) {
|
func startServer(t *testing.T, s *p2p.Server) {
|
||||||
|
Loading…
Reference in New Issue
Block a user