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

Fix empty address check in v0.5.11 #156

Merged
merged 1 commit into from
Feb 15, 2024
Merged
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
18 changes: 18 additions & 0 deletions dagsync/ipnisync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ func (s *Sync) NewSyncer(peerInfo peer.AddrInfo) (*Syncer, error) {
if s.authPeerID {
rtOpts = append(rtOpts, libp2phttp.ServerMustAuthenticatePeerID)
}

peerInfo = mautil.CleanPeerAddrInfo(peerInfo)
if len(peerInfo.Addrs) == 0 {
if s.clientHost.StreamHost == nil {
return nil, errors.New("no peer addrs and no stream host")
}
peerStore := s.clientHost.StreamHost.Peerstore()
if peerStore == nil {
return nil, errors.New("no peer addrs and no stream host peerstore")
}
peerInfo.Addrs = peerStore.Addrs(peerInfo.ID)
if len(peerInfo.Addrs) == 0 {
return nil, errors.New("no peer addrs and none found in peertore")
}

// TODO: double check that libp2p peerstore never returns nil addrs.
}

s.clientHostMutex.Lock()
cli, err = s.clientHost.NamespacedClient(ProtocolID, peerInfo, rtOpts...)
s.clientHostMutex.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions dagsync/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ func (s *Subscriber) SyncAdChain(ctx context.Context, peerInfo peer.AddrInfo, op
opts.blockHook = s.generalBlockHook
}

peerInfo = mautil.CleanPeerAddrInfo(peerInfo)
var err error
peerInfo, err = removeIDFromAddrs(peerInfo)
if err != nil {
Expand Down Expand Up @@ -563,6 +564,7 @@ func (s *Subscriber) syncEntries(ctx context.Context, peerInfo peer.AddrInfo, en
s.expSyncMutex.Unlock()
defer s.expSyncWG.Done()

peerInfo = mautil.CleanPeerAddrInfo(peerInfo)
peerInfo, err := removeIDFromAddrs(peerInfo)
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions mautil/mautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ func StringsToMultiaddrs(addrs []string) ([]multiaddr.Multiaddr, error) {
}
return maddrs, lastErr
}

func CleanPeerAddrInfo(target peer.AddrInfo) peer.AddrInfo {
for i := 0; i < len(target.Addrs); {
if target.Addrs[i] == nil {
target.Addrs[i] = target.Addrs[len(target.Addrs)-1]
target.Addrs = target.Addrs[:len(target.Addrs)-1]
continue
}
i++
}
return target
}
58 changes: 58 additions & 0 deletions mautil/mautil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/ipni/go-libipni/mautil"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -67,3 +68,60 @@ func TestFindHTTPAddrs(t *testing.T) {
filtered = mautil.FilterPublic(nil)
require.Nil(t, filtered)
}

func TestCleanPeerAddrInfo(t *testing.T) {
t.Run("all-nils", func(t *testing.T) {
require.Len(t, mautil.CleanPeerAddrInfo(
peer.AddrInfo{
Addrs: make([]multiaddr.Multiaddr, 3),
}).Addrs, 0)
})

goodAddrs, err := mautil.StringsToMultiaddrs([]string{
"/ip4/11.0.0.0/tcp/80/http",
"/ip6/fc00::/tcp/1717",
"/ip6/fe00::/tcp/8080/https",
})
require.NoError(t, err)

t.Run("nil-sandwich", func(t *testing.T) {
subject := peer.AddrInfo{
Addrs: make([]multiaddr.Multiaddr, 3),
}
subject.Addrs[1] = goodAddrs[0]
cleaned := mautil.CleanPeerAddrInfo(subject)
require.Len(t, cleaned.Addrs, 1)
require.ElementsMatch(t, cleaned.Addrs, goodAddrs[:1])
})
t.Run("nil-head", func(t *testing.T) {
subject := peer.AddrInfo{
Addrs: make([]multiaddr.Multiaddr, 3),
}
subject.Addrs[1] = goodAddrs[0]
subject.Addrs[2] = goodAddrs[1]
cleaned := mautil.CleanPeerAddrInfo(subject)
require.Len(t, cleaned.Addrs, 2)
require.ElementsMatch(t, goodAddrs[:2], cleaned.Addrs)
})
t.Run("nil-tail", func(t *testing.T) {
subject := peer.AddrInfo{
Addrs: make([]multiaddr.Multiaddr, 3),
}
subject.Addrs[0] = goodAddrs[0]
subject.Addrs[1] = goodAddrs[1]
cleaned := mautil.CleanPeerAddrInfo(subject)
require.Len(t, cleaned.Addrs, 2)
require.ElementsMatch(t, goodAddrs[:2], cleaned.Addrs)
})
t.Run("no-nils", func(t *testing.T) {
subject := peer.AddrInfo{
Addrs: make([]multiaddr.Multiaddr, 3),
}
subject.Addrs[0] = goodAddrs[0]
subject.Addrs[1] = goodAddrs[1]
subject.Addrs[2] = goodAddrs[2]
cleaned := mautil.CleanPeerAddrInfo(subject)
require.Len(t, cleaned.Addrs, 3)
require.ElementsMatch(t, goodAddrs, cleaned.Addrs)
})
}
Loading