Skip to content

Commit 57e0764

Browse files
committed
DHT defaults to adding a namespaced pk and ipns validator unless the Validator has been changed, in which case neither of them are added. Simply adding additional NamespacedValidators does not remove the pk or ipns validators
1 parent 0f35633 commit 57e0764

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

dht.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ func New(ctx context.Context, h host.Host, options ...Option) (*IpfsDHT, error)
135135
if err := cfg.apply(append([]Option{defaults}, options...)...); err != nil {
136136
return nil, err
137137
}
138+
if err := cfg.applyFallbacks(h); err != nil {
139+
return nil, err
140+
}
138141

139142
if err := cfg.validate(); err != nil {
140143
return nil, err

dht_options.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@ func (c *config) apply(opts ...Option) error {
7272

7373
// applyFallbacks sets default values that could not be applied during config creation since they are dependent
7474
// on other configuration parameters (e.g. optA is by default 2x optB) and/or on the Host
75-
func (c *config) applyFallbacks(h host.Host) {
75+
func (c *config) applyFallbacks(h host.Host) error {
7676
if !c.validatorChanged {
77-
c.validator = record.NamespacedValidator{
78-
"pk": record.PublicKeyValidator{},
79-
"ipns": ipns.Validator{KeyBook: h.Peerstore()},
77+
nsval, ok := c.validator.(record.NamespacedValidator)
78+
if ok {
79+
nsval["pk"] = record.PublicKeyValidator{}
80+
nsval["ipns"] = ipns.Validator{KeyBook: h.Peerstore()}
81+
} else {
82+
return fmt.Errorf("the default validator was changed without being marked as changed")
8083
}
8184
}
85+
return nil
8286
}
8387

8488
// Option DHT option type.
@@ -89,6 +93,7 @@ const defaultBucketSize = 20
8993
// defaults are the default DHT options. This option will be automatically
9094
// prepended to any options you pass to the DHT constructor.
9195
var defaults = func(o *config) error {
96+
o.validator = record.NamespacedValidator{}
9297
o.datastore = dssync.MutexWrap(ds.NewMapDatastore())
9398
o.protocolPrefix = DefaultPrefix
9499
o.enableProviders = true
@@ -186,7 +191,7 @@ func Mode(m ModeOpt) Option {
186191
// Validator configures the DHT to use the specified validator.
187192
//
188193
// Defaults to a namespaced validator that can validate both public key (under the "pk"
189-
// namespaced) and IPNS records (under the "ipns" namespace). Setting the validator
194+
// namespace) and IPNS records (under the "ipns" namespace). Setting the validator
190195
// implies that the user wants to control the validators and therefore the default
191196
// public key and IPNS validators will not be added.
192197
func Validator(v record.Validator) Option {
@@ -198,32 +203,32 @@ func Validator(v record.Validator) Option {
198203
}
199204

200205
// NamespacedValidator adds a validator namespaced under `ns`. This option fails
201-
// if the DHT is not using a `record.NamespacedValidator` as its validator. If
202-
// the validator has yet to be modified (e.g. by the `Validator` option) then
203-
// a NamespacedValidator will be created implicitly. Adding a namespaced
204-
// validator implies that the user wants to control the validators and therefore
205-
// the default public key and IPNS validators will not be added.
206+
// if the DHT is not using a `record.NamespacedValidator` as its validator (it
207+
// uses one by default but this can be overridden with the `Validator` option).
208+
// Adding a namespaced validator without changing the `Validator` will result in
209+
// adding a new validator in addition to the default public key and IPNS validators.
210+
// The "pk" and "ipns" namespaces cannot be overridden here unless a new `Validator`
211+
// has been set first.
206212
//
207213
// Example: Given a validator registered as `NamespacedValidator("ipns",
208214
// myValidator)`, all records with keys starting with `/ipns/` will be validated
209215
// with `myValidator`.
210216
func NamespacedValidator(ns string, v record.Validator) Option {
211217
return func(c *config) error {
212-
// while the validator will not change if there's an error setting the validator that is only possible
213-
// if the validator has already been changed
214-
c.validatorChanged = true
215-
216-
if c.validator == nil {
217-
c.validator = record.NamespacedValidator{
218-
ns: v,
218+
if !c.validatorChanged {
219+
if ns == "pk" {
220+
return fmt.Errorf("cannot override the pk namespace without first changing the Validator")
219221
}
220-
} else {
221-
nsval, ok := c.validator.(record.NamespacedValidator)
222-
if !ok {
223-
return fmt.Errorf("can only add namespaced validators to a NamespacedValidator")
222+
if ns == "ipns" {
223+
return fmt.Errorf("cannot override the ipns namespace without first changing the Validator")
224224
}
225-
nsval[ns] = v
226225
}
226+
227+
nsval, ok := c.validator.(record.NamespacedValidator)
228+
if !ok {
229+
return fmt.Errorf("can only add namespaced validators to a NamespacedValidator")
230+
}
231+
nsval[ns] = v
227232
return nil
228233
}
229234
}

0 commit comments

Comments
 (0)