Skip to content

Commit

Permalink
Add tests and remove uneeded code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnavarro committed Jun 29, 2022
1 parent 9f06f26 commit 0fbeac3
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 96 deletions.
3 changes: 1 addition & 2 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ functionality - performance of content discovery and data
fetching may be degraded.
`,
Transform: func(c *Config) error {
rt := "dhtclient"
c.Routing.Type = &OptionalString{value: &rt}
c.Routing.Type = NewOptionalString("dhtclient")
c.AutoNAT.ServiceMode = AutoNATServiceDisabled
c.Reprovider.Interval = "0"

Expand Down
11 changes: 9 additions & 2 deletions core/node/libp2p/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,15 @@ type p2pOnlineContentRoutingIn struct {
ContentRouter []routing.ContentRouting `group:"content-routers"`
}

func ContentRouting(in p2pOnlineContentRoutingIn) irouting.TieredContentRouter {
return &irouting.ContentRoutingWrapper{ContentRoutings: in.ContentRouter}
func ContentRouting(in p2pOnlineContentRoutingIn) routing.ContentRouting {
var routers []routing.Routing
for _, cr := range in.ContentRouter {
routers = append(routers, irouting.NewContentRoutingWrapper(cr))
}

return routinghelpers.Tiered{
Routers: routers,
}
}

type p2pOnlineRoutingIn struct {
Expand Down
5 changes: 2 additions & 3 deletions core/node/libp2p/topicdiscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (

"github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/routing"
disc "github.com/libp2p/go-libp2p-discovery"

irouting "github.com/ipfs/go-ipfs/routing"
)

func TopicDiscovery() interface{} {
return func(host host.Host, cr irouting.TieredContentRouter) (service discovery.Discovery, err error) {
return func(host host.Host, cr routing.ContentRouting) (service discovery.Discovery, err error) {
baseDisc := disc.NewRoutingDiscovery(cr)
minBackoff, maxBackoff := time.Second*60, time.Hour
rng := rand.New(rand.NewSource(rand.Int63()))
Expand Down
4 changes: 0 additions & 4 deletions routing/delegated.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ type TieredRouter interface {
ProviderManyWrapper() ProvideMany
}

type TieredContentRouter interface {
routing.ContentRouting
}

var _ TieredRouter = &Tiered{}

// Tiered is a routing Tiered implementation providing some extra methods to fill
Expand Down
123 changes: 123 additions & 0 deletions routing/delegated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package routing

import (
"context"
"testing"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs/config"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)

func TestPriority(t *testing.T) {
require := require.New(t)
params := make(map[string]string)
p := GetPriority(params)

require.Equal(defaultPriority, p)

params[string(config.RouterParamPriority)] = "101"

p = GetPriority(params)

require.Equal(101, p)

params[string(config.RouterParamPriority)] = "NAN"

p = GetPriority(params)

require.Equal(defaultPriority, p)
}

func TestRoutingFromConfig(t *testing.T) {
require := require.New(t)

r, err := RoutingFromConfig(config.Router{
Type: "unknown",
})

require.Nil(r)
require.EqualError(err, "router type unknown is not supported")

r, err = RoutingFromConfig(config.Router{
Type: string(config.RouterTypeReframe),
Parameters: make(map[string]string),
})

require.Nil(r)
require.EqualError(err, "configuration param 'address' is needed for reframe delegated routing types")

r, err = RoutingFromConfig(config.Router{
Type: string(config.RouterTypeReframe),
Parameters: map[string]string{
string(config.RouterParamAddress): "test",
},
})

require.NotNil(r)
require.NoError(err)

// TODO add dht
}

func TestTieredRouter(t *testing.T) {
require := require.New(t)

tr := &Tiered{
Tiered: routinghelpers.Tiered{
Routers: []routing.Routing{routinghelpers.Null{}},
},
}

pm := tr.ProviderManyWrapper()
require.Nil(pm)

tr.Tiered.Routers = append(tr.Tiered.Routers, &dummyRouter{})

pm = tr.ProviderManyWrapper()
require.NotNil(pm)
}

type dummyRouter struct {
}

func (dr *dummyRouter) Provide(context.Context, cid.Cid, bool) error {
panic("not implemented")

}

func (dr *dummyRouter) FindProvidersAsync(context.Context, cid.Cid, int) <-chan peer.AddrInfo {
panic("not implemented")
}

func (dr *dummyRouter) FindPeer(context.Context, peer.ID) (peer.AddrInfo, error) {
panic("not implemented")
}

func (dr *dummyRouter) PutValue(context.Context, string, []byte, ...routing.Option) error {
panic("not implemented")
}

func (dr *dummyRouter) GetValue(context.Context, string, ...routing.Option) ([]byte, error) {
panic("not implemented")
}

func (dr *dummyRouter) SearchValue(context.Context, string, ...routing.Option) (<-chan []byte, error) {
panic("not implemented")
}

func (dr *dummyRouter) Bootstrap(context.Context) error {
panic("not implemented")
}

func (dr *dummyRouter) ProvideMany(ctx context.Context, keys []multihash.Multihash) error {
panic("not implemented")
}

func (dr *dummyRouter) Ready() bool {
panic("not implemented")
}
104 changes: 19 additions & 85 deletions routing/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package routing

import (
"context"
"sync"

"github.com/hashicorp/go-multierror"
"github.com/ipfs/go-cid"
drc "github.com/ipfs/go-delegated-routing/client"
"github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -67,97 +65,33 @@ func (pmw *ProvideManyWrapper) Ready() bool {
return out
}

var _ TieredContentRouter = &ContentRoutingWrapper{}

type ContentRoutingWrapper struct {
ContentRoutings []routing.ContentRouting
routing.ContentRouting
routing.ValueStore
}

// Provide adds the given cid to the content routing system. If 'true' is
// passed, it also announces it, otherwise it is just kept in the local
// accounting of which objects are being provided.
func (crw *ContentRoutingWrapper) Provide(ctx context.Context, cid cid.Cid, announce bool) error {
c := len(crw.ContentRoutings)
wg := sync.WaitGroup{}
wg.Add(c)

errors := make([]error, c)

for i, cr := range crw.ContentRoutings {
go func(cr routing.ContentRouting, i int) {
errors[i] = cr.Provide(ctx, cid, announce)
wg.Done()
}(cr, i)
}

wg.Wait()

var out []error
success := false
for _, e := range errors {
switch e {
case nil:
success = true
case routing.ErrNotSupported:
default:
out = append(out, e)
}
}
switch len(out) {
case 0:
if success {
// No errors and at least one router succeeded.
return nil
}
// No routers supported this operation.
return routing.ErrNotSupported
case 1:
return out[0]
default:
return &multierror.Error{Errors: out}
func NewContentRoutingWrapper(cr routing.ContentRouting) *ContentRoutingWrapper {
return &ContentRoutingWrapper{
ContentRouting: cr,
}
}

// Search for peers who are able to provide a given key
//
// When count is 0, this method will return an unbounded number of
// results.
func (crw *ContentRoutingWrapper) FindProvidersAsync(ctx context.Context, cid cid.Cid, count int) <-chan peer.AddrInfo {
subCtx, cancel := context.WithCancel(ctx)

aich := make(chan peer.AddrInfo)

for _, ri := range crw.ContentRoutings {
fpch := ri.FindProvidersAsync(subCtx, cid, count)
go func() {
for ai := range fpch {
aich <- ai
}
}()
}

out := make(chan peer.AddrInfo)

go func() {
defer cancel()
c := 0
doCount := true
if count <= 0 {
doCount = false
}
func (crw *ContentRoutingWrapper) Bootstrap(context.Context) error {
return nil
}

for ai := range aich {
if c >= count && doCount {
return
}
func (crw *ContentRoutingWrapper) FindPeer(context.Context, peer.ID) (peer.AddrInfo, error) {
return peer.AddrInfo{}, routing.ErrNotSupported
}

out <- ai
func (crw *ContentRoutingWrapper) PutValue(context.Context, string, []byte, ...routing.Option) error {
return routing.ErrNotSupported
}

if doCount {
c++
}
}
}()
func (crw *ContentRoutingWrapper) GetValue(context.Context, string, ...routing.Option) ([]byte, error) {
return nil, routing.ErrNotSupported
}

return out
func (crw *ContentRoutingWrapper) SearchValue(context.Context, string, ...routing.Option) (<-chan []byte, error) {
return nil, routing.ErrNotSupported
}
Loading

0 comments on commit 0fbeac3

Please sign in to comment.