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 2, 2024
1 parent 174f82e commit a50fc3b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
47 changes: 47 additions & 0 deletions p2p/pipes/pipe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package pipes

import "net"

// 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
}
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/pipes"
"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 := pipes.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
9 changes: 6 additions & 3 deletions p2p/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package p2p

import (
"errors"
"net"
"reflect"
"sync"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/pipes"
)

func TestProtocolHandshake(t *testing.T) {
Expand All @@ -37,11 +37,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 := pipes.TCPPipe()
if err != nil {
t.Fatal(err)
}

wg.Add(2)
go func() {
defer wg.Done()
Expand Down

0 comments on commit a50fc3b

Please sign in to comment.