Skip to content

Commit 084d574

Browse files
authored
refactor: document and expose Amino DHT defaults (#990)
the intention here is to have public source of truth we can reference rather than hardcoding the same amino defaults everywhere
1 parent 18a758c commit 084d574

File tree

6 files changed

+79
-18
lines changed

6 files changed

+79
-18
lines changed

amino/defaults.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Package amino provides protocol parameters and suggested default values for the [Amino DHT].
2+
//
3+
// [Amino DHT] is an implementation of the Kademlia distributed hash table (DHT) algorithm,
4+
// originally designed for use in IPFS (InterPlanetary File System) network.
5+
// This package defines key constants and protocol identifiers used in the Amino DHT implementation.
6+
//
7+
// [Amino DHT]: https://probelab.io/ipfs/amino/
8+
package amino
9+
10+
import (
11+
"time"
12+
13+
"github.com/libp2p/go-libp2p/core/protocol"
14+
)
15+
16+
const (
17+
// ProtocolPrefix is the base prefix for Amono DHT protocols.
18+
ProtocolPrefix protocol.ID = "/ipfs"
19+
20+
// ProtocolID is the latest protocol identifier for the Amino DHT.
21+
ProtocolID protocol.ID = "/ipfs/kad/1.0.0"
22+
23+
// DefaultBucketSize is the Amino DHT bucket size (k in the Kademlia paper).
24+
// It represents the maximum number of peers stored in each
25+
// k-bucket of the routing table.
26+
DefaultBucketSize = 20
27+
28+
// DefaultConcurrency is the suggested number of concurrent requests (alpha
29+
// in the Kademlia paper) for a given query path in Amino DHT. It
30+
// determines how many parallel lookups are performed during network
31+
// traversal.
32+
DefaultConcurrency = 10
33+
34+
// DefaultResiliency is the suggested number of peers closest to a target
35+
// that must have responded in order for a given query path to complete in
36+
// Amino DHT. This helps ensure reliable results by requiring multiple
37+
// confirmations.
38+
DefaultResiliency = 3
39+
40+
// DefaultProvideValidity is the default time that a Provider Record should
41+
// last on Amino DHT before it needs to be refreshed or removed. This value
42+
// is also known as Provider Record Expiration Interval.
43+
DefaultProvideValidity = 48 * time.Hour
44+
45+
// DefaultProviderAddrTTL is the TTL to keep the multi addresses of
46+
// provider peers around. Those addresses are returned alongside provider.
47+
// After it expires, the returned records will require an extra lookup, to
48+
// find the multiaddress associated with the returned peer id.
49+
DefaultProviderAddrTTL = 24 * time.Hour
50+
)
51+
52+
var (
53+
// Protocols is a slice containing all supported protocol IDs for Amino DHT.
54+
// Currently, it only includes the main ProtocolID, but it's defined as a slice
55+
// to allow for potential future protocol versions or variants.
56+
Protocols = []protocol.ID{ProtocolID}
57+
)

crawler/options.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package crawler
33
import (
44
"time"
55

6+
"github.com/libp2p/go-libp2p-kad-dht/amino"
67
"github.com/libp2p/go-libp2p/core/protocol"
78
)
89

@@ -20,7 +21,7 @@ type options struct {
2021
// defaults are the default crawler options. This option will be automatically
2122
// prepended to any options you pass to the crawler constructor.
2223
var defaults = func(o *options) error {
23-
o.protocols = []protocol.ID{"/ipfs/kad/1.0.0"}
24+
o.protocols = amino.Protocols
2425
o.parallelism = 1000
2526
o.connectTimeout = time.Second * 5
2627
o.perMsgTimeout = time.Second * 5

dht_options.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/libp2p/go-libp2p-kad-dht/amino"
89
dhtcfg "github.com/libp2p/go-libp2p-kad-dht/internal/config"
910
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
1011
"github.com/libp2p/go-libp2p-kad-dht/providers"
@@ -34,7 +35,7 @@ const (
3435
)
3536

3637
// DefaultPrefix is the application specific prefix attached to all DHT protocols by default.
37-
const DefaultPrefix protocol.ID = "/ipfs"
38+
const DefaultPrefix protocol.ID = amino.ProtocolPrefix
3839

3940
type Option = dhtcfg.Option
4041

@@ -136,7 +137,7 @@ func NamespacedValidator(ns string, v record.Validator) Option {
136137
// ProtocolPrefix sets an application specific prefix to be attached to all DHT protocols. For example,
137138
// /myapp/kad/1.0.0 instead of /ipfs/kad/1.0.0. Prefix should be of the form /myapp.
138139
//
139-
// Defaults to dht.DefaultPrefix
140+
// Defaults to amino.ProtocolPrefix
140141
func ProtocolPrefix(prefix protocol.ID) Option {
141142
return func(c *dhtcfg.Config) error {
142143
c.ProtocolPrefix = prefix
@@ -167,7 +168,7 @@ func V1ProtocolOverride(proto protocol.ID) Option {
167168

168169
// BucketSize configures the bucket size (k in the Kademlia paper) of the routing table.
169170
//
170-
// The default value is 20.
171+
// The default value is amino.DefaultBucketSize
171172
func BucketSize(bucketSize int) Option {
172173
return func(c *dhtcfg.Config) error {
173174
c.BucketSize = bucketSize
@@ -177,7 +178,7 @@ func BucketSize(bucketSize int) Option {
177178

178179
// Concurrency configures the number of concurrent requests (alpha in the Kademlia paper) for a given query path.
179180
//
180-
// The default value is 10.
181+
// The default value is amino.DefaultConcurrency
181182
func Concurrency(alpha int) Option {
182183
return func(c *dhtcfg.Config) error {
183184
c.Concurrency = alpha
@@ -188,7 +189,7 @@ func Concurrency(alpha int) Option {
188189
// Resiliency configures the number of peers closest to a target that must have responded in order for a given query
189190
// path to complete.
190191
//
191-
// The default value is 3.
192+
// The default value is amino.DefaultResiliency
192193
func Resiliency(beta int) Option {
193194
return func(c *dhtcfg.Config) error {
194195
c.Resiliency = beta

internal/config/config.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/ipfs/boxo/ipns"
88
ds "github.com/ipfs/go-datastore"
99
dssync "github.com/ipfs/go-datastore/sync"
10+
"github.com/libp2p/go-libp2p-kad-dht/amino"
1011
"github.com/libp2p/go-libp2p-kad-dht/internal/net"
1112
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
1213
"github.com/libp2p/go-libp2p-kad-dht/providers"
@@ -19,9 +20,7 @@ import (
1920
)
2021

2122
// DefaultPrefix is the application specific prefix attached to all DHT protocols by default.
22-
const DefaultPrefix protocol.ID = "/ipfs"
23-
24-
const defaultBucketSize = 20
23+
const DefaultPrefix protocol.ID = amino.ProtocolPrefix
2524

2625
// ModeOpt describes what mode the dht should operate in
2726
type ModeOpt int
@@ -127,9 +126,9 @@ var Defaults = func(o *Config) error {
127126

128127
o.MaxRecordAge = providers.ProvideValidity
129128

130-
o.BucketSize = defaultBucketSize
131-
o.Concurrency = 10
132-
o.Resiliency = 3
129+
o.BucketSize = amino.DefaultBucketSize
130+
o.Concurrency = amino.DefaultConcurrency
131+
o.Resiliency = amino.DefaultResiliency
133132
o.LookupCheckConcurrency = 256
134133

135134
// MAGIC: It makes sense to set it to a multiple of OptProvReturnRatio * BucketSize. We chose a multiple of 4.
@@ -139,11 +138,12 @@ var Defaults = func(o *Config) error {
139138
}
140139

141140
func (c *Config) Validate() error {
141+
// Configuration is validated and enforced only if prefix matches Amino DHT
142142
if c.ProtocolPrefix != DefaultPrefix {
143143
return nil
144144
}
145-
if c.BucketSize != defaultBucketSize {
146-
return fmt.Errorf("protocol prefix %s must use bucket size %d", DefaultPrefix, defaultBucketSize)
145+
if c.BucketSize != amino.DefaultBucketSize {
146+
return fmt.Errorf("protocol prefix %s must use bucket size %d", DefaultPrefix, amino.DefaultBucketSize)
147147
}
148148
if !c.EnableProviders {
149149
return fmt.Errorf("protocol prefix %s must have providers enabled", DefaultPrefix)

protocol.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package dht
22

33
import (
4+
"github.com/libp2p/go-libp2p-kad-dht/amino"
45
"github.com/libp2p/go-libp2p/core/protocol"
56
)
67

78
var (
89
// ProtocolDHT is the default DHT protocol.
9-
ProtocolDHT protocol.ID = "/ipfs/kad/1.0.0"
10+
ProtocolDHT protocol.ID = amino.ProtocolID
1011
// DefaultProtocols spoken by the DHT.
11-
DefaultProtocols = []protocol.ID{ProtocolDHT}
12+
DefaultProtocols = amino.Protocols
1213
)

providers/providers_manager.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ipfs/go-datastore/autobatch"
1515
dsq "github.com/ipfs/go-datastore/query"
1616
logging "github.com/ipfs/go-log/v2"
17+
"github.com/libp2p/go-libp2p-kad-dht/amino"
1718
"github.com/libp2p/go-libp2p-kad-dht/internal"
1819
"github.com/libp2p/go-libp2p/core/peer"
1920
"github.com/libp2p/go-libp2p/core/peerstore"
@@ -30,12 +31,12 @@ const (
3031
// peers around. Those addresses are returned alongside provider. After
3132
// it expires, the returned records will require an extra lookup, to
3233
// find the multiaddress associated with the returned peer id.
33-
ProviderAddrTTL = 24 * time.Hour
34+
ProviderAddrTTL = amino.DefaultProviderAddrTTL
3435
)
3536

3637
// ProvideValidity is the default time that a Provider Record should last on DHT
3738
// This value is also known as Provider Record Expiration Interval.
38-
var ProvideValidity = time.Hour * 48
39+
var ProvideValidity = amino.DefaultProvideValidity
3940
var defaultCleanupInterval = time.Hour
4041
var lruCacheSize = 256
4142
var batchBufferSize = 256

0 commit comments

Comments
 (0)