[pong] actively sending pong message

Signed-off-by: Leo Chen <leo@harmony.one>
pull/627/head
Leo Chen 6 years ago
parent a7967c2f74
commit 78bcab7356
  1. 4
      consensus/consensus.go
  2. 4
      node/node.go
  3. 13
      node/node_handler.go

@ -404,13 +404,11 @@ func (consensus *Consensus) AddPeers(peers []*p2p.Peer) int {
count := 0 count := 0
for _, peer := range peers { for _, peer := range peers {
_, ok := consensus.validators.Load(peer.GetAddressHex()) _, ok := consensus.validators.LoadOrStore(peer.GetAddressHex(), *peer)
if !ok { if !ok {
consensus.validators.Store(peer.GetAddressHex(), *peer)
consensus.pubKeyLock.Lock() consensus.pubKeyLock.Lock()
consensus.PublicKeys = append(consensus.PublicKeys, peer.ConsensusPubKey) consensus.PublicKeys = append(consensus.PublicKeys, peer.ConsensusPubKey)
consensus.pubKeyLock.Unlock() consensus.pubKeyLock.Unlock()
// utils.GetLogInstance().Debug("[SYNC]", "new peer added", peer)
} }
count++ count++
} }

@ -321,7 +321,9 @@ func (node *Node) AddPeers(peers []*p2p.Peer) int {
} }
} }
if count > 0 { // Only leader needs to add the peer info into consensus
// Validators will receive the updated peer info from Leader via pong message
if count > 0 && node.NodeConfig.IsLeader() {
node.Consensus.AddPeers(peers) node.Consensus.AddPeers(peers)
// TODO: make peers into a context object shared by consensus and drand // TODO: make peers into a context object shared by consensus and drand
node.DRand.AddPeers(peers) node.DRand.AddPeers(peers)

@ -385,10 +385,7 @@ func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int {
if ping.Node.Role == proto_node.ClientRole { if ping.Node.Role == proto_node.ClientRole {
utils.GetLogInstance().Info("Add Client Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Client", peer) utils.GetLogInstance().Info("Add Client Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Client", peer)
node.ClientPeer = peer node.ClientPeer = peer
return 0 } else {
}
if node.NodeConfig.IsLeader() {
utils.GetLogInstance().Info("Add Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Pear", peer) utils.GetLogInstance().Info("Add Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Pear", peer)
node.AddPeers([]*p2p.Peer{peer}) node.AddPeers([]*p2p.Peer{peer})
} }
@ -398,10 +395,11 @@ func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int {
// SendPongMessage is the a goroutine to periodcally send pong message to all peers // SendPongMessage is the a goroutine to periodcally send pong message to all peers
func (node *Node) SendPongMessage() { func (node *Node) SendPongMessage() {
tick := time.NewTicker(3 * time.Second) tick := time.NewTicker(2 * time.Second)
numPeers := len(node.Consensus.GetValidatorPeers()) numPeers := len(node.Consensus.GetValidatorPeers())
numPubKeys := len(node.Consensus.PublicKeys) numPubKeys := len(node.Consensus.PublicKeys)
sentMessage := false sentMessage := false
firstTime := true
// Send Pong Message only when there is change on the number of peers // Send Pong Message only when there is change on the number of peers
for { for {
@ -417,6 +415,7 @@ func (node *Node) SendPongMessage() {
} }
// new peers added // new peers added
if numPubKeysNow != numPubKeys || numPeersNow != numPeers { if numPubKeysNow != numPubKeys || numPeersNow != numPeers {
utils.GetLogInstance().Info("[PONG] different number of peers", "numPeers", numPeers, "numPeersNow", numPeersNow)
sentMessage = false sentMessage = false
} else { } else {
// stable number of peers/pubkeys, sent the pong message // stable number of peers/pubkeys, sent the pong message
@ -436,7 +435,11 @@ func (node *Node) SendPongMessage() {
node.serviceManager.TakeAction(&service.Action{Action: service.Stop, ServiceType: service.PeerDiscovery}) node.serviceManager.TakeAction(&service.Action{Action: service.Stop, ServiceType: service.PeerDiscovery})
// wait a bit until all validators received pong message // wait a bit until all validators received pong message
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
// only need to notify consensus leader once to start the consensus
if firstTime {
node.startConsensus <- struct{}{} node.startConsensus <- struct{}{}
firstTime = false
}
} }
} }
numPeers = numPeersNow numPeers = numPeersNow

Loading…
Cancel
Save