Skip to content

Commit

Permalink
feat: add "autoclient" routing type
Browse files Browse the repository at this point in the history
This routing type is the same as "auto" but it creates the DHT in
"client" mode and hence does not start a DHT server.
  • Loading branch information
guseggert committed Mar 8, 2023
1 parent 983530c commit 14c0683
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 33 deletions.
61 changes: 35 additions & 26 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,33 @@ import (
)

const (
adjustFDLimitKwd = "manage-fdlimit"
enableGCKwd = "enable-gc"
initOptionKwd = "init"
initConfigOptionKwd = "init-config"
initProfileOptionKwd = "init-profile"
ipfsMountKwd = "mount-ipfs"
ipnsMountKwd = "mount-ipns"
migrateKwd = "migrate"
mountKwd = "mount"
offlineKwd = "offline" // global option
routingOptionKwd = "routing"
routingOptionSupernodeKwd = "supernode"
routingOptionDHTClientKwd = "dhtclient"
routingOptionDHTKwd = "dht"
routingOptionDHTServerKwd = "dhtserver"
routingOptionNoneKwd = "none"
routingOptionCustomKwd = "custom"
routingOptionDefaultKwd = "default"
routingOptionAutoKwd = "auto"
unencryptTransportKwd = "disable-transport-encryption"
unrestrictedAPIAccessKwd = "unrestricted-api"
writableKwd = "writable"
enablePubSubKwd = "enable-pubsub-experiment"
enableIPNSPubSubKwd = "enable-namesys-pubsub"
enableMultiplexKwd = "enable-mplex-experiment"
agentVersionSuffix = "agent-version-suffix"
adjustFDLimitKwd = "manage-fdlimit"
enableGCKwd = "enable-gc"
initOptionKwd = "init"
initConfigOptionKwd = "init-config"
initProfileOptionKwd = "init-profile"
ipfsMountKwd = "mount-ipfs"
ipnsMountKwd = "mount-ipns"
migrateKwd = "migrate"
mountKwd = "mount"
offlineKwd = "offline" // global option
routingOptionKwd = "routing"
routingOptionSupernodeKwd = "supernode"
routingOptionDHTClientKwd = "dhtclient"
routingOptionDHTKwd = "dht"
routingOptionDHTServerKwd = "dhtserver"
routingOptionNoneKwd = "none"
routingOptionCustomKwd = "custom"
routingOptionDefaultKwd = "default"
routingOptionAutoKwd = "auto"
routingOptionAutoClientKwd = "autoclient"
unencryptTransportKwd = "disable-transport-encryption"
unrestrictedAPIAccessKwd = "unrestricted-api"
writableKwd = "writable"
enablePubSubKwd = "enable-pubsub-experiment"
enableIPNSPubSubKwd = "enable-namesys-pubsub"
enableMultiplexKwd = "enable-mplex-experiment"
agentVersionSuffix = "agent-version-suffix"
// apiAddrKwd = "address-api"
// swarmAddrKwd = "address-swarm"
)
Expand Down Expand Up @@ -416,6 +417,14 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
cfg.Identity.PeerID,
cfg.Addresses.Swarm,
cfg.Identity.PrivKey,
libp2p.DHTOption,
)
case routingOptionAutoClientKwd:
ncfg.Routing = libp2p.ConstructDefaultRouting(
cfg.Identity.PeerID,
cfg.Addresses.Swarm,
cfg.Identity.PrivKey,
libp2p.DHTClientOption,
)
case routingOptionDHTClientKwd:
ncfg.Routing = libp2p.DHTClientOption
Expand Down
2 changes: 1 addition & 1 deletion config/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Routing struct {
// Type sets default daemon routing mode.
//
// Can be one of "auto", "dht", "dhtclient", "dhtserver", "none", or "custom".
// Can be one of "auto", "autoclient", "dht", "dhtclient", "dhtserver", "none", or "custom".
// When unset or set to "auto", DHT and implicit routers are used.
// When "custom" is set, user-provided Routing.Routers is used.
Type *OptionalString `json:",omitempty"`
Expand Down
5 changes: 2 additions & 3 deletions core/node/libp2p/routingopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func init() {
}

// ConstructDefaultRouting returns routers used when Routing.Type is unset or set to "auto"
func ConstructDefaultRouting(peerID string, addrs []string, privKey string) func(
func ConstructDefaultRouting(peerID string, addrs []string, privKey string, routingOpt RoutingOption) func(
ctx context.Context,
host host.Host,
dstore datastore.Batching,
Expand All @@ -58,8 +58,7 @@ func ConstructDefaultRouting(peerID string, addrs []string, privKey string) func
// Different trade-offs can be made by setting Routing.Type = "custom" with own Routing.Routers
var routers []*routinghelpers.ParallelRouter

// Run the default DHT routing (same as Routing.Type = "dht")
dhtRouting, err := DHTOption(ctx, host, dstore, validator, bootstrapPeers...)
dhtRouting, err := routingOpt(ctx, host, dstore, validator, bootstrapPeers...)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1349,11 +1349,13 @@ Contains options for content, peer, and IPNS routing mechanisms.

### `Routing.Type`

There are multiple routing options: "auto", "none", "dht" and "custom".
There are multiple routing options: "auto", "autoclient", "none", "dht", "dhtclient", and "custom".

* **DEFAULT:** If unset, or set to "auto", your node will use the IPFS DHT
and parallel HTTP routers listed below for additional speed.

* If set to "autoclient", your node will behave as in "auto" but without running a DHT server.

* If set to "none", your node will use _no_ routing system. You'll have to
explicitly connect to peers that have the content you're looking for.

Expand All @@ -1379,7 +1381,7 @@ To force a specific DHT-only mode, client or server, set `Routing.Type` to
`dhtclient` or `dhtserver` respectively. Please do not set this to `dhtserver`
unless you're sure your node is reachable from the public network.

When `Routing.Type` is set to `auto` your node will accelerate some types of routing
When `Routing.Type` is set to `auto` or `autoclient` your node will accelerate some types of routing
by leveraging HTTP endpoints compatible with [IPIP-337](https://github.com/ipfs/specs/pull/337)
in addition to the IPFS DHT.
By default, an instance of [IPNI](https://github.com/ipni/specs/blob/main/IPNI.md#readme)
Expand Down
2 changes: 1 addition & 1 deletion test/cli/delegated_routing_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestHTTPDelegatedRouting(t *testing.T) {
}))
t.Cleanup(server.Close)

node.IPFS("config", "Routing.Type", "--json", `"custom"`)
node.IPFS("config", "Routing.Type", "custom")
node.IPFS("config", "Routing.Routers.TestDelegatedRouter", "--json", ToJSONStr(JSONObj{
"Type": "http",
"Parameters": JSONObj{
Expand Down
39 changes: 39 additions & 0 deletions test/cli/dht_autoclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cli

import (
"bytes"
"testing"

"github.com/ipfs/kubo/test/cli/harness"
"github.com/ipfs/kubo/test/cli/testutils"
"github.com/stretchr/testify/assert"
)

func TestDHTAutoclient(t *testing.T) {
t.Parallel()
nodes := harness.NewT(t).NewNodes(10).Init()
harness.Nodes(nodes[8:]).ForEachPar(func(node *harness.Node) {
node.IPFS("config", "Routing.Type", "autoclient")
})
nodes.StartDaemons().Connect()

t.Run("file added on node in client mode is retrievable from node in client mode", func(t *testing.T) {
t.Parallel()
randomBytes := testutils.RandomBytes(1000)
hash := nodes[8].IPFSAdd(bytes.NewReader(randomBytes))

res := nodes[9].IPFS("cat", hash)
assert.Equal(t, randomBytes, []byte(res.Stdout.Trimmed()))
})

t.Run("file added on node in server mode is retrievable from all nodes", func(t *testing.T) {
t.Parallel()
randomBytes := testutils.RandomBytes(1000)
hash := nodes[0].IPFSAdd(bytes.NewReader(randomBytes))

for i := 0; i < 10; i++ {
res := nodes[i].IPFS("cat", hash)
assert.Equal(t, randomBytes, []byte(res.Stdout.Trimmed()))
}
})
}

0 comments on commit 14c0683

Please sign in to comment.