namesys: add entry to DHT cache after publish
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Dec 13, 2016 at 18:51 UTC
9fe9d9eb537058cea6673c11c1527d149c5b8efb
1 file changed
+41
-4
namesys/namesys.go
+41
-4
@@ -1,13 +1,15 @@
1
package namesys
2
3
import (
4
+ "context"
5
"strings"
6
"time"
7
7
- context "context"
8
path "github.com/ipfs/go-ipfs/path"
9
+
10
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
11
routing "gx/ipfs/QmbkGVaN9W6RYJK4Ws5FvMKXKDqdRQ5snhtaa92qP6L8eU/go-libp2p-routing"
12
+ peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
13
ci "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto"
14
)
15
@@ -87,9 +89,44 @@ func (ns *mpns) resolveOnce(ctx context.Context, name string) (path.Path, error)
89
90
// Publish implements Publisher
91
func (ns *mpns) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
90
- return ns.publishers["/ipns/"].Publish(ctx, name, value)
92
+ err := ns.publishers["/ipns/"].Publish(ctx, name, value)
93
+ if err != nil {
94
+ return err
95
+ }
96
+ ns.addToDHTCache(name, value, time.Now().Add(time.Hour*24))
97
+ return nil
98
+}
99
+
100
+func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.Path, eol time.Time) error {
101
+ err := ns.publishers["/ipns/"].PublishWithEOL(ctx, name, value, eol)
102
+ if err != nil {
103
+ return err
104
+ }
105
+ ns.addToDHTCache(name, value, eol)
106
+ return nil
107
}
108
93
-func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, val path.Path, eol time.Time) error {
94
- return ns.publishers["/ipns/"].PublishWithEOL(ctx, name, val, eol)
109
+func (ns *mpns) addToDHTCache(key ci.PrivKey, value path.Path, eol time.Time) {
110
+ var err error
111
+ value, err = path.ParsePath(value.String())
112
+ if err != nil {
113
+ log.Error("could not parse path")
114
+ return
115
+ }
116
+
117
+ name, err := peer.IDFromPrivateKey(key)
118
+ if err != nil {
119
+ log.Error("while adding to cache, could not get peerid from private key")
120
+ return
121
+ }
122
+
123
+ rr, ok := ns.resolvers["dht"].(*routingResolver)
124
+ if !ok {
125
+ // should never happen, purely for sanity
126
+ log.Panicf("unexpected type %T as DHT resolver.", ns.resolvers["dht"])
127
+ }
128
+ rr.cache.Add(name.Pretty(), cacheEntry{
129
+ val: value,
130
+ eol: eol,
131
+ })
132
}