Skip to content

Commit

Permalink
namesys: add entry to DHT cache after publish
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
  • Loading branch information
Kubuxu committed Dec 13, 2016
1 parent 4cb236c commit 9fe9d9e
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package namesys

import (
"context"
"strings"
"time"

context "context"
path "github.com/ipfs/go-ipfs/path"

ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
routing "gx/ipfs/QmbkGVaN9W6RYJK4Ws5FvMKXKDqdRQ5snhtaa92qP6L8eU/go-libp2p-routing"
peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
ci "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto"
)

Expand Down Expand Up @@ -87,9 +89,44 @@ func (ns *mpns) resolveOnce(ctx context.Context, name string) (path.Path, error)

// Publish implements Publisher
func (ns *mpns) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
return ns.publishers["/ipns/"].Publish(ctx, name, value)
err := ns.publishers["/ipns/"].Publish(ctx, name, value)
if err != nil {
return err
}
ns.addToDHTCache(name, value, time.Now().Add(time.Hour*24))
return nil
}

func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.Path, eol time.Time) error {
err := ns.publishers["/ipns/"].PublishWithEOL(ctx, name, value, eol)
if err != nil {
return err
}
ns.addToDHTCache(name, value, eol)
return nil
}

func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, val path.Path, eol time.Time) error {
return ns.publishers["/ipns/"].PublishWithEOL(ctx, name, val, eol)
func (ns *mpns) addToDHTCache(key ci.PrivKey, value path.Path, eol time.Time) {
var err error
value, err = path.ParsePath(value.String())
if err != nil {
log.Error("could not parse path")
return
}

name, err := peer.IDFromPrivateKey(key)
if err != nil {
log.Error("while adding to cache, could not get peerid from private key")
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"])
}
rr.cache.Add(name.Pretty(), cacheEntry{
val: value,
eol: eol,
})
}

0 comments on commit 9fe9d9e

Please sign in to comment.