Skip to content

Commit 693b89a

Browse files
committed
feat(ipld): use bindnode/registry
1 parent 7318c92 commit 693b89a

9 files changed

Lines changed: 174 additions & 148 deletions

File tree

benchmarks/benchmark_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func BenchmarkRoundtripSuccess(b *testing.B) {
7777

7878
func benchmarkRepeatedDisconnects(ctx context.Context, b *testing.B, numnodes int, df distFunc, tdm *tempDirMaker) {
7979
ctx, cancel := context.WithCancel(ctx)
80-
mn := mocknet.New(ctx)
80+
mn := mocknet.New()
8181
net := tn.StreamNet(ctx, mn)
8282
ig := testinstance.NewTestInstanceGenerator(ctx, net, nil, tdm, false)
8383
instances, err := ig.Instances(numnodes + 1)
@@ -145,7 +145,7 @@ func benchmarkRepeatedDisconnects(ctx context.Context, b *testing.B, numnodes in
145145
func p2pStrestTest(ctx context.Context, b *testing.B, numfiles int, df distFunc, tdm *tempDirMaker, options []graphsync.Option, diskBasedDatastore bool) {
146146
ctx, cancel := context.WithCancel(ctx)
147147
defer cancel()
148-
mn := mocknet.New(ctx)
148+
mn := mocknet.New()
149149
mn.SetLinkDefaults(mocknet.LinkOptions{Latency: 100 * time.Millisecond, Bandwidth: 3000000})
150150
net := tn.StreamNet(ctx, mn)
151151
ig := testinstance.NewTestInstanceGenerator(ctx, net, options, tdm, diskBasedDatastore)

go.mod

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ module github.com/ipfs/go-graphsync
33
go 1.16
44

55
require (
6-
github.com/google/go-cmp v0.5.7
6+
github.com/google/go-cmp v0.5.8
77
github.com/google/uuid v1.3.0
88
github.com/hannahhoward/cbor-gen-for v0.0.0-20200817222906-ea96cece81f1
99
github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e
10+
github.com/huin/goupnp v1.0.2 // indirect
1011
github.com/ipfs/go-block-format v0.0.3
1112
github.com/ipfs/go-blockservice v0.2.1
12-
github.com/ipfs/go-cid v0.1.0
13-
github.com/ipfs/go-datastore v0.5.0
13+
github.com/ipfs/go-cid v0.2.0
14+
github.com/ipfs/go-datastore v0.5.1
1415
github.com/ipfs/go-ds-badger v0.3.0
1516
github.com/ipfs/go-ipfs-blockstore v1.1.2
1617
github.com/ipfs/go-ipfs-blocksutil v0.0.1
@@ -22,26 +23,30 @@ require (
2223
github.com/ipfs/go-ipfs-routing v0.2.1
2324
github.com/ipfs/go-ipfs-util v0.0.2
2425
github.com/ipfs/go-ipld-format v0.2.0
25-
github.com/ipfs/go-log/v2 v2.3.0
26+
github.com/ipfs/go-log/v2 v2.5.1
2627
github.com/ipfs/go-merkledag v0.5.1
2728
github.com/ipfs/go-peertaskqueue v0.7.1
2829
github.com/ipfs/go-unixfs v0.3.1
2930
github.com/ipfs/go-unixfsnode v1.4.0
3031
github.com/ipld/go-codec-dagpb v1.3.1
31-
github.com/ipld/go-ipld-prime v0.16.0
32+
github.com/ipld/go-ipld-prime v0.16.1-0.20220616080238-da7f58c97b04
3233
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
34+
github.com/koron/go-ssdp v0.0.2 // indirect
3335
github.com/libp2p/go-buffer-pool v0.0.2
34-
github.com/libp2p/go-libp2p v0.16.0
35-
github.com/libp2p/go-libp2p-core v0.11.0
36+
github.com/libp2p/go-libp2p v0.20.1
37+
github.com/libp2p/go-libp2p-core v0.16.1
3638
github.com/libp2p/go-libp2p-netutil v0.1.0
37-
github.com/libp2p/go-libp2p-testing v0.5.0
38-
github.com/libp2p/go-msgio v0.1.0
39-
github.com/multiformats/go-multiaddr v0.4.0
39+
github.com/libp2p/go-libp2p-peerstore v0.7.0 // indirect
40+
github.com/libp2p/go-libp2p-testing v0.9.2
41+
github.com/libp2p/go-msgio v0.2.0
42+
github.com/multiformats/go-multiaddr v0.5.0
4043
github.com/multiformats/go-multihash v0.1.0
4144
github.com/stretchr/testify v1.7.0
4245
go.opentelemetry.io/otel v1.2.0
4346
go.opentelemetry.io/otel/sdk v1.2.0
4447
go.opentelemetry.io/otel/trace v1.2.0
48+
go.uber.org/atomic v1.9.0 // indirect
4549
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
50+
google.golang.org/grpc v1.40.0 // indirect
4651
google.golang.org/protobuf v1.27.1
4752
)

go.sum

Lines changed: 139 additions & 95 deletions
Large diffs are not rendered by default.

impl/graphsync_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ func newGsTestData(ctx context.Context, t *testing.T) *gsTestData {
19281928
func newOptionalGsTestData(ctx context.Context, t *testing.T, network1Protocols []protocol.ID, network2Protocols []protocol.ID) *gsTestData {
19291929
t.Helper()
19301930
td := &gsTestData{ctx: ctx}
1931-
td.mn = mocknet.New(ctx)
1931+
td.mn = mocknet.New()
19321932
var err error
19331933
// setup network
19341934
td.host1, err = td.mn.GenPeer()

message/ipldbind/message.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package ipldbind
22

33
import (
4+
_ "embed"
5+
46
cid "github.com/ipfs/go-cid"
57
"github.com/ipld/go-ipld-prime/datamodel"
8+
bindnoderegistry "github.com/ipld/go-ipld-prime/node/bindnode/registry"
69

710
"github.com/ipfs/go-graphsync"
811
"github.com/ipfs/go-graphsync/message"
912
)
1013

14+
//go:embed schema.ipldsch
15+
var embedSchema []byte
16+
17+
var BindnodeRegistry = bindnoderegistry.NewRegistry()
18+
1119
// GraphSyncExtensions is a container for representing extension data for
1220
// bindnode, it's converted to a graphsync.ExtensionData list by
1321
// ToExtensionsList()
@@ -95,3 +103,7 @@ type NamedExtension struct {
95103
Name graphsync.ExtensionName
96104
Data datamodel.Node
97105
}
106+
107+
func init() {
108+
BindnodeRegistry.RegisterType((*GraphSyncMessageRoot)(nil), string(embedSchema), "GraphSyncMessageRoot")
109+
}

message/ipldbind/schema.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

message/v2/ipld_roundtrip_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import (
55

66
blocks "github.com/ipfs/go-block-format"
77
"github.com/ipfs/go-cid"
8-
"github.com/ipld/go-ipld-prime"
98
"github.com/ipld/go-ipld-prime/codec/dagcbor"
109
"github.com/ipld/go-ipld-prime/datamodel"
1110
"github.com/ipld/go-ipld-prime/node/basicnode"
12-
"github.com/ipld/go-ipld-prime/node/bindnode"
1311
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
1412
"github.com/stretchr/testify/require"
1513

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

6765
// ipld TypedNode format
68-
node := bindnode.Wrap(igsm, ipldbind.Prototype.Message.Type())
69-
byts, err := ipld.Encode(node, dagcbor.Encode)
70-
// dag-cbor binary format
66+
byts, err := ipldbind.BindnodeRegistry.TypeToBytes(igsm, dagcbor.Encode)
7167
require.NoError(t, err)
7268

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

7873
// back to message format
7974
rtgsm, err := NewMessageHandler().fromIPLD(rtigsm.(*ipldbind.GraphSyncMessageRoot))

message/v2/message.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/ipld/go-ipld-prime"
1212
"github.com/ipld/go-ipld-prime/codec/dagcbor"
1313
"github.com/ipld/go-ipld-prime/datamodel"
14-
"github.com/ipld/go-ipld-prime/node/bindnode"
1514
"github.com/libp2p/go-libp2p-core/network"
1615
"github.com/libp2p/go-libp2p-core/peer"
1716
"github.com/libp2p/go-msgio"
@@ -44,11 +43,7 @@ func (mh *MessageHandler) FromMsgReader(_ peer.ID, r msgio.Reader) (message.Grap
4443
return message.GraphSyncMessage{}, err
4544
}
4645

47-
node, err := ipld.DecodeUsingPrototype(msg, dagcbor.Decode, ipldbind.Prototype.Message.Representation())
48-
if err != nil {
49-
return message.GraphSyncMessage{}, err
50-
}
51-
ipldGSM := bindnode.Unwrap(node)
46+
ipldGSM, err := ipldbind.BindnodeRegistry.TypeFromBytes(msg, (*ipldbind.GraphSyncMessageRoot)(nil), dagcbor.Decode)
5247
if err != nil {
5348
return message.GraphSyncMessage{}, err
5449
}
@@ -140,7 +135,7 @@ func (mh *MessageHandler) ToNet(_ peer.ID, gsm message.GraphSyncMessage, w io.Wr
140135
buf := new(bytes.Buffer)
141136
buf.Write(lbuf)
142137

143-
node := bindnode.Wrap(msg, ipldbind.Prototype.Message.Type())
138+
node := ipldbind.BindnodeRegistry.TypeToNode(msg)
144139
err = ipld.EncodeStreaming(buf, node.Representation(), dagcbor.Encode)
145140
if err != nil {
146141
return err

network/libp2p_impl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestMessageSendAndReceive(t *testing.T) {
5252
ctx := context.Background()
5353
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
5454
defer cancel()
55-
mn := mocknet.New(ctx)
55+
mn := mocknet.New()
5656

5757
host1, err := mn.GenPeer()
5858
require.NoError(t, err)

0 commit comments

Comments
 (0)