Skip to content

Commit

Permalink
Delegated Routing.
Browse files Browse the repository at this point in the history
Implementation of Reframe specs (https://github.com/ipfs/specs/blob/master/REFRAME.md) using go-delegated-routing library.
  • Loading branch information
ajnavarro committed Jun 27, 2022
1 parent 058803f commit 24d979b
Show file tree
Hide file tree
Showing 21 changed files with 370 additions and 63 deletions.
5 changes: 1 addition & 4 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment

routingOption, _ := req.Options[routingOptionKwd].(string)
if routingOption == routingOptionDefaultKwd {
routingOption = cfg.Routing.Type
if routingOption == "" {
routingOption = routingOptionDHTKwd
}
routingOption = cfg.Routing.Type.WithDefault(routingOptionDHTKwd)
}
switch routingOption {
case routingOptionSupernodeKwd:
Expand Down
4 changes: 1 addition & 3 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ func InitWithIdentity(identity Identity) (*Config, error) {
},
},

Routing: Routing{
Type: "dht",
},
Routing: Routing{},

// setup the node mount points.
Mounts: Mounts{
Expand Down
3 changes: 2 additions & 1 deletion config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ functionality - performance of content discovery and data
fetching may be degraded.
`,
Transform: func(c *Config) error {
c.Routing.Type = "dhtclient"
rt := "dhtclient"
c.Routing.Type = &OptionalString{value: &rt}
c.AutoNAT.ServiceMode = AutoNATServiceDisabled
c.Reprovider.Interval = "0"

Expand Down
41 changes: 40 additions & 1 deletion config/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,44 @@ type Routing struct {
// Type sets default daemon routing mode.
//
// Can be one of "dht", "dhtclient", "dhtserver", "none", or unset.
Type string
Type *OptionalString `json:",omitempty"`

Routers map[string]Router
}

type Router struct {

// Type can be one of "dht", "dhtclient", "dhtserver", "reframe".
// Reframe type allows to add other resolvers using the Reframe spec:
// https://github.com/ipfs/specs/blob/master/REFRAME.md
Type string
Enabled bool

// Methods that we want to use from this provider.
// Leave it empty to use all the supported and available ones.
// Actual supported methods: "FindProviders", "GetIPNS", "PutIPNS"
Methods []string

// Parameters are extra configuration that this router might need.
// A common one for reframe endpoints is "Address".
Parameters map[string]string
}

// Type is the routing type.
// Depending of the type we need to instantiate different Routing implementations.
type RouterType string

const (
RouterTypeReframe RouterType = "reframe"
RouterTypeDHT RouterType = "dht"
)

type RouterParam string

const (
// RouterParamAddress is the URL where the routing implementation will point to get the information.
// Usually used for reframe Routers.
RouterParamAddress RouterParam = "address"

RouterParamPriority RouterParam = "priority"
)
6 changes: 6 additions & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ func TestCommands(t *testing.T) {
"/dht/provide",
"/dht/put",
"/dht/query",
"/routing",
"/routing/put",
"/routing/get",
"/routing/findpeer",
"/routing/findprovs",
"/routing/provide",
"/diag",
"/diag/cmds",
"/diag/cmds/clear",
Expand Down
1 change: 1 addition & 0 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ var rootSubcommands = map[string]*cmds.Command{
"config": ConfigCmd,
"dag": dag.DagCmd,
"dht": DhtCmd,
"routing": RoutingCmd,
"diag": DiagCmd,
"dns": DNSCmd,
"id": IDCmd,
Expand Down
35 changes: 25 additions & 10 deletions core/commands/dht.go → core/commands/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,26 @@ var DhtCmd = &cmds.Command{

Subcommands: map[string]*cmds.Command{
"query": queryDhtCmd,
"findprovs": findProvidersDhtCmd,
"findpeer": findPeerDhtCmd,
"get": getValueDhtCmd,
"put": putValueDhtCmd,
"provide": provideRefDhtCmd,
"findprovs": findProvidersRoutingCmd,
"findpeer": findPeerRoutingCmd,
"get": getValueRoutingCmd,
"put": putValueRoutingCmd,
"provide": provideRefRoutingCmd,
},
}

var RoutingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Issue commands directly through the DHT.",
ShortDescription: ``,
},

Subcommands: map[string]*cmds.Command{
"findprovs": findProvidersRoutingCmd,
"findpeer": findPeerRoutingCmd,
"get": getValueRoutingCmd,
"put": putValueRoutingCmd,
"provide": provideRefRoutingCmd,
},
}

Expand Down Expand Up @@ -138,7 +153,7 @@ const (
numProvidersOptionName = "num-providers"
)

var findProvidersDhtCmd = &cmds.Command{
var findProvidersRoutingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Find peers that can provide a specific value, given a key.",
ShortDescription: "Outputs a list of newline-delimited provider Peer IDs.",
Expand Down Expand Up @@ -230,7 +245,7 @@ const (
recursiveOptionName = "recursive"
)

var provideRefDhtCmd = &cmds.Command{
var provideRefRoutingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Announce to the network that you are providing given values.",
},
Expand Down Expand Up @@ -365,7 +380,7 @@ func provideKeysRec(ctx context.Context, r routing.Routing, dserv ipld.DAGServic
return nil
}

var findPeerDhtCmd = &cmds.Command{
var findPeerRoutingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Find the multiaddresses associated with a Peer ID.",
ShortDescription: "Outputs a list of newline-delimited multiaddresses.",
Expand Down Expand Up @@ -441,7 +456,7 @@ var findPeerDhtCmd = &cmds.Command{
Type: routing.QueryEvent{},
}

var getValueDhtCmd = &cmds.Command{
var getValueRoutingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Given a key, query the routing system for its best value.",
ShortDescription: `
Expand Down Expand Up @@ -529,7 +544,7 @@ Different key types can specify other 'best' rules.
Type: routing.QueryEvent{},
}

var putValueDhtCmd = &cmds.Command{
var putValueRoutingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Write a key/value pair to the routing system.",
ShortDescription: `
Expand Down
7 changes: 4 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"io"

"github.com/ipfs/go-filestore"
"github.com/ipfs/go-ipfs-pinner"
pin "github.com/ipfs/go-ipfs-pinner"

bserv "github.com/ipfs/go-blockservice"
"github.com/ipfs/go-fetcher"
"github.com/ipfs/go-graphsync"
bstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
"github.com/ipfs/go-ipfs-provider"
provider "github.com/ipfs/go-ipfs-provider"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
mfs "github.com/ipfs/go-mfs"
Expand Down Expand Up @@ -50,6 +50,7 @@ import (
"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-ipfs/peering"
"github.com/ipfs/go-ipfs/repo"
irouting "github.com/ipfs/go-ipfs/routing"
"github.com/ipfs/go-namesys"
ipnsrp "github.com/ipfs/go-namesys/republisher"
)
Expand Down Expand Up @@ -90,7 +91,7 @@ type IpfsNode struct {
Peering *peering.PeeringService `optional:"true"`
Filters *ma.Filters `optional:"true"`
Bootstrapper io.Closer `optional:"true"` // the periodic bootstrapper
Routing routing.Routing `optional:"true"` // the routing system. recommend ipfs-dht
Routing irouting.TieredRouter `optional:"true"` // the routing system. recommend ipfs-dht
DNSResolver *madns.Resolver // the DNS resolver
Exchange exchange.Interface // the block exchange + strategy (bitswap)
Namesys namesys.NameSystem // the name system, resolves paths to hashes
Expand Down
4 changes: 2 additions & 2 deletions core/node/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
config "github.com/ipfs/go-ipfs/config"
irouting "github.com/ipfs/go-ipfs/routing"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/routing"
"go.uber.org/fx"

"github.com/ipfs/go-ipfs/core/node/helpers"
Expand All @@ -25,7 +25,7 @@ const (

// OnlineExchange creates new LibP2P backed block exchange (BitSwap)
func OnlineExchange(cfg *config.Config, provide bool) interface{} {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.Routing, bs blockstore.GCBlockstore) exchange.Interface {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt irouting.TieredRouter, bs blockstore.GCBlockstore) exchange.Interface {
bitswapNetwork := network.NewFromIpfsHost(host, rt)

var internalBsCfg config.InternalBitswap
Expand Down
5 changes: 3 additions & 2 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ipfs/go-ipfs/p2p"

offline "github.com/ipfs/go-ipfs-exchange-offline"
offroute "github.com/ipfs/go-ipfs-routing/offline"
uio "github.com/ipfs/go-unixfs/io"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -167,6 +166,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {

fx.Provide(libp2p.Routing),
fx.Provide(libp2p.BaseRouting(cfg.Experimental.AcceleratedDHTClient)),
fx.Provide(libp2p.DelegatedRouting(cfg.Routing.Routers)),
maybeProvide(libp2p.PubsubRouter, bcfg.getOpt("ipnsps")),

maybeProvide(libp2p.BandwidthCounter, !cfg.Swarm.DisableBandwidthMetrics),
Expand Down Expand Up @@ -313,7 +313,8 @@ func Offline(cfg *config.Config) fx.Option {
fx.Provide(offline.Exchange),
fx.Provide(DNSResolver),
fx.Provide(Namesys(0)),
fx.Provide(offroute.NewOfflineRouter),
fx.Provide(libp2p.Routing),
fx.Provide(libp2p.OfflineRouting),
OfflineProviders(cfg.Experimental.StrategicProviding, cfg.Experimental.AcceleratedDHTClient, cfg.Reprovider.Strategy, cfg.Reprovider.Interval),
)
}
Expand Down
11 changes: 6 additions & 5 deletions core/node/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"fmt"
"time"

"github.com/ipfs/go-ipfs-util"
util "github.com/ipfs/go-ipfs-util"
"github.com/ipfs/go-ipns"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-record"
record "github.com/libp2p/go-libp2p-record"
madns "github.com/multiformats/go-multiaddr-dns"

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

"github.com/ipfs/go-namesys"
"github.com/ipfs/go-namesys/republisher"
)
Expand All @@ -28,8 +29,8 @@ func RecordValidator(ps peerstore.Peerstore) record.Validator {
}

// Namesys creates new name system
func Namesys(cacheSize int) func(rt routing.Routing, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) {
return func(rt routing.Routing, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) {
func Namesys(cacheSize int) func(rt irouting.TieredRouter, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) {
return func(rt irouting.TieredRouter, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) {
opts := []namesys.Option{
namesys.WithDatastore(repo.Datastore()),
namesys.WithDNSResolver(rslv),
Expand Down
Loading

0 comments on commit 24d979b

Please sign in to comment.