2222 STATE_DONE = protocol .NewState (5 , "Done" )
2323)
2424
25+ var stateMap = protocol.StateMap {
26+ STATE_IDLE : protocol.StateMapEntry {
27+ Agency : protocol .AGENCY_CLIENT ,
28+ Transitions : []protocol.StateTransition {
29+ {
30+ MsgType : MESSAGE_TYPE_REQUEST_NEXT ,
31+ NewState : STATE_CAN_AWAIT ,
32+ },
33+ {
34+ MsgType : MESSAGE_TYPE_FIND_INTERSECT ,
35+ NewState : STATE_INTERSECT ,
36+ },
37+ {
38+ MsgType : MESSAGE_TYPE_DONE ,
39+ NewState : STATE_DONE ,
40+ },
41+ },
42+ },
43+ STATE_CAN_AWAIT : protocol.StateMapEntry {
44+ Agency : protocol .AGENCY_SERVER ,
45+ Transitions : []protocol.StateTransition {
46+ {
47+ MsgType : MESSAGE_TYPE_AWAIT_REPLY ,
48+ NewState : STATE_MUST_REPLY ,
49+ },
50+ {
51+ MsgType : MESSAGE_TYPE_ROLL_FORWARD ,
52+ NewState : STATE_IDLE ,
53+ },
54+ {
55+ MsgType : MESSAGE_TYPE_ROLL_BACKWARD ,
56+ NewState : STATE_IDLE ,
57+ },
58+ },
59+ },
60+ STATE_INTERSECT : protocol.StateMapEntry {
61+ Agency : protocol .AGENCY_SERVER ,
62+ Transitions : []protocol.StateTransition {
63+ {
64+ MsgType : MESSAGE_TYPE_INTERSECT_FOUND ,
65+ NewState : STATE_IDLE ,
66+ },
67+ {
68+ MsgType : MESSAGE_TYPE_INTERSECT_NOT_FOUND ,
69+ NewState : STATE_IDLE ,
70+ },
71+ },
72+ },
73+ STATE_MUST_REPLY : protocol.StateMapEntry {
74+ Agency : protocol .AGENCY_SERVER ,
75+ Transitions : []protocol.StateTransition {
76+ {
77+ MsgType : MESSAGE_TYPE_ROLL_FORWARD ,
78+ NewState : STATE_IDLE ,
79+ },
80+ {
81+ MsgType : MESSAGE_TYPE_ROLL_BACKWARD ,
82+ NewState : STATE_IDLE ,
83+ },
84+ },
85+ },
86+ STATE_DONE : protocol.StateMapEntry {
87+ Agency : protocol .AGENCY_NONE ,
88+ },
89+ }
90+
2591type ChainSync struct {
2692 proto * protocol.Protocol
2793 nodeToNode bool
@@ -56,9 +122,17 @@ func New(m *muxer.Muxer, errorChan chan error, nodeToNode bool, callbackConfig *
56122 nodeToNode : nodeToNode ,
57123 callbackConfig : callbackConfig ,
58124 }
59- c .proto = protocol .New (PROTOCOL_NAME , protocolId , m , errorChan , c .messageHandler , c .NewMsgFromCbor )
60- // Set initial state
61- c .proto .SetState (STATE_IDLE )
125+ protoConfig := protocol.ProtocolConfig {
126+ Name : PROTOCOL_NAME ,
127+ ProtocolId : protocolId ,
128+ Muxer : m ,
129+ ErrorChan : errorChan ,
130+ MessageHandlerFunc : c .messageHandler ,
131+ MessageFromCborFunc : c .NewMsgFromCbor ,
132+ StateMap : stateMap ,
133+ InitialState : STATE_IDLE ,
134+ }
135+ c .proto = protocol .New (protoConfig )
62136 return c
63137}
64138
@@ -84,45 +158,24 @@ func (c *ChainSync) messageHandler(msg protocol.Message) error {
84158}
85159
86160func (c * ChainSync ) RequestNext () error {
87- if err := c .proto .LockState ([]protocol.State {STATE_IDLE }); err != nil {
88- return fmt .Errorf ("%s: RequestNext: protocol not in expected state" , PROTOCOL_NAME )
89- }
90- // Create our request
91161 msg := newMsgRequestNext ()
92- // Unlock and change state when we're done
93- defer c .proto .UnlockState (STATE_CAN_AWAIT )
94- // Send request
95162 return c .proto .SendMessage (msg , false )
96163}
97164
98165func (c * ChainSync ) FindIntersect (points []interface {}) error {
99- if err := c .proto .LockState ([]protocol.State {STATE_IDLE }); err != nil {
100- return fmt .Errorf ("%s: FindIntersect: protocol not in expected state" , PROTOCOL_NAME )
101- }
102166 msg := newMsgFindIntersect (points )
103- // Unlock and change state when we're done
104- defer c .proto .UnlockState (STATE_INTERSECT )
105- // Send request
106167 return c .proto .SendMessage (msg , false )
107168}
108169
109170func (c * ChainSync ) handleAwaitReply () error {
110- if err := c .proto .LockState ([]protocol.State {STATE_CAN_AWAIT }); err != nil {
111- return fmt .Errorf ("received chain-sync AwaitReply message when protocol not in expected state" )
112- }
113171 if c .callbackConfig .AwaitReplyFunc == nil {
114172 return fmt .Errorf ("received chain-sync AwaitReply message but no callback function is defined" )
115173 }
116- // Unlock and change state when we're done
117- defer c .proto .UnlockState (STATE_MUST_REPLY )
118174 // Call the user callback function
119175 return c .callbackConfig .AwaitReplyFunc ()
120176}
121177
122178func (c * ChainSync ) handleRollForward (msgGeneric protocol.Message ) error {
123- if err := c .proto .LockState ([]protocol.State {STATE_CAN_AWAIT , STATE_MUST_REPLY }); err != nil {
124- return fmt .Errorf ("received chain-sync RollForward message when protocol not in expected state" )
125- }
126179 if c .callbackConfig .RollForwardFunc == nil {
127180 return fmt .Errorf ("received chain-sync RollForward message but no callback function is defined" )
128181 }
@@ -163,8 +216,6 @@ func (c *ChainSync) handleRollForward(msgGeneric protocol.Message) error {
163216 return err
164217 }
165218 }
166- // Unlock and change state when we're done
167- defer c .proto .UnlockState (STATE_IDLE )
168219 // Call the user callback function
169220 return c .callbackConfig .RollForwardFunc (blockType , blockHeader )
170221 } else {
@@ -178,64 +229,42 @@ func (c *ChainSync) handleRollForward(msgGeneric protocol.Message) error {
178229 if err != nil {
179230 return err
180231 }
181- // Unlock and change state when we're done
182- defer c .proto .UnlockState (STATE_IDLE )
183232 // Call the user callback function
184233 return c .callbackConfig .RollForwardFunc (wrapBlock .Type , blk )
185234 }
186235}
187236
188237func (c * ChainSync ) handleRollBackward (msgGeneric protocol.Message ) error {
189- if err := c .proto .LockState ([]protocol.State {STATE_CAN_AWAIT , STATE_MUST_REPLY }); err != nil {
190- return fmt .Errorf ("received chain-sync RollBackward message when protocol not in expected state" )
191- }
192238 if c .callbackConfig .RollBackwardFunc == nil {
193239 return fmt .Errorf ("received chain-sync RollBackward message but no callback function is defined" )
194240 }
195241 msg := msgGeneric .(* msgRollBackward )
196- // Unlock and change state when we're done
197- defer c .proto .UnlockState (STATE_IDLE )
198242 // Call the user callback function
199243 return c .callbackConfig .RollBackwardFunc (msg .Point , msg .Tip )
200244}
201245
202246func (c * ChainSync ) handleIntersectFound (msgGeneric protocol.Message ) error {
203- if err := c .proto .LockState ([]protocol.State {STATE_INTERSECT }); err != nil {
204- return fmt .Errorf ("received chain-sync IntersectFound message when protocol not in expected state" )
205- }
206247 if c .callbackConfig .IntersectFoundFunc == nil {
207248 return fmt .Errorf ("received chain-sync IntersectFound message but no callback function is defined" )
208249 }
209250 msg := msgGeneric .(* msgIntersectFound )
210- // Unlock and change state when we're done
211- defer c .proto .UnlockState (STATE_IDLE )
212251 // Call the user callback function
213252 return c .callbackConfig .IntersectFoundFunc (msg .Point , msg .Tip )
214253}
215254
216255func (c * ChainSync ) handleIntersectNotFound (msgGeneric protocol.Message ) error {
217- if err := c .proto .LockState ([]protocol.State {STATE_INTERSECT }); err != nil {
218- return fmt .Errorf ("received chain-sync IntersectNotFound message when protocol not in expected state" )
219- }
220256 if c .callbackConfig .IntersectNotFoundFunc == nil {
221257 return fmt .Errorf ("received chain-sync IntersectNotFound message but no callback function is defined" )
222258 }
223259 msg := msgGeneric .(* msgIntersectNotFound )
224- // Unlock and change state when we're done
225- defer c .proto .UnlockState (STATE_IDLE )
226260 // Call the user callback function
227261 return c .callbackConfig .IntersectNotFoundFunc (msg .Tip )
228262}
229263
230264func (c * ChainSync ) handleDone () error {
231- if err := c .proto .LockState ([]protocol.State {STATE_IDLE }); err != nil {
232- return fmt .Errorf ("received chain-sync Done message when protocol not in expected state" )
233- }
234265 if c .callbackConfig .DoneFunc == nil {
235266 return fmt .Errorf ("received chain-sync Done message but no callback function is defined" )
236267 }
237- // Unlock and change state when we're done
238- defer c .proto .UnlockState (STATE_DONE )
239268 // Call the user callback function
240269 return c .callbackConfig .DoneFunc ()
241270}
0 commit comments