Skip to content

Commit

Permalink
refactor: namesys cleanup, gateway /ipns/ ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 9, 2023
1 parent a5668d2 commit 5bffc27
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 63 deletions.
14 changes: 7 additions & 7 deletions client/rpc/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

iface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
)

Expand Down Expand Up @@ -49,9 +49,9 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
return nil, err
}

ropts := nsopts.ProcessOpts(options.ResolveOpts)
if ropts.Depth != nsopts.DefaultDepthLimit && ropts.Depth != 1 {
return nil, fmt.Errorf("Name.Resolve: depth other than 1 or %d not supported", nsopts.DefaultDepthLimit)
ropts := namesys.ProcessResolveOptions(options.ResolveOpts)
if ropts.Depth != namesys.DefaultDepthLimit && ropts.Depth != 1 {
return nil, fmt.Errorf("Name.Resolve: depth other than 1 or %d not supported", namesys.DefaultDepthLimit)

Check warning on line 54 in client/rpc/name.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/name.go#L52-L54

Added lines #L52 - L54 were not covered by tests
}

req := api.core().Request("name/resolve", name).
Expand Down Expand Up @@ -110,9 +110,9 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
return nil, err
}

ropts := nsopts.ProcessOpts(options.ResolveOpts)
if ropts.Depth != nsopts.DefaultDepthLimit && ropts.Depth != 1 {
return nil, fmt.Errorf("Name.Resolve: depth other than 1 or %d not supported", nsopts.DefaultDepthLimit)
ropts := namesys.ProcessResolveOptions(options.ResolveOpts)
if ropts.Depth != namesys.DefaultDepthLimit && ropts.Depth != 1 {
return nil, fmt.Errorf("Name.Resolve: depth other than 1 or %d not supported", namesys.DefaultDepthLimit)

Check warning on line 115 in client/rpc/name.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/name.go#L115

Added line #L115 was not covered by tests
}

req := api.core().Request("name/resolve", name).
Expand Down
2 changes: 1 addition & 1 deletion core/commands/dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestKeyTranslation(t *testing.T) {
pid := test.RandPeerIDFatal(t)
pkname := namesys.PkKeyForID(pid)
pkname := namesys.PkRoutingKey(pid)
ipnsname := ipns.NameFromPeer(pid).RoutingKey()

pkk, err := escapeDhtKey("/pk/" + pid.String())
Expand Down
15 changes: 10 additions & 5 deletions core/commands/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"io"

nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
namesys "github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
ncmd "github.com/ipfs/kubo/core/commands/name"

Expand Down Expand Up @@ -47,16 +47,21 @@ It will work across multiple DNSLinks and IPNS keys.
name := req.Arguments[0]
resolver := namesys.NewDNSResolver(node.DNSResolver.LookupTXT)

var routing []nsopts.ResolveOpt
var routing []namesys.ResolveOption

Check warning on line 50 in core/commands/dns.go

View check run for this annotation

Codecov / codecov/patch

core/commands/dns.go#L50

Added line #L50 was not covered by tests
if !recursive {
routing = append(routing, nsopts.Depth(1))
routing = append(routing, namesys.ResolveWithDepth(1))

Check warning on line 52 in core/commands/dns.go

View check run for this annotation

Codecov / codecov/patch

core/commands/dns.go#L52

Added line #L52 was not covered by tests
}

output, err := resolver.Resolve(req.Context, name, routing...)
p, err := path.NewPath(name)
if err != nil {
return err
}

Check warning on line 58 in core/commands/dns.go

View check run for this annotation

Codecov / codecov/patch

core/commands/dns.go#L55-L58

Added lines #L55 - L58 were not covered by tests

val, err := resolver.Resolve(req.Context, p, routing...)

Check warning on line 60 in core/commands/dns.go

View check run for this annotation

Codecov / codecov/patch

core/commands/dns.go#L60

Added line #L60 was not covered by tests
if err != nil && (recursive || err != namesys.ErrResolveRecursion) {
return err
}
return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: output.String()})
return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: val.Path.String()})

Check warning on line 64 in core/commands/dns.go

View check run for this annotation

Codecov / codecov/patch

core/commands/dns.go#L64

