diff --git a/go.mod b/go.mod index f6d15fd..8eabb43 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/ipfs/boxo v0.15.0 github.com/ipfs/go-cid v0.4.1 + github.com/ipfs/go-log v1.0.5 github.com/libp2p/go-libp2p v0.32.1 github.com/libp2p/go-libp2p-kad-dht v0.25.1 github.com/libp2p/go-libp2p-record v0.2.0 @@ -45,7 +46,6 @@ require ( github.com/huin/goupnp v1.3.0 // indirect github.com/ipfs/go-datastore v0.6.0 // indirect github.com/ipfs/go-ipfs-util v0.0.3 // indirect - github.com/ipfs/go-log v1.0.5 // indirect github.com/ipfs/go-log/v2 v2.5.1 // indirect github.com/ipld/go-ipld-prime v0.21.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect diff --git a/server.go b/server.go index 0638344..1ff646e 100644 --- a/server.go +++ b/server.go @@ -8,6 +8,7 @@ import ( "github.com/ipfs/boxo/routing/http/client" "github.com/ipfs/boxo/routing/http/server" + logging "github.com/ipfs/go-log" "github.com/libp2p/go-libp2p" dht "github.com/libp2p/go-libp2p-kad-dht" "github.com/libp2p/go-libp2p/core/host" @@ -15,6 +16,8 @@ import ( rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" ) +var logger = logging.Logger("someguy") + func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentEndpoints, peerEndpoints, ipnsEndpoints []string) error { h, err := newHost(runAcceleratedDHTClient) if err != nil { diff --git a/server_routers.go b/server_routers.go index 4d58894..b58e3fa 100644 --- a/server_routers.go +++ b/server_routers.go @@ -116,7 +116,10 @@ func find[T any](ctx context.Context, routers []router, call func(router) (iter. // If all iterators failed to be created, then return the error. if len(its) == 0 { + logger.Warnf("failed to create all iterators: %w", err) return nil, err + } else if err != nil { + logger.Warnf("failed to create some iterators: %w", err) } // Otherwise return manyIter with remaining iterators. @@ -191,6 +194,9 @@ func (mi *manyIter[T]) Close() error { for _, it := range mi.its { err = errors.Join(err, it.Close()) } + if err != nil { + logger.Warnf("errors on closing iterators: %w", err) + } return err } diff --git a/server_routers_test.go b/server_routers_test.go index 204e347..bb83182 100644 --- a/server_routers_test.go +++ b/server_routers_test.go @@ -15,7 +15,7 @@ import ( "github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/routing" - mh "github.com/multiformats/go-multihash" + "github.com/multiformats/go-multihash" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -264,14 +264,26 @@ func TestPutIPNS(t *testing.T) { }) } +func makeCID() cid.Cid { + buf := make([]byte, 63) + _, err := rand.Read(buf) + if err != nil { + panic(err) + } + mh, err := multihash.Encode(buf, multihash.SHA2_256) + if err != nil { + panic(err) + } + c := cid.NewCidV1(cid.Raw, mh) + return c +} + func TestFindProviders(t *testing.T) { t.Parallel() t.Run("Basic", func(t *testing.T) { - prefix := cid.NewPrefixV1(cid.Raw, mh.SHA2_256) - c, _ := prefix.Sum([]byte("foo")) - ctx := context.Background() + c := makeCID() d := parallelRouter{} it, err := d.FindProviders(ctx, c, 10) @@ -280,7 +292,8 @@ func TestFindProviders(t *testing.T) { require.False(t, it.Next()) mr1 := &mockRouter{} - mr1.On("FindProviders", mock.Anything, c, 10).Return(iter.ToResultIter(iter.FromSlice([]types.Record{})), nil) + mr1Iter := newMockIter[types.Record](ctx) + mr1.On("FindProviders", mock.Anything, c, 10).Return(mr1Iter, nil) d = parallelRouter{ routers: []router{ @@ -290,6 +303,73 @@ func TestFindProviders(t *testing.T) { }, } + peers := []peer.ID{"peer1", "peer2", "peer3"} + + go func() { + mr1Iter.ch <- iter.Result[types.Record]{Val: &types.PeerRecord{Schema: "peer", ID: &peers[0]}} + mr1Iter.ch <- iter.Result[types.Record]{Val: &types.PeerRecord{Schema: "peer", ID: &peers[1]}} + mr1Iter.ch <- iter.Result[types.Record]{Val: &types.PeerRecord{Schema: "peer", ID: &peers[2]}} + close(mr1Iter.ch) + }() + + it, err = d.FindProviders(ctx, c, 10) + require.NoError(t, err) + + results, err := iter.ReadAllResults(it) + require.NoError(t, err) + require.Len(t, results, 3) + }) + + t.Run("Failed to Create All Iterators", func(t *testing.T) { + ctx := context.Background() + c := makeCID() + + d := parallelRouter{} + it, err := d.FindProviders(ctx, c, 10) + + require.NoError(t, err) + require.False(t, it.Next()) + + mr1 := &mockRouter{} + mr1.On("FindProviders", mock.Anything, c, 10).Return(nil, errors.New("error a")) + + mr2 := &mockRouter{} + mr2.On("FindProviders", mock.Anything, c, 10).Return(nil, errors.New("error b")) + + d = parallelRouter{ + routers: []router{ + mr1, mr2, + }, + } + + _, err = d.FindProviders(ctx, c, 10) + require.ErrorContains(t, err, "error a") + require.ErrorContains(t, err, "error b") + }) + + t.Run("Failed to Create One Iterator", func(t *testing.T) { + ctx := context.Background() + + c := makeCID() + + d := parallelRouter{} + it, err := d.FindProviders(ctx, c, 10) + + require.NoError(t, err) + require.False(t, it.Next()) + + mr1 := &mockRouter{} + mr1.On("FindProviders", mock.Anything, c, 10).Return(iter.ToResultIter(iter.FromSlice([]types.Record{})), nil) + + mr2 := &mockRouter{} + mr2.On("FindProviders", mock.Anything, c, 10).Return(nil, errors.New("error b")) + + d = parallelRouter{ + routers: []router{ + mr1, mr2, + }, + } + it, err = d.FindProviders(ctx, c, 10) require.NoError(t, err) require.False(t, it.Next())