2018-05-15 19:00:16 +01:00
|
|
|
package dhtopts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
ds "github.com/ipfs/go-datastore"
|
|
|
|
dssync "github.com/ipfs/go-datastore/sync"
|
2019-05-26 23:33:15 +01:00
|
|
|
protocol "github.com/libp2p/go-libp2p-core/protocol"
|
2018-05-15 19:00:16 +01:00
|
|
|
record "github.com/libp2p/go-libp2p-record"
|
|
|
|
)
|
|
|
|
|
2019-04-15 13:20:48 +10:00
|
|
|
// Deprecated: The old format did not support more than one message per stream, and is not supported
|
|
|
|
// or relevant with stream pooling. ProtocolDHT should be used instead.
|
|
|
|
const ProtocolDHTOld protocol.ID = "/ipfs/dht"
|
|
|
|
|
|
|
|
var (
|
|
|
|
ProtocolDHT protocol.ID = "/ipfs/kad/1.0.0"
|
|
|
|
DefaultProtocols = []protocol.ID{ProtocolDHT}
|
|
|
|
)
|
2018-06-01 13:45:34 -07:00
|
|
|
|
2018-05-15 19:00:16 +01:00
|
|
|
// Options is a structure containing all the options that can be used when constructing a DHT.
|
|
|
|
type Options struct {
|
|
|
|
Datastore ds.Batching
|
|
|
|
Validator record.Validator
|
|
|
|
Client bool
|
2018-06-01 13:45:34 -07:00
|
|
|
Protocols []protocol.ID
|
2018-05-15 19:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply applies the given options to this Option
|
|
|
|
func (o *Options) Apply(opts ...Option) error {
|
|
|
|
for i, opt := range opts {
|
|
|
|
if err := opt(o); err != nil {
|
|
|
|
return fmt.Errorf("dht option %d failed: %s", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option DHT option type.
|
|
|
|
type Option func(*Options) error
|
|
|
|
|
|
|
|
// Defaults are the default DHT options. This option will be automatically
|
|
|
|
// prepended to any options you pass to the DHT constructor.
|
|
|
|
var Defaults = func(o *Options) error {
|
|
|
|
o.Validator = record.NamespacedValidator{
|
|
|
|
"pk": record.PublicKeyValidator{},
|
|
|
|
}
|
|
|
|
o.Datastore = dssync.MutexWrap(ds.NewMapDatastore())
|
2018-06-01 13:45:34 -07:00
|
|
|
o.Protocols = DefaultProtocols
|
2018-05-15 19:00:16 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Datastore configures the DHT to use the specified datastore.
|
|
|
|
//
|
|
|
|
// Defaults to an in-memory (temporary) map.
|
|
|
|
func Datastore(ds ds.Batching) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Datastore = ds
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client configures whether or not the DHT operates in client-only mode.
|
|
|
|
//
|
|
|
|
// Defaults to false.
|
|
|
|
func Client(only bool) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Client = only
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validator configures the DHT to use the specified validator.
|
|
|
|
//
|
|
|
|
// Defaults to a namespaced validator that can only validate public keys.
|
|
|
|
func Validator(v record.Validator) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Validator = v
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NamespacedValidator adds a validator namespaced under `ns`. This option fails
|
|
|
|
// if the DHT is not using a `record.NamespacedValidator` as it's validator (it
|
|
|
|
// uses one by default but this can be overridden with the `Validator` option).
|
|
|
|
//
|
|
|
|
// Example: Given a validator registered as `NamespacedValidator("ipns",
|
|
|
|
// myValidator)`, all records with keys starting with `/ipns/` will be validated
|
|
|
|
// with `myValidator`.
|
|
|
|
func NamespacedValidator(ns string, v record.Validator) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
nsval, ok := o.Validator.(record.NamespacedValidator)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can only add namespaced validators to a NamespacedValidator")
|
|
|
|
}
|
|
|
|
nsval[ns] = v
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 13:45:34 -07:00
|
|
|
|
|
|
|
// Protocols sets the protocols for the DHT
|
|
|
|
//
|
|
|
|
// Defaults to dht.DefaultProtocols
|
2018-06-01 15:54:17 -07:00
|
|
|
func Protocols(protocols ...protocol.ID) Option {
|
2018-06-01 13:45:34 -07:00
|
|
|
return func(o *Options) error {
|
|
|
|
o.Protocols = protocols
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|