fix: use bitswap sessions for ipfs refs
This isn't perfect (we only use sessions after resolving the root cid) but it's better than what we have. The real solution is #7198 so we can use sessions everywhere.
Steven Allen committed
May 28, 2020 at 17:12 UTC
62f61c588d75ceafa1974f0584ffbf35950be2ca
2 files changed
+24
-8
core/commands/refs.go
+14
-8
@@ -13,6 +13,7 @@ import (
13
cidenc "github.com/ipfs/go-cidutil/cidenc"
14
cmds "github.com/ipfs/go-ipfs-cmds"
15
ipld "github.com/ipfs/go-ipld-format"
16
+ merkledag "github.com/ipfs/go-merkledag"
17
iface "github.com/ipfs/interface-go-ipfs-core"
18
path "github.com/ipfs/interface-go-ipfs-core/path"
19
)
@@ -102,6 +103,7 @@ NOTE: List all references recursively by using the flag '-r'.
103
format = "<src> -> <dst>"
104
}
105
106
+ // TODO: use session for resolving as well.
107
objs, err := objectsForPaths(ctx, api, req.Arguments)
108
if err != nil {
109
return err
@@ -109,7 +111,7 @@ NOTE: List all references recursively by using the flag '-r'.
111
112
rw := RefWriter{
113
res: res,
112
- DAG: api.Dag(),
114
+ DAG: merkledag.NewSession(ctx, api.Dag()),
115
Ctx: ctx,
116
Unique: unique,
117
PrintFmt: format,
@@ -164,16 +166,16 @@ Displays the hashes of all local objects.
166
Type: RefWrapper{},
167
}
168
167
-func objectsForPaths(ctx context.Context, n iface.CoreAPI, paths []string) ([]ipld.Node, error) {
168
- objects := make([]ipld.Node, len(paths))
169
+func objectsForPaths(ctx context.Context, n iface.CoreAPI, paths []string) ([]cid.Cid, error) {
170
+ roots := make([]cid.Cid, len(paths))
171
for i, sp := range paths {
170
- o, err := n.ResolveNode(ctx, path.New(sp))
172
+ o, err := n.ResolvePath(ctx, path.New(sp))
173
if err != nil {
174
return nil, err
175
}
174
- objects[i] = o
176
+ roots[i] = o.Cid()
177
}
176
- return objects, nil
178
+ return roots, nil
179
}
180
181
type RefWrapper struct {
@@ -183,7 +185,7 @@ type RefWrapper struct {
185
186
type RefWriter struct {
187
res cmds.ResponseEmitter
186
- DAG ipld.DAGService
188
+ DAG ipld.NodeGetter
189
Ctx context.Context
190
191
Unique bool
@@ -194,7 +196,11 @@ type RefWriter struct {
196
}
197
198
// WriteRefs writes refs of the given object to the underlying writer.
197
-func (rw *RefWriter) WriteRefs(n ipld.Node, enc cidenc.Encoder) (int, error) {
199
+func (rw *RefWriter) WriteRefs(c cid.Cid, enc cidenc.Encoder) (int, error) {
200
+ n, err := rw.DAG.Get(rw.Ctx, c)
201
+ if err != nil {
202
+ return 0, err
203
+ }
204
return rw.writeRefsRecursive(n, 0, enc)
205
}
206
core/coreapi/dag.go
+10
@@ -6,6 +6,7 @@ import (
6
cid "github.com/ipfs/go-cid"
7
"github.com/ipfs/go-ipfs-pinner"
8
ipld "github.com/ipfs/go-ipld-format"
9
+ dag "github.com/ipfs/go-merkledag"
10
)
11
12
type dagAPI struct {
@@ -50,3 +51,12 @@ func (adder *pinningAdder) AddMany(ctx context.Context, nds []ipld.Node) error {
51
func (api *dagAPI) Pinning() ipld.NodeAdder {
52
return (*pinningAdder)(api.core)
53
}
54
+
55
+func (api *dagAPI) Session(ctx context.Context) ipld.NodeGetter {
56
+ return dag.NewSession(ctx, api.DAGService)
57
+}
58
+
59
+var (
60
+ _ ipld.DAGService = (*dagAPI)(nil)
61
+ _ dag.SessionMaker = (*dagAPI)(nil)
62
+)