Skip to content

Commit f7afb60

Browse files
authored
Merge pull request #127 from cloudstruct/feature/txsubmission-client-server-split
feat: split txsubmission protocol into client and server
2 parents daa3dee + e535cd2 commit f7afb60

File tree

5 files changed

+167
-109
lines changed

5 files changed

+167
-109
lines changed

protocol/txsubmission/client.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package txsubmission
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+
}
12+
13+
func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
14+
c := &Client{
15+
config: cfg,
16+
}
17+
protoConfig := protocol.ProtocolConfig{
18+
Name: PROTOCOL_NAME,
19+
ProtocolId: PROTOCOL_ID,
20+
Muxer: protoOptions.Muxer,
21+
ErrorChan: protoOptions.ErrorChan,
22+
Mode: protoOptions.Mode,
23+
Role: protocol.ProtocolRoleClient,
24+
MessageHandlerFunc: c.messageHandler,
25+
MessageFromCborFunc: NewMsgFromCbor,
26+
StateMap: StateMap,
27+
InitialState: STATE_INIT,
28+
}
29+
c.Protocol = protocol.New(protoConfig)
30+
return c
31+
}
32+
33+
func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error {
34+
var err error
35+
switch msg.Type() {
36+
case MESSAGE_TYPE_REQUEST_TX_IDS:
37+
err = c.handleRequestTxIds(msg)
38+
case MESSAGE_TYPE_REQUEST_TXS:
39+
err = c.handleRequestTxs(msg)
40+
default:
41+
err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type())
42+
}
43+
return err
44+
}
45+
46+
func (c *Client) handleRequestTxIds(msg protocol.Message) error {
47+
if c.config.RequestTxIdsFunc == nil {
48+
return fmt.Errorf("received tx-submission RequestTxIds message but no callback function is defined")
49+
}
50+
msgRequestTxIds := msg.(*MsgRequestTxIds)
51+
// Call the user callback function
52+
return c.config.RequestTxIdsFunc(msgRequestTxIds.Blocking, msgRequestTxIds.Ack, msgRequestTxIds.Req)
53+
}
54+
55+
func (c *Client) handleRequestTxs(msg protocol.Message) error {
56+
if c.config.RequestTxsFunc == nil {
57+
return fmt.Errorf("received tx-submission RequestTxs message but no callback function is defined")
58+
}
59+
msgRequestTxs := msg.(*MsgRequestTxs)
60+
// Call the user callback function
61+
return c.config.RequestTxsFunc(msgRequestTxs.TxIds)
62+
}

protocol/txsubmission/messages.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const (
1212
MESSAGE_TYPE_REQUEST_TXS = 2
1313
MESSAGE_TYPE_REPLY_TXS = 3
1414
MESSAGE_TYPE_DONE = 4
15-
MESSAGE_TYPE_HELLO = 6
15+
MESSAGE_TYPE_INIT = 6
1616
)
1717

1818
func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) {
@@ -28,8 +28,8 @@ func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) {
2828
ret = &MsgReplyTxs{}
2929
case MESSAGE_TYPE_DONE:
3030
ret = &MsgDone{}
31-
case MESSAGE_TYPE_HELLO:
32-
ret = &MsgHello{}
31+
case MESSAGE_TYPE_INIT:
32+
ret = &MsgInit{}
3333
}
3434
if _, err := utils.CborDecode(data, ret); err != nil {
3535
return nil, fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err)
@@ -118,14 +118,14 @@ func NewMsgDone() *MsgDone {
118118
return m
119119
}
120120