Added line #L64 was not covered by tests
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ncmd.ResolvedPath) error {
Expand Down
12 changes: 5 additions & 7 deletions core/commands/name/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (
"strings"
"time"

namesys "github.com/ipfs/boxo/namesys"
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"

options "github.com/ipfs/boxo/coreiface/options"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
)

var log = logging.Logger("core/commands/ipns")
Expand Down Expand Up @@ -108,10 +106,10 @@ Resolve the value of a dnslink:
}

if !recursive {
opts = append(opts, options.Name.ResolveOption(nsopts.Depth(1)))
opts = append(opts, options.Name.ResolveOption(namesys.ResolveWithDepth(1)))
}
if rcok {
opts = append(opts, options.Name.ResolveOption(nsopts.DhtRecordCount(rc)))
opts = append(opts, options.Name.ResolveOption(namesys.ResolveWithDhtRecordCount(rc)))
}
if dhttok {
d, err := time.ParseDuration(dhtt)
Expand All @@ -121,7 +119,7 @@ Resolve the value of a dnslink:
if d < 0 {
return errors.New("DHT timeout value must be >= 0")
}
opts = append(opts, options.Name.ResolveOption(nsopts.DhtTimeout(d)))
opts = append(opts, options.Name.ResolveOption(namesys.ResolveWithDhtTimeout(d)))
}

if !strings.HasPrefix(name, "/ipns/") {
Expand Down
9 changes: 4 additions & 5 deletions core/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"time"

ns "github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
cidenc "github.com/ipfs/go-cidutil/cidenc"
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
"github.com/ipfs/kubo/core/commands/cmdutils"
ncmd "github.com/ipfs/kubo/core/commands/name"

options "github.com/ipfs/boxo/coreiface/options"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/path"
cmds "github.com/ipfs/go-ipfs-cmds"
)

Expand Down Expand Up @@ -87,11 +86,11 @@ Resolve the value of an IPFS DAG path:
rc, rcok := req.Options[resolveDhtRecordCountOptionName].(uint)
dhtt, dhttok := req.Options[resolveDhtTimeoutOptionName].(string)
ropts := []options.NameResolveOption{
options.Name.ResolveOption(nsopts.Depth(1)),
options.Name.ResolveOption(ns.ResolveWithDepth(1)),

Check warning on line 89 in core/commands/resolve.go

View check run for this annotation

Codecov / codecov/patch

core/commands/resolve.go#L89

Added line #L89 was not covered by tests
}

if rcok {
ropts = append(ropts, options.Name.ResolveOption(nsopts.DhtRecordCount(rc)))
ropts = append(ropts, options.Name.ResolveOption(ns.ResolveWithDhtRecordCount(rc)))

Check warning on line 93 in core/commands/resolve.go

View check run for this annotation

Codecov / codecov/patch

core/commands/resolve.go#L93

Added line #L93 was not covered by tests
}
if dhttok {
d, err := time.ParseDuration(dhtt)
Expand All @@ -101,7 +100,7 @@ Resolve the value of an IPFS DAG path:
if d < 0 {
return errors.New("DHT timeout value must be >= 0")
}
ropts = append(ropts, options.Name.ResolveOption(nsopts.DhtTimeout(d)))
ropts = append(ropts, options.Name.ResolveOption(ns.ResolveWithDhtTimeout(d)))

Check warning on line 103 in core/commands/resolve.go

View check run for this annotation

Codecov / codecov/patch

core/commands/resolve.go#L103

Added line #L103 was not covered by tests
}
p, err := api.Name().Resolve(req.Context, name, ropts...)
// ErrResolveRecursion is fine
Expand Down
16 changes: 10 additions & 6 deletions core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

coreiface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/path"
ci "github.com/libp2p/go-libp2p/core/crypto"
peer "github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -57,13 +56,13 @@ func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.Nam

eol := time.Now().Add(options.ValidTime)

