11package localstatequery
22
33import (
4- "fmt"
54 "github.com/cloudstruct/go-ouroboros-network/protocol"
65)
76
2019
2120var StateMap = protocol.StateMap {
2221 STATE_IDLE : protocol.StateMapEntry {
23- Agency : protocol .AGENCY_SERVER ,
22+ Agency : protocol .AGENCY_CLIENT ,
2423 Transitions : []protocol.StateTransition {
2524 {
2625 MsgType : MESSAGE_TYPE_ACQUIRE ,
@@ -37,7 +36,7 @@ var StateMap = protocol.StateMap{
3736 },
3837 },
3938 STATE_ACQUIRING : protocol.StateMapEntry {
40- Agency : protocol .AGENCY_CLIENT ,
39+ Agency : protocol .AGENCY_SERVER ,
4140 Transitions : []protocol.StateTransition {
4241 {
4342 MsgType : MESSAGE_TYPE_FAILURE ,
@@ -50,7 +49,7 @@ var StateMap = protocol.StateMap{
5049 },
5150 },
5251 STATE_ACQUIRED : protocol.StateMapEntry {
53- Agency : protocol .AGENCY_SERVER ,
52+ Agency : protocol .AGENCY_CLIENT ,
5453 Transitions : []protocol.StateTransition {
5554 {
5655 MsgType : MESSAGE_TYPE_QUERY ,
@@ -71,7 +70,7 @@ var StateMap = protocol.StateMap{
7170 },
7271 },
7372 STATE_QUERYING : protocol.StateMapEntry {
74- Agency : protocol .AGENCY_CLIENT ,
73+ Agency : protocol .AGENCY_SERVER ,
7574 Transitions : []protocol.StateTransition {
7675 {
7776 MsgType : MESSAGE_TYPE_RESULT ,
@@ -85,11 +84,8 @@ var StateMap = protocol.StateMap{
8584}
8685
8786type LocalStateQuery struct {
88- * protocol.Protocol
89- config * Config
90- enableGetChainBlockNo bool
91- enableGetChainPoint bool
92- enableGetRewardInfoPoolsBlock bool
87+ Client * Client
88+ Server * Server
9389}
9490
9591type Config struct {
@@ -114,144 +110,10 @@ type ReleaseFunc func() error
114110type ReAcquireFunc func (interface {}) error
115111type DoneFunc func () error
116112
117- func New (options protocol.ProtocolOptions , cfg * Config ) * LocalStateQuery {
113+ func New (protoOptions protocol.ProtocolOptions , cfg * Config ) * LocalStateQuery {
118114 l := & LocalStateQuery {
119- config : cfg ,
120- }
121- protoConfig := protocol.ProtocolConfig {
122- Name : PROTOCOL_NAME ,
123- ProtocolId : PROTOCOL_ID ,
124- Muxer : options .Muxer ,
125- ErrorChan : options .ErrorChan ,
126- Mode : options .Mode ,
127- Role : options .Role ,
128- MessageHandlerFunc : l .messageHandler ,
129- MessageFromCborFunc : NewMsgFromCbor ,
130- StateMap : StateMap ,
131- InitialState : STATE_IDLE ,
132- }
133- // Enable version-dependent features
134- if options .Version >= 10 {
135- l .enableGetChainBlockNo = true
136- l .enableGetChainPoint = true
137- }
138- if options .Version >= 11 {
139- l .enableGetRewardInfoPoolsBlock = true
115+ Client : NewClient (protoOptions , cfg ),
116+ Server : NewServer (protoOptions , cfg ),
140117 }
141- l .Protocol = protocol .New (protoConfig )
142118 return l
143119}
144-
145- func (l * LocalStateQuery ) Start () {
146- l .Protocol .Start ()
147- }
148-
149- func (l * LocalStateQuery ) messageHandler (msg protocol.Message , isResponse bool ) error {
150- var err error
151- switch msg .Type () {
152- case MESSAGE_TYPE_ACQUIRE :
153- err = l .handleAcquire (msg )
154- case MESSAGE_TYPE_ACQUIRED :
155- err = l .handleAcquired ()
156- case MESSAGE_TYPE_FAILURE :
157- err = l .handleFailure (msg )
158- case MESSAGE_TYPE_QUERY :
159- err = l .handleQuery (msg )
160- case MESSAGE_TYPE_RESULT :
161- err = l .handleResult (msg )
162- case MESSAGE_TYPE_RELEASE :
163- err = l .handleRelease ()
164- case MESSAGE_TYPE_REACQUIRE :
165- err = l .handleReAcquire (msg )
166- case MESSAGE_TYPE_ACQUIRE_NO_POINT :
167- err = l .handleAcquire (msg )
168- case MESSAGE_TYPE_REACQUIRE_NO_POINT :
169- err = l .handleReAcquire (msg )
170- case MESSAGE_TYPE_DONE :
171- err = l .handleDone ()
172- default :
173- err = fmt .Errorf ("%s: received unexpected message type %d" , PROTOCOL_NAME , msg .Type ())
174- }
175- return err
176- }
177-
178- func (l * LocalStateQuery ) handleAcquire (msg protocol.Message ) error {
179- if l .config .AcquireFunc == nil {
180- return fmt .Errorf ("received local-state-query Acquire message but no callback function is defined" )
181- }
182- switch msgAcquire := msg .(type ) {
183- case * MsgAcquire :
184- // Call the user callback function
185- return l .config .AcquireFunc (msgAcquire .Point )
186- case * MsgAcquireNoPoint :
187- // Call the user callback function
188- return l .config .AcquireFunc (nil )
189- }
190- return nil
191- }
192-
193- func (l * LocalStateQuery ) handleAcquired () error {
194- if l .config .AcquiredFunc == nil {
195- return fmt .Errorf ("received local-state-query Acquired message but no callback function is defined" )
196- }
197- // Call the user callback function
198- return l .config .AcquiredFunc ()
199- }
200-
201- func (l * LocalStateQuery ) handleFailure (msg protocol.Message ) error {
202- if l .config .FailureFunc == nil {
203- return fmt .Errorf ("received local-state-query Failure message but no callback function is defined" )
204- }
205- msgFailure := msg .(* MsgFailure )
206- // Call the user callback function
207- return l .config .FailureFunc (msgFailure .Failure )
208- }
209-
210- func (l * LocalStateQuery ) handleQuery (msg protocol.Message ) error {
211- if l .config .QueryFunc == nil {
212- return fmt .Errorf ("received local-state-query Query message but no callback function is defined" )
213- }
214- msgQuery := msg .(* MsgQuery )
215- // Call the user callback function
216- return l .config .QueryFunc (msgQuery .Query )
217- }
218-
219- func (l * LocalStateQuery ) handleResult (msg protocol.Message ) error {
220- if l .config .ResultFunc == nil {
221- return fmt .Errorf ("received local-state-query Result message but no callback function is defined" )
222- }
223- msgResult := msg .(* MsgResult )
224- // Call the user callback function
225- return l .config .ResultFunc (msgResult .Result )
226- }
227-
228- func (l * LocalStateQuery ) handleRelease () error {
229- if l .config .ReleaseFunc == nil {
230- return fmt .Errorf ("received local-state-query Release message but no callback function is defined" )
231- }
232- // Call the user callback function
233- return l .config .ReleaseFunc ()
234- }
235-
236- func (l * LocalStateQuery ) handleReAcquire (msg protocol.Message ) error {
237- if l .config .ReAcquireFunc == nil {
238- return fmt .Errorf ("received local-state-query ReAcquire message but no callback function is defined" )
239- }
240- switch msgReAcquire := msg .(type ) {
241- case * MsgReAcquire :
242- // Call the user callback function
243- return l .config .ReAcquireFunc (msgReAcquire .Point )
244- case * MsgReAcquireNoPoint :
245- // Call the user callback function
246- return l .config .ReAcquireFunc (nil )
247- }
248- return nil
249- }
250-
251- func (l * LocalStateQuery ) handleDone () error {
252- if l .config .DoneFunc == nil {
253- return fmt .Errorf ("received local-state-query Done message but no callback function is defined" )
254- }
255- // Call the user callback function
256- return l .config .DoneFunc ()
257- }
0 commit comments