clean up duplicate funcs

pull/493/head
Minh Doan 6 years ago committed by Minh Doan
parent 51349ca685
commit 624d8c5dff
  1. 4
      node/node.go
  2. 27
      node/node_handler.go

@ -296,7 +296,7 @@ func New(host p2p.Host, consensus *bft.Consensus, db ethdb.Database) *Node {
if consensus != nil && consensus.IsLeader {
node.State = NodeLeader
go node.ReceiveClientGroupMessage()
go node.ReceiveGroupMessage(node.clientReceiver)
} else {
node.State = NodeInit
}
@ -309,7 +309,7 @@ func New(host p2p.Host, consensus *bft.Consensus, db ethdb.Database) *Node {
go node.RemovePeersHandler()
// start the goroutine to receive group message
go node.ReceiveGroupMessage()
go node.ReceiveGroupMessage(node.groupReceiver)
node.duplicatedPing = make(map[string]bool)

@ -53,36 +53,15 @@ func (node *Node) StreamHandler(s p2p.Stream) {
}
// ReceiveGroupMessage use libp2p pubsub mechanism to receive broadcast messages
func (node *Node) ReceiveGroupMessage() {
func (node *Node) ReceiveGroupMessage(receiver p2p.GroupReceiver) {
ctx := context.Background()
for {
if node.groupReceiver == nil {
if receiver == nil {
time.Sleep(100 * time.Millisecond)
continue
}
msg, sender, err := node.groupReceiver.Receive(ctx)
msg, sender, err := receiver.Receive(ctx)
if sender != node.host.GetID() {
// utils.GetLogInstance().Info("[PUBSUB]", "received group msg", len(msg), "sender", sender)
if err == nil {
// skip the first 5 bytes, 1 byte is p2p type, 4 bytes are message size
node.messageHandler(msg[5:], string(sender))
}
}
}
}
// ReceiveClientGroupMessage use libp2p pubsub mechanism to receive broadcast messages for client
func (node *Node) ReceiveClientGroupMessage() {
ctx := context.Background()
for {
if node.clientReceiver == nil {
// check less frequent on client messages
time.Sleep(1000 * time.Millisecond)
continue
}
msg, sender, err := node.clientReceiver.Receive(ctx)
if sender != node.host.GetID() {
utils.GetLogInstance().Info("[CLIENT]", "received group msg", len(msg), "sender", sender)
if err == nil {
// skip the first 5 bytes, 1 byte is p2p type, 4 bytes are message size
node.messageHandler(msg[5:], string(sender))

Loading…
Cancel
Save