Skip to content

Commit dfb7ac6

Browse files
authored
Merge pull request #131 from cloudstruct/feat/localtxsubmission-friendly-interface
feat: make the localtxsubmission interface friendlier
2 parents d2fb661 + da1425a commit dfb7ac6

File tree

6 files changed

+46
-56
lines changed

6 files changed

+46
-56
lines changed

cmd/go-ouroboros-network/localtxsubmission.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ import (
1212
"os"
1313
)
1414

15-
type localTxSubmissionState struct {
16-
submitResponse chan bool
17-
}
18-
19-
var localTxSubmitState localTxSubmissionState
20-
2115
type localTxSubmissionFlags struct {
2216
flagset *flag.FlagSet
2317
txFile string
@@ -27,15 +21,12 @@ func newLocalTxSubmissionFlags() *localTxSubmissionFlags {
2721
f := &localTxSubmissionFlags{
2822
flagset: flag.NewFlagSet("local-tx-submission", flag.ExitOnError),
2923
}
30-
f.flagset.StringVar(&f.txFile, "tx-file", "", "path to the transaction file to submit")
24+
f.flagset.StringVar(&f.txFile, "tx-file", "", "path to the JSON transaction file to submit")
3125
return f
3226
}
3327

3428
func buildLocalTxSubmissionConfig() localtxsubmission.Config {
35-
return localtxsubmission.Config{
36-
AcceptTxFunc: localTxSubmissionAcceptTxHandler,
37-
RejectTxFunc: localTxSubmissionRejectTxHandler,
38-
}
29+
return localtxsubmission.Config{}
3930
}
4031

4132
func testLocalTxSubmission(f *globalFlags) {
@@ -46,8 +37,6 @@ func testLocalTxSubmission(f *globalFlags) {
4637
os.Exit(1)
4738
}
4839

49-
localTxSubmitState.submitResponse = make(chan bool)
50-
5140
conn := createClientConnection(f)
5241
errorChan := make(chan error)
5342
go func() {
@@ -94,19 +83,5 @@ func testLocalTxSubmission(f *globalFlags) {
9483
fmt.Printf("Error submitting transaction: %s\n", err)
9584
os.Exit(1)
9685
}
97-
98-
// Wait for response
99-
<-localTxSubmitState.submitResponse
100-
}
101-
102-
func localTxSubmissionAcceptTxHandler() error {
10386
fmt.Print("The transaction was accepted\n")
104-
localTxSubmitState.submitResponse <- true
105-
return nil
106-
}
107-
108-
func localTxSubmissionRejectTxHandler(reasonCbor []byte) error {
109-
fmt.Printf("The transaction was rejected (reason in hex-encoded CBOR): %#v\n", reasonCbor)
110-
os.Exit(1)
111-
return nil
11287
}

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ volumes:
55

66
services:
77
cardano-node:
8-
image: inputoutput/cardano-node:1.33.0
8+
image: ghcr.io/cloudstruct/cardano-node:1.35.4
99
environment:
10-
NETWORK: ${CARDANO_NETWORK:-testnet}
10+
NETWORK: ${CARDANO_NETWORK:-preview}
1111
ports:
1212
- 8081:3001
1313
volumes:

protocol/localtxsubmission/client.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ package localtxsubmission
33
import (
44
"fmt"
55
"github.com/cloudstruct/go-ouroboros-network/protocol"
6+
"sync"
67
)
78

89
type Client struct {
910
*protocol.Protocol
10-
config *Config
11+
config *Config
12+
busyMutex sync.Mutex
13+
submitResultChan chan error
1114
}
1215

1316
func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
1417
c := &Client{
15-
config: cfg,
18+
config: cfg,
19+
submitResultChan: make(chan error),
1620
}
1721
protoConfig := protocol.ProtocolConfig{
1822
Name: PROTOCOL_NAME,
@@ -44,28 +48,36 @@ func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error {
4448
}
4549

4650
func (c *Client) SubmitTx(eraId uint16, tx []byte) error {
51+
c.busyMutex.Lock()
52+
defer c.busyMutex.Unlock()
4753
msg := NewMsgSubmitTx(eraId, tx)
48-
return c.SendMessage(msg)
54+
if err := c.SendMessage(msg); err != nil {
55+
return err
56+
}
57+
err := <-c.submitResultChan
58+
return err
4959
}
5060

51-
func (c *Client) Done(tx interface{}) error {
61+
func (c *Client) Stop() error {
62+
c.busyMutex.Lock()
63+
defer c.busyMutex.Unlock()
5264
msg := NewMsgDone()
53-
return c.SendMessage(msg)
65+
if err := c.SendMessage(msg); err != nil {
66+
return err
67+
}
68+
return nil
5469
}
5570

5671
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()
72+
c.submitResultChan <- nil
73+
return nil
6274
}
6375

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")
76+
func (c *Client) handleRejectTx(msg protocol.Message) error {
77+
msgRejectTx := msg.(*MsgRejectTx)
78+
err := TransactionRejectedError{
79+
ReasonCbor: []byte(msgRejectTx.Reason),
6780
}
68-
msg := msgGeneric.(*MsgRejectTx)
69-
// Call the user callback function
70-
return c.config.RejectTxFunc([]byte(msg.Reason))
81+
c.submitResultChan <- err
82+
return nil
7183
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package localtxsubmission
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type TransactionRejectedError struct {
8+
ReasonCbor []byte
9+
}
10+
11+
func (e TransactionRejectedError) Error() string {
12+
return fmt.Sprintf("transaction rejected: CBOR reason hex: %x", e.ReasonCbor)
13+
}

protocol/localtxsubmission/localtxsubmission.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,10 @@ type LocalTxSubmission struct {
5050

5151
type Config struct {
5252
SubmitTxFunc SubmitTxFunc
53-
AcceptTxFunc AcceptTxFunc
54-
RejectTxFunc RejectTxFunc
55-
DoneFunc DoneFunc
5653
}
5754

5855
// Callback function types
5956
type SubmitTxFunc func(interface{}) error
60-
type AcceptTxFunc func() error
61-
type RejectTxFunc func([]byte) error
62-
type DoneFunc func() error
6357

6458
func New(protoOptions protocol.ProtocolOptions, cfg *Config) *LocalTxSubmission {
6559
l := &LocalTxSubmission{

protocol/localtxsubmission/server.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,5 @@ func (s *Server) handleSubmitTx(msgGeneric protocol.Message) error {
5353
}
5454

5555
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()
56+
return nil
6157
}

0 commit comments

Comments
 (0)