Commit e4ffe287 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

add dedicated peerstore for every libp2p host (#18)

parent b5312204
...@@ -11,6 +11,7 @@ require ( ...@@ -11,6 +11,7 @@ require (
github.com/libp2p/go-libp2p v0.5.1 github.com/libp2p/go-libp2p v0.5.1
github.com/libp2p/go-libp2p-autonat-svc v0.1.0 github.com/libp2p/go-libp2p-autonat-svc v0.1.0
github.com/libp2p/go-libp2p-core v0.3.0 github.com/libp2p/go-libp2p-core v0.3.0
github.com/libp2p/go-libp2p-peerstore v0.1.4
github.com/libp2p/go-libp2p-quic-transport v0.2.2 github.com/libp2p/go-libp2p-quic-transport v0.2.2
github.com/libp2p/go-libp2p-secio v0.2.1 github.com/libp2p/go-libp2p-secio v0.2.1
github.com/libp2p/go-libp2p-tls v0.1.3 github.com/libp2p/go-libp2p-tls v0.1.3
......
...@@ -11,8 +11,6 @@ import ( ...@@ -11,8 +11,6 @@ import (
"fmt" "fmt"
"net" "net"
"github.com/libp2p/go-libp2p-core/helpers"
"github.com/ethersphere/bee/pkg/addressbook" "github.com/ethersphere/bee/pkg/addressbook"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p" "github.com/ethersphere/bee/pkg/p2p"
...@@ -22,10 +20,13 @@ import ( ...@@ -22,10 +20,13 @@ import (
"github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p"
autonat "github.com/libp2p/go-libp2p-autonat-svc" autonat "github.com/libp2p/go-libp2p-autonat-svc"
crypto "github.com/libp2p/go-libp2p-core/crypto" crypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/helpers"
"github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/network"
libp2ppeer "github.com/libp2p/go-libp2p-core/peer" libp2ppeer "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
protocol "github.com/libp2p/go-libp2p-core/protocol" protocol "github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
libp2pquic "github.com/libp2p/go-libp2p-quic-transport" libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
secio "github.com/libp2p/go-libp2p-secio" secio "github.com/libp2p/go-libp2p-secio"
libp2ptls "github.com/libp2p/go-libp2p-tls" libp2ptls "github.com/libp2p/go-libp2p-tls"
...@@ -37,6 +38,7 @@ var _ p2p.Service = (*Service)(nil) ...@@ -37,6 +38,7 @@ var _ p2p.Service = (*Service)(nil)
type Service struct { type Service struct {
host host.Host host host.Host
libp2pPeerstore peerstore.Peerstore
metrics metrics metrics metrics
networkID int32 networkID int32
handshakeService *handshake.Service handshakeService *handshake.Service
...@@ -100,6 +102,8 @@ func New(ctx context.Context, o Options) (*Service, error) { ...@@ -100,6 +102,8 @@ func New(ctx context.Context, o Options) (*Service, error) {
} }
} }
libp2pPeerstore := pstoremem.NewPeerstore()
opts := []libp2p.Option{ opts := []libp2p.Option{
// Multiple listen addresses // Multiple listen addresses
libp2p.ListenAddrStrings(listenAddrs...), libp2p.ListenAddrStrings(listenAddrs...),
...@@ -111,6 +115,8 @@ func New(ctx context.Context, o Options) (*Service, error) { ...@@ -111,6 +115,8 @@ func New(ctx context.Context, o Options) (*Service, error) {
libp2p.DefaultTransports, libp2p.DefaultTransports,
// Attempt to open ports using uPNP for NATed hosts. // Attempt to open ports using uPNP for NATed hosts.
libp2p.NATPortMap(), libp2p.NATPortMap(),
// Use dedicated peerstore instead the global DefaultPeerstore
libp2p.Peerstore(libp2pPeerstore),
} }
if o.PrivateKey != nil { if o.PrivateKey != nil {
...@@ -148,6 +154,7 @@ func New(ctx context.Context, o Options) (*Service, error) { ...@@ -148,6 +154,7 @@ func New(ctx context.Context, o Options) (*Service, error) {
peerRegistry := newPeerRegistry() peerRegistry := newPeerRegistry()
s := &Service{ s := &Service{
host: h, host: h,
libp2pPeerstore: libp2pPeerstore,
metrics: newMetrics(), metrics: newMetrics(),
networkID: o.NetworkID, networkID: o.NetworkID,
handshakeService: handshake.New(peerRegistry, o.Overlay, o.NetworkID, o.Logger), handshakeService: handshake.New(peerRegistry, o.Overlay, o.NetworkID, o.Logger),
...@@ -344,5 +351,8 @@ func (s *Service) newStreamForPeerID(ctx context.Context, peerID libp2ppeer.ID, ...@@ -344,5 +351,8 @@ func (s *Service) newStreamForPeerID(ctx context.Context, peerID libp2ppeer.ID,
} }
func (s *Service) Close() error { func (s *Service) Close() error {
if err := s.libp2pPeerstore.Close(); err != nil {
return err
}
return s.host.Close() return s.host.Close()
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment