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

bump go-libp2p version to v0.17.0 to add hole punching support #19

Closed
wants to merge 8 commits 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
2 changes: 2 additions & 0 deletions cmd/pcp/pcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/dennis-tra/pcp/internal/log"
"github.com/dennis-tra/pcp/pkg/receive"
"github.com/dennis-tra/pcp/pkg/send"
logging "github.com/ipfs/go-log/v2"
)

var (
Expand Down Expand Up @@ -45,6 +46,7 @@ func main() {
Before: func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
logging.SetLogLevel("p2p-holepunch", "debug")
}
return nil
},
Expand Down
32 changes: 14 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,26 @@ go 1.15

require (
github.com/adrg/xdg v0.3.0
github.com/golang/mock v1.5.0
github.com/golang/protobuf v1.4.3
github.com/google/go-cmp v0.5.4 // indirect
github.com/google/uuid v1.2.0
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.3.0
github.com/ipfs/go-cid v0.0.7
github.com/libp2p/go-libp2p v0.13.0
github.com/libp2p/go-libp2p-core v0.8.5
github.com/libp2p/go-libp2p-kad-dht v0.11.1
github.com/multiformats/go-multiaddr v0.3.1
github.com/multiformats/go-multihash v0.0.14
github.com/ipfs/go-log/v2 v2.4.0
github.com/libp2p/go-libp2p v0.17.0
github.com/libp2p/go-libp2p-core v0.13.0
github.com/libp2p/go-libp2p-kad-dht v0.15.0
github.com/multiformats/go-multiaddr v0.4.1
github.com/multiformats/go-multihash v0.0.15
github.com/multiformats/go-varint v0.0.6
github.com/pkg/errors v0.9.1
github.com/schollz/pake/v2 v2.0.6
github.com/schollz/progressbar/v3 v3.7.6
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
github.com/tyler-smith/go-bip39 v1.1.0
github.com/urfave/cli/v2 v2.3.0
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/mod v0.4.0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e
golang.org/x/term v0.0.0-20210317153231-de623e64d2a6 // indirect
golang.org/x/tools v0.0.0-20210101214203-2dba1e4ea05c // indirect
google.golang.org/protobuf v1.25.0
google.golang.org/protobuf v1.27.1
)

replace github.com/libp2p/go-libp2p v0.17.0 => github.com/optman/go-libp2p v0.17.1-0.20220224074507-81998574ff83
1,005 changes: 901 additions & 104 deletions go.sum

Large diffs are not rendered by default.

17 changes: 7 additions & 10 deletions internal/mock/discovery.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions internal/wrap/discovery.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package wrap

import (
"context"
"time"

"github.com/libp2p/go-libp2p-core/host"
stddiscovery "github.com/libp2p/go-libp2p/p2p/discovery"
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
)

type Discoverer interface {
NewMdnsService(ctx context.Context, peerhost host.Host, interval time.Duration, serviceTag string) (stddiscovery.Service, error)
NewMdnsService(peerhost host.Host, serviceTag string, notifee mdns.Notifee) mdns.Service
}

type Discovery struct{}

