|
|
@ -1,19 +1,10 @@ |
|
|
|
package discovery |
|
|
|
package discovery |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
|
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
proto_discovery "github.com/harmony-one/harmony/api/proto/discovery" |
|
|
|
proto_discovery "github.com/harmony-one/harmony/api/proto/discovery" |
|
|
|
"github.com/harmony-one/harmony/internal/utils" |
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
"github.com/harmony-one/harmony/p2p/host" |
|
|
|
"github.com/harmony-one/harmony/p2p/host" |
|
|
|
|
|
|
|
|
|
|
|
peerstore "github.com/libp2p/go-libp2p-peerstore" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
libp2pdis "github.com/libp2p/go-libp2p-discovery" |
|
|
|
|
|
|
|
libp2pdht "github.com/libp2p/go-libp2p-kad-dht" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Constants for discovery service.
|
|
|
|
// Constants for discovery service.
|
|
|
@ -24,107 +15,67 @@ const ( |
|
|
|
|
|
|
|
|
|
|
|
// Service is the struct for discovery service.
|
|
|
|
// Service is the struct for discovery service.
|
|
|
|
type Service struct { |
|
|
|
type Service struct { |
|
|
|
Host p2p.Host |
|
|
|
Host p2p.Host |
|
|
|
DHT *libp2pdht.IpfsDHT |
|
|
|
Rendezvous string |
|
|
|
Rendezvous string |
|
|
|
peerChan chan p2p.Peer |
|
|
|
ctx context.Context |
|
|
|
stakingChan chan p2p.Peer |
|
|
|
peerChan <-chan peerstore.PeerInfo |
|
|
|
stopChan chan struct{} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// New returns discovery service.
|
|
|
|
// New returns discovery service.
|
|
|
|
// h is the p2p host
|
|
|
|
// h is the p2p host
|
|
|
|
// r is the rendezvous string, we use shardID to start (TODO: leo, build two overlays of network)
|
|
|
|
// r is the rendezvous string, we use shardID to start (TODO: leo, build two overlays of network)
|
|
|
|
func New(h p2p.Host, r string) *Service { |
|
|
|
func New(h p2p.Host, r string, peerChan chan p2p.Peer, stakingChan chan p2p.Peer) *Service { |
|
|
|
ctx := context.Background() |
|
|
|
|
|
|
|
dht, err := libp2pdht.New(ctx, h.GetP2PHost()) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
panic(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return &Service{ |
|
|
|
return &Service{ |
|
|
|
Host: h, |
|
|
|
Host: h, |
|
|
|
DHT: dht, |
|
|
|
Rendezvous: r, |
|
|
|
Rendezvous: r, |
|
|
|
peerChan: peerChan, |
|
|
|
ctx: ctx, |
|
|
|
stakingChan: stakingChan, |
|
|
|
peerChan: make(<-chan peerstore.PeerInfo), |
|
|
|
stopChan: make(chan struct{}), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// StartService starts discovery service.
|
|
|
|
// StartService starts discovery service.
|
|
|
|
func (s *Service) StartService() { |
|
|
|
func (s *Service) StartService() { |
|
|
|
log.Info("Starting discovery service.") |
|
|
|
log.Info("Starting discovery service.") |
|
|
|
err := s.Init() |
|
|
|
s.Init() |
|
|
|
if err != nil { |
|
|
|
s.Run() |
|
|
|
log.Error("StartService Aborted", "Error", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We use a rendezvous point "shardID" to announce our location.
|
|
|
|
|
|
|
|
log.Info("Announcing ourselves...") |
|
|
|
|
|
|
|
routingDiscovery := libp2pdis.NewRoutingDiscovery(s.DHT) |
|
|
|
|
|
|
|
libp2pdis.Advertise(s.ctx, routingDiscovery, s.Rendezvous) |
|
|
|
|
|
|
|
log.Debug("Successfully announced!") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug("Searching for other peers...") |
|
|
|
|
|
|
|
s.peerChan, err = routingDiscovery.FindPeers(s.ctx, s.Rendezvous) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Error("FindPeers", "error", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// StopService shutdowns discovery service.
|
|
|
|
// StopService shutdowns discovery service.
|
|
|
|
func (s *Service) StopService() { |
|
|
|
func (s *Service) StopService() { |
|
|
|
log.Info("Shutting down discovery service.") |
|
|
|
log.Info("Shutting down discovery service.") |
|
|
|
|
|
|
|
s.stopChan <- struct{}{} |
|
|
|
|
|
|
|
log.Info("discovery service stopped.") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) foundPeers() { |
|
|
|
// Run is the main function of the service
|
|
|
|
|
|
|
|
func (s *Service) Run() { |
|
|
|
|
|
|
|
go s.contactP2pPeers() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) contactP2pPeers() { |
|
|
|
for { |
|
|
|
for { |
|
|
|
select { |
|
|
|
select { |
|
|
|
case peer, ok := <-s.peerChan: |
|
|
|
case peer, ok := <-s.peerChan: |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
log.Debug("end of info", "peer", peer.ID) |
|
|
|
log.Debug("end of info", "peer", peer.PeerID) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if peer.ID != s.Host.GetP2PHost().ID() && len(peer.ID) > 0 { |
|
|
|
log.Debug("[DISCOVERY]", "peer", peer) |
|
|
|
log.Debug("Found Peer", "peer", peer.ID, "addr", peer.Addrs, "len", len(peer.ID)) |
|
|
|
s.Host.AddPeer(&peer) |
|
|
|
p := p2p.Peer{PeerID: peer.ID, Addrs: peer.Addrs} |
|
|
|
// TODO: stop ping if pinged before
|
|
|
|
s.Host.AddPeer(&p) |
|
|
|
// TODO: call staking servcie here if it is a new node
|
|
|
|
// TODO: stop ping if pinged before
|
|
|
|
s.pingPeer(peer) |
|
|
|
s.pingPeer(p) |
|
|
|
case <-s.stopChan: |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Init is to initialize for discoveryService.
|
|
|
|
// Init is to initialize for discoveryService.
|
|
|
|
func (s *Service) Init() error { |
|
|
|
func (s *Service) Init() { |
|
|
|
log.Info("Init discovery service") |
|
|
|
log.Info("Init discovery service") |
|
|
|
|
|
|
|
|
|
|
|
// Bootstrap the DHT. In the default configuration, this spawns a Background
|
|
|
|
|
|
|
|
// thread that will refresh the peer table every five minutes.
|
|
|
|
|
|
|
|
log.Debug("Bootstrapping the DHT") |
|
|
|
|
|
|
|
if err := s.DHT.Bootstrap(s.ctx); err != nil { |
|
|
|
|
|
|
|
return ErrDHTBootstrap |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup |
|
|
|
|
|
|
|
for _, peerAddr := range utils.BootNodes { |
|
|
|
|
|
|
|
peerinfo, _ := peerstore.InfoFromP2pAddr(peerAddr) |
|
|
|
|
|
|
|
wg.Add(1) |
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
|
|
defer wg.Done() |
|
|
|
|
|
|
|
if err := s.Host.GetP2PHost().Connect(s.ctx, *peerinfo); err != nil { |
|
|
|
|
|
|
|
log.Warn("can't connect to bootnode", "error", err) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
log.Info("connected to bootnode", "node", *peerinfo) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
wg.Wait() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
go s.foundPeers() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) pingPeer(peer p2p.Peer) { |
|
|
|
func (s *Service) pingPeer(peer p2p.Peer) { |
|
|
@ -134,4 +85,5 @@ func (s *Service) pingPeer(peer p2p.Peer) { |
|
|
|
content := host.ConstructP2pMessage(byte(0), buffer) |
|
|
|
content := host.ConstructP2pMessage(byte(0), buffer) |
|
|
|
s.Host.SendMessage(peer, content) |
|
|
|
s.Host.SendMessage(peer, content) |
|
|
|
log.Debug("Sent Ping Message to", "peer", peer) |
|
|
|
log.Debug("Sent Ping Message to", "peer", peer) |
|
|
|
|
|
|
|
s.stakingChan <- peer |
|
|
|
} |
|
|
|
} |
|
|
|