Skip to content

Commit

Permalink
feat(ipld): use bindnode/registry
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jun 24, 2022
1 parent dc8dc0f commit 6565499
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 42 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/ipfs/go-unixfs v0.3.1
github.com/ipfs/go-unixfsnode v1.4.0
github.com/ipld/go-codec-dagpb v1.3.1
github.com/ipld/go-ipld-prime v0.17.0
github.com/ipld/go-ipld-prime v0.17.1-0.20220624062450-534ccf82237d
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/libp2p/go-buffer-pool v0.0.2
github.com/libp2p/go-libp2p v0.19.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ github.com/ipld/go-ipld-prime v0.9.1-0.20210324083106-dc342a9917db/go.mod h1:KvB
github.com/ipld/go-ipld-prime v0.11.0/go.mod h1:+WIAkokurHmZ/KwzDOMUuoeJgaRQktHtEaLglS3ZeV8=
github.com/ipld/go-ipld-prime v0.14.0/go.mod h1:9ASQLwUFLptCov6lIYc70GRB4V7UTyLD0IJtrDJe6ZM=
github.com/ipld/go-ipld-prime v0.16.0/go.mod h1:axSCuOCBPqrH+gvXr2w9uAOulJqBPhHPT2PjoiiU1qA=
github.com/ipld/go-ipld-prime v0.17.0 h1:+U2peiA3aQsE7mrXjD2nYZaZrCcakoz2Wge8K42Ld8g=
github.com/ipld/go-ipld-prime v0.17.0/go.mod h1:aYcKm5TIvGfY8P3QBKz/2gKcLxzJ1zDaD+o0bOowhgs=
github.com/ipld/go-ipld-prime v0.17.1-0.20220624062450-534ccf82237d h1:aY4pwcHVHonF+edc4gzRr3HA7vAaindLXz7InFIUgiY=
github.com/ipld/go-ipld-prime v0.17.1-0.20220624062450-534ccf82237d/go.mod h1:aYcKm5TIvGfY8P3QBKz/2gKcLxzJ1zDaD+o0bOowhgs=
github.com/ipld/go-ipld-prime/storage/bsadapter v0.0.0-20211210234204-ce2a1c70cd73/go.mod h1:2PJ0JgxyB08t0b2WKrcuqI3di0V+5n6RS/LTUJhkoxY=
github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA=
github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
Expand Down
14 changes: 14 additions & 0 deletions message/ipldbind/message.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package ipldbind

import (
_ "embed"

cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/datamodel"
bindnoderegistry "github.com/ipld/go-ipld-prime/node/bindnode/registry"

"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/message"
)

//go:embed schema.ipldsch
var embedSchema []byte

var BindnodeRegistry = bindnoderegistry.NewRegistry()

// GraphSyncExtensions is a container for representing extension data for
// bindnode, it's converted to a graphsync.ExtensionData list by
// ToExtensionsList()
Expand Down Expand Up @@ -95,3 +103,9 @@ type NamedExtension struct {
Name graphsync.ExtensionName
Data datamodel.Node
}

func init() {
if err := BindnodeRegistry.RegisterType((*GraphSyncMessageRoot)(nil), string(embedSchema), "GraphSyncMessageRoot"); err != nil {
panic(err.Error())
}
}
25 changes: 0 additions & 25 deletions message/ipldbind/schema.go

This file was deleted.

9 changes: 2 additions & 7 deletions message/v2/ipld_roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ 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"
"github.com/ipld/go-ipld-prime/node/bindnode"
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -65,15 +63,12 @@ func TestIPLDRoundTrip(t *testing.T) {
require.NoError(t, err)

// ipld TypedNode format
node := bindnode.Wrap(igsm, ipldbind.Prototype.Message.Type())
byts, err := ipld.Encode(node, dagcbor.Encode)
// dag-cbor binary format
byts, err := ipldbind.BindnodeRegistry.TypeToBytes(igsm, dagcbor.Encode)
require.NoError(t, err)

// back to bindnode internal format
rtnode, err := ipld.DecodeUsingPrototype(byts, dagcbor.Decode, ipldbind.Prototype.Message.Representation())
rtigsm, err := ipldbind.BindnodeRegistry.TypeFromBytes(byts, (*ipldbind.GraphSyncMessageRoot)(nil), dagcbor.Decode)
require.NoError(t, err)
rtigsm := bindnode.Unwrap(rtnode)

// back to message format
rtgsm, err := NewMessageHandler().fromIPLD(rtigsm.(*ipldbind.GraphSyncMessageRoot))
Expand Down
9 changes: 2 additions & 7 deletions message/v2/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"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"
Expand Down Expand Up @@ -44,11 +43,7 @@ func (mh *MessageHandler) FromMsgReader(_ peer.ID, r msgio.Reader) (message.Grap
return message.GraphSyncMessage{}, err
}

node, err := ipld.DecodeUsingPrototype(msg, dagcbor.Decode, ipldbind.Prototype.Message.Representation())
if err != nil {
return message.GraphSyncMessage{}, err
}
ipldGSM := bindnode.Unwrap(node)
ipldGSM, err := ipldbind.BindnodeRegistry.TypeFromBytes(msg, (*ipldbind.GraphSyncMessageRoot)(nil), dagcbor.Decode)
if err != nil {
return message.GraphSyncMessage{}, err
}
Expand Down Expand Up @@ -140,7 +135,7 @@ func (mh *MessageHandler) ToNet(_ peer.ID, gsm message.GraphSyncMessage, w io.Wr
buf := new(bytes.Buffer)
buf.Write(lbuf)

node := bindnode.Wrap(msg, ipldbind.Prototype.Message.Type())
node := ipldbind.BindnodeRegistry.TypeToNode(msg)
err = ipld.EncodeStreaming(buf, node.Representation(), dagcbor.Encode)
if err != nil {
return err
Expand Down

0 comments on commit 6565499

Please sign in to comment.