Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: path consolidation #10063

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/ipfs/kubo/core/coreapi"

options "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
cid "github.com/ipfs/go-cid"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
return cid.Cid{}, err
}

basePath := path.IpfsPath(dirb.Cid())
basePath := path.FromCid(dirb.Cid())

for _, p := range l {
d, err := Asset.ReadFile(p)
Expand All @@ -69,5 +69,5 @@ func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
return cid.Cid{}, err
}

return basePath.Cid(), nil
return basePath.RootCid(), nil
}
14 changes: 11 additions & 3 deletions client/rpc/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"time"

iface "github.com/ipfs/boxo/coreiface"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/coreiface/tests"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/test/cli/harness"
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/multierr"
Expand Down Expand Up @@ -70,7 +70,11 @@ func (np NodeProvider) MakeAPISwarm(t *testing.T, ctx context.Context, fullIdent
apis[i] = api

// empty node is pinned even with --empty-repo, we don't want that
emptyNode := path.New("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
emptyNode, err := path.NewPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
if err != nil {
return err
}

if err := api.Pin().Rm(ctx, emptyNode); err != nil {
return err
}
Expand Down Expand Up @@ -126,7 +130,11 @@ func Test_NewURLApiWithClient_With_Headers(t *testing.T) {
t.Fatal(err)
}
api.Headers.Set(headerToTest, expectedHeaderValue)
if err := api.Pin().Rm(context.Background(), path.New("/ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")); err != nil {
p, err := path.NewPath("/ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")
if err != nil {
t.Fatal(err)
}
if err := api.Pin().Rm(context.Background(), p); err != nil {
t.Fatal(err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions client/rpc/apifile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"fmt"
"io"

"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/files"
unixfs "github.com/ipfs/boxo/ipld/unixfs"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
)

Expand All @@ -17,7 +17,7 @@
func (api *UnixfsAPI) Get(ctx context.Context, p path.Path) (files.Node, error) {
if p.Mutable() { // use resolved path in case we are dealing with IPNS / MFS
var err error
p, err = api.core().ResolvePath(ctx, p)
p, _, err = api.core().ResolvePath(ctx, p)

Check warning on line 20 in client/rpc/apifile.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/apifile.go#L20

Added line #L20 was not covered by tests
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,13 +195,13 @@

switch it.cur.Type {
case unixfs.THAMTShard, unixfs.TMetadata, unixfs.TDirectory:
it.curFile, err = it.core.getDir(it.ctx, path.IpfsPath(c), int64(it.cur.Size))
it.curFile, err = it.core.getDir(it.ctx, path.FromCid(c), int64(it.cur.Size))
if err != nil {
it.err = err
return false
}
case unixfs.TFile:
it.curFile, err = it.core.getFile(it.ctx, path.IpfsPath(c), int64(it.cur.Size))
it.curFile, err = it.core.getFile(it.ctx, path.FromCid(c), int64(it.cur.Size))
if err != nil {
it.err = err
return false
Expand Down
6 changes: 3 additions & 3 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

iface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
mc "github.com/multiformats/go-multicodec"
mh "github.com/multiformats/go-multihash"
Expand All @@ -27,8 +27,8 @@ func (s *blockStat) Size() int {
return s.BSize
}

func (s *blockStat) Path() path.Resolved {
return path.IpldPath(s.cid)
func (s *blockStat) Path() path.ImmutablePath {
return path.FromCid(s.cid)
}

func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.BlockPutOption) (iface.BlockStat, error) {
Expand Down
12 changes: 6 additions & 6 deletions client/rpc/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"io"

"github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/go-block-format"
"github.com/ipfs/boxo/path"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
multicodec "github.com/multiformats/go-multicodec"
Expand All @@ -21,7 +21,7 @@
)

func (api *HttpDagServ) Get(ctx context.Context, c cid.Cid) (format.Node, error) {
r, err := api.core().Block().Get(ctx, path.IpldPath(c))
r, err := api.core().Block().Get(ctx, path.FromCid(c))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -79,8 +79,8 @@
if err != nil {
return err
}
if !stat.Path().Cid().Equals(c) {
return fmt.Errorf("cids didn't match - local %s, remote %s", c.String(), stat.Path().Cid().String())
if !stat.Path().RootCid().Equals(c) {
return fmt.Errorf("cids didn't match - local %s, remote %s", c.String(), stat.Path().RootCid().String())

Check warning on line 83 in client/rpc/dag.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/dag.go#L83

Added line #L83 was not covered by tests
}
return nil
}
Expand Down Expand Up @@ -116,7 +116,7 @@
}

func (api *HttpDagServ) Remove(ctx context.Context, c cid.Cid) error {
return api.core().Block().Rm(ctx, path.IpldPath(c)) // TODO: should we force rm?
return api.core().Block().Rm(ctx, path.FromCid(c)) // TODO: should we force rm?

Check warning on line 119 in client/rpc/dag.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/dag.go#L119

Added line #L119 was not covered by tests
}

func (api *HttpDagServ) RemoveMany(ctx context.Context, cids []cid.Cid) error {
Expand Down
10 changes: 5 additions & 5 deletions client/rpc/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"

caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/path"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
)
Expand Down Expand Up @@ -42,12 +42,12 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
return nil, err
}

rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}

resp, err := api.core().Request("dht/findprovs", rp.Cid().String()).
resp, err := api.core().Request("dht/findprovs", rp.RootCid().String()).
Option("num-providers", options.NumProviders).
Send(ctx)
if err != nil {
Expand Down Expand Up @@ -98,12 +98,12 @@ func (api *DhtAPI) Provide(ctx context.Context, p path.Path, opts ...caopts.DhtP
return err
}

rp, err := api.core().ResolvePath(ctx, p)
rp, _, err := api.core().ResolvePath(ctx, p)
if err != nil {
return err
}

return api.core().Request("dht/provide", rp.Cid().String()).
return api.core().Request("dht/provide", rp.RootCid().String()).
Option("recursive", options.Recursive).
Exec(ctx, nil)
}
Expand Down
74 changes: 47 additions & 27 deletions client/rpc/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,50 @@

iface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/path"
"github.com/libp2p/go-libp2p/core/peer"
)

type KeyAPI HttpApi

type keyOutput struct {
JName string `json:"Name"`
Id string
type key struct {
name string
pid peer.ID
path path.Path
}

func newKey(name, pidStr string) (*key, error) {
pid, err := peer.Decode(pidStr)
if err != nil {
return nil, err
}

Check warning on line 26 in client/rpc/key.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/key.go#L25-L26

Added lines #L25 - L26 were not covered by tests

pid peer.ID
path, err := path.NewPath("/ipns/" + ipns.NameFromPeer(pid).String())
if err != nil {
return nil, err
}

Check warning on line 31 in client/rpc/key.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/key.go#L30-L31

Added lines #L30 - L31 were not covered by tests

return &key{name: name, pid: pid, path: path}, nil
}

func (k *keyOutput) Name() string {
return k.JName
func (k *key) Name() string {
return k.name
}

func (k *keyOutput) Path() path.Path {
return path.New("/ipns/" + k.Id)
func (k *key) Path() path.Path {
return k.path
}

func (k *keyOutput) ID() peer.ID {
func (k *key) ID() peer.ID {
return k.pid
}

type keyOutput struct {
Name string
Id string
}

func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (iface.Key, error) {
options, err := caopts.KeyGenerateOptions(opts...)
if err != nil {
Expand All @@ -45,8 +64,8 @@
if err != nil {
return nil, err
}
out.pid, err = peer.Decode(out.Id)
return &out, err

return newKey(out.Name, out.Id)
}

func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (iface.Key, bool, error) {
Expand All @@ -68,25 +87,29 @@
return nil, false, err
}

id := &keyOutput{JName: out.Now, Id: out.Id}
id.pid, err = peer.Decode(id.Id)
return id, out.Overwrite, err
key, err := newKey(out.Now, out.Id)
if err != nil {
return nil, false, err
}

Check warning on line 93 in client/rpc/key.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/key.go#L92-L93

Added lines #L92 - L93 were not covered by tests

return key, out.Overwrite, err
}

func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
var out struct{ Keys []*keyOutput }
var out struct {
Keys []keyOutput
}
if err := api.core().Request("key/list").Exec(ctx, &out); err != nil {
return nil, err
}

res := make([]iface.Key, len(out.Keys))
for i, k := range out.Keys {
var err error
k.pid, err = peer.Decode(k.Id)
key, err := newKey(k.Name, k.Id)
if err != nil {
return nil, err
}
res[i] = k
res[i] = key
}

return res, nil
Expand All @@ -98,24 +121,21 @@
return nil, err
}

var err error
out := keyOutput{JName: "self", Id: id.ID}
out.pid, err = peer.Decode(out.Id)
return &out, err
return newKey("self", id.ID)
}

func (api *KeyAPI) Remove(ctx context.Context, name string) (iface.Key, error) {
var out struct{ Keys []keyOutput }
var out struct {
Keys []keyOutput
}
if err := api.core().Request("key/rm", name).Exec(ctx, &out); err != nil {
return nil, err
}
if len(out.Keys) != 1 {
return nil, errors.New("got unexpected number of keys back")
}

var err error
out.Keys[0].pid, err = peer.Decode(out.Keys[0].Id)
return &out.Keys[0], err
return newKey(out.Keys[0].Name, out.Keys[0].Id)
}

func (api *KeyAPI) core() *HttpApi {
Expand Down
10 changes: 7 additions & 3 deletions client/rpc/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
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/coreiface/path"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/path"
)

type NameAPI HttpApi
Expand Down Expand Up @@ -84,7 +84,11 @@
}
var ires iface.IpnsResult
if err == nil {
ires.Path = path.New(out.Path)
p, err := path.NewPath(out.Path)
if err != nil {
return
}
ires.Path = p

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

View check run for this annotation

Codecov / codecov/patch

client/rpc/name.go#L87-L91

Added lines #L87 - L91 were not covered by tests
}

select {
Expand Down Expand Up @@ -122,7 +126,7 @@
return nil, err
}

return path.New(out.Path), nil
return path.NewPath(out.Path)
}

func (api *NameAPI) core() *HttpApi {
Expand Down
Loading
Loading