github.com/aergoio/aergo@v1.3.1/p2p/mofactory.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package p2p 7 8 import ( 9 "github.com/aergoio/aergo/p2p/p2pcommon" 10 "github.com/aergoio/aergo/p2p/p2putil" 11 "github.com/aergoio/etcd/raft/raftpb" 12 "time" 13 14 "github.com/aergoio/aergo/types" 15 "github.com/gofrs/uuid" 16 ) 17 18 type baseMOFactory struct { 19 p2ps *P2P 20 tnt p2pcommon.TxNoticeTracer 21 } 22 23 24 func (mf *baseMOFactory) NewMsgRequestOrder(expectResponse bool, protocolID p2pcommon.SubProtocol, message p2pcommon.MessageBody) p2pcommon.MsgOrder { 25 rmo := &pbRequestOrder{} 26 msgID := uuid.Must(uuid.NewV4()) 27 if mf.fillUpMsgOrder(&rmo.pbMessageOrder, msgID, uuid.Nil, protocolID, message) { 28 return rmo 29 } 30 return nil 31 } 32 33 func (mf *baseMOFactory) NewMsgBlockRequestOrder(respReceiver p2pcommon.ResponseReceiver, protocolID p2pcommon.SubProtocol, message p2pcommon.MessageBody) p2pcommon.MsgOrder { 34 rmo := &pbRequestOrder{} 35 msgID := uuid.Must(uuid.NewV4()) 36 if mf.fillUpMsgOrder(&rmo.pbMessageOrder, msgID, uuid.Nil, protocolID, message) { 37 rmo.respReceiver = respReceiver 38 return rmo 39 } 40 return nil 41 } 42 43 func (mf *baseMOFactory) NewMsgResponseOrder(reqID p2pcommon.MsgID, protocolID p2pcommon.SubProtocol, message p2pcommon.MessageBody) p2pcommon.MsgOrder { 44 rmo := &pbResponseOrder{} 45 msgID := uuid.Must(uuid.NewV4()) 46 if mf.fillUpMsgOrder(&rmo.pbMessageOrder, msgID, uuid.FromBytesOrNil(reqID[:]), protocolID, message) { 47 return rmo 48 } 49 return nil 50 } 51 52 func (mf *baseMOFactory) NewMsgBlkBroadcastOrder(noticeMsg *types.NewBlockNotice) p2pcommon.MsgOrder { 53 rmo := &pbBlkNoticeOrder{} 54 msgID := uuid.Must(uuid.NewV4()) 55 if mf.fillUpMsgOrder(&rmo.pbMessageOrder, msgID, uuid.Nil, p2pcommon.NewBlockNotice, noticeMsg) { 56 rmo.blkHash = noticeMsg.BlockHash 57 rmo.blkNo = noticeMsg.BlockNo 58 return rmo 59 } 60 return nil 61 } 62 63 func (mf *baseMOFactory) NewMsgTxBroadcastOrder(message *types.NewTransactionsNotice) p2pcommon.MsgOrder { 64 rmo := &pbTxNoticeOrder{} 65 reqID := uuid.Must(uuid.NewV4()) 66 if mf.fillUpMsgOrder(&rmo.pbMessageOrder, reqID, uuid.Nil, p2pcommon.NewTxNotice, message) { 67 rmo.txHashes = make([]types.TxID, len(message.TxHashes)) 68 for i, h := range message.TxHashes { 69 rmo.txHashes[i] = types.ToTxID(h) 70 } 71 rmo.tnt = mf.tnt 72 return rmo 73 } 74 return nil 75 } 76 77 func (mf *baseMOFactory) NewMsgBPBroadcastOrder(noticeMsg *types.BlockProducedNotice) p2pcommon.MsgOrder { 78 rmo := &pbBpNoticeOrder{} 79 msgID := uuid.Must(uuid.NewV4()) 80 if mf.fillUpMsgOrder(&rmo.pbMessageOrder, msgID, uuid.Nil, p2pcommon.BlockProducedNotice, noticeMsg) { 81 rmo.block = noticeMsg.Block 82 return rmo 83 } 84 return nil 85 } 86 87 func (mf *baseMOFactory) NewRaftMsgOrder(msgType raftpb.MessageType, raftMsg *raftpb.Message) p2pcommon.MsgOrder { 88 rmo := &pbRaftMsgOrder{msg: raftMsg, raftAcc: mf.p2ps.consacc.RaftAccessor()} 89 msgID := uuid.Must(uuid.NewV4()) 90 if mf.fillUpMsgOrder(&rmo.pbMessageOrder, msgID, uuid.Nil, p2pcommon.RaftWrapperMessage, raftMsg) { 91 switch msgType { 92 case raftpb.MsgHeartbeat, raftpb.MsgHeartbeatResp: 93 rmo.trace = false 94 default: 95 // follow default policy 96 } 97 return rmo 98 } 99 return nil 100 } 101 102 // newPbMsgOrder is base form of making sendRequest struct 103 func (mf *baseMOFactory) fillUpMsgOrder(mo *pbMessageOrder, msgID, orgID uuid.UUID, protocolID p2pcommon.SubProtocol, messageBody p2pcommon.MessageBody) bool { 104 id := p2pcommon.MsgID(msgID) 105 originalID := p2pcommon.MsgID(orgID) 106 bytes, err := p2putil.MarshalMessageBody(messageBody) 107 if err != nil { 108 return false 109 } 110 msg := p2pcommon.NewMessageValue(protocolID, id, originalID, time.Now().UnixNano(), bytes) 111 mo.protocolID = protocolID 112 mo.needSign = true 113 mo.message = msg 114 mo.trace = true 115 116 return true 117 }