Namesys cache uses IPNS keys with their binary representation instead of string representation to avoid encoding mismatches
Adin Schmahmann committed
Aug 7, 2020 at 16:49 UTC
a1ae867badcd15527b4cc53ab8c40342247b2016
2 files changed
+21
-16
namesys/namesys.go
+20
-15
@@ -122,24 +122,13 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
122
123
key := segments[2]
124
125
- if p, ok := ns.cacheGet(key); ok {
126
- var err error
127
- if len(segments) > 3 {
128
- p, err = path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
129
- }
130
-
131
- out <- onceResult{value: p, err: err}
132
- close(out)
133
- return out
134
- }
135
-
125
// Resolver selection:
126
// 1. if it is a PeerID/CID/multihash resolve through "ipns".
127
// 2. if it is a domain name, resolve through "dns"
128
// 3. otherwise resolve through the "proquint" resolver
129
130
var res resolver
142
- _, err := peer.Decode(key)
131
+ ipnsKey, err := peer.Decode(key)
132
133
// CIDs in IPNS are expected to have libp2p-key multicodec
134
// We ease the transition by returning a more meaningful error with a valid CID
@@ -155,6 +144,22 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
144
}
145
}
146
147
+ cacheKey := key
148
+ if err == nil {
149
+ cacheKey = string(ipnsKey)
150
+ }
151
+
152
+ if p, ok := ns.cacheGet(cacheKey); ok {
153
+ var err error
154
+ if len(segments) > 3 {
155
+ p, err = path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
156
+ }
157
+
158
+ out <- onceResult{value: p, err: err}
159
+ close(out)
160
+ return out
161
+ }
162
+
163
if err == nil {
164
res = ns.ipnsResolver
165
} else if isd.IsDomain(key) {
@@ -172,7 +177,7 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
177
case res, ok := <-resCh:
178
if !ok {
179
if best != (onceResult{}) {
175
- ns.cacheSet(key, best.value, best.ttl)
180
+ ns.cacheSet(cacheKey, best.value, best.ttl)
181
}
182
return
183
}
@@ -218,7 +223,7 @@ func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.
223
if err := ns.ipnsPublisher.PublishWithEOL(ctx, name, value, eol); err != nil {
224
// Invalidate the cache. Publishing may _partially_ succeed but
225
// still return an error.
221
- ns.cacheInvalidate(peer.Encode(id))
226
+ ns.cacheInvalidate(string(id))
227
return err
228
}
229
ttl := DefaultResolverCacheTTL
@@ -228,6 +233,6 @@ func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.
233
if ttEol := time.Until(eol); ttEol < ttl {
234
ttl = ttEol
235
}
231
- ns.cacheSet(peer.Encode(id), value, ttl)
236
+ ns.cacheSet(string(id), value, ttl)
237
return nil
238
}
namesys/namesys_test.go
+1
-1
@@ -155,7 +155,7 @@ func TestPublishWithTTL(t *testing.T) {
155
if err != nil {
156
t.Fatal(err)
157
}
158
- ientry, ok := nsys.(*mpns).cache.Get(pid.Pretty())
158
+ ientry, ok := nsys.(*mpns).cache.Get(string(pid))
159
if !ok {
160
t.Fatal("cache get failed")
161
}