From 94896f4ed67afb080d26e350af3c8a5464812c96 Mon Sep 17 00:00:00 2001 From: Dennis Trautwein Date: Mon, 24 Oct 2022 10:02:57 +0100 Subject: [PATCH 1/7] feat: add holepunch address filter option --- p2p/protocol/holepunch/filter.go | 42 +++++++++++++++++++++++++++ p2p/protocol/holepunch/holepuncher.go | 8 +++-- p2p/protocol/holepunch/svc.go | 8 +++-- 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 p2p/protocol/holepunch/filter.go diff --git a/p2p/protocol/holepunch/filter.go b/p2p/protocol/holepunch/filter.go new file mode 100644 index 0000000000..c7c80f916c --- /dev/null +++ b/p2p/protocol/holepunch/filter.go @@ -0,0 +1,42 @@ +package holepunch + +import ( + "github.com/libp2p/go-libp2p/core/peer" + ma "github.com/multiformats/go-multiaddr" +) + +// WithAddrFilter is a Service option that enables multi address filtering. +// It allows to only allow a subset of observed addresses to the remote +// peer. E.g., only announce TCP or QUIC multi addresses instead of both. +// It also allows to only consider a subset of received multi addresses +// that remote peers announced to us. +// Theoretically, this API also allows to add multi addresses in both cases. +func WithAddrFilter(maf AddrFilter) Option { + return func(hps *Service) error { + hps.filter = maf + return nil + } +} + +// AddrFilter defines the interface for the multi address filtering. +// - FilterLocal is a function that filters the multi addresses that +// we send to the remote peer. +// - FilterRemote is a function that filters the multi addresses which +// we received from the remote peer. +type AddrFilter interface { + FilterLocal(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr + FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr +} + +// DefaultAddrFilter is the default address filtering logic. It strips +// all relayed multi addresses from both the locally observed addresses +// and received remote addresses. +type DefaultAddrFilter struct{} + +func (d DefaultAddrFilter) FilterLocal(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { + return removeRelayAddrs(maddrs) +} + +func (d DefaultAddrFilter) FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { + return removeRelayAddrs(maddrs) +} diff --git a/p2p/protocol/holepunch/holepuncher.go b/p2p/protocol/holepunch/holepuncher.go index 7f2bbbd121..3e7389ac60 100644 --- a/p2p/protocol/holepunch/holepuncher.go +++ b/p2p/protocol/holepunch/holepuncher.go @@ -49,14 +49,16 @@ type holePuncher struct { closed bool tracer *tracer + filter AddrFilter } -func newHolePuncher(h host.Host, ids identify.IDService, tracer *tracer) *holePuncher { +func newHolePuncher(h host.Host, ids identify.IDService, tracer *tracer, filter AddrFilter) *holePuncher { hp := &holePuncher{ host: h, ids: ids, active: make(map[peer.ID]struct{}), tracer: tracer, + filter: filter, } hp.ctx, hp.ctxCancel = context.WithCancel(context.Background()) h.Network().Notify((*netNotifiee)(hp)) @@ -207,7 +209,7 @@ func (hp *holePuncher) initiateHolePunchImpl(str network.Stream) ([]ma.Multiaddr start := time.Now() if err := w.WriteMsg(&pb.HolePunch{ Type: pb.HolePunch_CONNECT.Enum(), - ObsAddrs: addrsToBytes(removeRelayAddrs(hp.ids.OwnObservedAddrs())), + ObsAddrs: addrsToBytes(hp.filter.FilterLocal(str.Conn().RemotePeer(), hp.ids.OwnObservedAddrs())), }); err != nil { str.Reset() return nil, 0, err @@ -222,7 +224,7 @@ func (hp *holePuncher) initiateHolePunchImpl(str network.Stream) ([]ma.Multiaddr if t := msg.GetType(); t != pb.HolePunch_CONNECT { return nil, 0, fmt.Errorf("expect CONNECT message, got %s", t) } - addrs := removeRelayAddrs(addrsFromBytes(msg.ObsAddrs)) + addrs := hp.filter.FilterRemote(str.Conn().RemotePeer(), addrsFromBytes(msg.ObsAddrs)) if len(addrs) == 0 { return nil, 0, errors.New("didn't receive any public addresses in CONNECT") } diff --git a/p2p/protocol/holepunch/svc.go b/p2p/protocol/holepunch/svc.go index 1df779fc37..9d023c4717 100644 --- a/p2p/protocol/holepunch/svc.go +++ b/p2p/protocol/holepunch/svc.go @@ -53,6 +53,7 @@ type Service struct { hasPublicAddrsChan chan struct{} tracer *tracer + filter AddrFilter refCount sync.WaitGroup } @@ -74,6 +75,7 @@ func NewService(h host.Host, ids identify.IDService, opts ...Option) (*Service, host: h, ids: ids, hasPublicAddrsChan: make(chan struct{}), + filter: DefaultAddrFilter{}, } for _, opt := range opts { @@ -140,7 +142,7 @@ func (s *Service) watchForPublicAddr() { continue } s.holePuncherMx.Lock() - s.holePuncher = newHolePuncher(s.host, s.ids, s.tracer) + s.holePuncher = newHolePuncher(s.host, s.ids, s.tracer, s.filter) s.holePuncherMx.Unlock() close(s.hasPublicAddrsChan) return @@ -168,7 +170,7 @@ func (s *Service) incomingHolePunch(str network.Stream) (rtt time.Duration, addr if !isRelayAddress(str.Conn().RemoteMultiaddr()) { return 0, nil, fmt.Errorf("received hole punch stream: %s", str.Conn().RemoteMultiaddr()) } - ownAddrs := removeRelayAddrs(s.ids.OwnObservedAddrs()) + ownAddrs := s.filter.FilterLocal(str.Conn().RemotePeer(), s.ids.OwnObservedAddrs()) // If we can't tell the peer where to dial us, there's no point in starting the hole punching. if len(ownAddrs) == 0 { return 0, nil, errors.New("rejecting hole punch request, as we don't have any public addresses") @@ -194,7 +196,7 @@ func (s *Service) incomingHolePunch(str network.Stream) (rtt time.Duration, addr if t := msg.GetType(); t != pb.HolePunch_CONNECT { return 0, nil, fmt.Errorf("expected CONNECT message from initiator but got %d", t) } - obsDial := removeRelayAddrs(addrsFromBytes(msg.ObsAddrs)) + obsDial := s.filter.FilterRemote(str.Conn().RemotePeer(), addrsFromBytes(msg.ObsAddrs)) log.Debugw("received hole punch request", "peer", str.Conn().RemotePeer(), "addrs", obsDial) if len(obsDial) == 0 { return 0, nil, errors.New("expected CONNECT message to contain at least one address") From d078a46702ceb6f487c9cf25b310dc9a4e3c69a5 Mon Sep 17 00:00:00 2001 From: Dennis Trautwein Date: Mon, 24 Oct 2022 17:55:54 +0100 Subject: [PATCH 2/7] fix: exit early if all addresses were filtered out --- p2p/protocol/holepunch/holepunch_test.go | 79 +++++++++++++++++++----- p2p/protocol/holepunch/holepuncher.go | 6 +- 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/p2p/protocol/holepunch/holepunch_test.go b/p2p/protocol/holepunch/holepunch_test.go index 5c364ff013..bcbf785882 100644 --- a/p2p/protocol/holepunch/holepunch_test.go +++ b/p2p/protocol/holepunch/holepunch_test.go @@ -45,6 +45,21 @@ func (m *mockEventTracer) getEvents() []*holepunch.Event { var _ holepunch.EventTracer = &mockEventTracer{} +type mockMaddrFilter struct { + filterLocal func(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr + filterRemote func(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr +} + +func (m mockMaddrFilter) FilterLocal(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { + return m.filterLocal(remoteID, maddrs) +} + +func (m mockMaddrFilter) FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { + return m.filterRemote(remoteID, maddrs) +} + +var _ holepunch.AddrFilter = &mockMaddrFilter{} + type mockIDService struct { identify.IDService } @@ -110,7 +125,7 @@ func TestDirectDialWorks(t *testing.T) { func TestEndToEndSimConnect(t *testing.T) { h1tr := &mockEventTracer{} h2tr := &mockEventTracer{} - h1, h2, relay, _ := makeRelayedHosts(t, holepunch.WithTracer(h1tr), holepunch.WithTracer(h2tr), true) + h1, h2, relay, _ := makeRelayedHosts(t, []holepunch.Option{holepunch.WithTracer(h1tr)}, []holepunch.Option{holepunch.WithTracer(h2tr)}, true) defer h1.Close() defer h2.Close() defer relay.Close() @@ -151,6 +166,7 @@ func TestFailuresOnInitiator(t *testing.T) { rhandler func(s network.Stream) errMsg string holePunchTimeout time.Duration + filter func(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr }{ "responder does NOT send a CONNECT message": { rhandler: func(s network.Stream) { @@ -175,6 +191,12 @@ func TestFailuresOnInitiator(t *testing.T) { }, errMsg: "i/o deadline reached", }, + "no addrs after filtering": { + errMsg: "aborting hole punch initiation, as we have no public address after filtering", + filter: func(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { + return []ma.Multiaddr{} + }, + }, } for name, tc := range tcs { @@ -190,7 +212,17 @@ func TestFailuresOnInitiator(t *testing.T) { defer h1.Close() defer h2.Close() defer relay.Close() - hps := addHolePunchService(t, h2, holepunch.WithTracer(tr)) + + opts := []holepunch.Option{holepunch.WithTracer(tr)} + if tc.filter != nil { + f := mockMaddrFilter{ + filterLocal: tc.filter, + filterRemote: tc.filter, + } + opts = append(opts, holepunch.WithAddrFilter(f)) + } + + hps := addHolePunchService(t, h2, opts...) if tc.rhandler != nil { h1.SetStreamHandler(holepunch.Protocol, tc.rhandler) @@ -221,6 +253,7 @@ func TestFailuresOnResponder(t *testing.T) { initiator func(s network.Stream) errMsg string holePunchTimeout time.Duration + filter func(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr }{ "initiator does NOT send a CONNECT message": { initiator: func(s network.Stream) { @@ -258,6 +291,19 @@ func TestFailuresOnResponder(t *testing.T) { }, errMsg: "expected CONNECT message to contain at least one address", }, + "no addrs after filtering": { + errMsg: "rejecting hole punch request, as we don't have any public addresses", + initiator: func(s network.Stream) { + protoio.NewDelimitedWriter(s).WriteMsg(&holepunch_pb.HolePunch{ + Type: holepunch_pb.HolePunch_CONNECT.Enum(), + ObsAddrs: addrsToBytes([]ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/1234")}), + }) + time.Sleep(10 * time.Second) + }, + filter: func(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { + return []ma.Multiaddr{} + }, + }, } for name, tc := range tcs { @@ -267,9 +313,18 @@ func TestFailuresOnResponder(t *testing.T) { holepunch.StreamTimeout = tc.holePunchTimeout defer func() { holepunch.StreamTimeout = cpy }() } - tr := &mockEventTracer{} - h1, h2, relay, _ := makeRelayedHosts(t, holepunch.WithTracer(tr), nil, false) + + opts := []holepunch.Option{holepunch.WithTracer(tr)} + if tc.filter != nil { + f := mockMaddrFilter{ + filterLocal: tc.filter, + filterRemote: tc.filter, + } + opts = append(opts, holepunch.WithAddrFilter(f)) + } + + h1, h2, relay, _ := makeRelayedHosts(t, opts, nil, false) defer h1.Close() defer h2.Close() defer relay.Close() @@ -377,13 +432,9 @@ func mkHostWithStaticAutoRelay(t *testing.T, relay host.Host) host.Host { return h } -func makeRelayedHosts(t *testing.T, h1opt, h2opt holepunch.Option, addHolePuncher bool) (h1, h2, relay host.Host, hps *holepunch.Service) { +func makeRelayedHosts(t *testing.T, h1opt, h2opt []holepunch.Option, addHolePuncher bool) (h1, h2, relay host.Host, hps *holepunch.Service) { t.Helper() - var h1opts []holepunch.Option - if h1opt != nil { - h1opts = append(h1opts, h1opt) - } - h1, _ = mkHostWithHolePunchSvc(t, h1opts...) + h1, _ = mkHostWithHolePunchSvc(t, h1opt...) var err error relay, err = libp2p.New(libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0")), libp2p.DisableRelay()) require.NoError(t, err) @@ -393,7 +444,7 @@ func makeRelayedHosts(t *testing.T, h1opt, h2opt holepunch.Option, addHolePunche h2 = mkHostWithStaticAutoRelay(t, relay) if addHolePuncher { - hps = addHolePunchService(t, h2, h2opt) + hps = addHolePunchService(t, h2, h2opt...) } // h1 has a relay addr @@ -413,12 +464,8 @@ func makeRelayedHosts(t *testing.T, h1opt, h2opt holepunch.Option, addHolePunche return } -func addHolePunchService(t *testing.T, h host.Host, opt holepunch.Option) *holepunch.Service { +func addHolePunchService(t *testing.T, h host.Host, opts ...holepunch.Option) *holepunch.Service { t.Helper() - var opts []holepunch.Option - if opt != nil { - opts = append(opts, opt) - } hps, err := holepunch.NewService(h, newMockIDService(t, h), opts...) require.NoError(t, err) return hps diff --git a/p2p/protocol/holepunch/holepuncher.go b/p2p/protocol/holepunch/holepuncher.go index 3e7389ac60..d94bdcc345 100644 --- a/p2p/protocol/holepunch/holepuncher.go +++ b/p2p/protocol/holepunch/holepuncher.go @@ -206,10 +206,14 @@ func (hp *holePuncher) initiateHolePunchImpl(str network.Stream) ([]ma.Multiaddr str.SetDeadline(time.Now().Add(StreamTimeout)) // send a CONNECT and start RTT measurement. + obsAddrs := hp.filter.FilterLocal(str.Conn().RemotePeer(), hp.ids.OwnObservedAddrs()) + if len(obsAddrs) == 0 { + return nil, 0, errors.New("aborting hole punch initiation, as we have no public address after filtering") + } start := time.Now() if err := w.WriteMsg(&pb.HolePunch{ Type: pb.HolePunch_CONNECT.Enum(), - ObsAddrs: addrsToBytes(hp.filter.FilterLocal(str.Conn().RemotePeer(), hp.ids.OwnObservedAddrs())), + ObsAddrs: addrsToBytes(obsAddrs), }); err != nil { str.Reset() return nil, 0, err From b9e0bece54fff2f0ba34bccc01bdbedbe13145f5 Mon Sep 17 00:00:00 2001 From: Dennis Trautwein Date: Sat, 29 Oct 2022 12:57:47 +0200 Subject: [PATCH 3/7] incorporate PR feedback Co-authored-by: Marten Seemann --- p2p/protocol/holepunch/filter.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/p2p/protocol/holepunch/filter.go b/p2p/protocol/holepunch/filter.go index c7c80f916c..98932f045c 100644 --- a/p2p/protocol/holepunch/filter.go +++ b/p2p/protocol/holepunch/filter.go @@ -5,8 +5,8 @@ import ( ma "github.com/multiformats/go-multiaddr" ) -// WithAddrFilter is a Service option that enables multi address filtering. -// It allows to only allow a subset of observed addresses to the remote +// WithAddrFilter is a Service option that enables multiaddress filtering. +// It allows to only send a subset of observed addresses to the remote // peer. E.g., only announce TCP or QUIC multi addresses instead of both. // It also allows to only consider a subset of received multi addresses // that remote peers announced to us. @@ -19,12 +19,10 @@ func WithAddrFilter(maf AddrFilter) Option { } // AddrFilter defines the interface for the multi address filtering. -// - FilterLocal is a function that filters the multi addresses that -// we send to the remote peer. -// - FilterRemote is a function that filters the multi addresses which -// we received from the remote peer. type AddrFilter interface { + // FilterLocal is a function that filters the multi addresses that we send to the remote peer. FilterLocal(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr + // FilterRemote is a function that filters the multi addresses which we received from the remote peer. FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr } From ce34fcb55fc6b1d025444b32a13e7ea24a27bed0 Mon Sep 17 00:00:00 2001 From: Dennis Trautwein Date: Fri, 4 Nov 2022 22:31:38 +0100 Subject: [PATCH 4/7] remove: holepunch default filter --- p2p/protocol/holepunch/filter.go | 17 ++--------------- p2p/protocol/holepunch/holepuncher.go | 15 ++++++++++++--- p2p/protocol/holepunch/svc.go | 14 +++++++++++--- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/p2p/protocol/holepunch/filter.go b/p2p/protocol/holepunch/filter.go index 98932f045c..46bcf8f6a1 100644 --- a/p2p/protocol/holepunch/filter.go +++ b/p2p/protocol/holepunch/filter.go @@ -11,9 +11,9 @@ import ( // It also allows to only consider a subset of received multi addresses // that remote peers announced to us. // Theoretically, this API also allows to add multi addresses in both cases. -func WithAddrFilter(maf AddrFilter) Option { +func WithAddrFilter(f AddrFilter) Option { return func(hps *Service) error { - hps.filter = maf + hps.filter = f return nil } } @@ -25,16 +25,3 @@ type AddrFilter interface { // FilterRemote is a function that filters the multi addresses which we received from the remote peer. FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr } - -// DefaultAddrFilter is the default address filtering logic. It strips -// all relayed multi addresses from both the locally observed addresses -// and received remote addresses. -type DefaultAddrFilter struct{} - -func (d DefaultAddrFilter) FilterLocal(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { - return removeRelayAddrs(maddrs) -} - -func (d DefaultAddrFilter) FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { - return removeRelayAddrs(maddrs) -} diff --git a/p2p/protocol/holepunch/holepuncher.go b/p2p/protocol/holepunch/holepuncher.go index d94bdcc345..5cf8f8fd22 100644 --- a/p2p/protocol/holepunch/holepuncher.go +++ b/p2p/protocol/holepunch/holepuncher.go @@ -206,10 +206,14 @@ func (hp *holePuncher) initiateHolePunchImpl(str network.Stream) ([]ma.Multiaddr str.SetDeadline(time.Now().Add(StreamTimeout)) // send a CONNECT and start RTT measurement. - obsAddrs := hp.filter.FilterLocal(str.Conn().RemotePeer(), hp.ids.OwnObservedAddrs()) + obsAddrs := removeRelayAddrs(hp.ids.OwnObservedAddrs()) + if hp.filter != nil { + obsAddrs = hp.filter.FilterLocal(str.Conn().RemotePeer(), obsAddrs) + } if len(obsAddrs) == 0 { - return nil, 0, errors.New("aborting hole punch initiation, as we have no public address after filtering") + return nil, 0, errors.New("aborting hole punch initiation as we have no public address") } + start := time.Now() if err := w.WriteMsg(&pb.HolePunch{ Type: pb.HolePunch_CONNECT.Enum(), @@ -228,7 +232,12 @@ func (hp *holePuncher) initiateHolePunchImpl(str network.Stream) ([]ma.Multiaddr if t := msg.GetType(); t != pb.HolePunch_CONNECT { return nil, 0, fmt.Errorf("expect CONNECT message, got %s", t) } - addrs := hp.filter.FilterRemote(str.Conn().RemotePeer(), addrsFromBytes(msg.ObsAddrs)) + + addrs := removeRelayAddrs(addrsFromBytes(msg.ObsAddrs)) + if hp.filter != nil { + addrs = hp.filter.FilterRemote(str.Conn().RemotePeer(), addrs) + } + if len(addrs) == 0 { return nil, 0, errors.New("didn't receive any public addresses in CONNECT") } diff --git a/p2p/protocol/holepunch/svc.go b/p2p/protocol/holepunch/svc.go index 9d023c4717..2f9049c85b 100644 --- a/p2p/protocol/holepunch/svc.go +++ b/p2p/protocol/holepunch/svc.go @@ -75,7 +75,6 @@ func NewService(h host.Host, ids identify.IDService, opts ...Option) (*Service, host: h, ids: ids, hasPublicAddrsChan: make(chan struct{}), - filter: DefaultAddrFilter{}, } for _, opt := range opts { @@ -170,7 +169,11 @@ func (s *Service) incomingHolePunch(str network.Stream) (rtt time.Duration, addr if !isRelayAddress(str.Conn().RemoteMultiaddr()) { return 0, nil, fmt.Errorf("received hole punch stream: %s", str.Conn().RemoteMultiaddr()) } - ownAddrs := s.filter.FilterLocal(str.Conn().RemotePeer(), s.ids.OwnObservedAddrs()) + ownAddrs := removeRelayAddrs(s.ids.OwnObservedAddrs()) + if s.filter != nil { + ownAddrs = s.filter.FilterLocal(str.Conn().RemotePeer(), ownAddrs) + } + // If we can't tell the peer where to dial us, there's no point in starting the hole punching. if len(ownAddrs) == 0 { return 0, nil, errors.New("rejecting hole punch request, as we don't have any public addresses") @@ -196,7 +199,12 @@ func (s *Service) incomingHolePunch(str network.Stream) (rtt time.Duration, addr if t := msg.GetType(); t != pb.HolePunch_CONNECT { return 0, nil, fmt.Errorf("expected CONNECT message from initiator but got %d", t) } - obsDial := s.filter.FilterRemote(str.Conn().RemotePeer(), addrsFromBytes(msg.ObsAddrs)) + + obsDial := removeRelayAddrs(addrsFromBytes(msg.ObsAddrs)) + if s.filter != nil { + obsDial = s.filter.FilterRemote(str.Conn().RemotePeer(), obsDial) + } + log.Debugw("received hole punch request", "peer", str.Conn().RemotePeer(), "addrs", obsDial) if len(obsDial) == 0 { return 0, nil, errors.New("expected CONNECT message to contain at least one address") From f916c55dcc08199a2282ea0ac091d9e5940cc67c Mon Sep 17 00:00:00 2001 From: Dennis Trautwein Date: Thu, 10 Nov 2022 11:06:50 +0100 Subject: [PATCH 5/7] fix: hole punch failing test --- p2p/protocol/holepunch/holepunch_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/protocol/holepunch/holepunch_test.go b/p2p/protocol/holepunch/holepunch_test.go index bcbf785882..24ba546cc8 100644 --- a/p2p/protocol/holepunch/holepunch_test.go +++ b/p2p/protocol/holepunch/holepunch_test.go @@ -192,7 +192,7 @@ func TestFailuresOnInitiator(t *testing.T) { errMsg: "i/o deadline reached", }, "no addrs after filtering": { - errMsg: "aborting hole punch initiation, as we have no public address after filtering", + errMsg: "aborting hole punch initiation as we have no public address", filter: func(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr { return []ma.Multiaddr{} }, From 1d26ea895a8051567b8ee33b579ee66b1637ef58 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 16 Nov 2022 13:40:37 +1300 Subject: [PATCH 6/7] holepunch: fix race condition in test when adding holepunch service --- p2p/protocol/holepunch/holepunch_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/p2p/protocol/holepunch/holepunch_test.go b/p2p/protocol/holepunch/holepunch_test.go index 24ba546cc8..fd19a76946 100644 --- a/p2p/protocol/holepunch/holepunch_test.go +++ b/p2p/protocol/holepunch/holepunch_test.go @@ -223,6 +223,11 @@ func TestFailuresOnInitiator(t *testing.T) { } hps := addHolePunchService(t, h2, opts...) + // wait until the hole punching protocol has actually started + require.Eventually(t, func() bool { + protos, _ := h2.Peerstore().SupportsProtocols(h1.ID(), string(holepunch.Protocol)) + return len(protos) > 0 + }, 200*time.Millisecond, 10*time.Millisecond) if tc.rhandler != nil { h1.SetStreamHandler(holepunch.Protocol, tc.rhandler) From 94548ef791d8a6737942270617c093060716f7d2 Mon Sep 17 00:00:00 2001 From: Dennis Trautwein Date: Thu, 17 Nov 2022 11:22:34 +0100 Subject: [PATCH 7/7] improve holepunch filter interface comments --- p2p/protocol/holepunch/filter.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/p2p/protocol/holepunch/filter.go b/p2p/protocol/holepunch/filter.go index 46bcf8f6a1..5c1a4f5342 100644 --- a/p2p/protocol/holepunch/filter.go +++ b/p2p/protocol/holepunch/filter.go @@ -20,8 +20,8 @@ func WithAddrFilter(f AddrFilter) Option { // AddrFilter defines the interface for the multi address filtering. type AddrFilter interface { - // FilterLocal is a function that filters the multi addresses that we send to the remote peer. + // FilterLocal filters the multi addresses that are sent to the remote peer. FilterLocal(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr - // FilterRemote is a function that filters the multi addresses which we received from the remote peer. + // FilterRemote filters the multi addresses received from the remote peer. FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr }