Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix libp2p connection issue #1533

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/p2p/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/multiformats/go-multiaddr"
"go.uber.org/dig"

Expand Down Expand Up @@ -125,7 +126,7 @@ func provide(c *dig.Container) error {
createdHost, err := libp2p.New(libp2p.Identity(privKey),
libp2p.ListenAddrStrings(deps.P2PBindMultiAddresses...),
libp2p.Peerstore(peerStoreContainer.Peerstore()),
libp2p.DefaultTransports,
libp2p.Transport(tcp.NewTCPTransport),
libp2p.ConnectionManager(connManager),
libp2p.NATPortMap(),
)
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/blang/vfs v1.0.0
github.com/cockroachdb/pebble v0.0.0-20220527231317-d5bc42c2884b
github.com/cockroachdb/pebble v0.0.0-20220601013604-e7847d681b29
github.com/docker/docker v20.10.16+incompatible
github.com/docker/go-connections v0.4.0
github.com/dustin/go-humanize v1.0.0
Expand Down Expand Up @@ -37,7 +37,7 @@ require (
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
golang.org/x/time v0.0.0-20220411224347-583f2d630306
google.golang.org/grpc v1.46.2
google.golang.org/grpc v1.47.0
)

require (
Expand Down Expand Up @@ -136,7 +136,7 @@ require (
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.0.3 // indirect
github.com/multiformats/go-multicodec v0.4.1 // indirect
github.com/multiformats/go-multicodec v0.5.0 // indirect
github.com/multiformats/go-multihash v0.1.0 // indirect
github.com/multiformats/go-multistream v0.3.1 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
Expand Down Expand Up @@ -172,13 +172,13 @@ require (
go.uber.org/zap v1.21.0 // indirect
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
golang.org/x/net v0.0.0-20220531201128-c960675eff93 // indirect
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/genproto v0.0.0-20220527130721-00d5c0f3be58 // indirect
google.golang.org/genproto v0.0.0-20220531173845-685668d2de03 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
19 changes: 10 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ github.com/cockroachdb/errors v1.9.0/go.mod h1:vaNcEYYqbIqB5JhKBhFV9CneUqeuEbB2O
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f h1:6jduT9Hfc0njg5jJ1DdKCFPdMBrp/mdZfCpa5h+WM74=
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs=
github.com/cockroachdb/pebble v0.0.0-20220527231317-d5bc42c2884b h1:g0WHExsF5kb7KXvhdPcvqVTCGQfprKasPcggKJTgXgc=
github.com/cockroachdb/pebble v0.0.0-20220527231317-d5bc42c2884b/go.mod h1:pr479tNxFRmcfDyklTqoRMDDVmRlEbL+d7a7rhKnrI4=
github.com/cockroachdb/pebble v0.0.0-20220601013604-e7847d681b29 h1:ao8WW5bFk2IYioX1kG5mmDFUnpLnQZDA8gKSlLzWrlQ=
github.com/cockroachdb/pebble v0.0.0-20220601013604-e7847d681b29/go.mod h1:pr479tNxFRmcfDyklTqoRMDDVmRlEbL+d7a7rhKnrI4=
github.com/cockroachdb/redact v1.0.8/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
github.com/cockroachdb/redact v1.1.3 h1:AKZds10rFSIj7qADf0g46UixK8NNLwWTNdCIGS5wfSQ=
github.com/cockroachdb/redact v1.1.3/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
Expand Down Expand Up @@ -788,8 +788,8 @@ github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDu
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
github.com/multiformats/go-multicodec v0.4.1 h1:BSJbf+zpghcZMZrwTYBGwy0CPcVZGWiC72Cp8bBd4R4=
github.com/multiformats/go-multicodec v0.4.1/go.mod h1:1Hj/eHRaVWSXiSNNfcEPcwZleTmdNP81xlxDLnWU9GQ=
github.com/multiformats/go-multicodec v0.5.0 h1:EgU6cBe/D7WRwQb1KmnBvU7lrcFGMggZVTPtOW9dDHs=
github.com/multiformats/go-multicodec v0.5.0/go.mod h1:DiY2HFaEp5EhEXb/iYzVAunmyX/aSFMxq2KMKfWEues=
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po=
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
Expand Down Expand Up @@ -1221,8 +1221,8 @@ golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220531201128-c960675eff93 h1:MYimHLfoXEpOhqd/zgoA/uoXzHB86AEky4LAx5ij9xA=
golang.org/x/net v0.0.0-20220531201128-c960675eff93/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -1506,8 +1506,8 @@ google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84/go.mod h1:SzzZ/N+nwJDaO1kznhnlzqS8ocJICar6hYhVyhi++24=
google.golang.org/genproto v0.0.0-20220527130721-00d5c0f3be58 h1:a221mAAEAzq4Lz6ZWRkcS8ptb2mxoxYSt4N68aRyQHM=
google.golang.org/genproto v0.0.0-20220527130721-00d5c0f3be58/go.mod h1:yKyY4AMRwFiC8yMMNaMi+RkCnjZJt9LoWuvhXjMs+To=
google.golang.org/genproto v0.0.0-20220531173845-685668d2de03 h1:FG2YhwyltdDPC/0XuwzU0dijPcTzvfTtst0QdlDxoMU=
google.golang.org/genproto v0.0.0-20220531173845-685668d2de03/go.mod h1:yKyY4AMRwFiC8yMMNaMi+RkCnjZJt9LoWuvhXjMs+To=
google.golang.org/grpc v1.12.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
Expand All @@ -1528,8 +1528,9 @@ google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc v1.46.2 h1:u+MLGgVf7vRdjEYZ8wDFhAVNmhkbJ5hmrA1LMWK1CAQ=
google.golang.org/grpc v1.46.2/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
google.golang.org/grpc v1.47.0 h1:9n77onPX5F3qfFCqjy9dhn8PbNQsIKeVU04J9G7umt8=
google.golang.org/grpc v1.47.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
6 changes: 3 additions & 3 deletions pkg/p2p/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func NewPeerStoreContainer(peerStorePath string, dbEngine database.Engine, creat
}, nil
}

// parseEd25519PrivateKeyFromString parses an Ed25519 private key from a hex encoded string.
func parseEd25519PrivateKeyFromString(identityPrivKey string) (crypto.PrivKey, error) {
// ParseEd25519PrivateKeyFromString parses an Ed25519 private key from a hex encoded string.
func ParseEd25519PrivateKeyFromString(identityPrivKey string) (crypto.PrivKey, error) {
if identityPrivKey == "" {
return nil, ErrNoPrivKeyFound
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func WriteEd25519PrivateKeyToPEMFile(filepath string, privateKey crypto.PrivKey)
// or creates a new one and stores it as a PEM file in the p2p store folder.
func LoadOrCreateIdentityPrivateKey(p2pStorePath string, identityPrivKey string) (crypto.PrivKey, bool, error) {

privKeyFromConfig, err := parseEd25519PrivateKeyFromString(identityPrivKey)
privKeyFromConfig, err := ParseEd25519PrivateKeyFromString(identityPrivKey)
if err != nil {
if errors.Is(err, ErrPrivKeyInvalid) {
return nil, false, errors.New("configuration contains an invalid private key")
Expand Down
24 changes: 16 additions & 8 deletions pkg/protocol/gossip/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ func (s *Service) eventLoop(ctx context.Context) {
case connectedMsg := <-s.connectedChan:
s.handleConnected(connectedMsg.peer, connectedMsg.conn)

case disconnectMsg := <-s.closeStreamChan:
if err := s.deregisterProtocol(disconnectMsg.peerID); err != nil && !errors.Is(err, ErrProtocolDoesNotExist) {
disconnectMsg.back <- err
case closeStreamMsg := <-s.closeStreamChan:
if err := s.deregisterProtocol(closeStreamMsg.peerID); err != nil && !errors.Is(err, ErrProtocolDoesNotExist) {
closeStreamMsg.back <- err
}
disconnectMsg.back <- nil
closeStreamMsg.back <- nil

case disconnectedMsg := <-s.disconnectedChan:
if err := s.deregisterProtocol(disconnectedMsg.peer.ID); err != nil && !errors.Is(err, ErrProtocolDoesNotExist) {
Expand Down Expand Up @@ -450,7 +450,7 @@ func (s *Service) handleInboundStream(stream network.Stream) {

if len(cancelReason) > 0 {
s.Events.InboundStreamCanceled.Trigger(stream, cancelReason)
s.closeUnwantedStream(stream)
s.closeUnwantedStreamAndClosePeer(stream)
return
}

Expand All @@ -461,16 +461,24 @@ func (s *Service) handleInboundStream(stream network.Stream) {
s.registerProtocol(remotePeerID, stream)
}

// closes the given unwanted stream by closing the underlying
// connection and the stream itself.
// closeUnwantedStream closes the given unwanted stream.
func (s *Service) closeUnwantedStream(stream network.Stream) {
// using close and reset is the only way to make the remote's peer
// "ClosedStream" notifiee handler fire: this is important, because
// we want the remote peer to deregister the stream
_ = stream.Conn().Close()
_ = stream.Reset()
}

// closeUnwantedStreamAndClosePeer closes the given unwanted stream by closing the underlying
// peer and the stream itself.
func (s *Service) closeUnwantedStreamAndClosePeer(stream network.Stream) {
// using close and reset is the only way to make the remote's peer
// "ClosedStream" notifiee handler fire: this is important, because
// we want the remote peer to deregister the stream
_ = stream.Reset()
s.host.Network().ClosePeer(stream.Conn().RemotePeer())
}

// handles the automatic creation of a protocol instance if the given peer
// was connected outbound and its peer relation allows it.
func (s *Service) handleConnected(peer *p2p.Peer, conn network.Conn) {
Expand Down
44 changes: 33 additions & 11 deletions pkg/protocol/gossip/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/configuration"
Expand All @@ -23,13 +24,9 @@ import (

const protocolID = "/iota/abcdf/1.0.0"

func newNode(name string, ctx context.Context, t *testing.T, mngOpts []p2p.ManagerOption, srvOpts []gossip.ServiceOption) (
func newNode(name string, ctx context.Context, t *testing.T, mngOpts []p2p.ManagerOption, srvOpts []gossip.ServiceOption, privateKey crypto.PrivKey) (
host.Host, *p2p.Manager, *gossip.Service, peer.AddrInfo,
) {
// we use Ed25519 because otherwise it takes longer as the default is RSA
sk, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
require.NoError(t, err)

connManager, err := connmgr.NewConnManager(
1,
100,
Expand All @@ -38,8 +35,10 @@ func newNode(name string, ctx context.Context, t *testing.T, mngOpts []p2p.Manag
require.NoError(t, err)

n, err := libp2p.New(
libp2p.Identity(sk),
libp2p.DefaultListenAddrs,
libp2p.Identity(privateKey),
libp2p.ConnectionManager(connManager),
libp2p.Transport(tcp.NewTCPTransport),
)
require.NoError(t, err)

Expand Down Expand Up @@ -71,8 +70,17 @@ func TestServiceEvents(t *testing.T) {
}
var srvOpts []gossip.ServiceOption

node1, node1Manager, node1Service, node1AddrInfo := newNode("node1", ctx, t, mngOpts, srvOpts)
node2, node2Manager, node2Service, node2AddrInfo := newNode("node2", ctx, t, mngOpts, srvOpts)
node1PrvKey, err := p2p.ParseEd25519PrivateKeyFromString("5536d0d7eb7cb3780085d73d55079a373a726df58010d881167add08d7e8108c76d7a7f15c094c292faa22ac81b976034f0b11db86a8863d9a9b0c64820e087d")
require.NoError(t, err)

node2PrvKey, err := p2p.ParseEd25519PrivateKeyFromString("35764adaa5e02cbd677285ffd90f927644d2010dca7608876dd3ea3a44f8fcb491cdffa377a307e1d16df5c18e4beee9fffbd61998bd1f8c76a616c1b6c7ca7d")
require.NoError(t, err)

// node 1 <peer.ID 12*rd6tBe>
node1, node1Manager, node1Service, node1AddrInfo := newNode("node1", ctx, t, mngOpts, srvOpts, node1PrvKey)

// node 2 <peer.ID 12*g1issS>
node2, node2Manager, node2Service, node2AddrInfo := newNode("node2", ctx, t, mngOpts, srvOpts, node2PrvKey)

fmt.Println("node 1", node1.ID().ShortString())
fmt.Println("node 2", node2.ID().ShortString())
Expand Down Expand Up @@ -191,11 +199,25 @@ func TestWithUnknownPeersLimit(t *testing.T) {
gossip.WithUnknownPeersLimit(1),
}

node1, node1Manager, node1Service, node1AddrInfo := newNode("node1", ctx, t, mngOpts, srvOpts)
node2, node2Manager, node2Service, node2AddrInfo := newNode("node2", ctx, t, mngOpts, srvOpts)
node1PrvKey, err := p2p.ParseEd25519PrivateKeyFromString("5536d0d7eb7cb3780085d73d55079a373a726df58010d881167add08d7e8108c76d7a7f15c094c292faa22ac81b976034f0b11db86a8863d9a9b0c64820e087d")
require.NoError(t, err)

node2PrvKey, err := p2p.ParseEd25519PrivateKeyFromString("35764adaa5e02cbd677285ffd90f927644d2010dca7608876dd3ea3a44f8fcb491cdffa377a307e1d16df5c18e4beee9fffbd61998bd1f8c76a616c1b6c7ca7d")
require.NoError(t, err)

node3PrvKey, err := p2p.ParseEd25519PrivateKeyFromString("1d586a941f97be3d8ead709c9ff31579c9677f681ec05cd1e0233d36513b178bd2a54ee6c67c84037ae8da89033c1bcfc2252ecd466f6cf472c22cbe0e9a7842")
require.NoError(t, err)

// node 1 <peer.ID 12*rd6tBe>
node1, node1Manager, node1Service, node1AddrInfo := newNode("node1", ctx, t, mngOpts, srvOpts, node1PrvKey)

// node 2 <peer.ID 12*g1issS>
node2, node2Manager, node2Service, node2AddrInfo := newNode("node2", ctx, t, mngOpts, srvOpts, node2PrvKey)

// node 3 <peer.ID 12*e9jADP>
node3, node3Manager, node3Service, _ := newNode("node3", ctx, t, mngOpts, []gossip.ServiceOption{
gossip.WithUnknownPeersLimit(2),
})
}, node3PrvKey)

fmt.Println("node 1", node1.ID().ShortString())
fmt.Println("node 2", node2.ID().ShortString())
Expand Down