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

chore(ipld): switch to using top-level ipld-prime codec helpers #383

Merged
merged 3 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 0 additions & 34 deletions ipldutil/ipldutil.go

This file was deleted.

6 changes: 2 additions & 4 deletions message/message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package message

import (
"bytes"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -47,9 +46,8 @@ type GraphSyncRequest struct {
func (gsr GraphSyncRequest) String() string {
sel := "nil"
if gsr.selector != nil {
var buf bytes.Buffer
dagjson.Encode(gsr.selector, &buf)
sel = buf.String()
byts, _ := ipld.Encode(gsr.selector, dagjson.Encode)
sel = string(byts)
}
extStr := strings.Builder{}
for _, name := range gsr.ExtensionNames() {
Expand Down
14 changes: 8 additions & 6 deletions message/v1/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
pool "github.com/libp2p/go-buffer-pool"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-msgio"
"google.golang.org/protobuf/proto"

"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/ipldutil"
"github.com/ipfs/go-graphsync/message"
pb "github.com/ipfs/go-graphsync/message/pb"
"github.com/ipfs/go-graphsync/message/v1/metadata"
Expand Down Expand Up @@ -106,7 +108,7 @@ func (mh *MessageHandler) ToProto(p peer.ID, gsm message.GraphSyncMessage) (*pb.
var selector []byte
var err error
if request.Selector() != nil {
selector, err = ipldutil.EncodeNode(request.Selector())
selector, err = ipld.Encode(request.Selector(), dagcbor.Encode)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -202,7 +204,7 @@ func (mh *MessageHandler) fromProto(p peer.ID, pbm *pb.Message) (message.GraphSy
return message.GraphSyncMessage{}, err
}

selector, err := ipldutil.DecodeNode(req.Selector)
selector, err := ipld.DecodeUsingPrototype(req.Selector, dagcbor.Decode, basicnode.Prototype.Any)
if err != nil {
return message.GraphSyncMessage{}, err
}
Expand Down Expand Up @@ -263,7 +265,7 @@ func toEncodedExtensions(part message.MessagePartWithExtensions, linkMetadata gr
if !ok || data == nil {
out[string(name)] = nil
} else {
byts, err := ipldutil.EncodeNode(data)
byts, err := ipld.Encode(data, dagcbor.Encode)
if err != nil {
return nil, err
}
Expand All @@ -276,7 +278,7 @@ func toEncodedExtensions(part message.MessagePartWithExtensions, linkMetadata gr
md = append(md, metadata.Item{Link: c, BlockPresent: la == graphsync.LinkActionPresent})
})
mdNode := metadata.EncodeMetadata(md)
mdByts, err := ipldutil.EncodeNode(mdNode)
mdByts, err := ipld.Encode(mdNode, dagcbor.Encode)
if err != nil {
return nil, err
}
Expand All @@ -295,7 +297,7 @@ func fromEncodedExtensions(in map[string][]byte) ([]graphsync.ExtensionData, []m
var node datamodel.Node
var err error
if len(data) > 0 {
node, err = ipldutil.DecodeNode(data)
node, err = ipld.DecodeUsingPrototype(data, dagcbor.Decode, basicnode.Prototype.Any)
if err != nil {
return nil, nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions message/v1/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/require"

"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/ipldutil"
"github.com/ipfs/go-graphsync/message"
"github.com/ipfs/go-graphsync/testutil"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestAppendingRequests(t *testing.T) {

pbMessage, err := mh.ToProto(peer.ID("foo"), gsm)
require.NoError(t, err, "serialize to protobuf errored")
selectorEncoded, err := ipldutil.EncodeNode(selector)
selectorEncoded, err := ipld.Encode(selector, dagcbor.Encode)
require.NoError(t, err)

pbRequest := pbMessage.Requests[0]
Expand Down
10 changes: 3 additions & 7 deletions message/v2/ipld_roundtrip_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package v2

import (
"bytes"
"testing"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
Expand Down Expand Up @@ -65,18 +65,14 @@ func TestIPLDRoundTrip(t *testing.T) {
require.NoError(t, err)

// ipld TypedNode format
var buf bytes.Buffer
node := bindnode.Wrap(igsm, ipldbind.Prototype.Message.Type())

byts, err := ipld.Encode(node, dagcbor.Encode)
// dag-cbor binary format
err = dagcbor.Encode(node.Representation(), &buf)
require.NoError(t, err)

// back to bindnode internal format
builder := ipldbind.Prototype.Message.Representation().NewBuilder()
err = dagcbor.Decode(builder, &buf)
rtnode, err := ipld.DecodeUsingPrototype(byts, dagcbor.Decode, ipldbind.Prototype.Message.Representation())
require.NoError(t, err)
rtnode := builder.Build()
rtigsm := bindnode.Unwrap(rtnode)

// back to message format
Expand Down
7 changes: 4 additions & 3 deletions message/v2/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-msgio"

"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/ipldutil"
"github.com/ipfs/go-graphsync/message"
"github.com/ipfs/go-graphsync/message/ipldbind"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ func (mh *MessageHandler) FromMsgReader(_ peer.ID, r msgio.Reader) (message.Grap
return message.GraphSyncMessage{}, err
}

node, err := ipldutil.DecodeNodeInto(msg, ipldbind.Prototype.Message.Representation().NewBuilder())
node, err := ipld.DecodeUsingPrototype(msg, dagcbor.Decode, ipldbind.Prototype.Message.Representation())
if err != nil {
return message.GraphSyncMessage{}, err
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func (mh *MessageHandler) ToNet(_ peer.ID, gsm message.GraphSyncMessage, w io.Wr
buf.Write(lbuf)

node := bindnode.Wrap(msg, ipldbind.Prototype.Message.Type())
err = ipldutil.EncodeNodeInto(node.Representation(), buf)
err = ipld.EncodeStreaming(buf, node.Representation(), dagcbor.Encode)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions requestmanager/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-peertaskqueue/peertask"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
_ "github.com/ipld/go-ipld-prime/codec/raw"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Expand Down
3 changes: 2 additions & 1 deletion requestmanager/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ipfs/go-peertaskqueue/peertask"
"github.com/ipfs/go-peertaskqueue/peertracker"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Expand Down Expand Up @@ -376,7 +377,7 @@ func (rm *RequestManager) validateRequest(requestID graphsync.RequestID, p peer.
if err != nil {
return gsmsg.GraphSyncRequest{}, hooks.RequestResult{}, nil, err
}
_, err = ipldutil.EncodeNode(selectorSpec)
_, err = ipld.Encode(selectorSpec, dagcbor.Encode)
if err != nil {
return gsmsg.GraphSyncRequest{}, hooks.RequestResult{}, nil, err
}
Expand Down
1 change: 1 addition & 0 deletions testutil/testchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only awkward bit of removing ipldutil.go entirely - it preloaded dagcbor and raw codecs; but there are some places that use testchain that don't touch the codecs directly but they're encountered by the loader because testchain uses a dagcbor cidlink.LinkPrototype.
But I think it's not unreasonable to add this here since it's also referencing cid.DagCBOR in this file.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds fine. oy. I wonder if we should just preload them in the root package at some point

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my initial take was to put them both there - but when you use graphsync as a whole (not just as a unit test) you're going to get dagcbor no matter what because it's in the messaging layer. raw is going to come if you need it, but that's up to the user IMO, maybe they want to add dagjson, dagjose, git, and more, but that's up to them so I'm fine with localising this I think.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool.

"github.com/ipld/go-ipld-prime/datamodel"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
Expand Down