Skip to content

Commit

Permalink
refactor!: namesys package
Browse files Browse the repository at this point in the history
- move namesys options out of coreiface/options/namesys
  into namesys directly.
- rename options to make them consistent and make sense.
- move code around to be in self-explanatory file names.
  • Loading branch information
hacdias committed Sep 4, 2023
1 parent 1356946 commit c0f757a
Show file tree
Hide file tree
Showing 20 changed files with 747 additions and 885 deletions.
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.

3 changes: 1 addition & 2 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
ifacepath "github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/fetcher"
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
Expand Down Expand Up @@ -608,7 +607,7 @@ func (bb *BlocksBackend) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte,

func (bb *BlocksBackend) GetDNSLinkRecord(ctx context.Context, hostname string) (ifacepath.Path, error) {
if bb.namesys != nil {
p, err := bb.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
p, err := bb.namesys.Resolve(ctx, "/ipns/"+hostname, namesys.ResolveWithDepth(1))
if err == namesys.ErrResolveRecursion {
err = nil
}
Expand Down
17 changes: 8 additions & 9 deletions gateway/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"testing"

"github.com/ipfs/boxo/blockservice"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
ipath "github.com/ipfs/boxo/coreiface/path"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/files"
Expand Down Expand Up @@ -54,13 +53,13 @@ func mustDo(t *testing.T, req *http.Request) *http.Response {

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, name string, opts ...namesys.ResolveOption) (value path.Path, err 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)
}
Expand All @@ -80,15 +79,15 @@ func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.Re
return value, nil
}

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

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

Expand Down Expand Up @@ -163,7 +162,7 @@ func (mb *mockBackend) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, er

func (mb *mockBackend) GetDNSLinkRecord(ctx context.Context, hostname string) (ipath.Path, error) {
if mb.namesys != nil {
p, err := mb.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
p, err := mb.namesys.Resolve(ctx, "/ipns/"+hostname, namesys.ResolveWithDepth(1))
if err == namesys.ErrResolveRecursion {
err = nil
}
Expand Down
59 changes: 25 additions & 34 deletions namesys/dns.go → namesys/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
gpath "path"
"strings"

opts "github.com/ipfs/boxo/coreiface/options/namesys"
path "github.com/ipfs/boxo/path"
dns "github.com/miekg/dns"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -18,44 +17,36 @@ import (
// LookupTXTFunc is a function that lookups TXT record values.
type LookupTXTFunc func(ctx context.Context, name string) (txt []string, err error)

// DNSResolver implements a Resolver on DNS domains
// DNSResolver implements [Resolver] on DNS domains.
type DNSResolver struct {
lookupTXT LookupTXTFunc
// TODO: maybe some sort of caching?
// cache would need a timeout
}

var _ Resolver = &DNSResolver{}

// NewDNSResolver constructs a name resolver using DNS TXT records.
func NewDNSResolver(lookup LookupTXTFunc) *DNSResolver {
return &DNSResolver{lookupTXT: lookup}
}

// Resolve implements Resolver.
func (r *DNSResolver) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) {
ctx, span := StartSpan(ctx, "DNSResolver.Resolve")
func (r *DNSResolver) Resolve(ctx context.Context, name string, options ...ResolveOption) (path.Path, error) {
ctx, span := startSpan(ctx, "DNSResolver.Resolve")
defer span.End()

return resolve(ctx, r, name, opts.ProcessOpts(options))
return resolve(ctx, r, name, ProcessResolveOptions(options))
}

// ResolveAsync implements Resolver.
func (r *DNSResolver) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result {
ctx, span := StartSpan(ctx, "DNSResolver.ResolveAsync")
func (r *DNSResolver) ResolveAsync(ctx context.Context, name string, options ...ResolveOption) <-chan ResolveResult {
ctx, span := startSpan(ctx, "DNSResolver.ResolveAsync")
defer span.End()

return resolveAsync(ctx, r, name, opts.ProcessOpts(options))
}

type lookupRes struct {
path path.Path
error error
return resolveAsync(ctx, r, name, ProcessResolveOptions(options))
}

// resolveOnce implements resolver.
// TXT records for a given domain name should contain a b58
// encoded multihash.
func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
ctx, span := StartSpan(ctx, "DNSResolver.ResolveOnceAsync")
func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options ResolveOptions) <-chan onceResult {
ctx, span := startSpan(ctx, "DNSResolver.ResolveOnceAsync")
defer span.End()

var fqdn string
Expand All @@ -76,10 +67,10 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
fqdn = domain + "."
}

rootChan := make(chan lookupRes, 1)
rootChan := make(chan ResolveResult, 1)
go workDomain(ctx, r, fqdn, rootChan)

subChan := make(chan lookupRes, 1)
subChan := make(chan ResolveResult, 1)
go workDomain(ctx, r, "_dnslink."+fqdn, subChan)

appendPath := func(p path.Path) (path.Path, error) {
Expand All @@ -91,7 +82,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options

go func() {
defer close(out)
ctx, span := StartSpan(ctx, "DNSResolver.ResolveOnceAsync.Worker")
ctx, span := startSpan(ctx, "DNSResolver.ResolveOnceAsync.Worker")
defer span.End()

var rootResErr, subResErr error
Expand All @@ -102,26 +93,26 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
subChan = nil
break
}
if subRes.error == nil {
p, err := appendPath(subRes.path)
if subRes.Err == nil {
p, err := appendPath(subRes.Path)
emitOnceResult(ctx, out, onceResult{value: p, err: err})
// Return without waiting for rootRes, since this result
// (for "_dnslink."+fqdn) takes precedence
return
}
subResErr = subRes.error
subResErr = subRes.Err
case rootRes, ok := <-rootChan:
if !ok {
rootChan = nil
break
}
if rootRes.error == nil {
p, err := appendPath(rootRes.path)
if rootRes.Err == nil {
p, err := appendPath(rootRes.Path)
emitOnceResult(ctx, out, onceResult{value: p, err: err})
// Do not return here. Wait for subRes so that it is
// output last if good, thereby giving subRes precedence.
} else {
rootResErr = rootRes.error
rootResErr = rootRes.Err
}
case <-ctx.Done():
return
Expand All @@ -144,8 +135,8 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
return out
}

func workDomain(ctx context.Context, r *DNSResolver, name string, res chan lookupRes) {
ctx, span := StartSpan(ctx, "DNSResolver.WorkDomain", trace.WithAttributes(attribute.String("Name", name)))
func workDomain(ctx context.Context, r *DNSResolver, name string, res chan ResolveResult) {
ctx, span := startSpan(ctx, "DNSResolver.WorkDomain", trace.WithAttributes(attribute.String("Name", name)))
defer span.End()

defer close(res)
Expand All @@ -160,20 +151,20 @@ func workDomain(ctx context.Context, r *DNSResolver, name string, res chan looku
}
}
// Could not look up any text records for name
res <- lookupRes{"", err}
res <- ResolveResult{"", err}
return
}

for _, t := range txt {
p, err := parseEntry(t)
if err == nil {
res <- lookupRes{p, nil}
res <- ResolveResult{p, nil}
return
}
}

// There were no TXT records with a dnslink
res <- lookupRes{"", ErrResolveFailed}
res <- ResolveResult{"", ErrResolveFailed}
}

func parseEntry(txt string) (path.Path, error) {
Expand Down
Loading

0 comments on commit c0f757a

Please sign in to comment.