From 36a159c9e5b182ad5ecf11de5f2344e1888d424d Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Fri, 27 May 2022 19:10:52 +1000 Subject: [PATCH 1/2] chore(ipld): switch to using top-level ipld-prime codec helpers Ref: https://github.com/ipld/go-ipld-prime/issues/428 --- ipldutil/ipldutil.go | 19 ++++++------------- message/message.go | 6 ++---- message/v2/ipld_roundtrip_test.go | 10 +++------- message/v2/message.go | 2 +- 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/ipldutil/ipldutil.go b/ipldutil/ipldutil.go index c197b913..450f6e3f 100644 --- a/ipldutil/ipldutil.go +++ b/ipldutil/ipldutil.go @@ -3,6 +3,7 @@ package ipldutil import ( "bytes" + "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/codec/dagcbor" _ "github.com/ipld/go-ipld-prime/codec/raw" "github.com/ipld/go-ipld-prime/datamodel" @@ -10,25 +11,17 @@ import ( ) func EncodeNode(node datamodel.Node) ([]byte, error) { - var buffer bytes.Buffer - err := EncodeNodeInto(node, &buffer) - if err != nil { - return nil, err - } - return buffer.Bytes(), nil + return ipld.Encode(node, dagcbor.Encode) } func EncodeNodeInto(node datamodel.Node, buffer *bytes.Buffer) error { - return dagcbor.Encode(node, buffer) + return ipld.EncodeStreaming(buffer, node, dagcbor.Encode) } func DecodeNode(encoded []byte) (datamodel.Node, error) { - return DecodeNodeInto(encoded, basicnode.Prototype.Any.NewBuilder()) + return DecodeNodeInto(encoded, basicnode.Prototype.Any) } -func DecodeNodeInto(encoded []byte, nb datamodel.NodeBuilder) (datamodel.Node, error) { - if err := dagcbor.Decode(nb, bytes.NewReader(encoded)); err != nil { - return nil, err - } - return nb.Build(), nil +func DecodeNodeInto(encoded []byte, proto datamodel.NodePrototype) (datamodel.Node, error) { + return ipld.DecodeUsingPrototype(encoded, dagcbor.Decode, proto) } diff --git a/message/message.go b/message/message.go index 354cc236..59991a41 100644 --- a/message/message.go +++ b/message/message.go @@ -1,7 +1,6 @@ package message import ( - "bytes" "fmt" "io" "strings" @@ -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() { diff --git a/message/v2/ipld_roundtrip_test.go b/message/v2/ipld_roundtrip_test.go index 51e5ace3..1b83b6d8 100644 --- a/message/v2/ipld_roundtrip_test.go +++ b/message/v2/ipld_roundtrip_test.go @@ -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" @@ -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 diff --git a/message/v2/message.go b/message/v2/message.go index 2ce35bc0..de97df6b 100644 --- a/message/v2/message.go +++ b/message/v2/message.go @@ -43,7 +43,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 := ipldutil.DecodeNodeInto(msg, ipldbind.Prototype.Message.Representation()) if err != nil { return message.GraphSyncMessage{}, err } From e0985698466e0c6c570ae8b62d53e8d934391a1f Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 31 May 2022 13:13:27 +1000 Subject: [PATCH 2/2] fixup! chore(ipld): switch to using top-level ipld-prime codec helpers --- ipldutil/ipldutil.go | 27 ------------------------ message/v1/message.go | 14 ++++++------ message/v1/message_test.go | 5 +++-- message/v2/message.go | 7 +++--- requestmanager/executor/executor_test.go | 2 ++ requestmanager/server.go | 3 ++- testutil/testchain.go | 1 + 7 files changed, 20 insertions(+), 39 deletions(-) delete mode 100644 ipldutil/ipldutil.go diff --git a/ipldutil/ipldutil.go b/ipldutil/ipldutil.go deleted file mode 100644 index 450f6e3f..00000000 --- a/ipldutil/ipldutil.go +++ /dev/null @@ -1,27 +0,0 @@ -package ipldutil - -import ( - "bytes" - - "github.com/ipld/go-ipld-prime" - "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/node/basicnode" -) - -func EncodeNode(node datamodel.Node) ([]byte, error) { - return ipld.Encode(node, dagcbor.Encode) -} - -func EncodeNodeInto(node datamodel.Node, buffer *bytes.Buffer) error { - return ipld.EncodeStreaming(buffer, node, dagcbor.Encode) -} - -func DecodeNode(encoded []byte) (datamodel.Node, error) { - return DecodeNodeInto(encoded, basicnode.Prototype.Any) -} - -func DecodeNodeInto(encoded []byte, proto datamodel.NodePrototype) (datamodel.Node, error) { - return ipld.DecodeUsingPrototype(encoded, dagcbor.Decode, proto) -} diff --git a/message/v1/message.go b/message/v1/message.go index 0554c355..5d76163b 100644 --- a/message/v1/message.go +++ b/message/v1/message.go @@ -9,7 +9,10 @@ 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" @@ -17,7 +20,6 @@ import ( "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" @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/message/v1/message_test.go b/message/v1/message_test.go index d44fb38a..8a362418 100644 --- a/message/v1/message_test.go +++ b/message/v1/message_test.go @@ -8,6 +8,8 @@ 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" @@ -15,7 +17,6 @@ import ( "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" ) @@ -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] diff --git a/message/v2/message.go b/message/v2/message.go index de97df6b..196c5d89 100644 --- a/message/v2/message.go +++ b/message/v2/message.go @@ -8,6 +8,8 @@ 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" @@ -15,7 +17,6 @@ import ( "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" ) @@ -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()) + node, err := ipld.DecodeUsingPrototype(msg, dagcbor.Decode, ipldbind.Prototype.Message.Representation()) if err != nil { return message.GraphSyncMessage{}, err } @@ -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 } diff --git a/requestmanager/executor/executor_test.go b/requestmanager/executor/executor_test.go index a3e8562b..0617c698 100644 --- a/requestmanager/executor/executor_test.go +++ b/requestmanager/executor/executor_test.go @@ -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" diff --git a/requestmanager/server.go b/requestmanager/server.go index 96f8848f..780c7ea0 100644 --- a/requestmanager/server.go +++ b/requestmanager/server.go @@ -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" @@ -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 } diff --git a/testutil/testchain.go b/testutil/testchain.go index 01142537..cc2874ae 100644 --- a/testutil/testchain.go +++ b/testutil/testchain.go @@ -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"