add options support in hostv2.New

so we can add additional options to p2pimpl.NewHost function call

Signed-off-by: Leo Chen <leo@harmony.one>
pull/348/head
Leo Chen 6 years ago
parent 82f4a1751d
commit 5c129411b1
  1. 15
      p2p/host/hostv2/hostv2.go
  2. 8
      p2p/p2pimpl/p2pimpl.go

@ -9,10 +9,11 @@ import (
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
libp2p "github.com/libp2p/go-libp2p" libp2p "github.com/libp2p/go-libp2p"
p2p_crypto "github.com/libp2p/go-libp2p-crypto" p2p_crypto "github.com/libp2p/go-libp2p-crypto"
libp2phost "github.com/libp2p/go-libp2p-host" p2p_host "github.com/libp2p/go-libp2p-host"
net "github.com/libp2p/go-libp2p-net" net "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer" peer "github.com/libp2p/go-libp2p-peer"
peerstore "github.com/libp2p/go-libp2p-peerstore" peerstore "github.com/libp2p/go-libp2p-peerstore"
p2p_config "github.com/libp2p/go-libp2p/config"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
) )
@ -25,7 +26,7 @@ const (
// HostV2 is the version 2 p2p host // HostV2 is the version 2 p2p host
type HostV2 struct { type HostV2 struct {
h libp2phost.Host h p2p_host.Host
self p2p.Peer self p2p.Peer
priKey p2p_crypto.PrivKey priKey p2p_crypto.PrivKey
} }
@ -65,23 +66,19 @@ func (host *HostV2) Peerstore() peerstore.Peerstore {
} }
// New creates a host for p2p communication // New creates a host for p2p communication
func New(self *p2p.Peer, priKey p2p_crypto.PrivKey) *HostV2 { func New(self *p2p.Peer, priKey p2p_crypto.PrivKey, opts ...p2p_config.Option) *HostV2 {
listenAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", self.Port)) listenAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", self.Port))
if err != nil { if err != nil {
log.Error("New MA Error", "IP", self.IP, "Port", self.Port) log.Error("New MA Error", "IP", self.IP, "Port", self.Port)
return nil return nil
} }
// TODO (leo), use the [0] of Addrs for now, need to find a reliable way of using listenAddr
p2pHost, err := libp2p.New(context.Background(), p2pHost, err := libp2p.New(context.Background(),
libp2p.ListenAddrs(listenAddr), append(opts, libp2p.ListenAddrs(listenAddr), libp2p.Identity(priKey))...,
libp2p.Identity(priKey),
// TODO(ricl): Other features to probe
// libp2p.EnableRelay; libp2p.Routing;
) )
catchError(err)
self.PeerID = p2pHost.ID() self.PeerID = p2pHost.ID()
catchError(err)
log.Debug("HostV2 is up!", "port", self.Port, "id", p2pHost.ID().Pretty(), "addr", listenAddr) log.Debug("HostV2 is up!", "port", self.Port, "id", p2pHost.ID().Pretty(), "addr", listenAddr)
// has to save the private key for host // has to save the private key for host

@ -9,6 +9,7 @@ import (
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
p2p_crypto "github.com/libp2p/go-libp2p-crypto" p2p_crypto "github.com/libp2p/go-libp2p-crypto"
p2p_config "github.com/libp2p/go-libp2p/config"
) )
// Version The version number of p2p library // Version The version number of p2p library
@ -18,15 +19,14 @@ const Version = 2
// NewHost starts the host for p2p // NewHost starts the host for p2p
// for hostv2, it generates multiaddress, keypair and add PeerID to peer, add priKey to host // for hostv2, it generates multiaddress, keypair and add PeerID to peer, add priKey to host
// TODO (leo) the PriKey of the host has to be persistent in disk, so that we don't need to regenerate it // TODO (leo) The peerstore has to be persisted on disk.
// on the same host if the node software restarted. The peerstore has to be persistent as well. func NewHost(self *p2p.Peer, key p2p_crypto.PrivKey, opts ...p2p_config.Option) (p2p.Host, error) {
func NewHost(self *p2p.Peer, key p2p_crypto.PrivKey) (p2p.Host, error) {
if Version == 1 { if Version == 1 {
h := hostv1.New(self) h := hostv1.New(self)
return h, nil return h, nil
} }
h := hostv2.New(self, key) h := hostv2.New(self, key, opts...)
utils.GetLogInstance().Info("NewHost", "self", net.JoinHostPort(self.IP, self.Port), "PeerID", self.PeerID) utils.GetLogInstance().Info("NewHost", "self", net.JoinHostPort(self.IP, self.Port), "PeerID", self.PeerID)

Loading…
Cancel
Save