Skip to content

Commit 14c0683

Browse files
committed
feat: add "autoclient" routing type
This routing type is the same as "auto" but it creates the DHT in "client" mode and hence does not start a DHT server.
1 parent 983530c commit 14c0683

File tree

6 files changed

+82
-33
lines changed

6 files changed

+82
-33
lines changed

cmd/ipfs/daemon.go

+35-26
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,33 @@ import (
4545
)
4646

4747
const (
48-
adjustFDLimitKwd = "manage-fdlimit"
49-
enableGCKwd = "enable-gc"
50-
initOptionKwd = "init"
51-
initConfigOptionKwd = "init-config"
52-
initProfileOptionKwd = "init-profile"
53-
ipfsMountKwd = "mount-ipfs"
54-
ipnsMountKwd = "mount-ipns"
55-
migrateKwd = "migrate"
56-
mountKwd = "mount"
57-
offlineKwd = "offline" // global option
58-
routingOptionKwd = "routing"
59-
routingOptionSupernodeKwd = "supernode"
60-
routingOptionDHTClientKwd = "dhtclient"
61-
routingOptionDHTKwd = "dht"
62-
routingOptionDHTServerKwd = "dhtserver"
63-
routingOptionNoneKwd = "none"
64-
routingOptionCustomKwd = "custom"
65-
routingOptionDefaultKwd = "default"
66-
routingOptionAutoKwd = "auto"
67-
unencryptTransportKwd = "disable-transport-encryption"
68-
unrestrictedAPIAccessKwd = "unrestricted-api"
69-
writableKwd = "writable"
70-
enablePubSubKwd = "enable-pubsub-experiment"
71-
enableIPNSPubSubKwd = "enable-namesys-pubsub"
72-
enableMultiplexKwd = "enable-mplex-experiment"
73-
agentVersionSuffix = "agent-version-suffix"
48+
adjustFDLimitKwd = "manage-fdlimit"
49+
enableGCKwd = "enable-gc"
50+
initOptionKwd = "init"
51+
initConfigOptionKwd = "init-config"
52+
initProfileOptionKwd = "init-profile"
53+
ipfsMountKwd = "mount-ipfs"
54+
ipnsMountKwd = "mount-ipns"
55+
migrateKwd = "migrate"
56+
mountKwd = "mount"
57+
offlineKwd = "offline" // global option
58+
routingOptionKwd = "routing"
59+
routingOptionSupernodeKwd = "supernode"
60+
routingOptionDHTClientKwd = "dhtclient"
61+
routingOptionDHTKwd = "dht"
62+
routingOptionDHTServerKwd = "dhtserver"
63+
routingOptionNoneKwd = "none"
64+
routingOptionCustomKwd = "custom"
65+
routingOptionDefaultKwd = "default"
66+
routingOptionAutoKwd = "auto"
67+
routingOptionAutoClientKwd = "autoclient"
68+
unencryptTransportKwd = "disable-transport-encryption"
69+
unrestrictedAPIAccessKwd = "unrestricted-api"
70+
writableKwd = "writable"
71+
enablePubSubKwd = "enable-pubsub-experiment"
72+
enableIPNSPubSubKwd = "enable-namesys-pubsub"
73+
enableMultiplexKwd = "enable-mplex-experiment"
74+
agentVersionSuffix = "agent-version-suffix"
7475
// apiAddrKwd = "address-api"
7576
// swarmAddrKwd = "address-swarm"
7677
)
@@ -416,6 +417,14 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
416417
cfg.Identity.PeerID,
417418
cfg.Addresses.Swarm,
418419
cfg.Identity.PrivKey,
420+
libp2p.DHTOption,
421+
)
422+
case routingOptionAutoClientKwd:
423+
ncfg.Routing = libp2p.ConstructDefaultRouting(
424+
cfg.Identity.PeerID,
425+
cfg.Addresses.Swarm,
426+
cfg.Identity.PrivKey,
427+
libp2p.DHTClientOption,
419428
)
420429
case routingOptionDHTClientKwd:
421430
ncfg.Routing = libp2p.DHTClientOption

