Skip to content

Commit

Permalink
refactor!: namesys package
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 2, 2023
1 parent 546e33d commit 42af4f8
Show file tree
Hide file tree
Showing 40 changed files with 1,567 additions and 1,611 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The following emojis are used to highlight certain changes:

### Added

* The gateway now sets a `Cache-Control` header for requests under the `/ipns/` namespace
if the TTL for the corresponding IPNS Records or DNSLink entities is known.

### Changed

* 🛠 The `path` package has been massively refactored. With this refactor, we have
Expand All @@ -24,6 +27,18 @@ The following emojis are used to highlight certain changes:
for more details on how to use the new package.
* 🛠 The signature of `CoreAPI.ResolvePath` in `coreiface` has changed to now return
the remainder segments as a second return value, matching the signature of `resolver.ResolveToLastNode`.
* 🛠 The `namesys` package has been refactored. The following are the largest modifications:
* The options in `coreiface/options/namesys` have been moved to `namesys` and their names
have been made more consistent.
* Many of the exported structs and functions have been renamed in order to be consistent with
the remaining packages.
* `namesys.Resolver.Resolve` now returns a TTL, in addition to the resolved path. If the
TTL is unknown, 0 is returned. `IPNSResolver` is able to resolve a TTL, while `DNSResolver`
is not.
* `namesys/resolver.ResolveIPNS` has been moved to `namesys.ResolveIPNS` and now returns a TTL
in addition to the resolved path.
* 🛠 The `gateway`'s `IPFSBackend.ResolveMutable` is now expected to return a TTL in addition to
the resolved path. If the TTL is unknown, 0 should be returned.

### Removed

Expand Down
6 changes: 3 additions & 3 deletions coreiface/options/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package options
import (
"time"

ropts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/namesys"
)

