From 0864c8e20e67db9f897032b013894296c5c8f4b7 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 15 Dec 2016 02:16:40 +0100 Subject: [PATCH 1/2] namesys: fix case where there is no cache License: MIT Signed-off-by: Jakub Sztandera --- namesys/namesys.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/namesys/namesys.go b/namesys/namesys.go index 3e0456ce2..bf1c68967 100644 --- a/namesys/namesys.go +++ b/namesys/namesys.go @@ -107,6 +107,16 @@ func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path. } func (ns *mpns) addToDHTCache(key ci.PrivKey, value path.Path, eol time.Time) { + rr, ok := ns.resolvers["dht"].(*routingResolver) + if !ok { + // should never happen, purely for sanity + log.Panicf("unexpected type %T as DHT resolver.", ns.resolvers["dht"]) + } + if rr.cache == nil { + // resolver has no caching + return + } + var err error value, err = path.ParsePath(value.String()) if err != nil { @@ -120,11 +130,6 @@ func (ns *mpns) addToDHTCache(key ci.PrivKey, value path.Path, eol time.Time) { return } - rr, ok := ns.resolvers["dht"].(*routingResolver) - if !ok { - // should never happen, purely for sanity - log.Panicf("unexpected type %T as DHT resolver.", ns.resolvers["dht"]) - } if time.Now().Add(DefaultResolverCacheTTL).Before(eol) { eol = time.Now().Add(DefaultResolverCacheTTL) } From c1d5a60b6665ca4dc2cee623b0bd5955e76e5ae6 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 15 Dec 2016 22:05:23 +0100 Subject: [PATCH 2/2] namesys: add test for publish with cache size 0 License: MIT Signed-off-by: Jakub Sztandera --- namesys/namesys_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/namesys/namesys_test.go b/namesys/namesys_test.go index b2f92deb0..7d5c637b5 100644 --- a/namesys/namesys_test.go +++ b/namesys/namesys_test.go @@ -7,6 +7,11 @@ import ( context "context" path "github.com/ipfs/go-ipfs/path" + offroute "github.com/ipfs/go-ipfs/routing/offline" + "github.com/ipfs/go-ipfs/unixfs" + + ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore" + ci "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto" ) type mockResolver struct { @@ -69,3 +74,19 @@ func TestNamesysResolution(t *testing.T) { testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", 2, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion) testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", 3, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", ErrResolveRecursion) } + +func TestPublishWithCache0(t *testing.T) { + dst := ds.NewMapDatastore() + priv, _, err := ci.GenerateKeyPair(ci.RSA, 1024) + if err != nil { + t.Fatal(err) + } + routing := offroute.NewOfflineRouter(dst, priv) + + nsys := NewNameSystem(routing, dst, 0) + p, err := path.ParsePath(unixfs.EmptyDirNode().Cid().String()) + if err != nil { + t.Fatal(err) + } + nsys.Publish(context.Background(), priv, p) +}