Skip to content

Commit 9897fbe

Browse files
committed
Namesys cache uses IPNS keys with their binary representation instead of string representation to avoid encoding mismatches
1 parent c08b640 commit 9897fbe

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

namesys/namesys.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,13 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
122122

123123
key := segments[2]
124124

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-
136125
// Resolver selection:
137126
// 1. if it is a PeerID/CID/multihash resolve through "ipns".
138127
// 2. if it is a domain name, resolve through "dns"
139128
// 3. otherwise resolve through the "proquint" resolver
140129

141130
var res resolver
142-
_, err := peer.Decode(key)
131+
ipnsKey, err := peer.Decode(key)
143132

144133
// CIDs in IPNS are expected to have libp2p-key multicodec
145134
// 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.
155144
}
156145
}
157146

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+
158163
if err == nil {
159164
res = ns.ipnsResolver
160165
} else if isd.IsDomain(key) {
@@ -172,7 +177,7 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
172177
case res, ok := <-resCh:
173178
if !ok {
174179
if best != (onceResult{}) {
175-
ns.cacheSet(key, best.value, best.ttl)
180+
ns.cacheSet(cacheKey, best.value, best.ttl)
176181
}
177182
return
178183
}
@@ -218,7 +223,7 @@ func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.
218223
if err := ns.ipnsPublisher.PublishWithEOL(ctx, name, value, eol); err != nil {
219224
// Invalidate the cache. Publishing may _partially_ succeed but
220225
// still return an error.
221-
ns.cacheInvalidate(peer.Encode(id))
226+
ns.cacheInvalidate(string(id))
222227
return err
223228
}
224229
ttl := DefaultResolverCacheTTL
@@ -228,6 +233,6 @@ func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.
228233
if ttEol := time.Until(eol); ttEol < ttl {
229234
ttl = ttEol
230235
}
231-
ns.cacheSet(peer.Encode(id), value, ttl)
236+
ns.cacheSet(string(id), value, ttl)
232237
return nil
233238
}

0 commit comments

Comments
 (0)