Skip to content

Commit daa3dee

Browse files
authored
Merge pull request #126 from cloudstruct/feature/localtxsubmission-client-server-split
feat: split localtxsubmission protocol into client and server
2 parents bcab703 + 967d3e5 commit daa3dee

File tree

4 files changed

+139
-85
lines changed

4 files changed

+139
-85
lines changed

cmd/go-ouroboros-network/localtxsubmission.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func testLocalTxSubmission(f *globalFlags) {
6969
fmt.Printf("ERROR: %s\n", err)
7070
os.Exit(1)
7171
}
72-
o.LocalTxSubmission.Start()
72+
o.LocalTxSubmission.Client.Start()
7373

7474
txData, err := ioutil.ReadFile(localTxSubmissionFlags.txFile)
7575
if err != nil {
@@ -90,7 +90,7 @@ func testLocalTxSubmission(f *globalFlags) {
9090
os.Exit(1)
9191
}
9292

93-
if err = o.LocalTxSubmission.SubmitTx(ledger.TX_TYPE_ALONZO, txBytes); err != nil {
93+
if err = o.LocalTxSubmission.Client.SubmitTx(ledger.TX_TYPE_ALONZO, txBytes); err != nil {
9494
fmt.Printf("Error submitting transaction: %s\n", err)
9595
os.Exit(1)
9696
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package localtxsubmission
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_IDLE,
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_ACCEPT_TX:
37+
err = c.handleAcceptTx()
38+
case MESSAGE_TYPE_REJECT_TX:
39+
err = c.handleRejectTx(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) SubmitTx(eraId uint16, tx []byte) error {
47+
msg := NewMsgSubmitTx(eraId, tx)
48+
return c.SendMessage(msg)
49+
}
50+
51+
func (c *Client) Done(tx interface{}) error {
52+
msg := NewMsgDone()
53+
return c.SendMessage(msg)
54+
}
55+
56+
func (c *Client) handleAcceptTx() error {
57+
if c.config.AcceptTxFunc == nil {
58+
return fmt.Errorf("received local-tx-submission AcceptTx message but no callback function is defined")
59+
}
60+
// Call the user callback function
61+
return c.config.AcceptTxFunc()
62+
}
63+
64+
func (c *Client) handleRejectTx(msgGeneric protocol.Message) error {
65+
if c.config.RejectTxFunc == nil {
66+
return fmt.Errorf("received local-tx-submission RejectTx message but no callback function is defined")
67+
}
68+
msg := msgGeneric.(*MsgRejectTx)
69+
// Call the user callback function
70+
return c.config.RejectTxFunc([]byte(msg.Reason))
71+
}
Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package localtxsubmission
22

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

@@ -45,8 +44,8 @@ var StateMap = protocol.StateMap{
4544
}
4645

4746
type LocalTxSubmission struct {
48-
*protocol.Protocol
49-
config *Config
47+
Client *Client
48+
Server *Server
5049
}
5150

5251
type Config struct {
@@ -62,87 +61,10 @@ type AcceptTxFunc func() error
6261
type RejectTxFunc func([]byte) error
6362
type DoneFunc func() error
6463

65-
func New(options protocol.ProtocolOptions, cfg *Config) *LocalTxSubmission {
64+
func New(protoOptions protocol.ProtocolOptions, cfg *Config) *LocalTxSubmission {
6665
l := &LocalTxSubmission{
67-
config: cfg,
66+
Client: NewClient(protoOptions, cfg),
67+
Server: NewServer(protoOptions, cfg),
6868
}
69-
protoConfig := protocol.ProtocolConfig{
70-
Name: PROTOCOL_NAME,
71-
ProtocolId: PROTOCOL_ID,
72-
Muxer: options.Muxer,
73-
ErrorChan: options.ErrorChan,
74-
Mode: options.Mode,
75-
Role: options.Role,
76-
MessageHandlerFunc: l.messageHandler,
77-
MessageFromCborFunc: NewMsgFromCbor,
78-
StateMap: StateMap,
79-
InitialState: STATE_IDLE,
80-
}
81-
l.Protocol = protocol.New(protoConfig)
8269
return l
8370
}
84-
85-
func (l *LocalTxSubmission) Start() {
86-
l.Protocol.Start()
87-
}
88-
89-
func (l *LocalTxSubmission) messageHandler(msg protocol.Message, isResponse bool) error {
90-
var err error
91-
switch msg.Type() {
92-
case MESSAGE_TYPE_SUBMIT_TX:
93-
err = l.handleSubmitTx(msg)
94-
case MESSAGE_TYPE_ACCEPT_TX:
95-
err = l.handleAcceptTx()
96-
case MESSAGE_TYPE_REJECT_TX:
97-
err = l.handleRejectTx(msg)
98-
case MESSAGE_TYPE_DONE:
99-
err = l.handleDone()
100-
default:
101-
err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type())
102-
}
103-
return err
104-
}
105-
106-
func (l *LocalTxSubmission) SubmitTx(eraId uint16, tx []byte) error {
107-
msg := NewMsgSubmitTx(eraId, tx)
108-
return l.SendMessage(msg)
109-
}
110-
111-
func (l *LocalTxSubmission) Done(tx interface{}) error {
112-
msg := NewMsgDone()
113-
return l.SendMessage(msg)
114-
}
115-
116-
func (l *LocalTxSubmission) handleSubmitTx(msgGeneric protocol.Message) error {
117-
if l.config.SubmitTxFunc == nil {
118-
return fmt.Errorf("received local-tx-submission SubmitTx message but no callback function is defined")
119-
}
120-
msg := msgGeneric.(*MsgSubmitTx)
121-
// Call the user callback function
122-
return l.config.SubmitTxFunc(msg.Transaction)
123-
}
124-
125-
func (l *LocalTxSubmission) handleAcceptTx() error {
126-
if l.config.AcceptTxFunc == nil {
127-
return fmt.Errorf("received local-tx-submission AcceptTx message but no callback function is defined")
128-
}
129-
// Call the user callback function
130-
return l.config.AcceptTxFunc()
131-
}
132-
133-
func (l *LocalTxSubmission) handleRejectTx(msgGeneric protocol.Message) error {
134-
if l.config.RejectTxFunc == nil {
135-
return fmt.Errorf("received local-tx-submission RejectTx message but no callback function is defined")
136-
}
137-
msg := msgGeneric.(*MsgRejectTx)
138-
// Call the user callback function
139-
return l.config.RejectTxFunc([]byte(msg.Reason))
140-
}
141-
142-
func (l *LocalTxSubmission) handleDone() error {
143-
if l.config.DoneFunc == nil {
144-
return fmt.Errorf("received local-tx-submission Done message but no callback function is defined")
145-
}
146-
// Call the user callback function
147-
return l.config.DoneFunc()
148-
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package localtxsubmission
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_IDLE,
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_SUBMIT_TX:
37+
err = s.handleSubmitTx(msg)
38+
case MESSAGE_TYPE_DONE:
39+
err = s.handleDone()
40+
default:
41+
err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type())
42+
}
43+
return err
44+
}
45+
46+
func (s *Server) handleSubmitTx(msgGeneric protocol.Message) error {
47+
if s.config.SubmitTxFunc == nil {
48+
return fmt.Errorf("received local-tx-submission SubmitTx message but no callback function is defined")
49+
}
50+
msg := msgGeneric.(*MsgSubmitTx)
51+
// Call the user callback function
52+
return s.config.SubmitTxFunc(msg.Transaction)
53+
}
54+
55+
func (s *Server) handleDone() error {
56+
if s.config.DoneFunc == nil {
57+
return fmt.Errorf("received local-tx-submission Done message but no callback function is defined")
58+
}
59+
// Call the user callback function
60+
return s.config.DoneFunc()
61+
}

0 commit comments

Comments
 (0)