Skip to content

Commit

Permalink
error during initialization if misusing the default protocolID
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Mar 5, 2020
1 parent 7ce7ac1 commit 4c17e75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,9 @@ func TestProvideDisabled(t *testing.T) {
var (
optsA, optsB []opts.Option
)
optsA = append(optsA, opts.Protocols("/dht/provMaybeDisabled"))
optsB = append(optsB, opts.Protocols("/dht/provMaybeDisabled"))

if !enabledA {
optsA = append(optsA, opts.DisableProviders())
}
Expand Down
18 changes: 17 additions & 1 deletion opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func (o *Options) Apply(opts ...Option) error {
// Option DHT option type.
type Option func(*Options) error

const defaultBucketSize = 20

// 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 {
Expand All @@ -84,7 +86,7 @@ var Defaults = func(o *Options) error {
o.RoutingTable.AutoRefresh = true
o.MaxRecordAge = time.Hour * 36

o.BucketSize = 20
o.BucketSize = defaultBucketSize
o.Concurrency = 3

return nil
Expand All @@ -107,6 +109,20 @@ func UnsetDefaults(o *Options) error {
o.DisjointPaths = o.BucketSize / 2
}

for _, p := range o.Protocols {
if p == ProtocolDHT {
if o.BucketSize != defaultBucketSize {
return fmt.Errorf("protocol %s must use bucket size %d", ProtocolDHT, defaultBucketSize)
}
if !o.EnableProviders {
return fmt.Errorf("protocol %s must have providers enabled", ProtocolDHT)
}
if !o.EnableValues {
return fmt.Errorf("protocol %s must have values enabled", ProtocolDHT)
}
}
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ func TestValuesDisabled(t *testing.T) {
var (
optsA, optsB []dhtopt.Option
)
optsA = append(optsA, dhtopt.Protocols("/dht/valuesMaybeDisabled"))
optsB = append(optsB, dhtopt.Protocols("/dht/valuesMaybeDisabled"))

if !enabledA {
optsA = append(optsA, dhtopt.DisableValues())
}
Expand Down

0 comments on commit 4c17e75

Please sign in to comment.