Skip to content

Commit

Permalink
feat(ipld): use bindnode/registry (#386)
Browse files Browse the repository at this point in the history
Co-authored-by: Hannah Howard <hannah@hannahhoward.net>
  • Loading branch information
rvagg and hannahhoward committed Jun 25, 2022
1 parent bc1b9b0 commit a95496c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 42 deletions.
2 changes: 1 addition & 1 deletion go.mod
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-libp2p v0.19.4
github.com/libp2p/go-libp2p-core v0.15.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
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
@@ -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
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
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 a95496c

Please sign in to comment.