Skip to content

Commit 9f7306f

Browse files
committed
p2p/sim: remove SocketPipe which does not work on MS Windows
1 parent b841feb commit 9f7306f

File tree

3 files changed

+7
-163
lines changed

3 files changed

+7
-163
lines changed

p2p/simulations/adapters/inproc.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
)
3434

3535
// SimAdapter is a NodeAdapter which creates in-memory simulation nodes and
36-
// connects them using net.Pipe or OS socket connections
36+
// connects them using net.Pipe
3737
type SimAdapter struct {
3838
pipe func() (net.Conn, net.Conn, error)
3939
mtx sync.RWMutex
@@ -53,18 +53,6 @@ func NewSimAdapter(services map[string]ServiceFunc) *SimAdapter {
5353
}
5454
}
5555

56-
// NewSocketAdapter creates a SimAdapter which is capable of running in-memory
57-
// simulation nodes running any of the given services (the services to run on a
58-
// particular node are passed to the NewNode function in the NodeConfig)
59-
// the adapter uses a OS socketpairs for in-memory simulated network connections
60-
func NewSocketAdapter(services map[string]ServiceFunc) *SimAdapter {
61-
return &SimAdapter{
62-
pipe: pipes.SocketPipe,
63-
nodes: make(map[discover.NodeID]*SimNode),
64-
services: services,
65-
}
66-
}
67-
6856
func NewTCPAdapter(services map[string]ServiceFunc) *SimAdapter {
6957
return &SimAdapter{
7058
pipe: pipes.TCPPipe,
@@ -126,7 +114,7 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
126114
}
127115

128116
// Dial implements the p2p.NodeDialer interface by connecting to the node using
129-
// an in-memory net.Pipe or OS socket connection
117+
// an in-memory net.Pipe
130118
func (s *SimAdapter) Dial(dest *discover.Node) (conn net.Conn, err error) {
131119
node, ok := s.GetNode(dest.ID)
132120
if !ok {
@@ -136,7 +124,7 @@ func (s *SimAdapter) Dial(dest *discover.Node) (conn net.Conn, err error) {
136124
if srv == nil {
137125
return nil, fmt.Errorf("node not running: %s", dest.ID)
138126
}
139-
// SimAdapter.pipe is either net.Pipe (NewSimAdapter) or socketPipe (NewSocketAdapter)
127+
// SimAdapter.pipe is net.Pipe (NewSimAdapter)
140128
pipe1, pipe2, err := s.pipe()
141129
if err != nil {
142130
return nil, err
@@ -171,8 +159,8 @@ func (s *SimAdapter) GetNode(id discover.NodeID) (*SimNode, bool) {
171159
}
172160

173161
// SimNode is an in-memory simulation node which connects to other nodes using
174-
// net.Pipe or OS socket connection (see SimAdapter.Dial), running devp2p
175-
// protocols directly over that pipe
162+
// net.Pipe (see SimAdapter.Dial), running devp2p protocols directly over that
163+
// pipe
176164
type SimNode struct {
177165
lock sync.RWMutex
178166
ID discover.NodeID

p2p/simulations/adapters/inproc_test.go

Lines changed: 2 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -26,120 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/p2p/simulations/pipes"
2727
)
2828

29-
func TestSocketPipe(t *testing.T) {
30-
c1, c2, err := pipes.SocketPipe()
31-
if err != nil {
32-
t.Fatal(err)
33-
}
34-
35-
done := make(chan struct{})
36-
37-
go func() {
38-
msgs := 20
39-
size := 8
40-
41-
// OS socket pipe is blocking (depending on buffer size on OS), so writes are emitted asynchronously
42-
go func() {
43-
for i := 0; i < msgs; i++ {
44-
msg := make([]byte, size)
45-
_ = binary.PutUvarint(msg, uint64(i))
46-
47-
_, err := c1.Write(msg)
48-
if err != nil {
49-
t.Fatal(err)
50-
}
51-
}
52-
}()
53-
54-
for i := 0; i < msgs; i++ {
55-
msg := make([]byte, size)
56-
_ = binary.PutUvarint(msg, uint64(i))
57-
58-
out := make([]byte, size)
59-
_, err := c2.Read(out)
60-
if err != nil {
61-
t.Fatal(err)
62-
}
63-
64-
if !bytes.Equal(msg, out) {
65-
t.Fatalf("expected %#v, got %#v", msg, out)
66-
}
67-
}
68-
done <- struct{}{}
69-
}()
70-
71-
select {
72-
case <-done:
73-
case <-time.After(5 * time.Second):
74-
t.Fatal("test timeout")
75-
}
76-
}
77-
78-
func TestSocketPipeBidirections(t *testing.T) {
79-
c1, c2, err := pipes.SocketPipe()
80-
if err != nil {
81-
t.Fatal(err)
82-
}
83-
84-
done := make(chan struct{})
85-
86-
go func() {
87-
msgs := 100
88-
size := 4
89-
90-
// OS socket pipe is blocking (depending on buffer size on OS), so writes are emitted asynchronously
91-
go func() {
92-
for i := 0; i < msgs; i++ {
93-
msg := []byte(`ping`)
94-
95-
_, err := c1.Write(msg)
96-
if err != nil {
97-
t.Fatal(err)
98-
}
99-
}
100-
}()
101-
102-
for i := 0; i < msgs; i++ {
103-
out := make([]byte, size)
104-
_, err := c2.Read(out)
105-
if err != nil {
106-
t.Fatal(err)
107-
}
108-
109-
if bytes.Equal(out, []byte(`ping`)) {
110-
msg := []byte(`pong`)
111-
_, err := c2.Write(msg)
112-
if err != nil {
113-
t.Fatal(err)
114-
}
115-
}
116-
}
117-
118-
for i := 0; i < msgs; i++ {
119-
expected := []byte(`pong`)
120-
121-
out := make([]byte, size)
122-
_, err := c1.Read(out)
123-
if err != nil {
124-
t.Fatal(err)
125-
}
126-
127-
if !bytes.Equal(out, expected) {
128-
t.Fatalf("expected %#v, got %#v", expected, out)
129-
}
130-
}
131-
132-
done <- struct{}{}
133-
}()
134-
135-
select {
136-
case <-done:
137-
case <-time.After(5 * time.Second):
138-
t.Fatal("test timeout")
139-
}
140-
}
141-
142-
func TestTcpPipe(t *testing.T) {
29+
func TestTCPPipe(t *testing.T) {
14330
c1, c2, err := pipes.TCPPipe()
14431
if err != nil {
14532
t.Fatal(err)
@@ -184,7 +71,7 @@ func TestTcpPipe(t *testing.T) {
18471
}
18572
}
18673

187-
func TestTcpPipeBidirections(t *testing.T) {
74+
func TestTCPPipeBidirections(t *testing.T) {
18875
c1, c2, err := pipes.TCPPipe()
18976
if err != nil {
19077
t.Fatal(err)

p2p/simulations/pipes/pipes.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
package pipes
1818

1919
import (
20-
"crypto/rand"
2120
"net"
22-
"os"
23-
"syscall"
2421
)
2522

2623
// NetPipe wraps net.Pipe in a signature returning an error
@@ -56,31 +53,3 @@ func TCPPipe() (net.Conn, net.Conn, error) {
5653
}
5754
return aconn, dconn, nil
5855
}
59-
60-
// SocketPipe creates an in process full duplex pipe based on OS sockets
61-
// credit to @lmars & Flynn
62-
// https://github.com/flynn/flynn/blob/master/host/containerinit/init.go#L743-L749
63-
// using this in large simulations requires raising OS's max open file limit
64-
func SocketPipe() (net.Conn, net.Conn, error) {
65-
pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
66-
if err != nil {
67-
return nil, nil, err
68-
}
69-
nameb := make([]byte, 8)
70-
_, err = rand.Read(nameb)
71-
if err != nil {
72-
return nil, nil, err
73-
}
74-
f1 := os.NewFile(uintptr(pair[0]), string(nameb)+".out")
75-
f2 := os.NewFile(uintptr(pair[1]), string(nameb)+".in")
76-
pipe1, err := net.FileConn(f1)
77-
if err != nil {
78-
return nil, nil, err
79-
}
80-
pipe2, err := net.FileConn(f2)
81-
if err != nil {
82-
return nil, nil, err
83-
}
84-
85-
return pipe1, pipe2, nil
86-
}

0 commit comments

Comments
 (0)