From 9304f5575ea4c578d1316c2cf695a06b65c88dbe Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Fri, 28 Aug 2020 15:31:13 -0400 Subject: [PATCH] feat: add advanced V1ProtocolOverride option to be used by legacy networks --- dht.go | 5 +++++ dht_options.go | 39 ++++++++++++++++++++++++++------------- dht_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/dht.go b/dht.go index a662d8ed8..b599cb6bd 100644 --- a/dht.go +++ b/dht.go @@ -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} diff --git a/dht_options.go b/dht_options.go index 7e2ac1b76..c4b08c03a 100644 --- a/dht_options.go +++ b/dht_options.go @@ -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 @@ -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. diff --git a/dht_test.go b/dht_test.go index 42894a52b..a20f5c90c 100644 --- a/dht_test.go +++ b/dht_test.go @@ -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()