Skip to content

Commit a50f784

Browse files
authored
feat!: namesys refactor, ipns TTL bubbled up to gateway (#459)
1 parent 0a566c9 commit a50f784

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1819
-1843
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The following emojis are used to highlight certain changes:
1818

1919
* `boxo/gateway`:
2020
* A new `WithResolver(...)` option can be used with `NewBlocksBackend(...)` allowing the user to pass their custom `Resolver` implementation.
21+
* 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.
2122
* `boxo/bitswap/client`:
2223
* A new `WithoutDuplicatedBlockStats()` option can be used with `bitswap.New` and `bsclient.New`. This disable accounting for duplicated blocks, which requires a `blockstore.Has()` lookup for every received block and thus, can impact performance.
2324

@@ -41,6 +42,21 @@ The following emojis are used to highlight certain changes:
4142
* 🛠 The signature of `CoreAPI.ResolvePath` in `coreiface` has changed to now return
4243
the remainder segments as a second return value, matching the signature of `resolver.ResolveToLastNode`.
4344
* 🛠 `routing/http/client.FindPeers` now returns `iter.ResultIter[types.PeerRecord]` instead of `iter.ResultIter[types.Record]`. The specification indicates that records for this method will always be Peer Records.
45+
* 🛠 The `namesys` package has been refactored. The following are the largest modifications:
46+
* The options in `coreiface/options/namesys` have been moved to `namesys` and their names
47+
have been made more consistent.
48+
* Many of the exported structs and functions have been renamed in order to be consistent with
49+
the remaining packages.
50+
* `namesys.Resolver.Resolve` now returns a TTL, in addition to the resolved path. If the
51+
TTL is unknown, 0 is returned. `IPNSResolver` is able to resolve a TTL, while `DNSResolver`
52+
is not.
53+
* `namesys/resolver.ResolveIPNS` has been moved to `namesys.ResolveIPNS` and now returns a TTL
54+
in addition to the resolved path.
55+
*`boxo/ipns` record defaults follow recommendations from [IPNS Record Specification](https://specs.ipfs.tech/ipns/ipns-record/#ipns-record):
56+
* `DefaultRecordTTL` is now set to `1h`
57+
* `DefaultRecordLifetime` follows the increased expiration window of Amino DHT ([go-libp2p-kad-dht#793](https://github.com/libp2p/go-libp2p-kad-dht/pull/793)) and is set to `48h`
58+
* 🛠 The `gateway`'s `IPFSBackend.ResolveMutable` is now expected to return a TTL in addition to
59+
the resolved path. If the TTL is unknown, 0 should be returned.
4460

4561
### Removed
4662

coreiface/options/name.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package options
33
import (
44
"time"
55

6-
ropts "github.com/ipfs/boxo/coreiface/options/namesys"
6+
"github.com/ipfs/boxo/namesys"
77
)
88

99
const (
@@ -21,7 +21,7 @@ type NamePublishSettings struct {
2121
type NameResolveSettings struct {
2222
Cache bool
2323

24-
ResolveOpts []ropts.ResolveOpt
24+
ResolveOpts []namesys.ResolveOption
2525
}
2626

2727
type (
@@ -123,7 +123,7 @@ func (nameOpts) Cache(cache bool) NameResolveOption {
123123
}
124124
}
125125

126-
func (nameOpts) ResolveOption(opt ropts.ResolveOpt) NameResolveOption {
126+
func (nameOpts) ResolveOption(opt namesys.ResolveOption) NameResolveOption {
127127
return func(settings *NameResolveSettings) error {
128128
settings.ResolveOpts = append(settings.ResolveOpts, opt)
129129
return nil

coreiface/options/namesys/opts.go

Lines changed: 0 additions & 131 deletions
This file was deleted.

gateway/blocks_backend.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import (
88
"io"
99
"net/http"
1010
"strings"
11+
"time"
1112

1213
"github.com/ipfs/boxo/blockservice"
1314
blockstore "github.com/ipfs/boxo/blockstore"
14-
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
1515
"github.com/ipfs/boxo/fetcher"
1616
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
1717
"github.com/ipfs/boxo/files"
1818
"github.com/ipfs/boxo/ipld/merkledag"
1919
ufile "github.com/ipfs/boxo/ipld/unixfs/file"
2020
uio "github.com/ipfs/boxo/ipld/unixfs/io"
21+
"github.com/ipfs/boxo/ipns"
2122
"github.com/ipfs/boxo/namesys"
22-
"github.com/ipfs/boxo/namesys/resolve"
2323
"github.com/ipfs/boxo/path"
2424
"github.com/ipfs/boxo/path/resolver"
2525
blocks "github.com/ipfs/go-block-format"
@@ -39,7 +39,6 @@ import (
3939
"github.com/ipld/go-ipld-prime/traversal/selector"
4040
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
4141
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
42-
"github.com/libp2p/go-libp2p/core/peer"
4342
"github.com/libp2p/go-libp2p/core/routing"
4443
mc "github.com/multiformats/go-multicodec"
4544

@@ -623,18 +622,23 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
623622
return pathRoots, lastPath, remainder, nil
624623
}
625624

626-
func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
625+
func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, time.Duration, time.Time, error) {
627626
switch p.Namespace() {
628627
case path.IPNSNamespace:
629-
p, err := resolve.ResolveIPNS(ctx, bb.namesys, p)
628+
res, err := namesys.Resolve(ctx, bb.namesys, p)
630629
if err != nil {
631-
return path.ImmutablePath{}, err
630+
return path.ImmutablePath{}, 0, time.Time{}, err
632631
}
633-
return path.NewImmutablePath(p)
632+
ip, err := path.NewImmutablePath(res.Path)
633+
if err != nil {
634+
return path.ImmutablePath{}, 0, time.Time{}, err
635+
}
636+
return ip, res.TTL, res.LastMod, nil
634637
case path.IPFSNamespace:
635-
return path.NewImmutablePath(p)
638+
ip, err := path.NewImmutablePath(p)
639+
return ip, 0, time.Time{}, err
636640
default:
637-
return path.ImmutablePath{}, NewErrorStatusCode(fmt.Errorf("unsupported path namespace: %s", p.Namespace()), http.StatusNotImplemented)
641+
return path.ImmutablePath{}, 0, time.Time{}, NewErrorStatusCode(fmt.Errorf("unsupported path namespace: %s", p.Namespace()), http.StatusNotImplemented)
638642
}
639643
}
640644

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

646-
// Fails fast if the CID is not an encoded Libp2p Key, avoids wasteful
647-
// round trips to the remote routing provider.
648-
if mc.Code(c.Type()) != mc.Libp2pKey {
649-
return nil, NewErrorStatusCode(errors.New("cid codec must be libp2p-key"), http.StatusBadRequest)
650-
}
651-
652-
// The value store expects the key itself to be encoded as a multihash.
653-
id, err := peer.FromCid(c)
650+
name, err := ipns.NameFromCid(c)
654651
if err != nil {
655-
return nil, err
652+
return nil, NewErrorStatusCode(err, http.StatusBadRequest)
656653
}
657654

658-
return bb.routing.GetValue(ctx, "/ipns/"+string(id))
655+
return bb.routing.GetValue(ctx, string(name.RoutingKey()))
659656
}
660657

661658
func (bb *BlocksBackend) GetDNSLinkRecord(ctx context.Context, hostname string) (path.Path, error) {
662659
if bb.namesys != nil {
663-
p, err := bb.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
660+
p, err := path.NewPath("/ipns/" + hostname)
661+
if err != nil {
662+
return nil, err
663+
}
664+
res, err := bb.namesys.Resolve(ctx, p, namesys.ResolveWithDepth(1))
664665
if err == namesys.ErrResolveRecursion {
665666
err = nil
666667
}
667-
return p, err
668+
return res.Path, err
668669
}
669670

670671
return nil, NewErrorStatusCode(errors.New("not implemented"), http.StatusNotImplemented)
@@ -696,10 +697,11 @@ func (bb *BlocksBackend) ResolvePath(ctx context.Context, path path.ImmutablePat
696697
func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, []string, error) {
697698
var err error
698699
if p.Namespace() == path.IPNSNamespace {
699-
p, err = resolve.ResolveIPNS(ctx, bb.namesys, p)
700+
res, err := namesys.Resolve(ctx, bb.namesys, p)
700701
if err != nil {
701702
return path.ImmutablePath{}, nil, err
702703
}
704+
p = res.Path
703705
}
704706

705707
if p.Namespace() != path.IPFSNamespace {

gateway/gateway.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sort"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"github.com/ipfs/boxo/files"
1314
"github.com/ipfs/boxo/gateway/assets"
@@ -369,11 +370,11 @@ type IPFSBackend interface {
369370
GetIPNSRecord(context.Context, cid.Cid) ([]byte, error)
370371

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

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

0 commit comments

Comments
 (0)