Skip to content

Commit 617e6f9

Browse files
authored
Merge pull request #57 from cloudstruct/feature/protocol-message-new-function
Create New() functions for all message types
2 parents 9c93aa4 + 095edb0 commit 617e6f9

File tree

6 files changed

+162
-12
lines changed

6 files changed

+162
-12
lines changed

protocol/blockfetch/messages.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,56 @@ type MsgStartBatch struct {
7676
protocol.MessageBase
7777
}
7878

79+
func NewMsgStartBatch() *MsgStartBatch {
80+
m := &MsgStartBatch{
81+
MessageBase: protocol.MessageBase{
82+
MessageType: MESSAGE_TYPE_START_BATCH,
83+
},
84+
}
85+
return m
86+
}
87+
7988
type MsgNoBlocks struct {
8089
protocol.MessageBase
8190
}
8291

92+
func NewMsgNoBlocks() *MsgNoBlocks {
93+
m := &MsgNoBlocks{
94+
MessageBase: protocol.MessageBase{
95+
MessageType: MESSAGE_TYPE_NO_BLOCKS,
96+
},
97+
}
98+
return m
99+
}
100+
83101
type MsgBlock struct {
84102
protocol.MessageBase
85103
WrappedBlock []byte
86104
}
87105

106+
func NewMsgBlock(wrappedBlock []byte) *MsgBlock {
107+
m := &MsgBlock{
108+
MessageBase: protocol.MessageBase{
109+
MessageType: MESSAGE_TYPE_BLOCK,
110+
},
111+
WrappedBlock: wrappedBlock,
112+
}
113+
return m
114+
}
115+
88116
type MsgBatchDone struct {
89117
protocol.MessageBase
90118
}
91119

