Skip to content

Commit

Permalink
feat: add advanced V1ProtocolOverride option to be used by legacy net…
Browse files Browse the repository at this point in the history
…works
  • Loading branch information
aschmahmann committed Sep 2, 2020
1 parent f1f1e3f commit 9304f55
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
5 changes: 5 additions & 0 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
var protocols, serverProtocols []protocol.ID

v1proto := cfg.protocolPrefix + kad1

if cfg.v1ProtocolOverride != "" {
v1proto = cfg.v1ProtocolOverride
}

protocols = []protocol.ID{v1proto}
serverProtocols = []protocol.ID{v1proto}

Expand Down
39 changes: 26 additions & 13 deletions dht_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@ const DefaultPrefix protocol.ID = "/ipfs"

// Options is a structure containing all the options that can be used when constructing a DHT.
type config struct {
datastore ds.Batching
validator record.Validator
validatorChanged bool // if true implies that the validator has been changed and that defaults should not be used
mode ModeOpt
protocolPrefix protocol.ID
bucketSize int
concurrency int
resiliency int
maxRecordAge time.Duration
enableProviders bool
enableValues bool
providersOptions []providers.Option
queryPeerFilter QueryFilterFunc
datastore ds.Batching
validator record.Validator
validatorChanged bool // if true implies that the validator has been changed and that defaults should not be used
mode ModeOpt
protocolPrefix protocol.ID
v1ProtocolOverride protocol.ID
bucketSize int
concurrency int
resiliency int
maxRecordAge time.Duration
enableProviders bool
enableValues bool
providersOptions []providers.Option
queryPeerFilter QueryFilterFunc

routingTable struct {
refreshQueryTimeout time.Duration
Expand Down Expand Up @@ -275,6 +276,18 @@ func ProtocolExtension(ext protocol.ID) Option {
}
}

// V1ProtocolOverride overrides the protocolID used for /kad/1.0.0 with another. This is an
// advanced feature, and should only be used to handle legacy networks that have not been
// using protocolIDs of the form /app/kad/1.0.0.
//
// This option will override and ignore the ProtocolPrefix and ProtocolExtension options
func V1ProtocolOverride(proto protocol.ID) Option {
return func(c *config) error {
c.v1ProtocolOverride = proto
return nil
}
}

// BucketSize configures the bucket size (k in the Kademlia paper) of the routing table.
//
// The default value is 20.
Expand Down
31 changes: 31 additions & 0 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,37 @@ func TestInvalidKeys(t *testing.T) {
}
}

func TestV1ProtocolOverride(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

d1 := setupDHT(ctx, t, false, V1ProtocolOverride("/myproto") )
d2 := setupDHT(ctx, t, false, V1ProtocolOverride("/myproto") )
d3 := setupDHT(ctx, t, false, V1ProtocolOverride("/myproto2"))
d4 := setupDHT(ctx, t, false)

dhts := []*IpfsDHT{d1,d2,d3,d4}

for i, dout := range dhts {
for _, din := range dhts[i+1:] {
connectNoSync(t, ctx, dout, din)
}
}

wait(t, ctx, d1, d2)
wait(t, ctx, d2, d1)

time.Sleep(time.Second)

if d1.RoutingTable().Size() != 1 || d2.routingTable.Size() != 1 {
t.Fatal("should have one peer in the routing table")
}

if d3.RoutingTable().Size() > 0 || d4.RoutingTable().Size() > 0{
t.Fatal("should have an empty routing table")
}
}

func TestRoutingFilter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down

0 comments on commit 9304f55

Please sign in to comment.