Skip to content

Commit

Permalink
Check for no addrs before constructing syncer
Browse files Browse the repository at this point in the history
Assert that a there are addresses associated to a remote peer before
constructing a syncer.
  • Loading branch information
masih committed Feb 15, 2024
1 parent 08d98c2 commit 47e70e2
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
30 changes: 16 additions & 14 deletions dagsync/ipnisync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ 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")
}
}

s.clientHostMutex.Lock()
cli, err = s.clientHost.NamespacedClient(ProtocolID, peerInfo, rtOpts...)
s.clientHostMutex.Unlock()
Expand Down Expand Up @@ -131,20 +147,6 @@ func (s *Sync) NewSyncer(peerInfo peer.AddrInfo) (*Syncer, error) {
httpClient = rclient.StandardClient()
}

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")
}
}

urls := make([]*url.URL, len(peerInfo.Addrs))
for i, addr := range peerInfo.Addrs {
u, err := maurl.ToURL(addr)
Expand Down
2 changes: 2 additions & 0 deletions dagsync/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,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 @@ -553,6 +554,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)
})
}

0 comments on commit 47e70e2

Please sign in to comment.