Skip to content

Commit

Permalink
feat: human-readable cache keys for IPFS_NS_MAP (#38)
Browse files Browse the repository at this point in the history
* feat: use peer.String() to use a human-readable cache key
* fix: use normalized key representation
* fix: cache ipns key can be any format

This commit was moved from ipfs/go-namesys@af35385
  • Loading branch information
laurentsenta authored Mar 8, 2023
1 parent 00661e3 commit fdfe12c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
31 changes: 24 additions & 7 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/ipfs/go-path"
iface "github.com/ipfs/interface-go-ipfs-core"
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
ci "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -87,6 +88,23 @@ func WithDatastore(ds ds.Datastore) Option {
}
}

func loadStaticMap(list string) (map[string]path.Path, error) {
staticMap := make(map[string]path.Path)
for _, pair := range strings.Split(list, ",") {
mapping := strings.SplitN(pair, ":", 2)
key := mapping[0]
value := path.FromString(mapping[1])

ipnsKey, err := peer.Decode(key)
if err == nil {
key = iface.FormatKeyID(ipnsKey)
}

staticMap[key] = value
}
return staticMap, nil
}

// NewNameSystem will construct the IPFS naming system based on Routing
func NewNameSystem(r routing.ValueStore, opts ...Option) (NameSystem, error) {
var staticMap map[string]path.Path
Expand All @@ -96,12 +114,11 @@ func NewNameSystem(r routing.ValueStore, opts ...Option) (NameSystem, error) {
// Example:
// IPFS_NS_MAP="dnslink-test.example.com:/ipfs/bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am"
if list := os.Getenv("IPFS_NS_MAP"); list != "" {
staticMap = make(map[string]path.Path)
for _, pair := range strings.Split(list, ",") {
mapping := strings.SplitN(pair, ":", 2)
key := mapping[0]
value := path.FromString(mapping[1])
staticMap[key] = value
var err error
staticMap, err = loadStaticMap(list)

if err != nil {
return nil, err
}
}

Expand Down Expand Up @@ -215,7 +232,7 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.

cacheKey := key
if err == nil {
cacheKey = string(ipnsKey)
cacheKey = iface.FormatKeyID(ipnsKey)
}

if p, ok := ns.cacheGet(cacheKey); ok {
Expand Down
26 changes: 26 additions & 0 deletions namesys/namesys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ func TestNamesysResolution(t *testing.T) {
testResolution(t, r, "/ipns/bafzbeickencdqw37dpz3ha36ewrh4undfjt2do52chtcky4rxkj447qhdm", 1, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion)
}

func TestNamesysResolutionWithCache(t *testing.T) {
nsMap := "dnslink-test.example.com:/ipfs/bafyaaeykceeaeeqlgiydemzngazc2mrtbimaw,12D3KooWQbpsnyzdBcxw6GUMbijV8WgXE4L8EtfnbcQWLfyxBKho:/ipfs/bafyaagakcyeaeeqqgiydemzngazc2mrtfvuxa3ttbimba,k51qzi5uqu5dkwkqm42v9j9kqcam2jiuvloi16g72i4i4amoo2m8u3ol3mqu6s:/ipfs/bafyaahikdmeaeeqvgiydemzngazc2mrtfvuxa3ttfvsgk5lybimbk"

staticMap, err := loadStaticMap(nsMap)
if err != nil {
t.Fatal(err)
}

r := &mpns{
ipnsResolver: mockResolverOne(),
dnsResolver: mockResolverTwo(),
staticMap: staticMap,
}

testResolution(t, r, "/ipns/dnslink-test.example.com", opts.DefaultDepthLimit, "/ipfs/bafyaaeykceeaeeqlgiydemzngazc2mrtbimaw", nil)

testResolution(t, r, "/ipns/bafzaajaiaejcbw5i6oyqsktsn36r2vxgl2jzosyao46rybqztxt4rx4tfa3hpogg", opts.DefaultDepthLimit, "/ipfs/bafyaagakcyeaeeqqgiydemzngazc2mrtfvuxa3ttbimba", nil)
testResolution(t, r, "/ipns/k51qzi5uqu5dlnojhwrggtpty9c0cp5hvnkdozowth4eqb726jvoros8k9niyu", opts.DefaultDepthLimit, "/ipfs/bafyaagakcyeaeeqqgiydemzngazc2mrtfvuxa3ttbimba", nil)
testResolution(t, r, "/ipns/12D3KooWQbpsnyzdBcxw6GUMbijV8WgXE4L8EtfnbcQWLfyxBKho", opts.DefaultDepthLimit, "/ipfs/bafyaagakcyeaeeqqgiydemzngazc2mrtfvuxa3ttbimba", nil)

testResolution(t, r, "/ipns/bafzaajaiaejcbpltl72da5f3y7ojrtsa7hsfn5bbnkjbkwyesziqqtdry6vjilku", opts.DefaultDepthLimit, "/ipfs/bafyaahikdmeaeeqvgiydemzngazc2mrtfvuxa3ttfvsgk5lybimbk", nil)
testResolution(t, r, "/ipns/k51qzi5uqu5dkwkqm42v9j9kqcam2jiuvloi16g72i4i4amoo2m8u3ol3mqu6s", opts.DefaultDepthLimit, "/ipfs/bafyaahikdmeaeeqvgiydemzngazc2mrtfvuxa3ttfvsgk5lybimbk", nil)
testResolution(t, r, "/ipns/12D3KooWNZuG8phqhoNK9KWcUhwfzA3biDKNCUNVWEaJgigr6Acj", opts.DefaultDepthLimit, "/ipfs/bafyaahikdmeaeeqvgiydemzngazc2mrtfvuxa3ttfvsgk5lybimbk", nil)

}

func TestPublishWithCache0(t *testing.T) {
dst := dssync.MutexWrap(ds.NewMapDatastore())
priv, _, err := ci.GenerateKeyPair(ci.RSA, 2048)
Expand Down

0 comments on commit fdfe12c

Please sign in to comment.