diff --git a/gateway/blocks_backend.go b/gateway/blocks_backend.go index 7f58978b0f..edad729322 100644 --- a/gateway/blocks_backend.go +++ b/gateway/blocks_backend.go @@ -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 } @@ -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 } diff --git a/namesys/ipns_resolver.go b/namesys/ipns_resolver.go index 2b4597db26..9cd85fc135 100644 --- a/namesys/ipns_resolver.go +++ b/namesys/ipns_resolver.go @@ -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 { diff --git a/namesys/namesys.go b/namesys/namesys.go index e51cc7e8d0..afac2bdc4c 100644 --- a/namesys/namesys.go +++ b/namesys/namesys.go @@ -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) @@ -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}) @@ -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()) +} diff --git a/namesys/namesys_test.go b/namesys/namesys_test.go index 48eb77813a..2a01681bb6 100644 --- a/namesys/namesys_test.go +++ b/namesys/namesys_test.go @@ -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()) }