Skip to content

Commit

Permalink
p2p: add back TCPPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
lightclient committed Aug 1, 2024
1 parent 54543ce commit 8767c0e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
7 changes: 6 additions & 1 deletion p2p/rlpx/rlpx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -382,8 +383,12 @@ func BenchmarkHandshakeRead(b *testing.B) {
}

func BenchmarkThroughput(b *testing.B) {
pipe1, pipe2, err := p2p.TCPPipe()
if err != nil {
b.Fatal(err)
}

var (
pipe1, pipe2 = net.Pipe()
conn1, conn2 = NewConn(pipe1, nil), NewConn(pipe2, &keyA.PublicKey)
handshakeDone = make(chan error, 1)
msgdata = make([]byte, 1024)
Expand Down
8 changes: 5 additions & 3 deletions p2p/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package p2p

import (
"errors"
"net"
"reflect"
"sync"
"testing"
Expand All @@ -37,11 +36,14 @@ func TestProtocolHandshake(t *testing.T) {
pub1 = crypto.FromECDSAPub(&prv1.PublicKey)[1:]
hs1 = &protoHandshake{Version: 3, ID: pub1, Caps: []Cap{{"c", 1}, {"d", 3}}}

fd0, fd1 = net.Pipe()

wg sync.WaitGroup
)

fd0, fd1, err := TCPPipe()
if err != nil {
t.Fatal(err)
}

wg.Add(2)
go func() {
defer wg.Done()
Expand Down
29 changes: 29 additions & 0 deletions p2p/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package p2p

import (
"container/heap"
"net"

"github.com/ethereum/go-ethereum/common/mclock"
)
Expand Down Expand Up @@ -74,3 +75,31 @@ func (h *expHeap) Pop() interface{} {
*h = old[0 : n-1]
return x
}

// TCPPipe creates an in process full duplex pipe based on a localhost TCP socket
func TCPPipe() (net.Conn, net.Conn, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, nil, err
}
defer l.Close()

var aconn net.Conn
aerr := make(chan error, 1)
go func() {
var err error
aconn, err = l.Accept()
aerr <- err
}()

dconn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
<-aerr
return nil, nil, err
}
if err := <-aerr; err != nil {
dconn.Close()
return nil, nil, err
}
return aconn, dconn, nil
}

0 comments on commit 8767c0e

Please sign in to comment.