Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: relay integration #190

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

logging "github.com/ipfs/go-log"
goprocess "github.com/jbenet/goprocess"
circuit "github.com/libp2p/go-libp2p-circuit"
metrics "github.com/libp2p/go-libp2p-metrics"
mstream "github.com/libp2p/go-libp2p-metrics/stream"
inet "github.com/libp2p/go-libp2p-net"
Expand Down Expand Up @@ -51,10 +52,12 @@ type BasicHost struct {
proc goprocess.Process

bwc metrics.Reporter

relay *circuit.Relay
}

// New constructs and sets up a new *BasicHost with given Network
func New(net inet.Network, opts ...interface{}) *BasicHost {
func New(net inet.Network, opts ...interface{}) (*BasicHost, error) {
h := &BasicHost{
network: net,
mux: msmux.NewMultistreamMuxer(),
Expand All @@ -81,6 +84,14 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
}
case metrics.Reporter:
h.bwc = o
case *circuit.Relay:
net.AddDialer(o.Dialer())

list := o.Listener()
err := net.AddListener(list)
if err != nil {
return nil, err
}
}
}

Expand All @@ -89,7 +100,7 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
net.SetConnHandler(h.newConnHandler)
net.SetStreamHandler(h.newStreamHandler)

return h
return h, nil
}

// newConnHandler is the remote-opened conn handler for inet.Network
Expand Down
12 changes: 6 additions & 6 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
func TestHostSimple(t *testing.T) {

ctx := context.Background()
h1 := New(testutil.GenSwarmNetwork(t, ctx))
h2 := New(testutil.GenSwarmNetwork(t, ctx))
h1, _ := New(testutil.GenSwarmNetwork(t, ctx))
h2, _ := New(testutil.GenSwarmNetwork(t, ctx))
defer h1.Close()
defer h2.Close()

Expand Down Expand Up @@ -64,8 +64,8 @@ func TestHostSimple(t *testing.T) {
}

func getHostPair(ctx context.Context, t *testing.T) (host.Host, host.Host) {
h1 := New(testutil.GenSwarmNetwork(t, ctx))
h2 := New(testutil.GenSwarmNetwork(t, ctx))
h1, _ := New(testutil.GenSwarmNetwork(t, ctx))
h2, _ := New(testutil.GenSwarmNetwork(t, ctx))

h2pi := h2.Peerstore().PeerInfo(h2.ID())
if err := h1.Connect(ctx, h2pi); err != nil {
Expand Down Expand Up @@ -170,8 +170,8 @@ func TestHostProtoPreknowledge(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

h1 := New(testutil.GenSwarmNetwork(t, ctx))
h2 := New(testutil.GenSwarmNetwork(t, ctx))
h1, _ := New(testutil.GenSwarmNetwork(t, ctx))
h2, _ := New(testutil.GenSwarmNetwork(t, ctx))

conn := make(chan protocol.ID)
handler := func(s inet.Stream) {
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/mock/mock_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps pstore.Peerstore) (host.Ho
return nil, err
}

h := bhost.New(n)
h, _ := bhost.New(n)
h.NegotiateTimeout = 0

mn.proc.AddChild(n.proc)
Expand Down
10 changes: 10 additions & 0 deletions p2p/net/mock/mock_peernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (

"github.com/jbenet/goprocess"
goprocessctx "github.com/jbenet/goprocess/context"
iconn "github.com/libp2p/go-libp2p-interface-conn"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
tpt "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr"
)

Expand Down Expand Up @@ -220,6 +222,14 @@ func (pn *peernet) LocalPeer() peer.ID {
return pn.peer
}

func (pn *peernet) AddDialer(d iconn.SingleDialer) {
panic("mocknet doesnt support adding dialers")
}

func (pn *peernet) AddListener(l tpt.Listener) error {
return fmt.Errorf("mocknet doesnt support adding listeners")
}

// Peers returns the connected peers
func (pn *peernet) Peers() []peer.ID {
pn.RLock()
Expand Down