Skip to content

Commit

Permalink
complete the migration to consolidated types. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk authored May 26, 2019
1 parent b1dd953 commit 9bcb09e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
6 changes: 3 additions & 3 deletions p2p/net/upgrader/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package stream
import (
"fmt"

mux "github.com/libp2p/go-libp2p-core/mux"
network "github.com/libp2p/go-libp2p-core/network"
transport "github.com/libp2p/go-libp2p-core/transport"
"github.com/libp2p/go-libp2p-core/mux"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/transport"
)

type transportConn struct {
Expand Down
8 changes: 7 additions & 1 deletion p2p/net/upgrader/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"fmt"
"sync"

"github.com/libp2p/go-libp2p-core/transport"

logging "github.com/ipfs/go-log"
tec "github.com/jbenet/go-temp-err-catcher"
transport "github.com/libp2p/go-libp2p-core/transport"
manet "github.com/multiformats/go-multiaddr-net"
)

var log = logging.Logger("stream-upgrader")

type connErr struct {
conn transport.CapableConn
err error
}

type listener struct {
manet.Listener

Expand Down
32 changes: 16 additions & 16 deletions p2p/net/upgrader/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"sync"
"time"

core "github.com/libp2p/go-libp2p-core"
mux "github.com/libp2p/go-libp2p-core/mux"
insecure "github.com/libp2p/go-libp2p-core/sec/insecure"
"github.com/libp2p/go-libp2p-core/mux"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/sec/insecure"
tpt "github.com/libp2p/go-libp2p-core/transport"
mplex "github.com/libp2p/go-libp2p-mplex"
st "github.com/libp2p/go-libp2p-transport-upgrader"
Expand Down Expand Up @@ -64,7 +64,7 @@ func (m *errorMuxer) NewConn(c net.Conn, isServer bool) (mux.MuxedConn, error) {
var _ = Describe("Listener", func() {
var (
defaultUpgrader = &st.Upgrader{
Secure: insecure.New(core.PeerID(1)),
Secure: insecure.New(peer.ID(1)),
Muxer: &negotiatingMuxer{},
}
)
Expand All @@ -90,7 +90,7 @@ var _ = Describe("Listener", func() {
return upgrader.UpgradeListener(nil, ln)
}

dial := func(upgrader *st.Upgrader, raddr ma.Multiaddr, p core.PeerID) (tpt.CapableConn, error) {
dial := func(upgrader *st.Upgrader, raddr ma.Multiaddr, p peer.ID) (tpt.CapableConn, error) {
macon, err := manet.Dial(raddr)
if err != nil {
return nil, err
Expand All @@ -105,7 +105,7 @@ var _ = Describe("Listener", func() {
It("accepts a single connection", func() {
ln := createListener(defaultUpgrader)
defer ln.Close()
cconn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(1))
cconn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(1))
Expect(err).ToNot(HaveOccurred())
sconn, err := ln.Accept()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -117,7 +117,7 @@ var _ = Describe("Listener", func() {
defer ln.Close()
const num = 10
for i := 0; i < 10; i++ {
cconn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(1))
cconn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(1))
Expect(err).ToNot(HaveOccurred())
sconn, err := ln.Accept()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -130,7 +130,7 @@ var _ = Describe("Listener", func() {
tpt.AcceptTimeout = timeout
ln := createListener(defaultUpgrader)
defer ln.Close()
conn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(2))
conn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(2))
if !Expect(err).ToNot(HaveOccurred()) {
return
}
Expand All @@ -150,7 +150,7 @@ var _ = Describe("Listener", func() {

It("doesn't accept connections that fail to setup", func() {
upgrader := &st.Upgrader{
Secure: insecure.New(core.PeerID(1)),
Secure: insecure.New(peer.ID(1)),
Muxer: &errorMuxer{},
}
ln := createListener(upgrader)
Expand All @@ -163,7 +163,7 @@ var _ = Describe("Listener", func() {
}
close(done)
}()
conn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(2))
conn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(2))
if !Expect(err).To(HaveOccurred()) {
conn.Close()
}
Expand All @@ -178,7 +178,7 @@ var _ = Describe("Listener", func() {
num := 3 * st.AcceptQueueLength
bm := newBlockingMuxer()
upgrader := &st.Upgrader{
Secure: insecure.New(core.PeerID(1)),
Secure: insecure.New(peer.ID(1)),
Muxer: bm,
}
ln := createListener(upgrader)
Expand All @@ -200,7 +200,7 @@ var _ = Describe("Listener", func() {
wg.Add(1)
go func() {
defer GinkgoRecover()
conn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(2))
conn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(2))
if Expect(err).ToNot(HaveOccurred()) {
stream, err := conn.AcceptStream() // wait for conn to be accepted.
if !Expect(err).To(HaveOccurred()) {
Expand All @@ -227,7 +227,7 @@ var _ = Describe("Listener", func() {
for i := 0; i < st.AcceptQueueLength; i++ {
go func() {
defer GinkgoRecover()
conn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(2))
conn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(2))
Expect(err).ToNot(HaveOccurred())
dialed <- conn
}()
Expand All @@ -236,7 +236,7 @@ var _ = Describe("Listener", func() {
// dial a new connection. This connection should not complete setup, since the queue is full
go func() {
defer GinkgoRecover()
conn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(2))
conn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(2))
Expect(err).ToNot(HaveOccurred())
dialed <- conn
}()
Expand Down Expand Up @@ -279,15 +279,15 @@ var _ = Describe("Listener", func() {
It("doesn't accept new connections when it is closed", func() {
ln := createListener(defaultUpgrader)
Expect(ln.Close()).To(Succeed())
conn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(1))
conn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(1))
if !Expect(err).To(HaveOccurred()) {
conn.Close()
}
})

It("closes incoming connections that have not yet been accepted", func() {
ln := createListener(defaultUpgrader)
conn, err := dial(defaultUpgrader, ln.Multiaddr(), core.PeerID(2))
conn, err := dial(defaultUpgrader, ln.Multiaddr(), peer.ID(2))
if !Expect(err).ToNot(HaveOccurred()) {
ln.Close()
return
Expand Down
20 changes: 10 additions & 10 deletions p2p/net/upgrader/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"net"

core "github.com/libp2p/go-libp2p-core"
mux "github.com/libp2p/go-libp2p-core/mux"
pnet "github.com/libp2p/go-libp2p-core/pnet"
sec "github.com/libp2p/go-libp2p-core/sec"
transport "github.com/libp2p/go-libp2p-core/transport"
"github.com/libp2p/go-libp2p-core/mux"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/pnet"
"github.com/libp2p/go-libp2p-core/sec"
"github.com/libp2p/go-libp2p-core/transport"

filter "github.com/libp2p/go-maddr-filter"
manet "github.com/multiformats/go-multiaddr-net"
)
Expand Down Expand Up @@ -49,7 +50,7 @@ func (u *Upgrader) UpgradeListener(t transport.Transport, list manet.Listener) t

// UpgradeOutbound upgrades the given outbound multiaddr-net connection into a
// full libp2p-transport connection.
func (u *Upgrader) UpgradeOutbound(ctx context.Context, t transport.Transport, maconn manet.Conn, p core.PeerID) (transport.CapableConn, error) {
func (u *Upgrader) UpgradeOutbound(ctx context.Context, t transport.Transport, maconn manet.Conn, p peer.ID) (transport.CapableConn, error) {
if p == "" {
return nil, ErrNilPeer
}
Expand All @@ -62,7 +63,7 @@ func (u *Upgrader) UpgradeInbound(ctx context.Context, t transport.Transport, ma
return u.upgrade(ctx, t, maconn, "")
}

func (u *Upgrader) upgrade(ctx context.Context, t transport.Transport, maconn manet.Conn, p core.PeerID) (transport.CapableConn, error) {
func (u *Upgrader) upgrade(ctx context.Context, t transport.Transport, maconn manet.Conn, p peer.ID) (transport.CapableConn, error) {
if u.Filters != nil && u.Filters.AddrBlocked(maconn.RemoteMultiaddr()) {
log.Debugf("blocked connection from %s", maconn.RemoteMultiaddr())
maconn.Close()
Expand All @@ -78,7 +79,6 @@ func (u *Upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
}
conn = pconn
} else if pnet.ForcePrivateNetwork {
conn.Close()
log.Error("tried to dial with no Private Network Protector but usage" +
" of Private Networks is forced by the enviroment")
return nil, pnet.ErrNotInPrivateNetwork
Expand All @@ -101,14 +101,14 @@ func (u *Upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
}, nil
}

func (u *Upgrader) setupSecurity(ctx context.Context, conn net.Conn, p core.PeerID) (sec.SecureConn, error) {
func (u *Upgrader) setupSecurity(ctx context.Context, conn net.Conn, p peer.ID) (sec.SecureConn, error) {
if p == "" {
return u.Secure.SecureInbound(ctx, conn)
}
return u.Secure.SecureOutbound(ctx, conn, p)
}

func (u *Upgrader) setupMuxer(ctx context.Context, conn net.Conn, p core.PeerID) (mux.MuxedConn, error) {
func (u *Upgrader) setupMuxer(ctx context.Context, conn net.Conn, p peer.ID) (mux.MuxedConn, error) {
// TODO: The muxer should take a context.
done := make(chan struct{})

Expand Down

0 comments on commit 9bcb09e

Please sign in to comment.