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