github.com/turingchain2020/turingchain@v1.1.21/p2p/event.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package p2p 6 7 import ( 8 "github.com/turingchain2020/turingchain/common/log/log15" 9 "github.com/turingchain2020/turingchain/queue" 10 "github.com/turingchain2020/turingchain/types" 11 ) 12 13 var ( 14 log = log15.New("module", "p2p.manage") 15 ) 16 17 /** 处理系统发送至p2p模块的事件, 包含下载, 交易及区块广播等 18 * 主要是为了兼容多种类型p2p, 为不同的事件指定路由策略 19 */ 20 func (mgr *Manager) handleSysEvent() { 21 mgr.Client.Sub("p2p") 22 log.Debug("Manager handleSysEvent start") 23 for msg := range mgr.Client.Recv() { 24 switch msg.Ty { 25 26 case types.EventTxBroadcast, types.EventBlockBroadcast: //广播 27 mgr.pub2All(msg) 28 case types.EventFetchBlocks, types.EventGetMempool, types.EventFetchBlockHeaders: 29 mgr.pub2P2P(msg, mgr.p2pCfg.Types[0]) 30 case types.EventPeerInfo: 31 // 采用默认配置 32 p2pTy := mgr.p2pCfg.Types[0] 33 req, _ := msg.Data.(*types.P2PGetPeerReq) 34 for _, ty := range mgr.p2pCfg.Types { 35 if ty == req.GetP2PType() { 36 p2pTy = req.GetP2PType() 37 } 38 } 39 mgr.pub2P2P(msg, p2pTy) 40 41 case types.EventGetNetInfo: 42 // 采用默认配置 43 p2pTy := mgr.p2pCfg.Types[0] 44 req, _ := msg.Data.(*types.P2PGetNetInfoReq) 45 for _, ty := range mgr.p2pCfg.Types { 46 if ty == req.GetP2PType() { 47 p2pTy = req.GetP2PType() 48 } 49 } 50 mgr.pub2P2P(msg, p2pTy) 51 52 case types.EventPubTopicMsg, types.EventFetchTopics, types.EventRemoveTopic, types.EventSubTopic: 53 p2pTy := mgr.p2pCfg.Types[0] 54 mgr.pub2P2P(msg, p2pTy) 55 56 default: 57 mgr.pub2P2P(msg, "dht") 58 //log.Warn("unknown msgtype", "msg", msg) 59 //msg.Reply(mgr.Client.NewMessage("", msg.Ty, types.Reply{Msg: []byte("unknown msgtype")})) 60 //continue 61 } 62 } 63 log.Debug("Manager handleSysEvent stop") 64 } 65 66 // 处理p2p内部向外发送的消息, 主要是为了兼容多种类型p2p广播消息, 避免重复接交易或者区块 67 func (mgr *Manager) handleP2PSub() { 68 69 //mgr.subChan = mgr.PubSub.Sub("p2p") 70 log.Debug("Manager handleP2PSub start") 71 //for msg := range mgr.subChan { 72 // 73 //} 74 75 } 76 77 // PubBroadCast 兼容多种类型p2p广播消息, 避免重复接交易或者区块 78 func (mgr *Manager) PubBroadCast(hash string, data interface{}, eventTy int) error { 79 80 exist, _ := mgr.broadcastFilter.ContainsOrAdd(hash, true) 81 // eventTy, 交易=1, 区块=54 82 //log.Debug("PubBroadCast", "eventTy", eventTy, "hash", hash, "exist", exist) 83 if exist { 84 return nil 85 } 86 var err error 87 if eventTy == types.EventTx { 88 //同步模式发送交易,但不需要进行等待回复,目的是为了在消息队列内部使用高速模式 89 err = mgr.Client.Send(mgr.Client.NewMessage("mempool", types.EventTx, data), true) 90 } else if eventTy == types.EventBroadcastAddBlock { 91 err = mgr.Client.Send(mgr.Client.NewMessage("blockchain", types.EventBroadcastAddBlock, data), true) 92 } 93 if err != nil { 94 log.Error("PubBroadCast", "eventTy", eventTy, "sendMsgErr", err) 95 } 96 return err 97 } 98 99 // 100 func (mgr *Manager) pub2All(msg *queue.Message) { 101 102 for _, ty := range mgr.p2pCfg.Types { 103 mgr.PubSub.Pub(msg, ty) 104 } 105 106 } 107 108 // 109 func (mgr *Manager) pub2P2P(msg *queue.Message, p2pType string) { 110 111 mgr.PubSub.Pub(msg, p2pType) 112 }