Skip to content

Commit

Permalink
refactor: remove ResolveIPNS and replace by Resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Sep 8, 2023
1 parent 4e023b4 commit 12705cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 38 deletions.
4 changes: 2 additions & 2 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, time.Duration, error) {
switch p.Namespace() {
case path.IPNSNamespace:
p, ttl, err := namesys.ResolveIPNS(ctx, bb.namesys, p)
p, ttl, err := namesys.Resolve(ctx, bb.namesys, p)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -632,7 +632,7 @@ func (bb *BlocksBackend) ResolvePath(ctx context.Context, path path.ImmutablePat
func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, []string, error) {
var err error
if p.Namespace() == path.IPNSNamespace {
p, _, err = namesys.ResolveIPNS(ctx, bb.namesys, p)
p, _, err = namesys.Resolve(ctx, bb.namesys, p)
if err != nil {
return nil, nil, err
}
Expand Down
33 changes: 0 additions & 33 deletions namesys/ipns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,39 +123,6 @@ func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, nameStr string, opt
return out
}

// ResolveIPNS is an utility that takes a [NameSystem] and a [path.Path] and resolves the IPNS Path.
func ResolveIPNS(ctx context.Context, ns NameSystem, p path.Path) (path.Path, time.Duration, error) {
ctx, span := startSpan(ctx, "ResolveIPNS", trace.WithAttributes(attribute.String("Path", p.String())))
defer span.End()

if p.Namespace() != path.IPNSNamespace {
return p, 0, nil
}

if ns == nil {
return nil, 0, ErrNoNamesys
}

segments := p.Segments()

resolvablePath, err := path.NewPathFromSegments(segments[0], segments[1])
if err != nil {
return nil, 0, err
}

resolvedPath, ttl, err := ns.Resolve(ctx, resolvablePath.String())
if err != nil {
return nil, 0, err
}

p, err = path.Join(resolvedPath, segments[2:]...)
if err != nil {
return nil, 0, err
}

return p, ttl, nil
}

func calculateBestTTL(rec *ipns.Record) (time.Duration, error) {
ttl := DefaultResolverCacheTTL
if recordTTL, err := rec.TTL(); err == nil {
Expand Down
18 changes: 16 additions & 2 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, name string, options Re
if p, ttl, ok := ns.cacheGet(cacheKey); ok {
var err error
if len(segments) > 2 {
p, err = path.Join(p, segments[2])
p, err = path.Join(p, segments[2:]...)
}
span.SetAttributes(attribute.Bool("CacheHit", true))
span.RecordError(err)
Expand Down Expand Up @@ -234,7 +234,7 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, name string, options Re
// Attach rest of the path
p := res.Path
if p != nil && len(segments) > 2 {
p, err = path.Join(p, segments[2])
p, err = path.Join(p, segments[2:]...)
}

emitOnceResult(ctx, out, ResolveResult{Path: p, TTL: res.TTL, Err: res.Err})
Expand Down Expand Up @@ -294,3 +294,17 @@ func (ns *namesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path
ns.cacheSet(cacheKey, value, ttl)
return nil
}

// Resolve is an utility function that takes a [NameSystem] and a [path.Path], and
// returns the result of [NameSystem.Resolve] for the given path. If the given namesys
// is nil, [ErrNoNamesys] is returned.
func Resolve(ctx context.Context, ns NameSystem, p path.Path) (path.Path, time.Duration, error) {
ctx, span := startSpan(ctx, "Resolve", trace.WithAttributes(attribute.String("Path", p.String())))
defer span.End()

if ns == nil {
return nil, 0, ErrNoNamesys
}

return ns.Resolve(ctx, p.String())
}
2 changes: 1 addition & 1 deletion namesys/namesys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestResolveIPNS(t *testing.T) {
inputPath, err := path.NewPath("/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy/a/b/c")
require.NoError(t, err)

res, _, err := ResolveIPNS(context.Background(), ns, inputPath)
res, _, err := Resolve(context.Background(), ns, inputPath)
require.NoError(t, err)
require.Equal(t, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj/a/b/c", res.String())
}
Expand Down

0 comments on commit 12705cb

Please sign in to comment.