@@ -12,12 +12,14 @@ const (
1212 PROTOCOL_NAME = "chain-sync"
1313 PROTOCOL_ID_NTN uint16 = 2
1414 PROTOCOL_ID_NTC uint16 = 5
15+ )
1516
16- STATE_IDLE = iota
17- STATE_CAN_AWAIT
18- STATE_MUST_REPLY
19- STATE_INTERSECT
20- STATE_DONE
17+ var (
18+ STATE_IDLE = protocol .NewState (1 , "Idle" )
19+ STATE_CAN_AWAIT = protocol .NewState (2 , "CanAwait" )
20+ STATE_MUST_REPLY = protocol .NewState (3 , "MustReply" )
21+ STATE_INTERSECT = protocol .NewState (4 , "Intersect" )
22+ STATE_DONE = protocol .NewState (5 , "Done" )
2123)
2224
2325type ChainSync struct {
@@ -55,6 +57,7 @@ func New(m *muxer.Muxer, errorChan chan error, nodeToNode bool, callbackConfig *
5557 callbackConfig : callbackConfig ,
5658 }
5759 c .proto = protocol .New (PROTOCOL_NAME , protocolId , m , errorChan , c .messageHandler , c .NewMsgFromCbor )
60+ // Set initial state
5861 c .proto .SetState (STATE_IDLE )
5962 return c
6063}
@@ -81,39 +84,43 @@ func (c *ChainSync) messageHandler(msg protocol.Message) error {
8184}
8285
8386func (c * ChainSync ) RequestNext () error {
84- if c .proto .GetState () != STATE_IDLE {
85- return fmt .Errorf ("chain-sync : RequestNext: protocol not in expected state" )
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 )
8689 }
8790 // Create our request
8891 msg := newMsgRequestNext ()
89- c .proto .SetState (STATE_CAN_AWAIT )
92+ // Unlock and change state when we're done
93+ defer c .proto .UnlockState (STATE_CAN_AWAIT )
9094 // Send request
9195 return c .proto .SendMessage (msg , false )
9296}
9397
9498func (c * ChainSync ) FindIntersect (points []interface {}) error {
95- if c .proto .GetState () != STATE_IDLE {
96- return fmt .Errorf ("chain-sync : FindIntersect: protocol not in expected state" )
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 )
97101 }
98102 msg := newMsgFindIntersect (points )
99- c .proto .SetState (STATE_INTERSECT )
103+ // Unlock and change state when we're done
104+ defer c .proto .UnlockState (STATE_INTERSECT )
100105 // Send request
101106 return c .proto .SendMessage (msg , false )
102107}
103108
104109func (c * ChainSync ) handleAwaitReply () error {
105- if c .proto .GetState () != STATE_CAN_AWAIT {
110+ if err := c .proto .LockState ([]protocol. State { STATE_CAN_AWAIT }); err != nil {
106111 return fmt .Errorf ("received chain-sync AwaitReply message when protocol not in expected state" )
107112 }
108113 if c .callbackConfig .AwaitReplyFunc == nil {
109114 return fmt .Errorf ("received chain-sync AwaitReply message but no callback function is defined" )
110115 }
111- c .proto .SetState (STATE_MUST_REPLY )
116+ // Unlock and change state when we're done
117+ defer c .proto .UnlockState (STATE_MUST_REPLY )
118+ // Call the user callback function
112119 return c .callbackConfig .AwaitReplyFunc ()
113120}
114121
115122func (c * ChainSync ) handleRollForward (msgGeneric protocol.Message ) error {
116- if c . proto . GetState () != STATE_CAN_AWAIT && c .proto .GetState () != STATE_MUST_REPLY {
123+ if err := c .proto .LockState ([]protocol. State { STATE_CAN_AWAIT , STATE_MUST_REPLY }); err != nil {
117124 return fmt .Errorf ("received chain-sync RollForward message when protocol not in expected state" )
118125 }
119126 if c .callbackConfig .RollForwardFunc == nil {
@@ -128,7 +135,7 @@ func (c *ChainSync) handleRollForward(msgGeneric protocol.Message) error {
128135 case block .BLOCK_HEADER_TYPE_BYRON :
129136 var wrapHeaderByron wrappedHeaderByron
130137 if _ , err := utils .CborDecode (msg .WrappedHeader .RawData , & wrapHeaderByron ); err != nil {
131- return fmt .Errorf ("chain-sync : decode error: %s" , err )
138+ return fmt .Errorf ("%s : decode error: %s" , PROTOCOL_NAME , err )
132139 }
133140 blockType = wrapHeaderByron .Unknown .Type
134141 var err error
@@ -148,75 +155,87 @@ func (c *ChainSync) handleRollForward(msgGeneric protocol.Message) error {
148155 // We decode into a byte array to implicitly unwrap the CBOR tag object
149156 var payload []byte
150157 if _ , err := utils .CborDecode (msg .WrappedHeader .RawData , & payload ); err != nil {
151- return fmt .Errorf ("failed fallback decode: %s" , err )
158+ return fmt .Errorf ("%s: decode error : %s" , PROTOCOL_NAME , err )
152159 }
153160 var err error
154161 blockHeader , err = block .NewBlockHeaderFromCbor (blockType , payload )
155162 if err != nil {
156163 return err
157164 }
158165 }
159- c .proto .SetState (STATE_IDLE )
166+ // Unlock and change state when we're done
167+ defer c .proto .UnlockState (STATE_IDLE )
168+ // Call the user callback function
160169 return c .callbackConfig .RollForwardFunc (blockType , blockHeader )
161170 } else {
162171 msg := msgGeneric .(* msgRollForwardNtC )
163172 // Decode only enough to get the block type value
164173 var wrapBlock wrappedBlock
165174 if _ , err := utils .CborDecode (msg .WrappedData , & wrapBlock ); err != nil {
166- return fmt .Errorf ("chain-sync : decode error: %s" , err )
175+ return fmt .Errorf ("%s : decode error: %s" , PROTOCOL_NAME , err )
167176 }
168177 blk , err := block .NewBlockFromCbor (wrapBlock .Type , wrapBlock .RawBlock )
169178 if err != nil {
170179 return err
171180 }
172- c .proto .SetState (STATE_IDLE )
181+ // Unlock and change state when we're done
182+ defer c .proto .UnlockState (STATE_IDLE )
183+ // Call the user callback function
173184 return c .callbackConfig .RollForwardFunc (wrapBlock .Type , blk )
174185 }
175186}
176187
177188func (c * ChainSync ) handleRollBackward (msgGeneric protocol.Message ) error {
178- if c . proto . GetState () != STATE_CAN_AWAIT && c .proto .GetState () != STATE_MUST_REPLY {
189+ if err := c .proto .LockState ([]protocol. State { STATE_CAN_AWAIT , STATE_MUST_REPLY }); err != nil {
179190 return fmt .Errorf ("received chain-sync RollBackward message when protocol not in expected state" )
180191 }
181192 if c .callbackConfig .RollBackwardFunc == nil {
182193 return fmt .Errorf ("received chain-sync RollBackward message but no callback function is defined" )
183194 }
184195 msg := msgGeneric .(* msgRollBackward )
185- c .proto .SetState (STATE_IDLE )
196+ // Unlock and change state when we're done
197+ defer c .proto .UnlockState (STATE_IDLE )
198+ // Call the user callback function
186199 return c .callbackConfig .RollBackwardFunc (msg .Point , msg .Tip )
187200}
188201
189202func (c * ChainSync ) handleIntersectFound (msgGeneric protocol.Message ) error {
190- if c .proto .GetState () != STATE_INTERSECT {
203+ if err := c .proto .LockState ([]protocol. State { STATE_INTERSECT }); err != nil {
191204 return fmt .Errorf ("received chain-sync IntersectFound message when protocol not in expected state" )
192205 }
193206 if c .callbackConfig .IntersectFoundFunc == nil {
194207 return fmt .Errorf ("received chain-sync IntersectFound message but no callback function is defined" )
195208 }
196209 msg := msgGeneric .(* msgIntersectFound )
197- c .proto .SetState (STATE_IDLE )
210+ // Unlock and change state when we're done
211+ defer c .proto .UnlockState (STATE_IDLE )
212+ // Call the user callback function
198213 return c .callbackConfig .IntersectFoundFunc (msg .Point , msg .Tip )
199214}
200215
201216func (c * ChainSync ) handleIntersectNotFound (msgGeneric protocol.Message ) error {
202- if c .proto .GetState () != STATE_INTERSECT {
217+ if err := c .proto .LockState ([]protocol. State { STATE_INTERSECT }); err != nil {
203218 return fmt .Errorf ("received chain-sync IntersectNotFound message when protocol not in expected state" )
204219 }
205220 if c .callbackConfig .IntersectNotFoundFunc == nil {
206221 return fmt .Errorf ("received chain-sync IntersectNotFound message but no callback function is defined" )
207222 }
208223 msg := msgGeneric .(* msgIntersectNotFound )
209- c .proto .SetState (STATE_IDLE )
224+ // Unlock and change state when we're done
225+ defer c .proto .UnlockState (STATE_IDLE )
226+ // Call the user callback function
210227 return c .callbackConfig .IntersectNotFoundFunc (msg .Tip )
211228}
212229
213230func (c * ChainSync ) handleDone () error {
214- if c .proto .GetState () != STATE_IDLE {
231+ if err := c .proto .LockState ([]protocol. State { STATE_IDLE }); err != nil {
215232 return fmt .Errorf ("received chain-sync Done message when protocol not in expected state" )
216233 }
217234 if c .callbackConfig .DoneFunc == nil {
218235 return fmt .Errorf ("received chain-sync Done message but no callback function is defined" )
219236 }
220- c .proto .SetState (STATE_DONE )
237+ // Unlock and change state when we're done
238+ defer c .proto .UnlockState (STATE_DONE )
239+ // Call the user callback function
221240 return c .callbackConfig .DoneFunc ()
222241}
0 commit comments