Skip to content

Commit

Permalink
feat: move namesys into go-ipfs
Browse files Browse the repository at this point in the history
  • Loading branch information
guseggert committed Oct 29, 2021
1 parent 52a7477 commit 9d2b6d9
Show file tree
Hide file tree
Showing 30 changed files with 2,578 additions and 363 deletions.
2 changes: 1 addition & 1 deletion core/commands/dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package commands
import (
"testing"

"github.com/ipfs/go-namesys"
"github.com/ipfs/go-ipfs/lib/namesys"

ipns "github.com/ipfs/go-ipns"
"github.com/libp2p/go-libp2p-core/test"
Expand Down
2 changes: 1 addition & 1 deletion core/commands/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
ncmd "github.com/ipfs/go-ipfs/core/commands/name"
namesys "github.com/ipfs/go-namesys"
namesys "github.com/ipfs/go-ipfs/lib/namesys"
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"

cmds "github.com/ipfs/go-ipfs-cmds"
Expand Down
2 changes: 1 addition & 1 deletion core/commands/name/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
namesys "github.com/ipfs/go-namesys"
namesys "github.com/ipfs/go-ipfs/lib/namesys"

cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
Expand Down
2 changes: 1 addition & 1 deletion core/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
ncmd "github.com/ipfs/go-ipfs/core/commands/name"
ns "github.com/ipfs/go-namesys"
ns "github.com/ipfs/go-ipfs/lib/namesys"

cidenc "github.com/ipfs/go-cidutil/cidenc"
cmds "github.com/ipfs/go-ipfs-cmds"
Expand Down
8 changes: 4 additions & 4 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"io"

"github.com/ipfs/go-filestore"
"github.com/ipfs/go-ipfs-pinner"
pin "github.com/ipfs/go-ipfs-pinner"

bserv "github.com/ipfs/go-blockservice"
"github.com/ipfs/go-fetcher"
"github.com/ipfs/go-graphsync"
bstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
"github.com/ipfs/go-ipfs-provider"
provider "github.com/ipfs/go-ipfs-provider"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
mfs "github.com/ipfs/go-mfs"
Expand All @@ -46,11 +46,11 @@ import (
"github.com/ipfs/go-ipfs/core/node"
"github.com/ipfs/go-ipfs/core/node/libp2p"
"github.com/ipfs/go-ipfs/fuse/mount"
"github.com/ipfs/go-ipfs/lib/namesys"
ipnsrp "github.com/ipfs/go-ipfs/lib/namesys/republisher"
"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-ipfs/peering"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-namesys"
ipnsrp "github.com/ipfs/go-namesys/republisher"
)

var log = logging.Logger("core")
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/node"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-namesys"
"github.com/ipfs/go-ipfs/lib/namesys"
)

type CoreAPI struct {
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/ipfs/go-ipfs-keystore"
"github.com/ipfs/go-namesys"
"github.com/ipfs/go-ipfs/lib/namesys"

ipath "github.com/ipfs/go-path"
coreiface "github.com/ipfs/interface-go-ipfs-core"
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
gopath "path"

"github.com/ipfs/go-namesys/resolve"
"github.com/ipfs/go-ipfs/lib/namesys/resolve"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-fetcher"
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
repo "github.com/ipfs/go-ipfs/repo"
namesys "github.com/ipfs/go-namesys"
namesys "github.com/ipfs/go-ipfs/lib/namesys"

datastore "github.com/ipfs/go-datastore"
syncds "github.com/ipfs/go-datastore/sync"
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
cid "github.com/ipfs/go-cid"
core "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
namesys "github.com/ipfs/go-namesys"
namesys "github.com/ipfs/go-ipfs/lib/namesys"
"github.com/libp2p/go-libp2p-core/peer"
dns "github.com/miekg/dns"

Expand Down
8 changes: 4 additions & 4 deletions core/node/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"fmt"
"time"

"github.com/ipfs/go-ipfs-util"
util "github.com/ipfs/go-ipfs-util"
"github.com/ipfs/go-ipns"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-record"
record "github.com/libp2p/go-libp2p-record"
madns "github.com/multiformats/go-multiaddr-dns"

"github.com/ipfs/go-ipfs/lib/namesys"
"github.com/ipfs/go-ipfs/lib/namesys/republisher"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-namesys"
"github.com/ipfs/go-namesys/republisher"
)

