diff --git a/go.mod b/go.mod index fd1b4662..a3e30755 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 186c229a..c08ab07a 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/message/ipldbind/message.go b/message/ipldbind/message.go index f0e65f65..ac36e73f 100644 --- a/message/ipldbind/message.go +++ b/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() @@ -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()) + } +} diff --git a/message/ipldbind/schema.go b/message/ipldbind/schema.go deleted file mode 100644 index 8e570fda..00000000 --- a/message/ipldbind/schema.go +++ /dev/null @@ -1,25 +0,0 @@ -package ipldbind - -import ( - _ "embed" - - "github.com/ipld/go-ipld-prime" - "github.com/ipld/go-ipld-prime/node/bindnode" - "github.com/ipld/go-ipld-prime/schema" -) - -//go:embed schema.ipldsch -var embedSchema []byte - -var Prototype struct { - Message schema.TypedPrototype -} - -func init() { - ts, err := ipld.LoadSchemaBytes(embedSchema) - if err != nil { - panic(err) - } - - Prototype.Message = bindnode.Prototype((*GraphSyncMessageRoot)(nil), ts.TypeByName("GraphSyncMessageRoot")) -} diff --git a/message/v2/ipld_roundtrip_test.go b/message/v2/ipld_roundtrip_test.go index 1b83b6d8..cd0eecb0 100644 --- a/message/v2/ipld_roundtrip_test.go +++ b/message/v2/ipld_roundtrip_test.go @@ -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" @@ -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)) diff --git a/message/v2/message.go b/message/v2/message.go index 196c5d89..67098928 100644 --- a/message/v2/message.go +++ b/message/v2/message.go @@ -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" @@ -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 } @@ -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