config/routing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
type Routing struct {
1111
// Type sets default daemon routing mode.
1212
//
13-
// Can be one of "auto", "dht", "dhtclient", "dhtserver", "none", or "custom".
13+
// Can be one of "auto", "autoclient", "dht", "dhtclient", "dhtserver", "none", or "custom".
1414
// When unset or set to "auto", DHT and implicit routers are used.
1515
// When "custom" is set, user-provided Routing.Routers is used.
1616
Type *OptionalString `json:",omitempty"`

core/node/libp2p/routingopt.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func init() {
4040
}
4141

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

61-
// Run the default DHT routing (same as Routing.Type = "dht")
62-
dhtRouting, err := DHTOption(ctx, host, dstore, validator, bootstrapPeers...)
61+
dhtRouting, err := routingOpt(ctx, host, dstore, validator, bootstrapPeers...)
6362
if err != nil {
6463
return nil, err
6564
}

docs/config.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1349,11 +1349,13 @@ Contains options for content, peer, and IPNS routing mechanisms.
13491349

13501350
### `Routing.Type`
13511351

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

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

1357+
* If set to "autoclient", your node will behave as in "auto" but without running a DHT server.
1358+
13571359
* If set to "none", your node will use _no_ routing system. You'll have to
13581360
explicitly connect to peers that have the content you're looking for.
13591361

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

1382-
When `Routing.Type` is set to `auto` your node will accelerate some types of routing
1384+
When `Routing.Type` is set to `auto` or `autoclient` your node will accelerate some types of routing
13831385
by leveraging HTTP endpoints compatible with [IPIP-337](https://github.com/ipfs/specs/pull/337)
13841386
in addition to the IPFS DHT.
13851387
By default, an instance of [IPNI](https://github.com/ipni/specs/blob/main/IPNI.md#readme)

test/cli/delegated_routing_http_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestHTTPDelegatedRouting(t *testing.T) {
9494
}))
9595
t.Cleanup(server.Close)
9696

97-
node.IPFS("config", "Routing.Type", "--json", `"custom"`)
97+
node.IPFS("config", "Routing.Type", "custom")
9898
node.IPFS("config", "Routing.Routers.TestDelegatedRouter", "--json", ToJSONStr(JSONObj{
9999
"Type": "http",
100100
"Parameters": JSONObj{

test/cli/dht_autoclient_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cli
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/ipfs/kubo/test/cli/harness"
8+
"github.com/ipfs/kubo/test/cli/testutils"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestDHTAutoclient(t *testing.T) {
13+
t.Parallel()
14+
nodes := harness.NewT(t).NewNodes(10).Init()
15+
harness.Nodes(nodes[8:]).ForEachPar(func(node *harness.Node) {
16+
node.IPFS("config", "Routing.Type", "autoclient")
17+
})
18+
nodes.StartDaemons().Connect()
19+
20+
t.Run("file added on node in client mode is retrievable from node in client mode", func(t *testing.T) {
21+
t.Parallel()
22+
randomBytes := testutils.RandomBytes(1000)
23+
hash := nodes[8].IPFSAdd(bytes.NewReader(randomBytes))
24+
25+
res := nodes[9].IPFS("cat", hash)
26+
assert.Equal(t, randomBytes, []byte(res.Stdout.Trimmed()))
27+
})
28+
29+
t.Run("file added on node in server mode is retrievable from all nodes", func(t *testing.T) {
30+
t.Parallel()
31+
randomBytes := testutils.RandomBytes(1000)
32+
hash := nodes[0].IPFSAdd(bytes.NewReader(randomBytes))
33+
34+
for i := 0; i < 10; i++ {
35+
res := nodes[i].IPFS("cat", hash)
36+
assert.Equal(t, randomBytes, []byte(res.Stdout.Trimmed()))
37+
}
38+
})
39+
}

0 commit comments

Comments
 (0)