From 624d8c5dffa50980d5fb62c7d7c8ce6e5b4ebd0c Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Thu, 21 Feb 2019 21:01:01 -0800 Subject: [PATCH] clean up duplicate funcs --- node/node.go | 4 ++-- node/node_handler.go | 27 +++------------------------ 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/node/node.go b/node/node.go index f5387a508..8ce566bde 100644 --- a/node/node.go +++ b/node/node.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) diff --git a/node/node_handler.go b/node/node_handler.go index d46a44d3d..613b765cd 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -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))