diff --git a/api/service/discovery/discovery_test.go b/api/service/discovery/discovery_test.go index e9f3043c9..1eb35fc22 100644 --- a/api/service/discovery/discovery_test.go +++ b/api/service/discovery/discovery_test.go @@ -2,6 +2,7 @@ package discovery import ( "testing" + "time" "github.com/harmony-one/harmony/api/service" "github.com/harmony-one/harmony/internal/utils" @@ -16,10 +17,17 @@ var ( ) func TestDiscoveryService(t *testing.T) { - selfPeer := p2p.Peer{IP: ip, Port: port} - priKey, _, err := utils.GenKeyP2P(ip, port) + nodePriKey, _, err := utils.LoadKeyFromFile("/tmp/127.0.0.1.12345.key") + if err != nil { + t.Fatal(err) + } + peerPriKey, peerPubKey := utils.GenKey("127.0.0.1", "12345") + if peerPriKey == nil || peerPubKey == nil { + t.Fatal("generate key error") + } + selfPeer := p2p.Peer{IP: "127.0.0.1", Port: "12345", ValidatorID: -1, PubKey: peerPubKey} - host, err := p2pimpl.NewHost(&selfPeer, priKey) + host, err := p2pimpl.NewHost(&selfPeer, nodePriKey) if err != nil { t.Fatalf("unable to new host in harmony: %v", err) } @@ -31,4 +39,10 @@ func TestDiscoveryService(t *testing.T) { if dService == nil { t.Fatalf("unable to create new discovery service") } + + dService.StartService() + + time.Sleep(3 * time.Second) + + dService.StopService() } diff --git a/api/service/discovery/service.go b/api/service/discovery/service.go index 1f22c5bd0..cc124d77f 100644 --- a/api/service/discovery/service.go +++ b/api/service/discovery/service.go @@ -81,6 +81,7 @@ func (s *Service) contactP2pPeers() { pingMsg.Node.Role = proto_node.ClientRole clientMsgBuf := host.ConstructP2pMessage(byte(0), pingMsg.ConstructPingMessage()) + s.sentPingMessage(p2p.GroupIDBeacon, regMsgBuf, clientMsgBuf) for { select { case peer, ok := <-s.peerChan: @@ -104,7 +105,6 @@ func (s *Service) contactP2pPeers() { case action := <-s.actionChan: s.config.Actions[action.Name] = action.Action case <-tick.C: - var err error for g, a := range s.config.Actions { if a == p2p.ActionPause { // Recived Pause Message, to reduce the frequency of ping message to every 1 minute @@ -115,38 +115,46 @@ func (s *Service) contactP2pPeers() { } if a == p2p.ActionStart || a == p2p.ActionResume || a == p2p.ActionPause { - if g == p2p.GroupIDBeacon || g == p2p.GroupIDBeaconClient { - if s.config.IsBeacon { - // beacon chain node - err = s.host.SendMessageToGroups([]p2p.GroupID{s.config.Beacon}, regMsgBuf) - } else { - // non-beacon chain node, reg as client node - err = s.host.SendMessageToGroups([]p2p.GroupID{s.config.Beacon}, clientMsgBuf) - } - } else { - // The following logical will be used for 2nd stage peer discovery process - if s.config.Group == p2p.GroupIDUnknown { - continue - } - if s.config.IsClient { - // client node of reg shard, such as wallet/txgen - err = s.host.SendMessageToGroups([]p2p.GroupID{s.config.Group}, clientMsgBuf) - } else { - // regular node of a shard - err = s.host.SendMessageToGroups([]p2p.GroupID{s.config.Group}, regMsgBuf) - } - } - if err != nil { - utils.GetLogInstance().Error("[DISCOVERY] Failed to send ping message", "group", g) - } else { - //utils.GetLogInstance().Info("[DISCOVERY]", "Sent Ping Message", g) - } + s.sentPingMessage(g, regMsgBuf, clientMsgBuf) } } } } } +// sentPingMessage sends a ping message to a pubsub topic +func (s *Service) sentPingMessage(g p2p.GroupID, regMsgBuf, clientMsgBuf []byte) { + var err error + if g == p2p.GroupIDBeacon || g == p2p.GroupIDBeaconClient { + if s.config.IsBeacon { + // beacon chain node + err = s.host.SendMessageToGroups([]p2p.GroupID{s.config.Beacon}, regMsgBuf) + } else { + // non-beacon chain node, reg as client node + err = s.host.SendMessageToGroups([]p2p.GroupID{s.config.Beacon}, clientMsgBuf) + } + } else { + // The following logical will be used for 2nd stage peer discovery process + // do nothing when the groupID is unknown + if s.config.Group == p2p.GroupIDUnknown { + return + } + if s.config.IsClient { + // client node of reg shard, such as wallet/txgen + err = s.host.SendMessageToGroups([]p2p.GroupID{s.config.Group}, clientMsgBuf) + } else { + // regular node of a shard + err = s.host.SendMessageToGroups([]p2p.GroupID{s.config.Group}, regMsgBuf) + } + } + if err != nil { + utils.GetLogInstance().Error("Failed to send ping message", "group", g) + } else { + utils.GetLogInstance().Info("[DISCOVERY]", "Sent Ping Message", g) + } + +} + // Init is to initialize for discoveryService. func (s *Service) Init() { utils.GetLogInstance().Info("Init discovery service")