From 7a58f873f4dfc8e87de25021963e08e8315a5c89 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Fri, 8 May 2020 12:10:14 +0530 Subject: [PATCH 1/3] peer supports any protocol --- go.mod | 3 ++- go.sum | 2 ++ pstoreds/protobook.go | 22 ++++++++++++++++++++++ pstoremem/protobook.go | 17 +++++++++++++++++ test/peerstore_suite.go | 13 +++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d6e8eee..bc6b55b 100644 --- a/go.mod +++ b/go.mod @@ -8,13 +8,14 @@ require ( github.com/ipfs/go-ds-leveldb v0.4.2 github.com/ipfs/go-log v1.0.3 github.com/libp2p/go-buffer-pool v0.0.2 - github.com/libp2p/go-libp2p-core v0.5.1 + github.com/libp2p/go-libp2p-core v0.5.4-0.20200508062439-98b95a487749 github.com/multiformats/go-base32 v0.0.3 github.com/multiformats/go-multiaddr v0.2.1 github.com/multiformats/go-multiaddr-fmt v0.1.0 github.com/multiformats/go-multiaddr-net v0.1.4 github.com/multiformats/go-multihash v0.0.13 github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.4.0 github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 go.uber.org/goleak v1.0.0 golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect diff --git a/go.sum b/go.sum index c352e93..f37a6c1 100644 --- a/go.sum +++ b/go.sum @@ -95,6 +95,8 @@ github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoR github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= github.com/libp2p/go-libp2p-core v0.5.1 h1:6Cu7WljPQtGY2krBlMoD8L/zH3tMUsCbqNFH7cZwCoI= github.com/libp2p/go-libp2p-core v0.5.1/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= +github.com/libp2p/go-libp2p-core v0.5.4-0.20200508062439-98b95a487749 h1:G0zRpRnpZ8sAZ5E5IRB6x7np9iuulPIWE+y3I/xGNfo= +github.com/libp2p/go-libp2p-core v0.5.4-0.20200508062439-98b95a487749/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-openssl v0.0.4 h1:d27YZvLoTyMhIN4njrkr8zMDOM4lfpHIp6A+TK9fovg= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= diff --git a/pstoreds/protobook.go b/pstoreds/protobook.go index 84d546b..ae121a2 100644 --- a/pstoreds/protobook.go +++ b/pstoreds/protobook.go @@ -122,6 +122,28 @@ func (pb *dsProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]string, return res, nil } +func (pb *dsProtoBook) SupportsAnyProtocol(p peer.ID, protos ...string) (bool, error) { + if err := p.Validate(); err != nil { + return false, err + } + + s := pb.segments.get(p) + s.RLock() + defer s.RUnlock() + + pmap, err := pb.getProtocolMap(p) + if err != nil { + return false, err + } + for _, proto := range protos { + if _, ok := pmap[proto]; ok { + return true, nil + } + } + + return false, nil +} + func (pb *dsProtoBook) RemoveProtocols(p peer.ID, protos ...string) error { if err := p.Validate(); err != nil { return err diff --git a/pstoremem/protobook.go b/pstoremem/protobook.go index af44c6f..83e1d25 100644 --- a/pstoremem/protobook.go +++ b/pstoremem/protobook.go @@ -163,3 +163,20 @@ func (pb *memoryProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]str return out, nil } + +func (pb *memoryProtoBook) SupportsAnyProtocol(p peer.ID, protos ...string) (bool, error) { + if err := p.Validate(); err != nil { + return false, err + } + + s := pb.segments.get(p) + s.RLock() + defer s.RUnlock() + + for _, proto := range protos { + if _, ok := s.protocols[p][proto]; ok { + return true, nil + } + } + return false, nil +} diff --git a/test/peerstore_suite.go b/test/peerstore_suite.go index 5797297..a008f5d 100644 --- a/test/peerstore_suite.go +++ b/test/peerstore_suite.go @@ -14,6 +14,7 @@ import ( ma "github.com/multiformats/go-multiaddr" pstore "github.com/libp2p/go-libp2p-core/peerstore" + "github.com/stretchr/testify/require" ) var peerstoreSuite = map[string]func(pstore.Peerstore) func(*testing.T){ @@ -242,6 +243,18 @@ func testPeerstoreProtoStore(ps pstore.Peerstore) func(t *testing.T) { t.Fatal("got wrong supported array: ", supported) } + b, err := ps.SupportsAnyProtocol(p1, "q", "w", "a", "y", "b") + require.NoError(t, err) + require.True(t, b) + + b, err = ps.SupportsAnyProtocol(p1, "a") + require.NoError(t, err) + require.True(t, b) + + b, err = ps.SupportsAnyProtocol(p1, "q") + require.NoError(t, err) + require.False(t, b) + protos = []string{"other", "yet another", "one more"} err = ps.SetProtocols(p1, protos...) if err != nil { From dd0775df6d2286cf73efeae131add0bd540b162a Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Mon, 11 May 2020 13:07:08 +0530 Subject: [PATCH 2/3] changes for the new interface --- go.mod | 2 +- go.sum | 2 ++ pstoreds/protobook.go | 10 +++++----- pstoremem/protobook.go | 8 ++++---- test/peerstore_suite.go | 12 ++++++------ 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index bc6b55b..f0bd68c 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/ipfs/go-ds-leveldb v0.4.2 github.com/ipfs/go-log v1.0.3 github.com/libp2p/go-buffer-pool v0.0.2 - github.com/libp2p/go-libp2p-core v0.5.4-0.20200508062439-98b95a487749 + github.com/libp2p/go-libp2p-core v0.5.4-0.20200511073005-dabf5bbfb028 github.com/multiformats/go-base32 v0.0.3 github.com/multiformats/go-multiaddr v0.2.1 github.com/multiformats/go-multiaddr-fmt v0.1.0 diff --git a/go.sum b/go.sum index f37a6c1..c0c3b4a 100644 --- a/go.sum +++ b/go.sum @@ -97,6 +97,8 @@ github.com/libp2p/go-libp2p-core v0.5.1 h1:6Cu7WljPQtGY2krBlMoD8L/zH3tMUsCbqNFH7 github.com/libp2p/go-libp2p-core v0.5.1/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= github.com/libp2p/go-libp2p-core v0.5.4-0.20200508062439-98b95a487749 h1:G0zRpRnpZ8sAZ5E5IRB6x7np9iuulPIWE+y3I/xGNfo= github.com/libp2p/go-libp2p-core v0.5.4-0.20200508062439-98b95a487749/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= +github.com/libp2p/go-libp2p-core v0.5.4-0.20200511073005-dabf5bbfb028 h1:nQb4SDWcadn/kpWtMwsWweogIzYviVE5qOMT42bkyNE= +github.com/libp2p/go-libp2p-core v0.5.4-0.20200511073005-dabf5bbfb028/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-openssl v0.0.4 h1:d27YZvLoTyMhIN4njrkr8zMDOM4lfpHIp6A+TK9fovg= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc= diff --git a/pstoreds/protobook.go b/pstoreds/protobook.go index ae121a2..6518c4e 100644 --- a/pstoreds/protobook.go +++ b/pstoreds/protobook.go @@ -122,9 +122,9 @@ func (pb *dsProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]string, return res, nil } -func (pb *dsProtoBook) SupportsAnyProtocol(p peer.ID, protos ...string) (bool, error) { +func (pb *dsProtoBook) FirstSupportedProtocol(p peer.ID, protos ...string) (string, error) { if err := p.Validate(); err != nil { - return false, err + return "", err } s := pb.segments.get(p) @@ -133,15 +133,15 @@ func (pb *dsProtoBook) SupportsAnyProtocol(p peer.ID, protos ...string) (bool, e pmap, err := pb.getProtocolMap(p) if err != nil { - return false, err + return "", err } for _, proto := range protos { if _, ok := pmap[proto]; ok { - return true, nil + return proto, nil } } - return false, nil + return "", nil } func (pb *dsProtoBook) RemoveProtocols(p peer.ID, protos ...string) error { diff --git a/pstoremem/protobook.go b/pstoremem/protobook.go index 83e1d25..1042825 100644 --- a/pstoremem/protobook.go +++ b/pstoremem/protobook.go @@ -164,9 +164,9 @@ func (pb *memoryProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]str return out, nil } -func (pb *memoryProtoBook) SupportsAnyProtocol(p peer.ID, protos ...string) (bool, error) { +func (pb *memoryProtoBook) FirstSupportedProtocol(p peer.ID, protos ...string) (string, error) { if err := p.Validate(); err != nil { - return false, err + return "", err } s := pb.segments.get(p) @@ -175,8 +175,8 @@ func (pb *memoryProtoBook) SupportsAnyProtocol(p peer.ID, protos ...string) (boo for _, proto := range protos { if _, ok := s.protocols[p][proto]; ok { - return true, nil + return proto, nil } } - return false, nil + return "", nil } diff --git a/test/peerstore_suite.go b/test/peerstore_suite.go index a008f5d..ec0a39c 100644 --- a/test/peerstore_suite.go +++ b/test/peerstore_suite.go @@ -243,17 +243,17 @@ func testPeerstoreProtoStore(ps pstore.Peerstore) func(t *testing.T) { t.Fatal("got wrong supported array: ", supported) } - b, err := ps.SupportsAnyProtocol(p1, "q", "w", "a", "y", "b") + b, err := ps.FirstSupportedProtocol(p1, "q", "w", "a", "y", "b") require.NoError(t, err) - require.True(t, b) + require.Equal(t, "a", b) - b, err = ps.SupportsAnyProtocol(p1, "a") + b, err = ps.FirstSupportedProtocol(p1, "q", "x", "z") require.NoError(t, err) - require.True(t, b) + require.Empty(t, b) - b, err = ps.SupportsAnyProtocol(p1, "q") + b, err = ps.FirstSupportedProtocol(p1, "a") require.NoError(t, err) - require.False(t, b) + require.Equal(t, "a", b) protos = []string{"other", "yet another", "one more"} err = ps.SetProtocols(p1, protos...) From 21a1c143cc03c81960180169aa2323137d9a8465 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Thu, 14 May 2020 16:22:42 +0530 Subject: [PATCH 3/3] upgrade core version --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f0bd68c..7621583 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/ipfs/go-ds-leveldb v0.4.2 github.com/ipfs/go-log v1.0.3 github.com/libp2p/go-buffer-pool v0.0.2 - github.com/libp2p/go-libp2p-core v0.5.4-0.20200511073005-dabf5bbfb028 + github.com/libp2p/go-libp2p-core v0.5.4 github.com/multiformats/go-base32 v0.0.3 github.com/multiformats/go-multiaddr v0.2.1 github.com/multiformats/go-multiaddr-fmt v0.1.0 diff --git a/go.sum b/go.sum index c0c3b4a..ec7e801 100644 --- a/go.sum +++ b/go.sum @@ -99,6 +99,8 @@ github.com/libp2p/go-libp2p-core v0.5.4-0.20200508062439-98b95a487749 h1:G0zRpRn github.com/libp2p/go-libp2p-core v0.5.4-0.20200508062439-98b95a487749/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= github.com/libp2p/go-libp2p-core v0.5.4-0.20200511073005-dabf5bbfb028 h1:nQb4SDWcadn/kpWtMwsWweogIzYviVE5qOMT42bkyNE= github.com/libp2p/go-libp2p-core v0.5.4-0.20200511073005-dabf5bbfb028/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= +github.com/libp2p/go-libp2p-core v0.5.4 h1:Z8Tt3R5or2pkl3Wgywfcc0GCNjf18aYWA30OjBpbmRs= +github.com/libp2p/go-libp2p-core v0.5.4/go.mod h1:uN7L2D4EvPCvzSH5SrhR72UWbnSGpt5/a35Sm4upn4Y= github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ= github.com/libp2p/go-openssl v0.0.4 h1:d27YZvLoTyMhIN4njrkr8zMDOM4lfpHIp6A+TK9fovg= github.com/libp2p/go-openssl v0.0.4/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc=