@@ -3,16 +3,20 @@ package localtxsubmission
33import (
44 "fmt"
55 "github.com/cloudstruct/go-ouroboros-network/protocol"
6+ "sync"
67)
78
89type Client struct {
910 * protocol.Protocol
10- config * Config
11+ config * Config
12+ busyMutex sync.Mutex
13+ submitResultChan chan error
1114}
1215
1316func 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
4650func (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
5671func (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}
0 commit comments