11package txsubmission
22
33import (
4- "fmt"
54 "github.com/cloudstruct/go-ouroboros-network/protocol"
65)
76
@@ -11,7 +10,7 @@ const (
1110)
1211
1312var (
14- STATE_HELLO = protocol .NewState (1 , "Hello " )
13+ STATE_INIT = protocol .NewState (1 , "Init " )
1514 STATE_IDLE = protocol .NewState (2 , "Idle" )
1615 STATE_TX_IDS_BLOCKING = protocol .NewState (3 , "TxIdsBlocking" )
1716 STATE_TX_IDS_NONBLOCKING = protocol .NewState (4 , "TxIdsNonBlocking" )
@@ -20,11 +19,11 @@ var (
2019)
2120
2221var StateMap = protocol.StateMap {
23- STATE_HELLO : protocol.StateMapEntry {
22+ STATE_INIT : protocol.StateMapEntry {
2423 Agency : protocol .AGENCY_CLIENT ,
2524 Transitions : []protocol.StateTransition {
2625 {
27- MsgType : MESSAGE_TYPE_HELLO ,
26+ MsgType : MESSAGE_TYPE_INIT ,
2827 NewState : STATE_IDLE ,
2928 },
3029 },
@@ -63,6 +62,10 @@ var StateMap = protocol.StateMap{
6362 MsgType : MESSAGE_TYPE_REPLY_TX_IDS ,
6463 NewState : STATE_IDLE ,
6564 },
65+ {
66+ MsgType : MESSAGE_TYPE_DONE ,
67+ NewState : STATE_DONE ,
68+ },
6669 },
6770 },
6871 STATE_TX_IDS_NONBLOCKING : protocol.StateMapEntry {
@@ -89,8 +92,8 @@ var StateMap = protocol.StateMap{
8992}
9093
9194type TxSubmission struct {
92- * protocol. Protocol
93- config * Config
95+ Client * Client
96+ Server * Server
9497}
9598
9699type Config struct {
@@ -99,7 +102,7 @@ type Config struct {
99102 RequestTxsFunc RequestTxsFunc
100103 ReplyTxsFunc ReplyTxsFunc
101104 DoneFunc DoneFunc
102- HelloFunc HelloFunc
105+ InitFunc InitFunc
103106}
104107
105108// Callback function types
@@ -108,101 +111,12 @@ type ReplyTxIdsFunc func(interface{}) error
108111type RequestTxsFunc func (interface {}) error
109112type ReplyTxsFunc func (interface {}) error
110113type DoneFunc func () error
111- type HelloFunc func () error
114+ type InitFunc func () error
112115
113- func New (options protocol.ProtocolOptions , cfg * Config ) * TxSubmission {
116+ func New (protoOptions protocol.ProtocolOptions , cfg * Config ) * TxSubmission {
114117 t := & TxSubmission {
115- config : cfg ,
118+ Client : NewClient (protoOptions , cfg ),
119+ Server : NewServer (protoOptions , cfg ),
116120 }
117- protoConfig := protocol.ProtocolConfig {
118- Name : PROTOCOL_NAME ,
119- ProtocolId : PROTOCOL_ID ,
120- Muxer : options .Muxer ,
121- ErrorChan : options .ErrorChan ,
122- Mode : options .Mode ,
123- Role : options .Role ,
124- MessageHandlerFunc : t .messageHandler ,
125- MessageFromCborFunc : NewMsgFromCbor ,
126- StateMap : StateMap ,
127- InitialState : STATE_HELLO ,
128- }
129- t .Protocol = protocol .New (protoConfig )
130121 return t
131122}
132-
133- func (t * TxSubmission ) Start () {
134- t .Protocol .Start ()
135- }
136-
137- func (t * TxSubmission ) messageHandler (msg protocol.Message , isResponse bool ) error {
138- var err error
139- switch msg .Type () {
140- case MESSAGE_TYPE_REQUEST_TX_IDS :
141- err = t .handleRequestTxIds (msg )
142- case MESSAGE_TYPE_REPLY_TX_IDS :
143- err = t .handleReplyTxIds (msg )
144- case MESSAGE_TYPE_REQUEST_TXS :
145- err = t .handleRequestTxs (msg )
146- case MESSAGE_TYPE_REPLY_TXS :
147- err = t .handleReplyTxs (msg )
148- case MESSAGE_TYPE_DONE :
149- err = t .handleDone ()
150- case MESSAGE_TYPE_HELLO :
151- err = t .handleHello ()
152- default :
153- err = fmt .Errorf ("%s: received unexpected message type %d" , PROTOCOL_NAME , msg .Type ())
154- }
155- return err
156- }
157-
158- func (t * TxSubmission ) handleRequestTxIds (msg protocol.Message ) error {
159- if t .config .RequestTxIdsFunc == nil {
160- return fmt .Errorf ("received tx-submission RequestTxIds message but no callback function is defined" )
161- }
162- msgRequestTxIds := msg .(* MsgRequestTxIds )
163- // Call the user callback function
164- return t .config .RequestTxIdsFunc (msgRequestTxIds .Blocking , msgRequestTxIds .Ack , msgRequestTxIds .Req )
165- }
166-
167- func (t * TxSubmission ) handleReplyTxIds (msg protocol.Message ) error {
168- if t .config .ReplyTxIdsFunc == nil {
169- return fmt .Errorf ("received tx-submission ReplyTxIds message but no callback function is defined" )
170- }
171- msgReplyTxIds := msg .(* MsgReplyTxIds )
172- // Call the user callback function
173- return t .config .ReplyTxIdsFunc (msgReplyTxIds .TxIds )
174- }
175-
176- func (t * TxSubmission ) handleRequestTxs (msg protocol.Message ) error {
177- if t .config .RequestTxsFunc == nil {
178- return fmt .Errorf ("received tx-submission RequestTxs message but no callback function is defined" )
179- }
180- msgRequestTxs := msg .(* MsgRequestTxs )
181- // Call the user callback function
182- return t .config .RequestTxsFunc (msgRequestTxs .TxIds )
183- }
184-
185- func (t * TxSubmission ) handleReplyTxs (msg protocol.Message ) error {
186- if t .config .ReplyTxsFunc == nil {
187- return fmt .Errorf ("received tx-submission ReplyTxs message but no callback function is defined" )
188- }
189- msgReplyTxs := msg .(* MsgReplyTxs )
190- // Call the user callback function
191- return t .config .ReplyTxsFunc (msgReplyTxs .Txs )
192- }
193-
194- func (t * TxSubmission ) handleDone () error {
195- if t .config .DoneFunc == nil {
196- return fmt .Errorf ("received tx-submission Done message but no callback function is defined" )
197- }
198- // Call the user callback function
199- return t .config .DoneFunc ()
200- }
201-
202- func (t * TxSubmission ) handleHello () error {
203- if t .config .HelloFunc == nil {
204- return fmt .Errorf ("received tx-submission Hello message but no callback function is defined" )
205- }
206- // Call the user callback function
207- return t .config .HelloFunc ()
208- }
0 commit comments