Skip to content

Commit

Permalink
store IPNS records *outside* of the DHT
Browse files Browse the repository at this point in the history
fixes #4749

License: MIT
Signed-off-by: Steven Allen <[email protected]>
  • Loading branch information
Stebalien committed Jun 1, 2018
1 parent 77a7b39 commit d68a748
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 174 deletions.
2 changes: 1 addition & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func (n *IpfsNode) setupIpnsRepublisher() error {
return err
}

n.IpnsRepub = ipnsrp.NewRepublisher(n.Routing, n.Repo.Datastore(), n.PrivateKey, n.Repo.Keystore())
n.IpnsRepub = ipnsrp.NewRepublisher(n.Namesys, n.Repo.Datastore(), n.PrivateKey, n.Repo.Keystore())

if cfg.Ipns.RepublishPeriod != "" {
d, err := time.ParseDuration(cfg.Ipns.RepublishPeriod)
Expand Down
111 changes: 30 additions & 81 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package namesys
import (
"context"
"strings"
"sync"
"time"

opts "github.com/ipfs/go-ipfs/namesys/opts"
Expand Down Expand Up @@ -37,10 +36,10 @@ func NewNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSys
resolvers: map[string]resolver{
"dns": NewDNSResolver(),
"proquint": new(ProquintResolver),
"dht": NewRoutingResolver(r, cachesize),
"ipns": NewRoutingResolver(r, cachesize),
},
publishers: map[string]Publisher{
"dht": NewRoutingPublisher(r, ds),
"ipns": NewRoutingPublisher(r, ds),
},
}
}
Expand Down Expand Up @@ -71,66 +70,32 @@ func (ns *mpns) resolveOnce(ctx context.Context, name string, options *opts.Reso
return "", ErrResolveFailed
}

makePath := func(p path.Path) (path.Path, error) {
if len(segments) > 3 {
return path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
} else {
return p, nil
}
}

// Resolver selection:
// 1. if it is a multihash resolve through "pubsub" (if available),
// with fallback to "dht"
// 1. if it is a multihash resolve through "ipns".
// 2. if it is a domain name, resolve through "dns"
// 3. otherwise resolve through the "proquint" resolver
key := segments[2]

_, err := mh.FromB58String(key)
if err == nil {
res, ok := ns.resolvers["pubsub"]
if ok {
p, err := res.resolveOnce(ctx, key, options)
if err == nil {
return makePath(p)
}
}

res, ok = ns.resolvers["dht"]
if ok {
p, err := res.resolveOnce(ctx, key, options)
if err == nil {
return makePath(p)
}
}

return "", ErrResolveFailed
resName := "proquint"
if _, err := mh.FromB58String(key); err == nil {
resName = "ipns"
} else if isd.IsDomain(key) {
resName = "dns"
}

if isd.IsDomain(key) {
res, ok := ns.resolvers["dns"]
if ok {
p, err := res.resolveOnce(ctx, key, options)
if err == nil {
return makePath(p)
}
}

res, ok := ns.resolvers[resName]
if !ok {
log.Debugf("no resolver found for %s", name)
return "", ErrResolveFailed
}

res, ok := ns.resolvers["proquint"]
if ok {
p, err := res.resolveOnce(ctx, key, options)
if err == nil {
return makePath(p)
}

p, err := res.resolveOnce(ctx, key, options)
if err != nil {
return "", ErrResolveFailed
}

log.Debugf("no resolver found for %s", name)
return "", ErrResolveFailed
if len(segments) > 3 {
return path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
}
return p, nil
}

// Publish implements Publisher
Expand All @@ -139,39 +104,23 @@ func (ns *mpns) Publish(ctx context.Context, name ci.PrivKey, value path.Path) e
}

func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.Path, eol time.Time) error {
var dhtErr error

wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
dhtErr = ns.publishers["dht"].PublishWithEOL(ctx, name, value, eol)
if dhtErr == nil {
ns.addToDHTCache(name, value, eol)
}
wg.Done()
}()

pub, ok := ns.publishers["pubsub"]
if ok {
wg.Add(1)
go func() {
err := pub.PublishWithEOL(ctx, name, value, eol)
if err != nil {
log.Warningf("error publishing %s with pubsub: %s", name, err.Error())
}
wg.Done()
}()
}

wg.Wait()
return dhtErr
pub, ok := ns.publishers["ipns"]
if !ok {
return ErrPublishFailed
}
if err := pub.PublishWithEOL(ctx, name, value, eol); err != nil {
return err
}
ns.addToIpnsCache(name, value, eol)
return nil

}

func (ns *mpns) addToDHTCache(key ci.PrivKey, value path.Path, eol time.Time) {
rr, ok := ns.resolvers["dht"].(*routingResolver)
func (ns *mpns) addToIpnsCache(key ci.PrivKey, value path.Path, eol time.Time) {
rr, ok := ns.resolvers["ipns"].(*routingResolver)
if !ok {
// should never happen, purely for sanity
log.Panicf("unexpected type %T as DHT resolver.", ns.resolvers["dht"])
log.Panicf("unexpected type %T as DHT resolver.", ns.resolvers["ipns"])
}
if rr.cache == nil {
// resolver has no caching
Expand Down
4 changes: 2 additions & 2 deletions namesys/namesys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func mockResolverTwo() *mockResolver {
func TestNamesysResolution(t *testing.T) {
r := &mpns{
resolvers: map[string]resolver{
"dht": mockResolverOne(),
"dns": mockResolverTwo(),
"ipns": mockResolverOne(),
"dns": mockResolverTwo(),
},
}

Expand Down
Loading

0 comments on commit d68a748

Please sign in to comment.