121-
type MsgHello struct {
121+
type MsgInit struct {
122122
protocol.MessageBase
123123
}
124124

125-
func NewMsgHello() *MsgHello {
126-
m := &MsgHello{
125+
func NewMsgInit() *MsgInit {
126+
m := &MsgInit{
127127
MessageBase: protocol.MessageBase{
128-
MessageType: MESSAGE_TYPE_HELLO,
128+
MessageType: MESSAGE_TYPE_INIT,
129129
},
130130
}
131131
return m

protocol/txsubmission/messages_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ var tests = []testDefinition{
2323
},
2424
{
2525
CborHex: "8106",
26-
Message: NewMsgHello(),
27-
MessageType: MESSAGE_TYPE_HELLO,
26+
Message: NewMsgInit(),
27+
MessageType: MESSAGE_TYPE_INIT,
2828
},
2929
}
3030

protocol/txsubmission/server.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package txsubmission
2+
3+
import (
4+
"fmt"
5+
"github.com/cloudstruct/go-ouroboros-network/protocol"
6+
)
7+
8+
type Server struct {
9+
*protocol.Protocol
10+
config *Config
11+
}
12+
13+
func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server {
14+
s := &Server{
15+
config: cfg,
16+
}
17+
protoConfig := protocol.ProtocolConfig{
18+
Name: PROTOCOL_NAME,
19+
ProtocolId: PROTOCOL_ID,
20+
Muxer: protoOptions.Muxer,
21+
ErrorChan: protoOptions.ErrorChan,
22+
Mode: protoOptions.Mode,
23+
Role: protocol.ProtocolRoleServer,
24+
MessageHandlerFunc: s.messageHandler,
25+
MessageFromCborFunc: NewMsgFromCbor,
26+
StateMap: StateMap,
27+
InitialState: STATE_INIT,
28+
}
29+
s.Protocol = protocol.New(protoConfig)
30+
return s
31+
}
32+
33+
func (s *Server) messageHandler(msg protocol.Message, isResponse bool) error {
34+
var err error
35+
switch msg.Type() {
36+
case MESSAGE_TYPE_REPLY_TX_IDS:
37+
err = s.handleReplyTxIds(msg)
38+
case MESSAGE_TYPE_REPLY_TXS:
39+
err = s.handleReplyTxs(msg)
40+
case MESSAGE_TYPE_DONE:
41+
err = s.handleDone()
42+
case MESSAGE_TYPE_INIT:
43+
err = s.handleInit()
44+
default:
45+
err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type())
46+
}
47+
return err
48+
}
49+
50+
func (s *Server) handleReplyTxIds(msg protocol.Message) error {
51+
if s.config.ReplyTxIdsFunc == nil {
52+
return fmt.Errorf("received tx-submission ReplyTxIds message but no callback function is defined")
53+
}
54+
msgReplyTxIds := msg.(*MsgReplyTxIds)
55+
// Call the user callback function
56+
return s.config.ReplyTxIdsFunc(msgReplyTxIds.TxIds)
57+
}
58+
59+
func (s *Server) handleReplyTxs(msg protocol.Message) error {
60+
if s.config.ReplyTxsFunc == nil {
61+
return fmt.Errorf("received tx-submission ReplyTxs message but no callback function is defined")
62+
}
63+
msgReplyTxs := msg.(*MsgReplyTxs)
64+
// Call the user callback function
65+
return s.config.ReplyTxsFunc(msgReplyTxs.Txs)
66+
}
67+
68+
func (s *Server) handleDone() error {
69+
if s.config.DoneFunc == nil {
70+
return fmt.Errorf("received tx-submission Done message but no callback function is defined")
71+
}
72+
// Call the user callback function
73+
return s.config.DoneFunc()
74+
}
75+
76+
func (s *Server) handleInit() error {
77+
if s.config.InitFunc == nil {
78+
return fmt.Errorf("received tx-submission Init message but no callback function is defined")
79+
}
80+
// Call the user callback function
81+
return s.config.InitFunc()
82+
}
Lines changed: 14 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package txsubmission
22

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

@@ -11,7 +10,7 @@ const (
1110
)
1211

1312
var (
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

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

9194
type TxSubmission struct {
92-
*protocol.Protocol
93-
config *Config
95+
Client *Client
96+
Server *Server
9497
}
9598

9699
type 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
108111
type RequestTxsFunc func(interface{}) error
109112
type ReplyTxsFunc func(interface{}) error
110113
type 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

Comments
 (0)