Skip to content

Commit 195dff6

Browse files
authored
Merge pull request #125 from cloudstruct/feature/localstatequery-client-server-split
feat: split localstatequery protocol into client and server
2 parents 1da19f7 + 7c5d97e commit 195dff6

File tree

3 files changed

+212
-147
lines changed

3 files changed

+212
-147
lines changed

protocol/localstatequery/client.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package localstatequery
2+
3+
import (
4+
"fmt"
5+
"github.com/cloudstruct/go-ouroboros-network/protocol"
6+
)
7+
8+
type Client struct {
9+
*protocol.Protocol
10+
config *Config
11+
enableGetChainBlockNo bool
12+
enableGetChainPoint bool
13+
enableGetRewardInfoPoolsBlock bool
14+
}
15+
16+
func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
17+
c := &Client{
18+
config: cfg,
19+
}
20+
protoConfig := protocol.ProtocolConfig{
21+
Name: PROTOCOL_NAME,
22+
ProtocolId: PROTOCOL_ID,
23+
Muxer: protoOptions.Muxer,
24+
ErrorChan: protoOptions.ErrorChan,
25+
Mode: protoOptions.Mode,
26+
Role: protocol.ProtocolRoleClient,
27+
MessageHandlerFunc: c.messageHandler,
28+
MessageFromCborFunc: NewMsgFromCbor,
29+
StateMap: StateMap,
30+
InitialState: STATE_IDLE,
31+
}
32+
// Enable version-dependent features
33+
if protoOptions.Version >= 10 {
34+
c.enableGetChainBlockNo = true
35+
c.enableGetChainPoint = true
36+
}
37+
if protoOptions.Version >= 11 {
38+
c.enableGetRewardInfoPoolsBlock = true
39+
}
40+
c.Protocol = protocol.New(protoConfig)
41+
return c
42+
}
43+
44+
func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error {
45+
var err error
46+
switch msg.Type() {
47+
case MESSAGE_TYPE_ACQUIRED:
48+
err = c.handleAcquired()
49+
case MESSAGE_TYPE_FAILURE:
50+
err = c.handleFailure(msg)
51+
case MESSAGE_TYPE_RESULT:
52+
err = c.handleResult(msg)
53+
default:
54+
err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type())
55+
}
56+
return err
57+
}
58+
59+
func (c *Client) handleAcquired() error {
60+
if c.config.AcquiredFunc == nil {
61+
return fmt.Errorf("received local-state-query Acquired message but no callback function is defined")
62+
}
63+
// Call the user callback function
64+
return c.config.AcquiredFunc()
65+
}
66+
67+
func (c *Client) handleFailure(msg protocol.Message) error {
68+
if c.config.FailureFunc == nil {
69+
return fmt.Errorf("received local-state-query Failure message but no callback function is defined")
70+
}
71+
msgFailure := msg.(*MsgFailure)
72+
// Call the user callback function
73+
return c.config.FailureFunc(msgFailure.Failure)
74+
}
75+
76+
func (c *Client) handleResult(msg protocol.Message) error {
77+
if c.config.ResultFunc == nil {
78+
return fmt.Errorf("received local-state-query Result message but no callback function is defined")
79+
}
80+
msgResult := msg.(*MsgResult)
81+
// Call the user callback function
82+
return c.config.ResultFunc(msgResult.Result)
83+
}
Lines changed: 9 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package localstatequery
22

33
import (
4-
"fmt"
54
"github.com/cloudstruct/go-ouroboros-network/protocol"
65
)
76

@@ -20,7 +19,7 @@ var (
2019

2120
var 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

8786
type 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

9591
type Config struct {
@@ -114,144 +110,10 @@ type ReleaseFunc func() error
114110
type ReAcquireFunc func(interface{}) error
115111
type 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

Comments
 (0)