Skip to content

Commit

Permalink
Merge pull request #3509 from ipfs/fix/namesys/dht-cache-panic
Browse files Browse the repository at this point in the history
namesys: fix case where there is no cache
  • Loading branch information
whyrusleeping authored Dec 16, 2016
2 parents 9e8d108 + c1d5a60 commit f6948bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
15 changes: 10 additions & 5 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
21 changes: 21 additions & 0 deletions namesys/namesys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

0 comments on commit f6948bb

Please sign in to comment.