publishOptions := []nsopts.PublishOption{
nsopts.PublishWithEOL(eol),
nsopts.PublishCompatibleWithV1(options.CompatibleWithV1),
publishOptions := []namesys.PublishOption{
namesys.PublishWithEOL(eol),
namesys.PublishWithIPNSOption(ipns.WithV1Compatibility(options.CompatibleWithV1)),
}

if options.TTL != nil {
publishOptions = append(publishOptions, nsopts.PublishWithTTL(*options.TTL))
publishOptions = append(publishOptions, namesys.PublishWithTTL(*options.TTL))
}

err = api.namesys.Publish(ctx, k, p, publishOptions...)
Expand Down Expand Up @@ -109,10 +108,15 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
name = "/ipns/" + name
}

p, err := path.NewPath(name)
if err != nil {
return nil, err
}

out := make(chan coreiface.IpnsResult)
go func() {
defer close(out)
for res := range resolver.ResolveAsync(ctx, name, options.ResolveOpts...) {
for res := range resolver.ResolveAsync(ctx, p, options.ResolveOpts...) {
select {
case out <- coreiface.IpnsResult{Path: res.Path, Err: res.Err}:
case <-ctx.Done():
Expand Down
8 changes: 5 additions & 3 deletions core/coreapi/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package coreapi

import (
"context"
"errors"
"fmt"

"github.com/ipfs/boxo/namesys/resolve"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/kubo/tracing"

"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -40,12 +41,13 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p path.Path) (path.Immutabl
ctx, span := tracing.Span(ctx, "CoreAPI", "ResolvePath", trace.WithAttributes(attribute.String("path", p.String())))
defer span.End()

p, err := resolve.ResolveIPNS(ctx, api.namesys, p)
if err == resolve.ErrNoNamesys {
res, err := namesys.Resolve(ctx, api.namesys, p)
if errors.Is(err, namesys.ErrNoNamesys) {
return path.ImmutablePath{}, nil, coreiface.ErrOffline
} else if err != nil {
return path.ImmutablePath{}, nil, err
}
p = res.Path

var resolver ipfspathresolver.Resolver
switch p.Namespace() {
Expand Down
7 changes: 4 additions & 3 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net"
"net/http"
"time"

"github.com/ipfs/boxo/blockservice"
iface "github.com/ipfs/boxo/coreiface"
Expand Down Expand Up @@ -195,10 +196,10 @@ func (o *offlineGatewayErrWrapper) GetIPNSRecord(ctx context.Context, c cid.Cid)
return rec, err
}

func (o *offlineGatewayErrWrapper) ResolveMutable(ctx context.Context, path path.Path) (path.ImmutablePath, error) {
imPath, err := o.gwimpl.ResolveMutable(ctx, path)
func (o *offlineGatewayErrWrapper) ResolveMutable(ctx context.Context, path path.Path) (path.ImmutablePath, time.Duration, time.Time, error) {
imPath, ttl, lastMod, err := o.gwimpl.ResolveMutable(ctx, path)

Check warning on line 200 in core/corehttp/gateway.go

View check run for this annotation

Codecov / codecov/patch

core/corehttp/gateway.go#L199-L200

Added lines #L199 - L200 were not covered by tests
err = offlineErrWrap(err)
return imPath, err
return imPath, ttl, lastMod, err

Check warning on line 202 in core/corehttp/gateway.go

View check run for this annotation

Codecov / codecov/patch

core/corehttp/gateway.go#L202

Added line #L202 was not covered by tests
}

func (o *offlineGatewayErrWrapper) GetDNSLinkRecord(ctx context.Context, s string) (path.Path, error) {
Expand Down
33 changes: 19 additions & 14 deletions core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/stretchr/testify/assert"

iface "github.com/ipfs/boxo/coreiface"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-datastore"
syncds "github.com/ipfs/go-datastore/sync"
Expand All @@ -27,41 +26,47 @@ import (

type mockNamesys map[string]path.Path

func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.ResolveOpt) (value path.Path, err error) {
cfg := nsopts.DefaultResolveOpts()
func (m mockNamesys) Resolve(ctx context.Context, p path.Path, opts ...namesys.ResolveOption) (namesys.ResolveResult, error) {
cfg := namesys.DefaultResolveOptions()
for _, o := range opts {
o(&cfg)
}
depth := cfg.Depth
if depth == nsopts.UnlimitedDepth {
if depth == namesys.UnlimitedDepth {
// max uint
depth = ^uint(0)
}
var (
value path.Path
)
name := path.SegmentsToString(p.Segments()[:2]...)
for strings.HasPrefix(name, "/ipns/") {
if depth == 0 {
return value, namesys.ErrResolveRecursion
return namesys.ResolveResult{Path: value}, namesys.ErrResolveRecursion
}
depth--

var ok bool
value, ok = m[name]
v, ok := m[name]
if !ok {
return nil, namesys.ErrResolveFailed
return namesys.ResolveResult{}, namesys.ErrResolveFailed
}
value = v
name = value.String()
}
return value, nil

value, err := path.Join(value, p.Segments()[2:]...)
return namesys.ResolveResult{Path: value}, err
}

func (m mockNamesys) ResolveAsync(ctx context.Context, name string, opts ...nsopts.ResolveOpt) <-chan namesys.Result {
out := make(chan namesys.Result, 1)
v, err := m.Resolve(ctx, name, opts...)
out <- namesys.Result{Path: v, Err: err}
func (m mockNamesys) ResolveAsync(ctx context.Context, p path.Path, opts ...namesys.ResolveOption) <-chan namesys.ResolveAsyncResult {
out := make(chan namesys.ResolveAsyncResult, 1)
res, err := m.Resolve(ctx, p, opts...)
out <- namesys.ResolveAsyncResult{Path: res.Path, TTL: res.TTL, LastMod: res.LastMod, Err: err}
close(out)
return out
}

func (m mockNamesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path, opts ...nsopts.PublishOption) error {
func (m mockNamesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path, opts ...namesys.PublishOption) error {
return errors.New("not implemented for mockNamesys")
}

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.20
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/boxo v0.13.2-0.20231009073559-45c797e0ccea
github.com/ipfs/boxo v0.13.2-0.20231009112208-310943fd3447
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.31.0
github.com/multiformats/go-multiaddr v0.11.0
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.13.2-0.20231009073559-45c797e0ccea h1:CygWxN8BL+d1F6PBOI+02UKqkpASP0ai459aw6ANZTg=
github.com/ipfs/boxo v0.13.2-0.20231009073559-45c797e0ccea/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
github.com/ipfs/boxo v0.13.2-0.20231009112208-310943fd3447 h1:xeNmQd7iQlcYgAEwymR1ui8+3Vzext6BC3IKGatyfhU=
github.com/ipfs/boxo v0.13.2-0.20231009112208-310943fd3447/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
Expand Down
4 changes: 2 additions & 2 deletions fuse/ipns/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

ft "github.com/ipfs/boxo/ipld/unixfs"
nsys "github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/core"
ci "github.com/libp2p/go-libp2p/core/crypto"
Expand All @@ -28,7 +28,7 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error {
return err
}

pub := nsys.NewIpnsPublisher(n.Routing, n.Repo.Datastore())
pub := namesys.NewIPNSPublisher(n.Routing, n.Repo.Datastore())

return pub.Publish(ctx, key, path.FromCid(emptyDir.Cid()))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0
github.com/google/uuid v1.3.1
github.com/hashicorp/go-multierror v1.1.1
github.com/ipfs/boxo v0.13.2-0.20231009073559-45c797e0ccea
github.com/ipfs/boxo v0.13.2-0.20231009112208-310943fd3447
github.com/ipfs/go-block-format v0.2.0
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-cidutil v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.13.2-0.20231009073559-45c797e0ccea h1:CygWxN8BL+d1F6PBOI+02UKqkpASP0ai459aw6ANZTg=
github.com/ipfs/boxo v0.13.2-0.20231009073559-45c797e0ccea/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
github.com/ipfs/boxo v0.13.2-0.20231009112208-310943fd3447 h1:xeNmQd7iQlcYgAEwymR1ui8+3Vzext6BC3IKGatyfhU=
github.com/ipfs/boxo v0.13.2-0.20231009112208-310943fd3447/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-bitswap v0.11.0 h1:j1WVvhDX1yhG32NTC9xfxnqycqYIlhzEzLXG/cU1HyQ=
Expand Down
Loading

0 comments on commit 5bffc27

Please sign in to comment.