|
|
@ -9,6 +9,7 @@ import ( |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/internal/utils" |
|
|
|
"github.com/harmony-one/harmony/internal/utils" |
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
|
|
|
|
|
|
|
@ -50,6 +51,9 @@ type HostV2 struct { |
|
|
|
|
|
|
|
|
|
|
|
//incomingPeers []p2p.Peer // list of incoming Peers. TODO: fixed number incoming
|
|
|
|
//incomingPeers []p2p.Peer // list of incoming Peers. TODO: fixed number incoming
|
|
|
|
//outgoingPeers []p2p.Peer // list of outgoing Peers. TODO: fixed number of outgoing
|
|
|
|
//outgoingPeers []p2p.Peer // list of outgoing Peers. TODO: fixed number of outgoing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// logger
|
|
|
|
|
|
|
|
logger log.Logger |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SendMessageToGroups sends a message to one or more multicast groups.
|
|
|
|
// SendMessageToGroups sends a message to one or more multicast groups.
|
|
|
@ -114,7 +118,7 @@ func (host *HostV2) AddPeer(p *p2p.Peer) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if p.PeerID == "" { |
|
|
|
if p.PeerID == "" { |
|
|
|
log.Error("AddPeer PeerID is EMPTY") |
|
|
|
host.logger.Error("AddPeer PeerID is EMPTY") |
|
|
|
return fmt.Errorf("AddPeer error: peerID is empty") |
|
|
|
return fmt.Errorf("AddPeer error: peerID is empty") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -123,14 +127,14 @@ func (host *HostV2) AddPeer(p *p2p.Peer) error { |
|
|
|
addr := fmt.Sprintf("/ip4/%s/tcp/%s", p.IP, p.Port) |
|
|
|
addr := fmt.Sprintf("/ip4/%s/tcp/%s", p.IP, p.Port) |
|
|
|
targetAddr, err := ma.NewMultiaddr(addr) |
|
|
|
targetAddr, err := ma.NewMultiaddr(addr) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error("AddPeer NewMultiaddr error", "error", err) |
|
|
|
host.logger.Error("AddPeer NewMultiaddr error", "error", err) |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
p.Addrs = append(p.Addrs, targetAddr) |
|
|
|
p.Addrs = append(p.Addrs, targetAddr) |
|
|
|
|
|
|
|
|
|
|
|
host.Peerstore().AddAddrs(p.PeerID, p.Addrs, libp2p_peerstore.PermanentAddrTTL) |
|
|
|
host.Peerstore().AddAddrs(p.PeerID, p.Addrs, libp2p_peerstore.PermanentAddrTTL) |
|
|
|
log.Info("AddPeer add to libp2p_peerstore", "peer", *p) |
|
|
|
host.logger.Info("AddPeer add to libp2p_peerstore", "peer", *p) |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
@ -153,8 +157,9 @@ func (host *HostV2) Peerstore() libp2p_peerstore.Peerstore { |
|
|
|
// New creates a host for p2p communication
|
|
|
|
// New creates a host for p2p communication
|
|
|
|
func New(self *p2p.Peer, priKey libp2p_crypto.PrivKey) *HostV2 { |
|
|
|
func New(self *p2p.Peer, priKey libp2p_crypto.PrivKey) *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)) |
|
|
|
|
|
|
|
logger := utils.GetLogInstance() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error("New MA Error", "IP", self.IP, "Port", self.Port) |
|
|
|
logger.Error("New MA Error", "IP", self.IP, "Port", self.Port) |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
// TODO – use WithCancel for orderly host teardown (which we don't have yet)
|
|
|
|
// TODO – use WithCancel for orderly host teardown (which we don't have yet)
|
|
|
@ -169,16 +174,18 @@ func New(self *p2p.Peer, priKey libp2p_crypto.PrivKey) *HostV2 { |
|
|
|
|
|
|
|
|
|
|
|
self.PeerID = p2pHost.ID() |
|
|
|
self.PeerID = p2pHost.ID() |
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
h := &HostV2{ |
|
|
|
h := &HostV2{ |
|
|
|
h: p2pHost, |
|
|
|
h: p2pHost, |
|
|
|
pubsub: pubsub, |
|
|
|
pubsub: pubsub, |
|
|
|
self: *self, |
|
|
|
self: *self, |
|
|
|
priKey: priKey, |
|
|
|
priKey: priKey, |
|
|
|
|
|
|
|
logger: logger.New("hostID", p2pHost.ID().Pretty()), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
h.logger.Debug("HostV2 is up!", |
|
|
|
|
|
|
|
"port", self.Port, "id", p2pHost.ID().Pretty(), "addr", listenAddr) |
|
|
|
|
|
|
|
|
|
|
|
return h |
|
|
|
return h |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -203,7 +210,7 @@ func (host *HostV2) BindHandlerAndServe(handler p2p.StreamHandler) { |
|
|
|
|
|
|
|
|
|
|
|
// SendMessage a p2p message sending function with signature compatible to p2pv1.
|
|
|
|
// SendMessage a p2p message sending function with signature compatible to p2pv1.
|
|
|
|
func (host *HostV2) SendMessage(p p2p.Peer, message []byte) error { |
|
|
|
func (host *HostV2) SendMessage(p p2p.Peer, message []byte) error { |
|
|
|
logger := log.New("from", host.self, "to", p, "PeerID", p.PeerID) |
|
|
|
logger := host.logger.New("from", host.self, "to", p, "PeerID", p.PeerID) |
|
|
|
err := host.Peerstore().AddProtocols(p.PeerID, ProtocolID) |
|
|
|
err := host.Peerstore().AddProtocols(p.PeerID, ProtocolID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
logger.Error("AddProtocols() failed", "error", err) |
|
|
|
logger.Error("AddProtocols() failed", "error", err) |
|
|
@ -243,17 +250,18 @@ func (host *HostV2) ConnectHostPeer(peer p2p.Peer) { |
|
|
|
addr := fmt.Sprintf("/ip4/%s/tcp/%s/ipfs/%s", peer.IP, peer.Port, peer.PeerID.Pretty()) |
|
|
|
addr := fmt.Sprintf("/ip4/%s/tcp/%s/ipfs/%s", peer.IP, peer.Port, peer.PeerID.Pretty()) |
|
|
|
peerAddr, err := ma.NewMultiaddr(addr) |
|
|
|
peerAddr, err := ma.NewMultiaddr(addr) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
utils.GetLogInstance().Error("ConnectHostPeer", "new ma error", err, "peer", peer) |
|
|
|
host.logger.Error("ConnectHostPeer", "new ma error", err, "peer", peer) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
peerInfo, err := libp2p_peerstore.InfoFromP2pAddr(peerAddr) |
|
|
|
peerInfo, err := libp2p_peerstore.InfoFromP2pAddr(peerAddr) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
utils.GetLogInstance().Error("ConnectHostPeer", "new peerinfo error", err, "peer", peer) |
|
|
|
host.logger.Error("ConnectHostPeer", "new peerinfo error", err, "peer", |
|
|
|
|
|
|
|
peer) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if err := host.h.Connect(ctx, *peerInfo); err != nil { |
|
|
|
if err := host.h.Connect(ctx, *peerInfo); err != nil { |
|
|
|
utils.GetLogInstance().Warn("can't connect to peer", "error", err, "peer", peer) |
|
|
|
host.logger.Warn("can't connect to peer", "error", err, "peer", peer) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
utils.GetLogInstance().Info("connected to peer host", "node", *peerInfo) |
|
|
|
host.logger.Info("connected to peer host", "node", *peerInfo) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|