From 5c129411b185dd71950853829d4d2bee2cde4c1e Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Wed, 23 Jan 2019 07:00:02 +0000 Subject: [PATCH] add options support in hostv2.New so we can add additional options to p2pimpl.NewHost function call Signed-off-by: Leo Chen --- p2p/host/hostv2/hostv2.go | 15 ++++++--------- p2p/p2pimpl/p2pimpl.go | 8 ++++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/p2p/host/hostv2/hostv2.go b/p2p/host/hostv2/hostv2.go index b081c9ec6..720e9460b 100644 --- a/p2p/host/hostv2/hostv2.go +++ b/p2p/host/hostv2/hostv2.go @@ -9,10 +9,11 @@ import ( "github.com/harmony-one/harmony/p2p" libp2p "github.com/libp2p/go-libp2p" 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" peer "github.com/libp2p/go-libp2p-peer" peerstore "github.com/libp2p/go-libp2p-peerstore" + p2p_config "github.com/libp2p/go-libp2p/config" ma "github.com/multiformats/go-multiaddr" ) @@ -25,7 +26,7 @@ const ( // HostV2 is the version 2 p2p host type HostV2 struct { - h libp2phost.Host + h p2p_host.Host self p2p.Peer priKey p2p_crypto.PrivKey } @@ -65,23 +66,19 @@ func (host *HostV2) Peerstore() peerstore.Peerstore { } // 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)) if err != nil { log.Error("New MA Error", "IP", self.IP, "Port", self.Port) 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(), - libp2p.ListenAddrs(listenAddr), - libp2p.Identity(priKey), - // TODO(ricl): Other features to probe - // libp2p.EnableRelay; libp2p.Routing; + append(opts, libp2p.ListenAddrs(listenAddr), libp2p.Identity(priKey))..., ) + catchError(err) self.PeerID = p2pHost.ID() - catchError(err) log.Debug("HostV2 is up!", "port", self.Port, "id", p2pHost.ID().Pretty(), "addr", listenAddr) // has to save the private key for host diff --git a/p2p/p2pimpl/p2pimpl.go b/p2p/p2pimpl/p2pimpl.go index ec6cc4b7f..1cdcf7102 100644 --- a/p2p/p2pimpl/p2pimpl.go +++ b/p2p/p2pimpl/p2pimpl.go @@ -9,6 +9,7 @@ import ( "github.com/harmony-one/harmony/internal/utils" p2p_crypto "github.com/libp2p/go-libp2p-crypto" + p2p_config "github.com/libp2p/go-libp2p/config" ) // Version The version number of p2p library @@ -18,15 +19,14 @@ const Version = 2 // NewHost starts the host for p2p // 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 -// 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) (p2p.Host, error) { +// TODO (leo) The peerstore has to be persisted on disk. +func NewHost(self *p2p.Peer, key p2p_crypto.PrivKey, opts ...p2p_config.Option) (p2p.Host, error) { if Version == 1 { h := hostv1.New(self) 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)