forked from mirror/go-ethereum
all: library changes for swarm-network-rewrite (#16898)
This commit adds all changes needed for the merge of swarm-network-rewrite. The changes: - build: increase linter timeout - contracts/ens: export ensNode - log: add Output method and enable fractional seconds in format - metrics: relax test timeout - p2p: reduced some log levels, updates to simulation packages - rpc: increased maxClientSubscriptionBuffer to 20000release/1.8
parent
591cef17d4
commit
1836366ac1
@ -0,0 +1,259 @@ |
|||||||
|
// Copyright 2017 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 adapters |
||||||
|
|
||||||
|
import ( |
||||||
|
"bytes" |
||||||
|
"encoding/binary" |
||||||
|
"fmt" |
||||||
|
"testing" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/simulations/pipes" |
||||||
|
) |
||||||
|
|
||||||
|
func TestTCPPipe(t *testing.T) { |
||||||
|
c1, c2, err := pipes.TCPPipe() |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
done := make(chan struct{}) |
||||||
|
|
||||||
|
go func() { |
||||||
|
msgs := 50 |
||||||
|
size := 1024 |
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
msg := make([]byte, size) |
||||||
|
_ = binary.PutUvarint(msg, uint64(i)) |
||||||
|
|
||||||
|
_, err := c1.Write(msg) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
msg := make([]byte, size) |
||||||
|
_ = binary.PutUvarint(msg, uint64(i)) |
||||||
|
|
||||||
|
out := make([]byte, size) |
||||||
|
_, err := c2.Read(out) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
if !bytes.Equal(msg, out) { |
||||||
|
t.Fatalf("expected %#v, got %#v", msg, out) |
||||||
|
} |
||||||
|
} |
||||||
|
done <- struct{}{} |
||||||
|
}() |
||||||
|
|
||||||
|
select { |
||||||
|
case <-done: |
||||||
|
case <-time.After(5 * time.Second): |
||||||
|
t.Fatal("test timeout") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestTCPPipeBidirections(t *testing.T) { |
||||||
|
c1, c2, err := pipes.TCPPipe() |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
done := make(chan struct{}) |
||||||
|
|
||||||
|
go func() { |
||||||
|
msgs := 50 |
||||||
|
size := 7 |
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
msg := []byte(fmt.Sprintf("ping %02d", i)) |
||||||
|
|
||||||
|
_, err := c1.Write(msg) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
expected := []byte(fmt.Sprintf("ping %02d", i)) |
||||||
|
|
||||||
|
out := make([]byte, size) |
||||||
|
_, err := c2.Read(out) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
if !bytes.Equal(expected, out) { |
||||||
|
t.Fatalf("expected %#v, got %#v", out, expected) |
||||||
|
} else { |
||||||
|
msg := []byte(fmt.Sprintf("pong %02d", i)) |
||||||
|
_, err := c2.Write(msg) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
expected := []byte(fmt.Sprintf("pong %02d", i)) |
||||||
|
|
||||||
|
out := make([]byte, size) |
||||||
|
_, err := c1.Read(out) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
if !bytes.Equal(expected, out) { |
||||||
|
t.Fatalf("expected %#v, got %#v", out, expected) |
||||||
|
} |
||||||
|
} |
||||||
|
done <- struct{}{} |
||||||
|
}() |
||||||
|
|
||||||
|
select { |
||||||
|
case <-done: |
||||||
|
case <-time.After(5 * time.Second): |
||||||
|
t.Fatal("test timeout") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestNetPipe(t *testing.T) { |
||||||
|
c1, c2, err := pipes.NetPipe() |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
done := make(chan struct{}) |
||||||
|
|
||||||
|
go func() { |
||||||
|
msgs := 50 |
||||||
|
size := 1024 |
||||||
|
// netPipe is blocking, so writes are emitted asynchronously
|
||||||
|
go func() { |
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
msg := make([]byte, size) |
||||||
|
_ = binary.PutUvarint(msg, uint64(i)) |
||||||
|
|
||||||
|
_, err := c1.Write(msg) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
} |
||||||
|
}() |
||||||
|
|
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
msg := make([]byte, size) |
||||||
|
_ = binary.PutUvarint(msg, uint64(i)) |
||||||
|
|
||||||
|
out := make([]byte, size) |
||||||
|
_, err := c2.Read(out) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
if !bytes.Equal(msg, out) { |
||||||
|
t.Fatalf("expected %#v, got %#v", msg, out) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
done <- struct{}{} |
||||||
|
}() |
||||||
|
|
||||||
|
select { |
||||||
|
case <-done: |
||||||
|
case <-time.After(5 * time.Second): |
||||||
|
t.Fatal("test timeout") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestNetPipeBidirections(t *testing.T) { |
||||||
|
c1, c2, err := pipes.NetPipe() |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
done := make(chan struct{}) |
||||||
|
|
||||||
|
go func() { |
||||||
|
msgs := 1000 |
||||||
|
size := 8 |
||||||
|
pingTemplate := "ping %03d" |
||||||
|
pongTemplate := "pong %03d" |
||||||
|
|
||||||
|
// netPipe is blocking, so writes are emitted asynchronously
|
||||||
|
go func() { |
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
msg := []byte(fmt.Sprintf(pingTemplate, i)) |
||||||
|
|
||||||
|
_, err := c1.Write(msg) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
} |
||||||
|
}() |
||||||
|
|
||||||
|
// netPipe is blocking, so reads for pong are emitted asynchronously
|
||||||
|
go func() { |
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
expected := []byte(fmt.Sprintf(pongTemplate, i)) |
||||||
|
|
||||||
|
out := make([]byte, size) |
||||||
|
_, err := c1.Read(out) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
if !bytes.Equal(expected, out) { |
||||||
|
t.Fatalf("expected %#v, got %#v", expected, out) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
done <- struct{}{} |
||||||
|
}() |
||||||
|
|
||||||
|
// expect to read pings, and respond with pongs to the alternate connection
|
||||||
|
for i := 0; i < msgs; i++ { |
||||||
|
expected := []byte(fmt.Sprintf(pingTemplate, i)) |
||||||
|
|
||||||
|
out := make([]byte, size) |
||||||
|
_, err := c2.Read(out) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
if !bytes.Equal(expected, out) { |
||||||
|
t.Fatalf("expected %#v, got %#v", expected, out) |
||||||
|
} else { |
||||||
|
msg := []byte(fmt.Sprintf(pongTemplate, i)) |
||||||
|
|
||||||
|
_, err := c2.Write(msg) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
}() |
||||||
|
|
||||||
|
select { |
||||||
|
case <-done: |
||||||
|
case <-time.After(5 * time.Second): |
||||||
|
t.Fatal("test timeout") |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,55 @@ |
|||||||
|
// Copyright 2017 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" |
||||||
|
) |
||||||
|
|
||||||
|
// NetPipe wraps net.Pipe in a signature returning an error
|
||||||
|
func NetPipe() (net.Conn, net.Conn, error) { |
||||||
|
p1, p2 := net.Pipe() |
||||||
|
return p1, p2, nil |
||||||
|
} |
||||||
|
|
||||||
|
// 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 |
||||||
|
} |
Loading…
Reference in new issue