120+
func NewMsgBatchDone() *MsgBatchDone {
121+
m := &MsgBatchDone{
122+
MessageBase: protocol.MessageBase{
123+
MessageType: MESSAGE_TYPE_BATCH_DONE,
124+
},
125+
}
126+
return m
127+
}
128+
92129
// TODO: use this above and expose it, or just remove it
93130
/*
94131
type point struct {

protocol/chainsync/messages.go

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,78 @@ type MsgRequestNext struct {
6565
}
6666

6767
func NewMsgRequestNext() *MsgRequestNext {
68-
r := &MsgRequestNext{
68+
m := &MsgRequestNext{
6969
MessageBase: protocol.MessageBase{
7070
MessageType: MESSAGE_TYPE_REQUEST_NEXT,
7171
},
7272
}
73-
return r
73+
return m
7474
}
7575

7676
type MsgAwaitReply struct {
7777
protocol.MessageBase
7878
}
7979

80+
func NewMsgAwaitReply() *MsgAwaitReply {
81+
m := &MsgAwaitReply{
82+
MessageBase: protocol.MessageBase{
83+
MessageType: MESSAGE_TYPE_AWAIT_REPLY,
84+
},
85+
}
86+
return m
87+
}
88+
8089
type MsgRollForwardNtC struct {
8190
protocol.MessageBase
8291
WrappedData []byte
8392
Tip Tip
8493
}
8594

95+
func NewMsgRollForwardNtC(wrappedData []byte, tip Tip) *MsgRollForwardNtC {
96+
m := &MsgRollForwardNtC{
97+
MessageBase: protocol.MessageBase{
98+
MessageType: MESSAGE_TYPE_ROLL_FORWARD,
99+
},
100+
WrappedData: wrappedData,
101+
Tip: tip,
102+
}
103+
return m
104+
}
105+
86106
type MsgRollForwardNtN struct {
87107
protocol.MessageBase
88108
WrappedHeader WrappedHeader
89109
Tip Tip
90110
}
91111

112+
func NewMsgRollForwardNtN(wrappedHeader WrappedHeader, tip Tip) *MsgRollForwardNtN {
113+
m := &MsgRollForwardNtN{
114+
MessageBase: protocol.MessageBase{
115+
MessageType: MESSAGE_TYPE_ROLL_FORWARD,
116+
},
117+
WrappedHeader: wrappedHeader,
118+
Tip: tip,
119+
}
120+
return m
121+
}
122+
92123
type MsgRollBackward struct {
93124
protocol.MessageBase
94125
Point Point
95126
Tip Tip
96127
}
97128

129+
func NewMsgRollBackward(point Point, tip Tip) *MsgRollBackward {
130+
m := &MsgRollBackward{
131+
MessageBase: protocol.MessageBase{
132+
MessageType: MESSAGE_TYPE_ROLL_BACKWARD,
133+
},
134+
Point: point,
135+
Tip: tip,
136+
}
137+
return m
138+
}
139+
98140
type MsgFindIntersect struct {
99141
protocol.MessageBase
100142
Points []interface{}
@@ -116,15 +158,45 @@ type MsgIntersectFound struct {
116158
Tip Tip
117159
}
118160

161+
func NewMsgIntersectFound(point Point, tip Tip) *MsgIntersectFound {
162+
m := &MsgIntersectFound{
163+
MessageBase: protocol.MessageBase{
164+
MessageType: MESSAGE_TYPE_INTERSECT_FOUND,
165+
},
166+
Point: point,
167+
Tip: tip,
168+
}
169+
return m
170+
}
171+
119172
type MsgIntersectNotFound struct {
120173
protocol.MessageBase
121174
Tip Tip
122175
}
123176

177+
func NewMsgIntersectNotFound(tip Tip) *MsgIntersectNotFound {
178+
m := &MsgIntersectNotFound{
179+
MessageBase: protocol.MessageBase{
180+
MessageType: MESSAGE_TYPE_INTERSECT_NOT_FOUND,
181+
},
182+
Tip: tip,
183+
}
184+
return m
185+
}
186+
124187
type MsgDone struct {
125188
protocol.MessageBase
126189
}
127190

191+
func NewMsgDone() *MsgDone {
192+
m := &MsgDone{
193+
MessageBase: protocol.MessageBase{
194+
MessageType: MESSAGE_TYPE_DONE,
195+
},
196+
}
197+
return m
198+
}
199+
128200
type Tip struct {
129201
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
130202
_ struct{} `cbor:",toarray"`

protocol/handshake/messages.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ type MsgRefuse struct {
7272
protocol.MessageBase
7373
Reason []interface{}
7474
}
75+
76+
func NewMsgRefuse(reason []interface{}) *MsgRefuse {
77+
m := &MsgRefuse{
78+
MessageBase: protocol.MessageBase{
79+
MessageType: MESSAGE_TYPE_REFUSE,
80+
},
81+
Reason: reason,
82+
}
83+
return m
84+
}

protocol/keepalive/messages.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,12 @@ func NewMsgKeepAliveResponse(cookie uint16) *MsgKeepAliveResponse {
6565
type MsgDone struct {
6666
protocol.MessageBase
6767
}
68+
69+
func NewMsgDone() *MsgDone {
70+
m := &MsgDone{
71+
MessageBase: protocol.MessageBase{
72+
MessageType: MESSAGE_TYPE_DONE,
73+
},
74+
}
75+
return m
76+
}

protocol/localtxsubmission/messages.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,30 @@ type MsgAcceptTx struct {
6969
protocol.MessageBase
7070
}
7171

72+
func NewMsgAcceptTx() *MsgAcceptTx {
73+
m := &MsgAcceptTx{
74+
MessageBase: protocol.MessageBase{
75+
MessageType: MESSAGE_TYPE_ACCEPT_TX,
76+
},
77+
}
78+
return m
79+
}
80+
7281
type MsgRejectTx struct {
7382
protocol.MessageBase
7483
Reason interface{}
7584
}
7685

86+
func NewMsgRejectTx(reason interface{}) *MsgRejectTx {
87+
m := &MsgRejectTx{
88+
MessageBase: protocol.MessageBase{
89+
MessageType: MESSAGE_TYPE_REJECT_TX,
90+
},
91+
Reason: reason,
92+
}
93+
return m
94+
}
95+
7796
type MsgDone struct {
7897
protocol.MessageBase
7998
}

protocol/txsubmission/messages.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,42 +62,45 @@ func NewMsgRequestTxIds(blocking bool, ack uint16, req uint16) *MsgRequestTxIds
6262

6363
type MsgReplyTxIds struct {
6464
protocol.MessageBase
65-
TxIds []txIdAndSize
65+
TxIds []TxIdAndSize
6666
}
6767

68-
func NewMsgReplyTxIds() *MsgReplyTxIds {
68+
func NewMsgReplyTxIds(txIds []TxIdAndSize) *MsgReplyTxIds {
6969
m := &MsgReplyTxIds{
7070
MessageBase: protocol.MessageBase{
7171
MessageType: MESSAGE_TYPE_REPLY_TX_IDS,
7272
},
73+
TxIds: txIds,
7374
}
7475
return m
7576
}
7677

7778
type MsgRequestTxs struct {
7879
protocol.MessageBase
79-
TxIds []txId
80+
TxIds []TxId
8081
}
8182

82-
func NewMsgRequestTxs() *MsgRequestTxs {
83+
func NewMsgRequestTxs(txIds []TxId) *MsgRequestTxs {
8384
m := &MsgRequestTxs{
8485
MessageBase: protocol.MessageBase{
8586
MessageType: MESSAGE_TYPE_REQUEST_TXS,
8687
},
88+
TxIds: txIds,
8789
}
8890
return m
8991
}
9092

9193
type MsgReplyTxs struct {
9294
protocol.MessageBase
93-
Txs []txBody
95+
Txs []TxBody
9496
}
9597

96-
func NewMsgReplyTxs() *MsgReplyTxs {
98+
func NewMsgReplyTxs(txs []TxBody) *MsgReplyTxs {
9799
m := &MsgReplyTxs{
98100
MessageBase: protocol.MessageBase{
99101
MessageType: MESSAGE_TYPE_REPLY_TXS,
100102
},
103+
Txs: txs,
101104
}
102105
return m
103106
}
@@ -128,17 +131,17 @@ func NewMsgHello() *MsgHello {
128131
return m
129132
}
130133

131-
type txId struct {
134+
type TxId struct {
132135
EraId uint16
133136
TxId [32]byte
134137
}
135138

136-
type txBody struct {
139+
type TxBody struct {
137140
EraId uint16
138141
TxBody []byte
139142
}
140143

141-
type txIdAndSize struct {
142-
TxId txId
144+
type TxIdAndSize struct {
145+
TxId TxId
143146
Size uint32
144147
}

0 commit comments

Comments
 (0)