From 44e967184a6f44f9d788015f265e9bb1a926688c Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 21 Jan 2018 00:33:53 -0500 Subject: [PATCH] p2p: tmconn->conn and types->p2p --- node/node.go | 3 +- p2p/base_reactor.go | 6 +-- p2p/{tmconn => conn}/conn_go110.go | 2 +- p2p/{tmconn => conn}/conn_notgo110.go | 2 +- p2p/{tmconn => conn}/connection.go | 2 +- p2p/{tmconn => conn}/connection_test.go | 2 +- p2p/{tmconn => conn}/secret_connection.go | 2 +- .../secret_connection_test.go | 2 +- p2p/{types => }/errors.go | 2 +- p2p/{types => }/key.go | 2 +- p2p/{types => }/key_test.go | 2 +- p2p/listener.go | 27 +++++----- p2p/{types => }/netaddress.go | 2 +- p2p/{types => }/netaddress_test.go | 2 +- p2p/{types => }/node_info.go | 2 +- p2p/peer.go | 29 +++++----- p2p/peer_set.go | 16 +++--- p2p/peer_set_test.go | 5 +- p2p/peer_test.go | 15 +++--- p2p/pex/addrbook.go | 53 +++++++++---------- p2p/pex/addrbook_test.go | 14 ++--- p2p/pex/known_address.go | 10 ++-- p2p/pex/pex_reactor.go | 25 +++++---- p2p/pex/pex_reactor_test.go | 27 +++++----- p2p/switch.go | 33 ++++++------ p2p/switch_test.go | 23 ++++---- p2p/test_util.go | 17 +++--- p2p/types.go | 10 ++-- 28 files changed, 160 insertions(+), 177 deletions(-) rename p2p/{tmconn => conn}/conn_go110.go (96%) rename p2p/{tmconn => conn}/conn_notgo110.go (98%) rename p2p/{tmconn => conn}/connection.go (99%) rename p2p/{tmconn => conn}/connection_test.go (99%) rename p2p/{tmconn => conn}/secret_connection.go (99%) rename p2p/{tmconn => conn}/secret_connection_test.go (99%) rename p2p/{types => }/errors.go (96%) rename p2p/{types => }/key.go (99%) rename p2p/{types => }/key_test.go (98%) rename p2p/{types => }/netaddress.go (99%) rename p2p/{types => }/netaddress_test.go (99%) rename p2p/{types => }/node_info.go (99%) diff --git a/node/node.go b/node/node.go index cab8aa805..bdbf12f82 100644 --- a/node/node.go +++ b/node/node.go @@ -24,7 +24,6 @@ import ( "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/p2p/pex" "github.com/tendermint/tendermint/p2p/trust" - p2ptypes "github.com/tendermint/tendermint/p2p/types" "github.com/tendermint/tendermint/proxy" rpccore "github.com/tendermint/tendermint/rpc/core" grpccore "github.com/tendermint/tendermint/rpc/grpc" @@ -376,7 +375,7 @@ func (n *Node) OnStart() error { // Generate node PrivKey // TODO: pass in like priv_val - nodeKey, err := p2ptypes.LoadOrGenNodeKey(n.config.NodeKeyFile()) + nodeKey, err := p2p.LoadOrGenNodeKey(n.config.NodeKeyFile()) if err != nil { return err } diff --git a/p2p/base_reactor.go b/p2p/base_reactor.go index a24a7629b..20525e675 100644 --- a/p2p/base_reactor.go +++ b/p2p/base_reactor.go @@ -1,7 +1,7 @@ package p2p import ( - "github.com/tendermint/tendermint/p2p/tmconn" + "github.com/tendermint/tendermint/p2p/conn" cmn "github.com/tendermint/tmlibs/common" ) @@ -9,7 +9,7 @@ type Reactor interface { cmn.Service // Start, Stop SetSwitch(*Switch) - GetChannels() []*tmconn.ChannelDescriptor + GetChannels() []*conn.ChannelDescriptor AddPeer(peer Peer) RemovePeer(peer Peer, reason interface{}) Receive(chID byte, peer Peer, msgBytes []byte) // CONTRACT: msgBytes are not nil @@ -32,7 +32,7 @@ func NewBaseReactor(name string, impl Reactor) *BaseReactor { func (br *BaseReactor) SetSwitch(sw *Switch) { br.Switch = sw } -func (_ *BaseReactor) GetChannels() []*tmconn.ChannelDescriptor { return nil } +func (_ *BaseReactor) GetChannels() []*conn.ChannelDescriptor { return nil } func (_ *BaseReactor) AddPeer(peer Peer) {} func (_ *BaseReactor) RemovePeer(peer Peer, reason interface{}) {} func (_ *BaseReactor) Receive(chID byte, peer Peer, msgBytes []byte) {} diff --git a/p2p/tmconn/conn_go110.go b/p2p/conn/conn_go110.go similarity index 96% rename from p2p/tmconn/conn_go110.go rename to p2p/conn/conn_go110.go index 75e55d855..682188101 100644 --- a/p2p/tmconn/conn_go110.go +++ b/p2p/conn/conn_go110.go @@ -1,6 +1,6 @@ // +build go1.10 -package tmconn +package conn // Go1.10 has a proper net.Conn implementation that // has the SetDeadline method implemented as per diff --git a/p2p/tmconn/conn_notgo110.go b/p2p/conn/conn_notgo110.go similarity index 98% rename from p2p/tmconn/conn_notgo110.go rename to p2p/conn/conn_notgo110.go index bb72d64a1..ed642eb54 100644 --- a/p2p/tmconn/conn_notgo110.go +++ b/p2p/conn/conn_notgo110.go @@ -1,6 +1,6 @@ // +build !go1.10 -package tmconn +package conn import ( "net" diff --git a/p2p/tmconn/connection.go b/p2p/conn/connection.go similarity index 99% rename from p2p/tmconn/connection.go rename to p2p/conn/connection.go index 92c48c360..71b2a13d0 100644 --- a/p2p/tmconn/connection.go +++ b/p2p/conn/connection.go @@ -1,4 +1,4 @@ -package tmconn +package conn import ( "bufio" diff --git a/p2p/tmconn/connection_test.go b/p2p/conn/connection_test.go similarity index 99% rename from p2p/tmconn/connection_test.go rename to p2p/conn/connection_test.go index 65ae017cb..9c8eccbe4 100644 --- a/p2p/tmconn/connection_test.go +++ b/p2p/conn/connection_test.go @@ -1,4 +1,4 @@ -package tmconn +package conn import ( "net" diff --git a/p2p/tmconn/secret_connection.go b/p2p/conn/secret_connection.go similarity index 99% rename from p2p/tmconn/secret_connection.go rename to p2p/conn/secret_connection.go index e1a3e0506..aa6db05bb 100644 --- a/p2p/tmconn/secret_connection.go +++ b/p2p/conn/secret_connection.go @@ -4,7 +4,7 @@ // is known ahead of time, and thus we are technically // still vulnerable to MITM. (TODO!) // See docs/sts-final.pdf for more info -package tmconn +package conn import ( "bytes" diff --git a/p2p/tmconn/secret_connection_test.go b/p2p/conn/secret_connection_test.go similarity index 99% rename from p2p/tmconn/secret_connection_test.go rename to p2p/conn/secret_connection_test.go index 5ef2c410e..8af9cdeb5 100644 --- a/p2p/tmconn/secret_connection_test.go +++ b/p2p/conn/secret_connection_test.go @@ -1,4 +1,4 @@ -package tmconn +package conn import ( "io" diff --git a/p2p/types/errors.go b/p2p/errors.go similarity index 96% rename from p2p/types/errors.go rename to p2p/errors.go index ead2a8334..cb6a7051a 100644 --- a/p2p/types/errors.go +++ b/p2p/errors.go @@ -1,4 +1,4 @@ -package types +package p2p import ( "errors" diff --git a/p2p/types/key.go b/p2p/key.go similarity index 99% rename from p2p/types/key.go rename to p2p/key.go index 4ce5ee500..ea0f0b071 100644 --- a/p2p/types/key.go +++ b/p2p/key.go @@ -1,4 +1,4 @@ -package types +package p2p import ( "bytes" diff --git a/p2p/types/key_test.go b/p2p/key_test.go similarity index 98% rename from p2p/types/key_test.go rename to p2p/key_test.go index f18fb3b92..c2e1f3e0e 100644 --- a/p2p/types/key_test.go +++ b/p2p/key_test.go @@ -1,4 +1,4 @@ -package types +package p2p import ( "bytes" diff --git a/p2p/listener.go b/p2p/listener.go index 01d718330..884c45ee8 100644 --- a/p2p/listener.go +++ b/p2p/listener.go @@ -6,7 +6,6 @@ import ( "strconv" "time" - "github.com/tendermint/tendermint/p2p/types" "github.com/tendermint/tendermint/p2p/upnp" cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/log" @@ -14,8 +13,8 @@ import ( type Listener interface { Connections() <-chan net.Conn - InternalAddress() *types.NetAddress - ExternalAddress() *types.NetAddress + InternalAddress() *NetAddress + ExternalAddress() *NetAddress String() string Stop() error } @@ -25,8 +24,8 @@ type DefaultListener struct { cmn.BaseService listener net.Listener - intAddr *types.NetAddress - extAddr *types.NetAddress + intAddr *NetAddress + extAddr *NetAddress connections chan net.Conn } @@ -72,14 +71,14 @@ func NewDefaultListener(protocol string, lAddr string, skipUPNP bool, logger log logger.Info("Local listener", "ip", listenerIP, "port", listenerPort) // Determine internal address... - var intAddr *types.NetAddress - intAddr, err = types.NewNetAddressString(lAddr) + var intAddr *NetAddress + intAddr, err = NewNetAddressString(lAddr) if err != nil { panic(err) } // Determine external address... - var extAddr *types.NetAddress + var extAddr *NetAddress if !skipUPNP { // If the lAddrIP is INADDR_ANY, try UPnP if lAddrIP == "" || lAddrIP == "0.0.0.0" { @@ -152,11 +151,11 @@ func (l *DefaultListener) Connections() <-chan net.Conn { return l.connections } -func (l *DefaultListener) InternalAddress() *types.NetAddress { +func (l *DefaultListener) InternalAddress() *NetAddress { return l.intAddr } -func (l *DefaultListener) ExternalAddress() *types.NetAddress { +func (l *DefaultListener) ExternalAddress() *NetAddress { return l.extAddr } @@ -173,7 +172,7 @@ func (l *DefaultListener) String() string { /* external address helpers */ // UPNP external address discovery & port mapping -func getUPNPExternalAddress(externalPort, internalPort int, logger log.Logger) *types.NetAddress { +func getUPNPExternalAddress(externalPort, internalPort int, logger log.Logger) *NetAddress { logger.Info("Getting UPNP external address") nat, err := upnp.Discover() if err != nil { @@ -199,11 +198,11 @@ func getUPNPExternalAddress(externalPort, internalPort int, logger log.Logger) * } logger.Info("Got UPNP external address", "address", ext) - return types.NewNetAddressIPPort(ext, uint16(externalPort)) + return NewNetAddressIPPort(ext, uint16(externalPort)) } // TODO: use syscalls: see issue #712 -func getNaiveExternalAddress(port int, settleForLocal bool, logger log.Logger) *types.NetAddress { +func getNaiveExternalAddress(port int, settleForLocal bool, logger log.Logger) *NetAddress { addrs, err := net.InterfaceAddrs() if err != nil { panic(cmn.Fmt("Could not fetch interface addresses: %v", err)) @@ -218,7 +217,7 @@ func getNaiveExternalAddress(port int, settleForLocal bool, logger log.Logger) * if v4 == nil || (!settleForLocal && v4[0] == 127) { continue } // loopback - return types.NewNetAddressIPPort(ipnet.IP, uint16(port)) + return NewNetAddressIPPort(ipnet.IP, uint16(port)) } // try again, but settle for local diff --git a/p2p/types/netaddress.go b/p2p/netaddress.go similarity index 99% rename from p2p/types/netaddress.go rename to p2p/netaddress.go index f0b397e8e..333d16e5d 100644 --- a/p2p/types/netaddress.go +++ b/p2p/netaddress.go @@ -2,7 +2,7 @@ // Originally Copyright (c) 2013-2014 Conformal Systems LLC. // https://github.com/conformal/btcd/blob/master/LICENSE -package types +package p2p import ( "encoding/hex" diff --git a/p2p/types/netaddress_test.go b/p2p/netaddress_test.go similarity index 99% rename from p2p/types/netaddress_test.go rename to p2p/netaddress_test.go index 0119cc3b1..6c1930a2f 100644 --- a/p2p/types/netaddress_test.go +++ b/p2p/netaddress_test.go @@ -1,4 +1,4 @@ -package types +package p2p import ( "net" diff --git a/p2p/types/node_info.go b/p2p/node_info.go similarity index 99% rename from p2p/types/node_info.go rename to p2p/node_info.go index 10c486852..552c464d9 100644 --- a/p2p/types/node_info.go +++ b/p2p/node_info.go @@ -1,4 +1,4 @@ -package types +package p2p import ( "fmt" diff --git a/p2p/peer.go b/p2p/peer.go index 17c5861f6..60f9dceba 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -12,18 +12,17 @@ import ( cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/log" - "github.com/tendermint/tendermint/p2p/tmconn" - "github.com/tendermint/tendermint/p2p/types" + tmconn "github.com/tendermint/tendermint/p2p/conn" ) // Peer is an interface representing a peer connected on a reactor. type Peer interface { cmn.Service - ID() types.ID // peer's cryptographic ID - IsOutbound() bool // did we dial the peer - IsPersistent() bool // do we redial this peer when we disconnect - NodeInfo() types.NodeInfo // peer's info + ID() ID // peer's cryptographic ID + IsOutbound() bool // did we dial the peer + IsPersistent() bool // do we redial this peer when we disconnect + NodeInfo() NodeInfo // peer's info Status() tmconn.ConnectionStatus Send(byte, interface{}) bool @@ -49,7 +48,7 @@ type peer struct { persistent bool config *PeerConfig - nodeInfo types.NodeInfo + nodeInfo NodeInfo Data *cmn.CMap // User data. } @@ -79,7 +78,7 @@ func DefaultPeerConfig() *PeerConfig { } } -func newOutboundPeer(addr *types.NetAddress, reactorsByCh map[byte]Reactor, chDescs []*tmconn.ChannelDescriptor, +func newOutboundPeer(addr *NetAddress, reactorsByCh map[byte]Reactor, chDescs []*tmconn.ChannelDescriptor, onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig, persistent bool) (*peer, error) { conn, err := dial(addr, config) @@ -174,8 +173,8 @@ func (p *peer) OnStop() { // Implements Peer // ID returns the peer's ID - the hex encoded hash of its pubkey. -func (p *peer) ID() types.ID { - return types.PubKeyToID(p.PubKey()) +func (p *peer) ID() ID { + return PubKeyToID(p.PubKey()) } // IsOutbound returns true if the connection is outbound, false otherwise. @@ -189,7 +188,7 @@ func (p *peer) IsPersistent() bool { } // NodeInfo returns a copy of the peer's NodeInfo. -func (p *peer) NodeInfo() types.NodeInfo { +func (p *peer) NodeInfo() NodeInfo { return p.nodeInfo } @@ -239,13 +238,13 @@ func (p *peer) CloseConn() { // HandshakeTimeout performs the Tendermint P2P handshake between a given node and the peer // by exchanging their NodeInfo. It sets the received nodeInfo on the peer. // NOTE: blocking -func (p *peer) HandshakeTimeout(ourNodeInfo types.NodeInfo, timeout time.Duration) error { +func (p *peer) HandshakeTimeout(ourNodeInfo NodeInfo, timeout time.Duration) error { // Set deadline for handshake so we don't block forever on conn.ReadFull if err := p.conn.SetDeadline(time.Now().Add(timeout)); err != nil { return errors.Wrap(err, "Error setting deadline") } - var peerNodeInfo types.NodeInfo + var peerNodeInfo NodeInfo var err1 error var err2 error cmn.Parallel( @@ -255,7 +254,7 @@ func (p *peer) HandshakeTimeout(ourNodeInfo types.NodeInfo, timeout time.Duratio }, func() { var n int - wire.ReadBinary(&peerNodeInfo, p.conn, types.MaxNodeInfoSize(), &n, &err2) + wire.ReadBinary(&peerNodeInfo, p.conn, MaxNodeInfoSize(), &n, &err2) p.Logger.Info("Peer handshake", "peerNodeInfo", peerNodeInfo) }) if err1 != nil { @@ -311,7 +310,7 @@ func (p *peer) String() string { //------------------------------------------------------------------ // helper funcs -func dial(addr *types.NetAddress, config *PeerConfig) (net.Conn, error) { +func dial(addr *NetAddress, config *PeerConfig) (net.Conn, error) { conn, err := addr.DialTimeout(config.DialTimeout * time.Second) if err != nil { return nil, err diff --git a/p2p/peer_set.go b/p2p/peer_set.go index 7a0680cb7..dc53174a1 100644 --- a/p2p/peer_set.go +++ b/p2p/peer_set.go @@ -2,14 +2,12 @@ package p2p import ( "sync" - - "github.com/tendermint/tendermint/p2p/types" ) // IPeerSet has a (immutable) subset of the methods of PeerSet. type IPeerSet interface { - Has(key types.ID) bool - Get(key types.ID) Peer + Has(key ID) bool + Get(key ID) Peer List() []Peer Size() int } @@ -20,7 +18,7 @@ type IPeerSet interface { // Iteration over the peers is super fast and thread-safe. type PeerSet struct { mtx sync.Mutex - lookup map[types.ID]*peerSetItem + lookup map[ID]*peerSetItem list []Peer } @@ -32,7 +30,7 @@ type peerSetItem struct { // NewPeerSet creates a new peerSet with a list of initial capacity of 256 items. func NewPeerSet() *PeerSet { return &PeerSet{ - lookup: make(map[types.ID]*peerSetItem), + lookup: make(map[ID]*peerSetItem), list: make([]Peer, 0, 256), } } @@ -43,7 +41,7 @@ func (ps *PeerSet) Add(peer Peer) error { ps.mtx.Lock() defer ps.mtx.Unlock() if ps.lookup[peer.ID()] != nil { - return types.ErrSwitchDuplicatePeer + return ErrSwitchDuplicatePeer } index := len(ps.list) @@ -56,7 +54,7 @@ func (ps *PeerSet) Add(peer Peer) error { // Has returns true iff the PeerSet contains // the peer referred to by this peerKey. -func (ps *PeerSet) Has(peerKey types.ID) bool { +func (ps *PeerSet) Has(peerKey ID) bool { ps.mtx.Lock() _, ok := ps.lookup[peerKey] ps.mtx.Unlock() @@ -64,7 +62,7 @@ func (ps *PeerSet) Has(peerKey types.ID) bool { } // Get looks up a peer by the provided peerKey. -func (ps *PeerSet) Get(peerKey types.ID) Peer { +func (ps *PeerSet) Get(peerKey ID) Peer { ps.mtx.Lock() defer ps.mtx.Unlock() item, ok := ps.lookup[peerKey] diff --git a/p2p/peer_set_test.go b/p2p/peer_set_test.go index 7d7ed1062..e906eb8e7 100644 --- a/p2p/peer_set_test.go +++ b/p2p/peer_set_test.go @@ -8,14 +8,13 @@ import ( "github.com/stretchr/testify/assert" crypto "github.com/tendermint/go-crypto" - "github.com/tendermint/tendermint/p2p/types" cmn "github.com/tendermint/tmlibs/common" ) // Returns an empty dummy peer func randPeer() *peer { return &peer{ - nodeInfo: types.NodeInfo{ + nodeInfo: NodeInfo{ ListenAddr: cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256), PubKey: crypto.GenPrivKeyEd25519().Wrap().PubKey(), }, @@ -120,7 +119,7 @@ func TestPeerSetAddDuplicate(t *testing.T) { // Our next procedure is to ensure that only one addition // succeeded and that the rest are each ErrSwitchDuplicatePeer. - wantErrCount, gotErrCount := n-1, errsTally[types.ErrSwitchDuplicatePeer] + wantErrCount, gotErrCount := n-1, errsTally[ErrSwitchDuplicatePeer] assert.Equal(t, wantErrCount, gotErrCount, "invalid ErrSwitchDuplicatePeer count") wantNilErrCount, gotNilErrCount := 1, errsTally[nil] diff --git a/p2p/peer_test.go b/p2p/peer_test.go index dc13cf9d6..978775c8e 100644 --- a/p2p/peer_test.go +++ b/p2p/peer_test.go @@ -10,8 +10,7 @@ import ( "github.com/stretchr/testify/require" crypto "github.com/tendermint/go-crypto" - "github.com/tendermint/tendermint/p2p/tmconn" - "github.com/tendermint/tendermint/p2p/types" + tmconn "github.com/tendermint/tendermint/p2p/conn" ) func TestPeerBasic(t *testing.T) { @@ -82,7 +81,7 @@ func TestPeerSend(t *testing.T) { assert.True(p.Send(0x01, "Asylum")) } -func createOutboundPeerAndPerformHandshake(addr *types.NetAddress, config *PeerConfig) (*peer, error) { +func createOutboundPeerAndPerformHandshake(addr *NetAddress, config *PeerConfig) (*peer, error) { chDescs := []*tmconn.ChannelDescriptor{ {ID: 0x01, Priority: 1}, } @@ -92,7 +91,7 @@ func createOutboundPeerAndPerformHandshake(addr *types.NetAddress, config *PeerC if err != nil { return nil, err } - err = p.HandshakeTimeout(types.NodeInfo{ + err = p.HandshakeTimeout(NodeInfo{ PubKey: pk.PubKey(), Moniker: "host_peer", Network: "testing", @@ -107,11 +106,11 @@ func createOutboundPeerAndPerformHandshake(addr *types.NetAddress, config *PeerC type remotePeer struct { PrivKey crypto.PrivKey Config *PeerConfig - addr *types.NetAddress + addr *NetAddress quit chan struct{} } -func (p *remotePeer) Addr() *types.NetAddress { +func (p *remotePeer) Addr() *NetAddress { return p.addr } @@ -124,7 +123,7 @@ func (p *remotePeer) Start() { if e != nil { golog.Fatalf("net.Listen tcp :0: %+v", e) } - p.addr = types.NewNetAddress("", l.Addr()) + p.addr = NewNetAddress("", l.Addr()) p.quit = make(chan struct{}) go p.accept(l) } @@ -143,7 +142,7 @@ func (p *remotePeer) accept(l net.Listener) { if err != nil { golog.Fatalf("Failed to create a peer: %+v", err) } - err = peer.HandshakeTimeout(types.NodeInfo{ + err = peer.HandshakeTimeout(NodeInfo{ PubKey: p.PrivKey.PubKey(), Moniker: "remote_peer", Network: "testing", diff --git a/p2p/pex/addrbook.go b/p2p/pex/addrbook.go index 93f352117..b7f60682d 100644 --- a/p2p/pex/addrbook.go +++ b/p2p/pex/addrbook.go @@ -15,9 +15,8 @@ import ( "time" crypto "github.com/tendermint/go-crypto" + "github.com/tendermint/tendermint/p2p" cmn "github.com/tendermint/tmlibs/common" - - "github.com/tendermint/tendermint/p2p/types" ) const ( @@ -32,25 +31,25 @@ type AddrBook interface { cmn.Service // Add our own addresses so we don't later add ourselves - AddOurAddress(*types.NetAddress) + AddOurAddress(*p2p.NetAddress) // Add and remove an address - AddAddress(addr *types.NetAddress, src *types.NetAddress) error - RemoveAddress(addr *types.NetAddress) + AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error + RemoveAddress(addr *p2p.NetAddress) // Do we need more peers? NeedMoreAddrs() bool // Pick an address to dial - PickAddress(newBias int) *types.NetAddress + PickAddress(newBias int) *p2p.NetAddress // Mark address - MarkGood(*types.NetAddress) - MarkAttempt(*types.NetAddress) - MarkBad(*types.NetAddress) + MarkGood(*p2p.NetAddress) + MarkAttempt(*p2p.NetAddress) + MarkBad(*p2p.NetAddress) // Send a selection of addresses to peers - GetSelection() []*types.NetAddress + GetSelection() []*p2p.NetAddress // TODO: remove ListOfKnownAddresses() []*knownAddress @@ -71,8 +70,8 @@ type addrBook struct { // accessed concurrently mtx sync.Mutex rand *rand.Rand - ourAddrs map[string]*types.NetAddress - addrLookup map[types.ID]*knownAddress // new & old + ourAddrs map[string]*p2p.NetAddress + addrLookup map[p2p.ID]*knownAddress // new & old bucketsOld []map[string]*knownAddress bucketsNew []map[string]*knownAddress nOld int @@ -86,8 +85,8 @@ type addrBook struct { func NewAddrBook(filePath string, routabilityStrict bool) *addrBook { am := &addrBook{ rand: rand.New(rand.NewSource(time.Now().UnixNano())), // TODO: seed from outside - ourAddrs: make(map[string]*types.NetAddress), - addrLookup: make(map[types.ID]*knownAddress), + ourAddrs: make(map[string]*p2p.NetAddress), + addrLookup: make(map[p2p.ID]*knownAddress), filePath: filePath, routabilityStrict: routabilityStrict, } @@ -139,7 +138,7 @@ func (a *addrBook) Wait() { //------------------------------------------------------- // AddOurAddress one of our addresses. -func (a *addrBook) AddOurAddress(addr *types.NetAddress) { +func (a *addrBook) AddOurAddress(addr *p2p.NetAddress) { a.mtx.Lock() defer a.mtx.Unlock() a.Logger.Info("Add our address to book", "addr", addr) @@ -148,14 +147,14 @@ func (a *addrBook) AddOurAddress(addr *types.NetAddress) { // AddAddress implements AddrBook - adds the given address as received from the given source. // NOTE: addr must not be nil -func (a *addrBook) AddAddress(addr *types.NetAddress, src *types.NetAddress) error { +func (a *addrBook) AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error { a.mtx.Lock() defer a.mtx.Unlock() return a.addAddress(addr, src) } // RemoveAddress implements AddrBook - removes the address from the book. -func (a *addrBook) RemoveAddress(addr *types.NetAddress) { +func (a *addrBook) RemoveAddress(addr *p2p.NetAddress) { a.mtx.Lock() defer a.mtx.Unlock() ka := a.addrLookup[addr.ID] @@ -177,7 +176,7 @@ func (a *addrBook) NeedMoreAddrs() bool { // and determines how biased we are to pick an address from a new bucket. // PickAddress returns nil if the AddrBook is empty or if we try to pick // from an empty bucket. -func (a *addrBook) PickAddress(newBias int) *types.NetAddress { +func (a *addrBook) PickAddress(newBias int) *p2p.NetAddress { a.mtx.Lock() defer a.mtx.Unlock() @@ -223,7 +222,7 @@ func (a *addrBook) PickAddress(newBias int) *types.NetAddress { // MarkGood implements AddrBook - it marks the peer as good and // moves it into an "old" bucket. -func (a *addrBook) MarkGood(addr *types.NetAddress) { +func (a *addrBook) MarkGood(addr *p2p.NetAddress) { a.mtx.Lock() defer a.mtx.Unlock() ka := a.addrLookup[addr.ID] @@ -237,7 +236,7 @@ func (a *addrBook) MarkGood(addr *types.NetAddress) { } // MarkAttempt implements AddrBook - it marks that an attempt was made to connect to the address. -func (a *addrBook) MarkAttempt(addr *types.NetAddress) { +func (a *addrBook) MarkAttempt(addr *p2p.NetAddress) { a.mtx.Lock() defer a.mtx.Unlock() ka := a.addrLookup[addr.ID] @@ -249,13 +248,13 @@ func (a *addrBook) MarkAttempt(addr *types.NetAddress) { // MarkBad implements AddrBook. Currently it just ejects the address. // TODO: black list for some amount of time -func (a *addrBook) MarkBad(addr *types.NetAddress) { +func (a *addrBook) MarkBad(addr *p2p.NetAddress) { a.RemoveAddress(addr) } // GetSelection implements AddrBook. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols. -func (a *addrBook) GetSelection() []*types.NetAddress { +func (a *addrBook) GetSelection() []*p2p.NetAddress { a.mtx.Lock() defer a.mtx.Unlock() @@ -263,7 +262,7 @@ func (a *addrBook) GetSelection() []*types.NetAddress { return nil } - allAddr := make([]*types.NetAddress, a.size()) + allAddr := make([]*p2p.NetAddress, a.size()) i := 0 for _, ka := range a.addrLookup { allAddr[i] = ka.Addr @@ -466,7 +465,7 @@ func (a *addrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress { // adds the address to a "new" bucket. if its already in one, // it only adds it probabilistically -func (a *addrBook) addAddress(addr, src *types.NetAddress) error { +func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error { if a.routabilityStrict && !addr.Routable() { return fmt.Errorf("Cannot add non-routable address %v", addr) } @@ -573,7 +572,7 @@ func (a *addrBook) moveToOld(ka *knownAddress) { // doublesha256( key + sourcegroup + // int64(doublesha256(key + group + sourcegroup))%bucket_per_group ) % num_new_buckets -func (a *addrBook) calcNewBucket(addr, src *types.NetAddress) int { +func (a *addrBook) calcNewBucket(addr, src *p2p.NetAddress) int { data1 := []byte{} data1 = append(data1, []byte(a.key)...) data1 = append(data1, []byte(a.groupKey(addr))...) @@ -594,7 +593,7 @@ func (a *addrBook) calcNewBucket(addr, src *types.NetAddress) int { // doublesha256( key + group + // int64(doublesha256(key + addr))%buckets_per_group ) % num_old_buckets -func (a *addrBook) calcOldBucket(addr *types.NetAddress) int { +func (a *addrBook) calcOldBucket(addr *p2p.NetAddress) int { data1 := []byte{} data1 = append(data1, []byte(a.key)...) data1 = append(data1, []byte(addr.String())...) @@ -616,7 +615,7 @@ func (a *addrBook) calcOldBucket(addr *types.NetAddress) int { // This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string // "local" for a local address and the string "unroutable" for an unroutable // address. -func (a *addrBook) groupKey(na *types.NetAddress) string { +func (a *addrBook) groupKey(na *p2p.NetAddress) string { if a.routabilityStrict && na.Local() { return "local" } diff --git a/p2p/pex/addrbook_test.go b/p2p/pex/addrbook_test.go index 206e3401f..166d31847 100644 --- a/p2p/pex/addrbook_test.go +++ b/p2p/pex/addrbook_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/tendermint/tendermint/p2p/types" + "github.com/tendermint/tendermint/p2p" cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/log" ) @@ -169,8 +169,8 @@ func TestAddrBookHandlesDuplicates(t *testing.T) { } type netAddressPair struct { - addr *types.NetAddress - src *types.NetAddress + addr *p2p.NetAddress + src *p2p.NetAddress } func randNetAddressPairs(t *testing.T, n int) []netAddressPair { @@ -181,7 +181,7 @@ func randNetAddressPairs(t *testing.T, n int) []netAddressPair { return randAddrs } -func randIPv4Address(t *testing.T) *types.NetAddress { +func randIPv4Address(t *testing.T) *p2p.NetAddress { for { ip := fmt.Sprintf("%v.%v.%v.%v", rand.Intn(254)+1, @@ -190,9 +190,9 @@ func randIPv4Address(t *testing.T) *types.NetAddress { rand.Intn(255), ) port := rand.Intn(65535-1) + 1 - id := types.ID(hex.EncodeToString(cmn.RandBytes(types.IDByteLength))) - idAddr := types.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port)) - addr, err := types.NewNetAddressString(idAddr) + id := p2p.ID(hex.EncodeToString(cmn.RandBytes(p2p.IDByteLength))) + idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port)) + addr, err := p2p.NewNetAddressString(idAddr) assert.Nil(t, err, "error generating rand network address") if addr.Routable() { return addr diff --git a/p2p/pex/known_address.go b/p2p/pex/known_address.go index db6d021f2..e26fdb7bd 100644 --- a/p2p/pex/known_address.go +++ b/p2p/pex/known_address.go @@ -3,14 +3,14 @@ package pex import ( "time" - "github.com/tendermint/tendermint/p2p/types" + "github.com/tendermint/tendermint/p2p" ) // knownAddress tracks information about a known network address // that is used to determine how viable an address is. type knownAddress struct { - Addr *types.NetAddress - Src *types.NetAddress + Addr *p2p.NetAddress + Src *p2p.NetAddress Attempts int32 LastAttempt time.Time LastSuccess time.Time @@ -18,7 +18,7 @@ type knownAddress struct { Buckets []int } -func newKnownAddress(addr *types.NetAddress, src *types.NetAddress) *knownAddress { +func newKnownAddress(addr *p2p.NetAddress, src *p2p.NetAddress) *knownAddress { return &knownAddress{ Addr: addr, Src: src, @@ -29,7 +29,7 @@ func newKnownAddress(addr *types.NetAddress, src *types.NetAddress) *knownAddres } } -func (ka *knownAddress) ID() types.ID { +func (ka *knownAddress) ID() p2p.ID { return ka.Addr.ID } diff --git a/p2p/pex/pex_reactor.go b/p2p/pex/pex_reactor.go index 24c9417f7..53075a1d6 100644 --- a/p2p/pex/pex_reactor.go +++ b/p2p/pex/pex_reactor.go @@ -13,8 +13,7 @@ import ( cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tendermint/p2p" - "github.com/tendermint/tendermint/p2p/tmconn" - "github.com/tendermint/tendermint/p2p/types" + "github.com/tendermint/tendermint/p2p/conn" ) type Peer = p2p.Peer @@ -117,8 +116,8 @@ func (r *PEXReactor) OnStop() { } // GetChannels implements Reactor -func (r *PEXReactor) GetChannels() []*tmconn.ChannelDescriptor { - return []*tmconn.ChannelDescriptor{ +func (r *PEXReactor) GetChannels() []*conn.ChannelDescriptor { + return []*conn.ChannelDescriptor{ { ID: PexChannel, Priority: 1, @@ -231,7 +230,7 @@ func (r *PEXReactor) RequestAddrs(p Peer) { // ReceiveAddrs adds the given addrs to the addrbook if theres an open // request for this peer and deletes the open request. // If there's no open request for the src peer, it returns an error. -func (r *PEXReactor) ReceiveAddrs(addrs []*types.NetAddress, src Peer) error { +func (r *PEXReactor) ReceiveAddrs(addrs []*p2p.NetAddress, src Peer) error { id := string(src.ID()) if !r.requestsSent.Has(id) { @@ -250,7 +249,7 @@ func (r *PEXReactor) ReceiveAddrs(addrs []*types.NetAddress, src Peer) error { } // SendAddrs sends addrs to the peer. -func (r *PEXReactor) SendAddrs(p Peer, netAddrs []*types.NetAddress) { +func (r *PEXReactor) SendAddrs(p Peer, netAddrs []*p2p.NetAddress) { p.Send(PexChannel, struct{ PexMessage }{&pexAddrsMessage{Addrs: netAddrs}}) } @@ -300,7 +299,7 @@ func (r *PEXReactor) ensurePeers() { // NOTE: range here is [10, 90]. Too high ? newBias := cmn.MinInt(numOutPeers, 8)*10 + 10 - toDial := make(map[types.ID]*types.NetAddress) + toDial := make(map[p2p.ID]*p2p.NetAddress) // Try maxAttempts times to pick numToDial addresses to dial maxAttempts := numToDial * 3 for i := 0; i < maxAttempts && len(toDial) < numToDial; i++ { @@ -323,11 +322,11 @@ func (r *PEXReactor) ensurePeers() { // Dial picked addresses for _, item := range toDial { - go func(picked *types.NetAddress) { + go func(picked *p2p.NetAddress) { _, err := r.Switch.DialPeerWithAddress(picked, false) if err != nil { // TODO: detect more "bad peer" scenarios - if _, ok := err.(types.ErrSwitchAuthenticationFailure); ok { + if _, ok := err.(p2p.ErrSwitchAuthenticationFailure); ok { r.book.MarkBad(picked) } else { r.book.MarkAttempt(picked) @@ -360,7 +359,7 @@ func (r *PEXReactor) checkSeeds() error { if lSeeds == 0 { return nil } - _, errs := types.NewNetAddressStrings(r.config.Seeds) + _, errs := p2p.NewNetAddressStrings(r.config.Seeds) for _, err := range errs { if err != nil { return err @@ -375,7 +374,7 @@ func (r *PEXReactor) dialSeeds() { if lSeeds == 0 { return } - seedAddrs, _ := types.NewNetAddressStrings(r.config.Seeds) + seedAddrs, _ := p2p.NewNetAddressStrings(r.config.Seeds) perm := rand.Perm(lSeeds) // perm := r.Switch.rng.Perm(lSeeds) @@ -420,7 +419,7 @@ func (r *PEXReactor) crawlPeersRoutine() { // network crawling performed during seed/crawler mode. type crawlPeerInfo struct { // The listening address of a potential peer we learned about - Addr *types.NetAddress + Addr *p2p.NetAddress // The last time we attempt to reach this address LastAttempt time.Time @@ -544,7 +543,7 @@ func (m *pexRequestMessage) String() string { A message with announced peer addresses. */ type pexAddrsMessage struct { - Addrs []*types.NetAddress + Addrs []*p2p.NetAddress } func (m *pexAddrsMessage) String() string { diff --git a/p2p/pex/pex_reactor_test.go b/p2p/pex/pex_reactor_test.go index 439914ac8..c52e45b47 100644 --- a/p2p/pex/pex_reactor_test.go +++ b/p2p/pex/pex_reactor_test.go @@ -17,8 +17,7 @@ import ( cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/p2p" - "github.com/tendermint/tendermint/p2p/tmconn" - "github.com/tendermint/tendermint/p2p/types" + "github.com/tendermint/tendermint/p2p/conn" ) var ( @@ -101,7 +100,7 @@ func TestPEXReactorRunning(t *testing.T) { // fill the address book and add listeners for _, s := range switches { - addr, _ := types.NewNetAddressString(s.NodeInfo().ListenAddr) + addr, _ := p2p.NewNetAddressString(s.NodeInfo().ListenAddr) book.AddAddress(addr, addr) s.AddListener(p2p.NewDefaultListener("tcp", s.NodeInfo().ListenAddr, true, log.TestingLogger())) } @@ -171,7 +170,7 @@ func TestPEXReactorReceive(t *testing.T) { r.RequestAddrs(peer) size := book.Size() - addrs := []*types.NetAddress{peer.NodeInfo().NetAddress()} + addrs := []*p2p.NetAddress{peer.NodeInfo().NetAddress()} msg := wire.BinaryBytes(struct{ PexMessage }{&pexAddrsMessage{Addrs: addrs}}) r.Receive(PexChannel, peer, msg) assert.Equal(size+1, book.Size()) @@ -246,7 +245,7 @@ func TestPEXReactorAddrsMessageAbuse(t *testing.T) { assert.True(r.requestsSent.Has(id)) assert.True(sw.Peers().Has(peer.ID())) - addrs := []*types.NetAddress{peer.NodeInfo().NetAddress()} + addrs := []*p2p.NetAddress{peer.NodeInfo().NetAddress()} msg := wire.BinaryBytes(struct{ PexMessage }{&pexAddrsMessage{Addrs: addrs}}) // receive some addrs. should clear the request @@ -340,7 +339,7 @@ func TestPEXReactorCrawlStatus(t *testing.T) { type mockPeer struct { *cmn.BaseService pubKey crypto.PubKey - addr *types.NetAddress + addr *p2p.NetAddress outbound, persistent bool } @@ -355,17 +354,17 @@ func newMockPeer() mockPeer { return mp } -func (mp mockPeer) ID() types.ID { return types.PubKeyToID(mp.pubKey) } +func (mp mockPeer) ID() p2p.ID { return p2p.PubKeyToID(mp.pubKey) } func (mp mockPeer) IsOutbound() bool { return mp.outbound } func (mp mockPeer) IsPersistent() bool { return mp.persistent } -func (mp mockPeer) NodeInfo() types.NodeInfo { - return types.NodeInfo{ +func (mp mockPeer) NodeInfo() p2p.NodeInfo { + return p2p.NodeInfo{ PubKey: mp.pubKey, ListenAddr: mp.addr.DialString(), } } -func (mp mockPeer) Status() tmconn.ConnectionStatus { return tmconn.ConnectionStatus{} } -func (mp mockPeer) Send(byte, interface{}) bool { return false } -func (mp mockPeer) TrySend(byte, interface{}) bool { return false } -func (mp mockPeer) Set(string, interface{}) {} -func (mp mockPeer) Get(string) interface{} { return nil } +func (mp mockPeer) Status() conn.ConnectionStatus { return conn.ConnectionStatus{} } +func (mp mockPeer) Send(byte, interface{}) bool { return false } +func (mp mockPeer) TrySend(byte, interface{}) bool { return false } +func (mp mockPeer) Set(string, interface{}) {} +func (mp mockPeer) Get(string) interface{} { return nil } diff --git a/p2p/switch.go b/p2p/switch.go index c9938374c..f29d1b273 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -11,8 +11,7 @@ import ( crypto "github.com/tendermint/go-crypto" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/p2p/tmconn" - "github.com/tendermint/tendermint/p2p/types" + "github.com/tendermint/tendermint/p2p/conn" cmn "github.com/tendermint/tmlibs/common" ) @@ -35,7 +34,7 @@ const ( //----------------------------------------------------------------------------- type AddrBook interface { - AddAddress(addr *types.NetAddress, src *types.NetAddress) error + AddAddress(addr *NetAddress, src *NetAddress) error } //----------------------------------------------------------------------------- @@ -51,12 +50,12 @@ type Switch struct { peerConfig *PeerConfig listeners []Listener reactors map[string]Reactor - chDescs []*tmconn.ChannelDescriptor + chDescs []*conn.ChannelDescriptor reactorsByCh map[byte]Reactor peers *PeerSet dialing *cmn.CMap - nodeInfo types.NodeInfo // our node info - nodeKey *types.NodeKey // our node privkey + nodeInfo NodeInfo // our node info + nodeKey *NodeKey // our node privkey filterConnByAddr func(net.Addr) error filterConnByPubKey func(crypto.PubKey) error @@ -69,7 +68,7 @@ func NewSwitch(config *cfg.P2PConfig) *Switch { config: config, peerConfig: DefaultPeerConfig(), reactors: make(map[string]Reactor), - chDescs: make([]*tmconn.ChannelDescriptor, 0), + chDescs: make([]*conn.ChannelDescriptor, 0), reactorsByCh: make(map[byte]Reactor), peers: NewPeerSet(), dialing: cmn.NewCMap(), @@ -143,19 +142,19 @@ func (sw *Switch) IsListening() bool { // SetNodeInfo sets the switch's NodeInfo for checking compatibility and handshaking with other nodes. // NOTE: Not goroutine safe. -func (sw *Switch) SetNodeInfo(nodeInfo types.NodeInfo) { +func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo) { sw.nodeInfo = nodeInfo } // NodeInfo returns the switch's NodeInfo. // NOTE: Not goroutine safe. -func (sw *Switch) NodeInfo() types.NodeInfo { +func (sw *Switch) NodeInfo() NodeInfo { return sw.nodeInfo } // SetNodeKey sets the switch's private key for authenticated encryption. // NOTE: Not goroutine safe. -func (sw *Switch) SetNodeKey(nodeKey *types.NodeKey) { +func (sw *Switch) SetNodeKey(nodeKey *NodeKey) { sw.nodeKey = nodeKey } @@ -314,13 +313,13 @@ func (sw *Switch) reconnectToPeer(peer Peer) { // Dialing // IsDialing returns true if the switch is currently dialing the given ID. -func (sw *Switch) IsDialing(id types.ID) bool { +func (sw *Switch) IsDialing(id ID) bool { return sw.dialing.Has(string(id)) } // DialPeersAsync dials a list of peers asynchronously in random order (optionally, making them persistent). func (sw *Switch) DialPeersAsync(addrBook AddrBook, peers []string, persistent bool) error { - netAddrs, errs := types.NewNetAddressStrings(peers) + netAddrs, errs := NewNetAddressStrings(peers) for _, err := range errs { sw.Logger.Error("Error in peer's address", "err", err) } @@ -357,7 +356,7 @@ func (sw *Switch) DialPeersAsync(addrBook AddrBook, peers []string, persistent b // DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects and authenticates successfully. // If `persistent == true`, the switch will always try to reconnect to this peer if the connection ever fails. -func (sw *Switch) DialPeerWithAddress(addr *types.NetAddress, persistent bool) (Peer, error) { +func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) (Peer, error) { sw.dialing.Set(string(addr.ID), addr) defer sw.dialing.Delete(string(addr.ID)) return sw.addOutboundPeerWithConfig(addr, sw.peerConfig, persistent) @@ -443,7 +442,7 @@ func (sw *Switch) addInboundPeerWithConfig(conn net.Conn, config *PeerConfig) er // dial the peer; make secret connection; authenticate against the dialed ID; // add the peer. -func (sw *Switch) addOutboundPeerWithConfig(addr *types.NetAddress, config *PeerConfig, persistent bool) (Peer, error) { +func (sw *Switch) addOutboundPeerWithConfig(addr *NetAddress, config *PeerConfig, persistent bool) (Peer, error) { sw.Logger.Info("Dialing peer", "address", addr) peer, err := newOutboundPeer(addr, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError, sw.nodeKey.PrivKey, config, persistent) if err != nil { @@ -457,7 +456,7 @@ func (sw *Switch) addOutboundPeerWithConfig(addr *types.NetAddress, config *Peer peer.Logger.Info("Dialed peer with unknown ID - unable to authenticate", "addr", addr) } else if addr.ID != peer.ID() { peer.CloseConn() - return nil, types.ErrSwitchAuthenticationFailure{addr, peer.ID()} + return nil, ErrSwitchAuthenticationFailure{addr, peer.ID()} } err = sw.addPeer(peer) @@ -478,12 +477,12 @@ func (sw *Switch) addOutboundPeerWithConfig(addr *types.NetAddress, config *Peer func (sw *Switch) addPeer(peer *peer) error { // Avoid self if sw.nodeKey.ID() == peer.ID() { - return types.ErrSwitchConnectToSelf + return ErrSwitchConnectToSelf } // Avoid duplicate if sw.peers.Has(peer.ID()) { - return types.ErrSwitchDuplicatePeer + return ErrSwitchDuplicatePeer } diff --git a/p2p/switch_test.go b/p2p/switch_test.go index ae7e89e77..75f9640b1 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -16,8 +16,7 @@ import ( "github.com/tendermint/tmlibs/log" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/p2p/tmconn" - "github.com/tendermint/tendermint/p2p/types" + "github.com/tendermint/tendermint/p2p/conn" ) var ( @@ -30,7 +29,7 @@ func init() { } type PeerMessage struct { - PeerID types.ID + PeerID ID Bytes []byte Counter int } @@ -39,7 +38,7 @@ type TestReactor struct { BaseReactor mtx sync.Mutex - channels []*tmconn.ChannelDescriptor + channels []*conn.ChannelDescriptor peersAdded []Peer peersRemoved []Peer logMessages bool @@ -47,7 +46,7 @@ type TestReactor struct { msgsReceived map[byte][]PeerMessage } -func NewTestReactor(channels []*tmconn.ChannelDescriptor, logMessages bool) *TestReactor { +func NewTestReactor(channels []*conn.ChannelDescriptor, logMessages bool) *TestReactor { tr := &TestReactor{ channels: channels, logMessages: logMessages, @@ -58,7 +57,7 @@ func NewTestReactor(channels []*tmconn.ChannelDescriptor, logMessages bool) *Tes return tr } -func (tr *TestReactor) GetChannels() []*tmconn.ChannelDescriptor { +func (tr *TestReactor) GetChannels() []*conn.ChannelDescriptor { return tr.channels } @@ -102,11 +101,11 @@ func MakeSwitchPair(t testing.TB, initSwitch func(int, *Switch) *Switch) (*Switc func initSwitchFunc(i int, sw *Switch) *Switch { // Make two reactors of two channels each - sw.AddReactor("foo", NewTestReactor([]*tmconn.ChannelDescriptor{ + sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{ {ID: byte(0x00), Priority: 10}, {ID: byte(0x01), Priority: 10}, }, true)) - sw.AddReactor("bar", NewTestReactor([]*tmconn.ChannelDescriptor{ + sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{ {ID: byte(0x02), Priority: 10}, {ID: byte(0x03), Priority: 10}, }, true)) @@ -163,7 +162,7 @@ func TestConnAddrFilter(t *testing.T) { defer s1.Stop() defer s2.Stop() - c1, c2 := tmconn.NetPipe() + c1, c2 := conn.NetPipe() s1.SetAddrFilter(func(addr net.Addr) error { if addr.String() == c1.RemoteAddr().String() { @@ -199,7 +198,7 @@ func TestConnPubKeyFilter(t *testing.T) { defer s1.Stop() defer s2.Stop() - c1, c2 := tmconn.NetPipe() + c1, c2 := conn.NetPipe() // set pubkey filter s1.SetPubKeyFilter(func(pubkey crypto.PubKey) error { @@ -306,11 +305,11 @@ func BenchmarkSwitches(b *testing.B) { s1, s2 := MakeSwitchPair(b, func(i int, sw *Switch) *Switch { // Make bar reactors of bar channels each - sw.AddReactor("foo", NewTestReactor([]*tmconn.ChannelDescriptor{ + sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{ {ID: byte(0x00), Priority: 10}, {ID: byte(0x01), Priority: 10}, }, false)) - sw.AddReactor("bar", NewTestReactor([]*tmconn.ChannelDescriptor{ + sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{ {ID: byte(0x02), Priority: 10}, {ID: byte(0x03), Priority: 10}, }, false)) diff --git a/p2p/test_util.go b/p2p/test_util.go index aad6fb232..c4c0fe0b7 100644 --- a/p2p/test_util.go +++ b/p2p/test_util.go @@ -9,8 +9,7 @@ import ( "github.com/tendermint/tmlibs/log" cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/p2p/tmconn" - "github.com/tendermint/tendermint/p2p/types" + "github.com/tendermint/tendermint/p2p/conn" ) func AddPeerToSwitch(sw *Switch, peer Peer) { @@ -20,22 +19,22 @@ func AddPeerToSwitch(sw *Switch, peer Peer) { func CreateRandomPeer(outbound bool) *peer { addr, netAddr := CreateRoutableAddr() p := &peer{ - nodeInfo: types.NodeInfo{ + nodeInfo: NodeInfo{ ListenAddr: netAddr.DialString(), PubKey: crypto.GenPrivKeyEd25519().Wrap().PubKey(), }, outbound: outbound, - mconn: &tmconn.MConnection{}, + mconn: &conn.MConnection{}, } p.SetLogger(log.TestingLogger().With("peer", addr)) return p } -func CreateRoutableAddr() (addr string, netAddr *types.NetAddress) { +func CreateRoutableAddr() (addr string, netAddr *NetAddress) { for { var err error addr = cmn.Fmt("%X@%v.%v.%v.%v:46656", cmn.RandBytes(20), rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256) - netAddr, err = types.NewNetAddressString(addr) + netAddr, err = NewNetAddressString(addr) if err != nil { panic(err) } @@ -78,7 +77,7 @@ func MakeConnectedSwitches(cfg *cfg.P2PConfig, n int, initSwitch func(int, *Swit func Connect2Switches(switches []*Switch, i, j int) { switchI := switches[i] switchJ := switches[j] - c1, c2 := tmconn.NetPipe() + c1, c2 := conn.NetPipe() doneCh := make(chan struct{}) go func() { err := switchI.addPeerWithConnection(c1) @@ -130,13 +129,13 @@ func StartSwitches(switches []*Switch) error { func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch func(int, *Switch) *Switch) *Switch { // new switch, add reactors // TODO: let the config be passed in? - nodeKey := &types.NodeKey{ + nodeKey := &NodeKey{ PrivKey: crypto.GenPrivKeyEd25519().Wrap(), } s := NewSwitch(cfg) s.SetLogger(log.TestingLogger()) s = initSwitch(i, s) - s.SetNodeInfo(types.NodeInfo{ + s.SetNodeInfo(NodeInfo{ PubKey: nodeKey.PubKey(), Moniker: cmn.Fmt("switch%d", i), Network: network, diff --git a/p2p/types.go b/p2p/types.go index db7469ec0..b11765bb5 100644 --- a/p2p/types.go +++ b/p2p/types.go @@ -1,12 +1,8 @@ package p2p import ( - "github.com/tendermint/tendermint/p2p/tmconn" - "github.com/tendermint/tendermint/p2p/types" + "github.com/tendermint/tendermint/p2p/conn" ) -type ID = types.ID -type NodeInfo = types.NodeInfo - -type ChannelDescriptor = tmconn.ChannelDescriptor -type ConnectionStatus = tmconn.ConnectionStatus +type ChannelDescriptor = conn.ChannelDescriptor +type ConnectionStatus = conn.ConnectionStatus