const (
Expand All @@ -21,7 +21,7 @@ type NamePublishSettings struct {
type NameResolveSettings struct {
Cache bool

ResolveOpts []ropts.ResolveOpt
ResolveOpts []namesys.ResolveOption
}

type (
Expand Down Expand Up @@ -123,7 +123,7 @@ func (nameOpts) Cache(cache bool) NameResolveOption {
}
}

func (nameOpts) ResolveOption(opt ropts.ResolveOpt) NameResolveOption {
func (nameOpts) ResolveOption(opt namesys.ResolveOption) NameResolveOption {
return func(settings *NameResolveSettings) error {
settings.ResolveOpts = append(settings.ResolveOpts, opt)
return nil
Expand Down
131 changes: 0 additions & 131 deletions coreiface/options/namesys/opts.go

This file was deleted.

46 changes: 24 additions & 22 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"io"
"net/http"
"strings"
"time"

"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/fetcher"
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/ipld/merkledag"
ufile "github.com/ipfs/boxo/ipld/unixfs/file"
uio "github.com/ipfs/boxo/ipld/unixfs/io"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/namesys/resolve"
"github.com/ipfs/boxo/path"
"github.com/ipfs/boxo/path/resolver"
blocks "github.com/ipfs/go-block-format"
Expand All @@ -39,7 +39,6 @@ import (
"github.com/ipld/go-ipld-prime/traversal/selector"
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
mc "github.com/multiformats/go-multicodec"

Expand Down Expand Up @@ -562,18 +561,23 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
return pathRoots, lastPath, remainder, nil
}

func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, time.Duration, time.Time, error) {
switch p.Namespace() {
case path.IPNSNamespace:
p, err := resolve.ResolveIPNS(ctx, bb.namesys, p)
res, err := namesys.Resolve(ctx, bb.namesys, p)
if err != nil {
return nil, err
return nil, 0, time.Time{}, err
}
ip, err := path.NewImmutablePath(res.Path)
if err != nil {
return nil, 0, time.Time{}, err

Check warning on line 573 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L573

Added line #L573 was not covered by tests
}
return path.NewImmutablePath(p)
return ip, res.TTL, res.LastMod, nil
case path.IPFSNamespace:
return path.NewImmutablePath(p)
ip, err := path.NewImmutablePath(p)
return ip, 0, time.Time{}, err

Check warning on line 578 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L576-L578

Added lines #L576 - L578 were not covered by tests
default:
return nil, NewErrorStatusCode(fmt.Errorf("unsupported path namespace: %s", p.Namespace()), http.StatusNotImplemented)
return nil, 0, time.Time{}, NewErrorStatusCode(fmt.Errorf("unsupported path namespace: %s", p.Namespace()), http.StatusNotImplemented)

Check warning on line 580 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L580

Added line #L580 was not covered by tests
}
}

Expand All @@ -582,28 +586,25 @@ func (bb *BlocksBackend) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte,
return nil, NewErrorStatusCode(errors.New("IPNS Record responses are not supported by this gateway"), http.StatusNotImplemented)
}

// Fails fast if the CID is not an encoded Libp2p Key, avoids wasteful
// round trips to the remote routing provider.
if mc.Code(c.Type()) != mc.Libp2pKey {
return nil, NewErrorStatusCode(errors.New("cid codec must be libp2p-key"), http.StatusBadRequest)
}

// The value store expects the key itself to be encoded as a multihash.
id, err := peer.FromCid(c)
name, err := ipns.NameFromCid(c)

Check warning on line 589 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L589

Added line #L589 was not covered by tests
if err != nil {
return nil, err
return nil, NewErrorStatusCode(err, http.StatusBadRequest)

Check warning on line 591 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L591

Added line #L591 was not covered by tests
}

return bb.routing.GetValue(ctx, "/ipns/"+string(id))
return bb.routing.GetValue(ctx, string(name.RoutingKey()))

Check warning on line 594 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L594

Added line #L594 was not covered by tests
}

func (bb *BlocksBackend) GetDNSLinkRecord(ctx context.Context, hostname string) (path.Path, error) {

Check warning on line 597 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L597

Added line #L597 was not covered by tests
if bb.namesys != nil {
p, err := bb.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
p, err := path.NewPath("/ipns/" + hostname)
if err != nil {
return nil, err
}
res, err := bb.namesys.Resolve(ctx, p, namesys.ResolveWithDepth(1))

Check warning on line 603 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L599-L603

Added lines #L599 - L603 were not covered by tests
if err == namesys.ErrResolveRecursion {
err = nil
}
return p, err
return res.Path, err

Check warning on line 607 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L607

Added line #L607 was not covered by tests
}

return nil, NewErrorStatusCode(errors.New("not implemented"), http.StatusNotImplemented)
Expand Down Expand Up @@ -635,10 +636,11 @@ 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 = resolve.ResolveIPNS(ctx, bb.namesys, p)
res, err := namesys.Resolve(ctx, bb.namesys, p)
if err != nil {
return nil, nil, err
}
p = res.Path

Check warning on line 643 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L639-L643

Added lines #L639 - L643 were not covered by tests
}

if p.Namespace() != path.IPFSNamespace {
Expand Down
5 changes: 3 additions & 2 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/gateway/assets"
Expand Down Expand Up @@ -309,11 +310,11 @@ type IPFSBackend interface {
GetIPNSRecord(context.Context, cid.Cid) ([]byte, error)

// ResolveMutable takes a mutable path and resolves it into an immutable one. This means recursively resolving any
// DNSLink or IPNS records.
// DNSLink or IPNS records. It should also return a TTL. If the TTL is unknown, 0 should be returned.
//
// For example, given a mapping from `/ipns/dnslink.tld -> /ipns/ipns-id/mydirectory` and `/ipns/ipns-id` to
// `/ipfs/some-cid`, the result of passing `/ipns/dnslink.tld/myfile` would be `/ipfs/some-cid/mydirectory/myfile`.
ResolveMutable(context.Context, path.Path) (path.ImmutablePath, error)
ResolveMutable(context.Context, path.Path) (path.ImmutablePath, time.Duration, time.Time, error)

// GetDNSLinkRecord returns the DNSLink TXT record for the provided FQDN.
// Unlike ResolvePath, it does not perform recursive resolution. It only
Expand Down
Loading

0 comments on commit 42af4f8

Please sign in to comment.