Skip to content

Commit

Permalink
chore(ipld): switch to using top-level ipld-prime codec helpers (#383)
Browse files Browse the repository at this point in the history
* chore(ipld): switch to using top-level ipld-prime codec helpers

Ref: ipld/go-ipld-prime#428

* fixup! chore(ipld): switch to using top-level ipld-prime codec helpers

Co-authored-by: Hannah Howard <[email protected]>
  • Loading branch information
rvagg and hannahhoward authored May 31, 2022
1 parent a868ee9 commit fa5a9f2
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 57 deletions.
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 @@ -375,7 +376,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"
"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

0 comments on commit fa5a9f2

Please sign in to comment.