Browse Source

Let peerConn handle IPs in for tests

pull/1520/head
Alexander Simmerl 7 years ago
parent
commit
d596ed1bc2
No known key found for this signature in database GPG Key ID: 4694E95C9CC61BDA
3 changed files with 92 additions and 110 deletions
  1. +12
    -0
      p2p/peer.go
  2. +79
    -0
      p2p/peer_test.go
  3. +1
    -110
      p2p/test_util.go

+ 12
- 0
p2p/peer.go View File

@ -12,6 +12,8 @@ import (
tmconn "github.com/tendermint/tendermint/p2p/conn"
)
var testIPSuffix = 0
// Peer is an interface representing a peer connected on a reactor.
type Peer interface {
cmn.Service
@ -53,6 +55,16 @@ func (pc peerConn) RemoteIP() net.IP {
return pc.ips[0]
}
if pc.conn.RemoteAddr().String() == "pipe" {
pc.ips = []net.IP{
net.IP{172, 16, 0, byte(testIPSuffix)},
}
testIPSuffix++
return pc.ips[0]
}
host, _, err := net.SplitHostPort(pc.conn.RemoteAddr().String())
if err != nil {
panic(err)


+ 79
- 0
p2p/peer_test.go View File

@ -1,6 +1,8 @@
package p2p
import (
golog "log"
"net"
"testing"
"time"
@ -9,9 +11,12 @@ import (
crypto "github.com/tendermint/go-crypto"
tmconn "github.com/tendermint/tendermint/p2p/conn"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
)
const testCh = 0x01
func TestPeerBasic(t *testing.T) {
assert, require := assert.New(t), require.New(t)
@ -105,3 +110,77 @@ func createOutboundPeerAndPerformHandshake(addr *NetAddress, config *PeerConfig)
p.SetLogger(log.TestingLogger().With("peer", addr))
return p, nil
}
type remotePeer struct {
PrivKey crypto.PrivKey
Config *PeerConfig
addr *NetAddress
quit chan struct{}
channels cmn.HexBytes
}
func (rp *remotePeer) Addr() *NetAddress {
return rp.addr
}
func (rp *remotePeer) ID() ID {
return PubKeyToID(rp.PrivKey.PubKey())
}
func (rp *remotePeer) Start() {
l, e := net.Listen("tcp", "127.0.0.1:0") // any available address
if e != nil {
golog.Fatalf("net.Listen tcp :0: %+v", e)
}
rp.addr = NewNetAddress(PubKeyToID(rp.PrivKey.PubKey()), l.Addr())
rp.quit = make(chan struct{})
if rp.channels == nil {
rp.channels = []byte{testCh}
}
go rp.accept(l)
}
func (rp *remotePeer) Stop() {
close(rp.quit)
}
func (rp *remotePeer) accept(l net.Listener) {
conns := []net.Conn{}
for {
conn, err := l.Accept()
if err != nil {
golog.Fatalf("Failed to accept conn: %+v", err)
}
pc, err := newInboundPeerConn(conn, rp.Config, rp.PrivKey)
if err != nil {
golog.Fatalf("Failed to create a peer: %+v", err)
}
_, err = pc.HandshakeTimeout(NodeInfo{
ID: rp.Addr().ID,
Moniker: "remote_peer",
Network: "testing",
Version: "123.123.123",
ListenAddr: l.Addr().String(),
Channels: rp.channels,
}, 1*time.Second)
if err != nil {
golog.Fatalf("Failed to perform handshake: %+v", err)
}
conns = append(conns, conn)
select {
case <-rp.quit:
for _, conn := range conns {
if err := conn.Close(); err != nil {
golog.Fatal(err)
}
}
return
default:
}
}
}

+ 1
- 110
p2p/test_util.go View File

@ -1,10 +1,7 @@
package p2p
import (
"fmt"
golog "log"
"net"
"time"
crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
@ -14,8 +11,6 @@ import (
"github.com/tendermint/tendermint/p2p/conn"
)
const testCh = 0x01
func AddPeerToSwitch(sw *Switch, peer Peer) {
sw.peers.Add(peer)
}
@ -86,37 +81,7 @@ func Connect2Switches(switches []*Switch, i, j int) {
switchI := switches[i]
switchJ := switches[j]
p1 := &remotePeer{
Config: switchJ.peerConfig,
PrivKey: switchJ.nodeKey.PrivKey,
channels: switchJ.NodeInfo().Channels,
}
p1.Start()
c1, err := net.DialTimeout(
"tcp",
fmt.Sprintf("%s:%d", p1.addr.IP.String(), p1.addr.Port),
100*time.Millisecond,
)
if err != nil {
panic(err)
}
p2 := &remotePeer{
Config: switchI.peerConfig,
PrivKey: switchI.nodeKey.PrivKey,
channels: switchI.NodeInfo().Channels,
}
p2.Start()
c2, err := net.DialTimeout(
"tcp",
fmt.Sprintf("%s:%d", p2.addr.IP.String(), p2.addr.Port),
100*time.Millisecond,
)
if err != nil {
panic(err)
}
c1, c2 := conn.NetPipe()
doneCh := make(chan struct{})
go func() {
@ -188,77 +153,3 @@ func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch f
sw.SetNodeKey(nodeKey)
return sw
}
type remotePeer struct {
PrivKey crypto.PrivKey
Config *PeerConfig
addr *NetAddress
quit chan struct{}
channels cmn.HexBytes
}
func (rp *remotePeer) Addr() *NetAddress {
return rp.addr
}
func (rp *remotePeer) ID() ID {
return PubKeyToID(rp.PrivKey.PubKey())
}
func (rp *remotePeer) Start() {
l, e := net.Listen("tcp", "127.0.0.1:0") // any available address
if e != nil {
golog.Fatalf("net.Listen tcp :0: %+v", e)
}
rp.addr = NewNetAddress(PubKeyToID(rp.PrivKey.PubKey()), l.Addr())
rp.quit = make(chan struct{})
if rp.channels == nil {
rp.channels = []byte{testCh}
}
go rp.accept(l)
}
func (rp *remotePeer) Stop() {
close(rp.quit)
}
func (rp *remotePeer) accept(l net.Listener) {
conns := []net.Conn{}
for {
conn, err := l.Accept()
if err != nil {
golog.Fatalf("Failed to accept conn: %+v", err)
}
pc, err := newInboundPeerConn(conn, rp.Config, rp.PrivKey)
if err != nil {
golog.Fatalf("Failed to create a peer: %+v", err)
}
_, err = pc.HandshakeTimeout(NodeInfo{
ID: rp.Addr().ID,
Moniker: "remote_peer",
Network: "localhost",
Version: "123.123.123",
ListenAddr: l.Addr().String(),
Channels: rp.channels,
}, 1*time.Second)
if err != nil {
golog.Fatalf("Failed to perform handshake: %+v", err)
}
conns = append(conns, conn)
select {
case <-rp.quit:
for _, conn := range conns {
if err := conn.Close(); err != nil {
golog.Fatal(err)
}
}
return
default:
}
}
}

Loading…
Cancel
Save