const DefaultIpnsCacheSize = 128
Expand Down
2 changes: 1 addition & 1 deletion fuse/ipns/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/ipfs/go-ipfs/core"
nsys "github.com/ipfs/go-namesys"
nsys "github.com/ipfs/go-ipfs/lib/namesys"
path "github.com/ipfs/go-path"
ft "github.com/ipfs/go-unixfs"
ci "github.com/libp2p/go-libp2p-core/crypto"
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ require (
github.com/fsnotify/fsnotify v1.4.9
github.com/gabriel-vasile/mimetype v1.1.2
github.com/go-bindata/go-bindata/v3 v3.1.3
github.com/gogo/protobuf v1.3.2
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru v0.5.4
github.com/ipfs/go-bitswap v0.4.0
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-blockservice v0.1.7
Expand All @@ -31,6 +33,7 @@ require (
github.com/ipfs/go-ipfs-chunker v0.0.5
github.com/ipfs/go-ipfs-cmds v0.6.0
github.com/ipfs/go-ipfs-config v0.16.0
github.com/ipfs/go-ipfs-ds-help v0.1.1
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-files v0.0.9
Expand All @@ -50,7 +53,6 @@ require (
github.com/ipfs/go-metrics-interface v0.0.1
github.com/ipfs/go-metrics-prometheus v0.0.2
github.com/ipfs/go-mfs v0.1.2
github.com/ipfs/go-namesys v0.3.1
github.com/ipfs/go-path v0.1.2
github.com/ipfs/go-pinning-service-http-client v0.1.0
github.com/ipfs/go-unixfs v0.2.5
Expand Down Expand Up @@ -102,6 +104,7 @@ require (
github.com/prometheus/statsd_exporter v0.21.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/syndtr/goleveldb v1.0.0
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc
github.com/whyrusleeping/go-sysinfo v0.0.0-20190219211824-4a357d4b90b1
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
go.opencensus.io v0.23.0
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+u
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM=
github.com/google/gopacket v1.1.18/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand Down Expand Up @@ -511,8 +510,6 @@ github.com/ipfs/go-ipld-git v0.1.1 h1:TWGnZjS0htmEmlMFEkA3ogrNCqWjIxwr16x1OsdhG+
github.com/ipfs/go-ipld-git v0.1.1/go.mod h1:+VyMqF5lMcJh4rwEppV0e6g4nCCHXThLYYDpKUkJubI=
github.com/ipfs/go-ipld-legacy v0.1.0 h1:wxkkc4k8cnvIGIjPO0waJCe7SHEyFgl+yQdafdjGrpA=
github.com/ipfs/go-ipld-legacy v0.1.0/go.mod h1:86f5P/srAmh9GcIcWQR9lfFLZPrIyyXQeVlOWeeWEuI=
github.com/ipfs/go-ipns v0.0.2/go.mod h1:WChil4e0/m9cIINWLxZe1Jtf77oz5L05rO2ei/uKJ5U=
github.com/ipfs/go-ipns v0.1.0/go.mod h1:3IbsuPkR6eAGcnx+E7j6HpOSbSQJPZ6zlRj+NK3jPxQ=
github.com/ipfs/go-ipns v0.1.2 h1:O/s/0ht+4Jl9+VoxoUo0zaHjnZUS+aBQIKTuzdZ/ucI=
github.com/ipfs/go-ipns v0.1.2/go.mod h1:ioQ0j02o6jdIVW+bmi18f4k2gRf0AV3kZ9KeHYHICnQ=
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
Expand Down Expand Up @@ -542,10 +539,7 @@ github.com/ipfs/go-metrics-prometheus v0.0.2 h1:9i2iljLg12S78OhC6UAiXi176xvQGiZa
github.com/ipfs/go-metrics-prometheus v0.0.2/go.mod h1:ELLU99AQQNi+zX6GCGm2lAgnzdSH3u5UVlCdqSXnEks=
github.com/ipfs/go-mfs v0.1.2 h1:DlelNSmH+yz/Riy0RjPKlooPg0KML4lXGdLw7uZkfAg=
github.com/ipfs/go-mfs v0.1.2/go.mod h1:T1QBiZPEpkPLzDqEJLNnbK55BVKVlNi2a+gVm4diFo0=
github.com/ipfs/go-namesys v0.3.1 h1:DqmeXlVODejOyECAqoqhSB5JGRv8aRFhtG0oPDmxsMc=
github.com/ipfs/go-namesys v0.3.1/go.mod h1:/BL4xk8LP5Lq82AmaRKyxZv/eYRlumNiU9SZUe1Hlps=
github.com/ipfs/go-path v0.0.7/go.mod h1:6KTKmeRnBXgqrTvzFrPV3CamxcgvXX/4z79tfAd2Sno=
github.com/ipfs/go-path v0.0.9/go.mod h1:VpDkSBKQ9EFQOUgi54Tq/O/tGi8n1RfYNks13M3DEs8=
github.com/ipfs/go-path v0.1.1/go.mod h1:vC8q4AKOtrjJz2NnllIrmr2ZbGlF5fW2OKKyhV9ggb0=
github.com/ipfs/go-path v0.1.2 h1:yQxN9JNhO4KbaaYtVgVpIH0BQDzn5Zpl5A6to93O7Ck=
github.com/ipfs/go-path v0.1.2/go.mod h1:3DdbxZb0PtT0g3UlMqyzms1UBKPc0pQ2NHx5/XScYdY=
Expand Down Expand Up @@ -755,7 +749,6 @@ github.com/libp2p/go-libp2p-interface-connmgr v0.0.1/go.mod h1:GarlRLH0LdeWcLnYM
github.com/libp2p/go-libp2p-interface-connmgr v0.0.4/go.mod h1:GarlRLH0LdeWcLnYM/SaBykKFl9U5JFnbBGruAk/D5k=
github.com/libp2p/go-libp2p-interface-connmgr v0.0.5/go.mod h1:GarlRLH0LdeWcLnYM/SaBykKFl9U5JFnbBGruAk/D5k=
github.com/libp2p/go-libp2p-interface-pnet v0.0.1/go.mod h1:el9jHpQAXK5dnTpKA4yfCNBZXvrzdOU75zz+C6ryp3k=
github.com/libp2p/go-libp2p-kad-dht v0.11.1/go.mod h1:5ojtR2acDPqh/jXf5orWy8YGb8bHQDS+qeDcoscL/PI=
github.com/libp2p/go-libp2p-kad-dht v0.13.1 h1:wQgzOpoc+dcPVDb3h0HNWUjon5JiYEqsA4iNBUtIA7A=
github.com/libp2p/go-libp2p-kad-dht v0.13.1/go.mod h1:iVdxmsKHVPQSCGPP4V/A+tDFCLsxrREZUBX8ohOcKDw=
github.com/libp2p/go-libp2p-kbucket v0.3.1/go.mod h1:oyjT5O7tS9CQurok++ERgc46YLwEpuGoFq9ubvoUOio=
Expand Down
120 changes: 120 additions & 0 deletions lib/namesys/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package namesys

import (
"context"
"strings"
"time"

path "github.com/ipfs/go-path"
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
)

type onceResult struct {
value path.Path
ttl time.Duration
err error
}

type resolver interface {
resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult
}

// resolve is a helper for implementing Resolver.ResolveN using resolveOnce.
func resolve(ctx context.Context, r resolver, name string, options opts.ResolveOpts) (path.Path, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

err := ErrResolveFailed
var p path.Path

resCh := resolveAsync(ctx, r, name, options)

for res := range resCh {
p, err = res.Path, res.Err
if err != nil {
break
}
}

return p, err
}

func resolveAsync(ctx context.Context, r resolver, name string, options opts.ResolveOpts) <-chan Result {
resCh := r.resolveOnceAsync(ctx, name, options)
depth := options.Depth
outCh := make(chan Result, 1)

go func() {
defer close(outCh)
var subCh <-chan Result
var cancelSub context.CancelFunc
defer func() {
if cancelSub != nil {
cancelSub()
}
}()

for {
select {
case res, ok := <-resCh:
if !ok {
resCh = nil
break
}

if res.err != nil {
emitResult(ctx, outCh, Result{Err: res.err})
return
}
log.Debugf("resolved %s to %s", name, res.value.String())
if !strings.HasPrefix(res.value.String(), ipnsPrefix) {
emitResult(ctx, outCh, Result{Path: res.value})
break
}

if depth == 1 {
emitResult(ctx, outCh, Result{Path: res.value, Err: ErrResolveRecursion})
break
}

subopts := options
if subopts.Depth > 1 {
subopts.Depth--
}

var subCtx context.Context
if cancelSub != nil {
// Cancel previous recursive resolve since it won't be used anyways
cancelSub()
}
subCtx, cancelSub = context.WithCancel(ctx)
_ = cancelSub

p := strings.TrimPrefix(res.value.String(), ipnsPrefix)
subCh = resolveAsync(subCtx, r, p, subopts)
case res, ok := <-subCh:
if !ok {
subCh = nil
break
}

// We don't bother returning here in case of context timeout as there is
// no good reason to do that, and we may still be able to emit a result
emitResult(ctx, outCh, res)
case <-ctx.Done():
return
}
if resCh == nil && subCh == nil {
return
}
}
}()
return outCh
}

func emitResult(ctx context.Context, outCh chan<- Result, r Result) {
select {
case outCh <- r:
case <-ctx.Done():
}
}
62 changes: 62 additions & 0 deletions lib/namesys/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package namesys

import (
"time"

path "github.com/ipfs/go-path"
)

func (ns *mpns) cacheGet(name string) (path.Path, bool) {
// existence of optional mapping defined via IPFS_NS_MAP is checked first
if ns.staticMap != nil {
val, ok := ns.staticMap[name]
if ok {
return val, true
}
}

if ns.cache == nil {
return "", false
}

ientry, ok := ns.cache.Get(name)
if !ok {
return "", false
}

entry, ok := ientry.(cacheEntry)
if !ok {
// should never happen, purely for sanity
log.Panicf("unexpected type %T in cache for %q.", ientry, name)
}

if time.Now().Before(entry.eol) {
return entry.val, true
}

ns.cache.Remove(name)

return "", false
}

func (ns *mpns) cacheSet(name string, val path.Path, ttl time.Duration) {
if ns.cache == nil || ttl <= 0 {
return
}
ns.cache.Add(name, cacheEntry{
val: val,
eol: time.Now().Add(ttl),
})
}

func (ns *mpns) cacheInvalidate(name string) {
if ns.cache == nil {
return
}
ns.cache.Remove(name)
}

type cacheEntry struct {
val path.Path
eol time.Time
}
Loading

0 comments on commit 9d2b6d9

Please sign in to comment.