func (d Discovery) NewMdnsService(ctx context.Context, peerhost host.Host, interval time.Duration, serviceTag string) (stddiscovery.Service, error) {
return stddiscovery.NewMdnsService(ctx, peerhost, interval, serviceTag)
func (d Discovery) NewMdnsService(peerhost host.Host, serviceTag string, notifee mdns.Notifee) mdns.Service {
return mdns.NewMdnsService(peerhost, serviceTag, notifee)
}
56 changes: 30 additions & 26 deletions pkg/mdns/advertiser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package mdns
import (
"context"

"github.com/dennis-tra/pcp/internal/log"

"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/dennis-tra/pcp/internal/log"
)

type Advertiser struct {
Expand All @@ -25,35 +26,38 @@ func (a *Advertiser) Advertise(chanID int) error {
}
defer a.ServiceStopped()

for {
did := a.DiscoveryID(chanID)
log.Debugln("mDNS - Advertising ", did)
ctx, cancel := context.WithTimeout(a.ServiceContext(), Timeout)
mdns, err := wrapdiscovery.NewMdnsService(ctx, a, a.interval, did)
if err != nil {
cancel()
return err
}
ctx := a.ServiceContext()

did := a.DiscoveryID(chanID)
log.Debugln("mDNS - Advertising ", did)
mdns := wrapdiscovery.NewMdnsService(a, did, a)
if err := mdns.Start(); err != nil {
return err
}

select {
case <-a.SigShutdown():
log.Debugln("mDNS - Advertising", did, " done - shutdown signal")
cancel()
return mdns.Close()
case <-ctx.Done():
log.Debugln("mDNS - Advertising", did, "done -", ctx.Err())
cancel()
if ctx.Err() == context.DeadlineExceeded {
_ = mdns.Close()
continue
} else if ctx.Err() == context.Canceled {
_ = mdns.Close()
return nil
}
select {
case <-a.SigShutdown():
log.Debugln("mDNS - Advertising", did, " done - shutdown signal")
if err := mdns.Close(); err != nil {
log.Warningln("Error closing mdns service", err)
}
return nil
case <-ctx.Done():
log.Debugln("mDNS - Advertising", did, "done -", ctx.Err())
if err := mdns.Close(); err != nil {
log.Warningln("Error closing mdns service", err)
}
if ctx.Err() == context.Canceled {
return nil
}
return ctx.Err()
}
}

func (a *Advertiser) Shutdown() {
a.Service.Shutdown()
}

func (a *Advertiser) HandlePeerFound(info peer.AddrInfo) {
// no-op
}
59 changes: 11 additions & 48 deletions pkg/mdns/advertiser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import (
"context"
"sync"
"testing"
"time"

"github.com/libp2p/go-libp2p/p2p/discovery"

"github.com/dennis-tra/pcp/internal/wrap"
"github.com/golang/mock/gomock"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/dennis-tra/pcp/internal/mock"
"github.com/dennis-tra/pcp/internal/wrap"
)

func setup(t *testing.T) (*gomock.Controller, host.Host, func(t *testing.T)) {
Expand All @@ -28,7 +25,6 @@ func setup(t *testing.T) (*gomock.Controller, host.Host, func(t *testing.T)) {
net := mocknet.New(context.Background())

tmpTruncateDuration := TruncateDuration
tmpInterval := Interval
tmpTimeout := Timeout

local, err := net.GenPeer()
Expand All @@ -38,7 +34,6 @@ func setup(t *testing.T) (*gomock.Controller, host.Host, func(t *testing.T)) {
ctrl.Finish()

TruncateDuration = tmpTruncateDuration
Interval = tmpInterval
Timeout = tmpTimeout

wrapdiscovery = wrap.Discovery{}
Expand All @@ -59,10 +54,10 @@ func TestAdvertiser_Advertise(t *testing.T) {
wg.Add(1)

d.EXPECT().
NewMdnsService(gomock.Any(), a, Interval, gomock.Any()).
DoAndReturn(func(ctx context.Context, peerhost host.Host, interval time.Duration, serviceTag string) (discovery.Service, error) {
wg.Done()
return DummyMDNSService{}, nil
NewMdnsService(a, gomock.Any(), gomock.Any()).
DoAndReturn(func(peerhost host.Host, serviceTag string, notifee mdns.Notifee) mdns.Service {
defer wg.Done()
return DummyMDNSService{}
}).
Times(1)

Expand All @@ -78,44 +73,12 @@ func TestAdvertiser_Advertise(t *testing.T) {
wg.Wait()
}

func TestAdvertiser_Advertise_multipleTimes(t *testing.T) {
ctrl, local, teardown := setup(t)
defer teardown(t)

Timeout = 20 * time.Millisecond

d := mock.NewMockDiscoverer(ctrl)
wrapdiscovery = d

a := NewAdvertiser(local)

var wg sync.WaitGroup
wg.Add(5)

d.EXPECT().
NewMdnsService(gomock.Any(), a, Interval, gomock.Any()).
DoAndReturn(func(ctx context.Context, peerhost host.Host, interval time.Duration, serviceTag string) (discovery.Service, error) {
wg.Done()
return DummyMDNSService{}, nil
}).
Times(5)

go func() {
err := a.Advertise(333)
assert.NoError(t, err)
wg.Done()
}()
wg.Wait()
wg.Add(1)

a.Shutdown()
wg.Wait()
}

type DummyMDNSService struct{}

func (mdns DummyMDNSService) Close() error {
return nil
}
func (mdns DummyMDNSService) RegisterNotifee(notifee discovery.Notifee) {}
func (mdns DummyMDNSService) UnregisterNotifee(notifee discovery.Notifee) {}

func (mdns DummyMDNSService) Start() error {
return nil
}
Loading