Skip to content

Commit

Permalink
Merge pull request #131 from cloudstruct/feat/localtxsubmission-frien…
Browse files Browse the repository at this point in the history
…dly-interface

feat: make the localtxsubmission interface friendlier
  • Loading branch information
agaffney authored Nov 16, 2022
2 parents d2fb661 + da1425a commit dfb7ac6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 56 deletions.
29 changes: 2 additions & 27 deletions cmd/go-ouroboros-network/localtxsubmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (
"os"
)

type localTxSubmissionState struct {
submitResponse chan bool
}

var localTxSubmitState localTxSubmissionState

type localTxSubmissionFlags struct {
flagset *flag.FlagSet
txFile string
Expand All @@ -27,15 +21,12 @@ func newLocalTxSubmissionFlags() *localTxSubmissionFlags {
f := &localTxSubmissionFlags{
flagset: flag.NewFlagSet("local-tx-submission", flag.ExitOnError),
}
f.flagset.StringVar(&f.txFile, "tx-file", "", "path to the transaction file to submit")
f.flagset.StringVar(&f.txFile, "tx-file", "", "path to the JSON transaction file to submit")
return f
}

func buildLocalTxSubmissionConfig() localtxsubmission.Config {
return localtxsubmission.Config{
AcceptTxFunc: localTxSubmissionAcceptTxHandler,
RejectTxFunc: localTxSubmissionRejectTxHandler,
}
return localtxsubmission.Config{}
}

func testLocalTxSubmission(f *globalFlags) {
Expand All @@ -46,8 +37,6 @@ func testLocalTxSubmission(f *globalFlags) {
os.Exit(1)
}

localTxSubmitState.submitResponse = make(chan bool)

conn := createClientConnection(f)
errorChan := make(chan error)
go func() {
Expand Down Expand Up @@ -94,19 +83,5 @@ func testLocalTxSubmission(f *globalFlags) {
fmt.Printf("Error submitting transaction: %s\n", err)
os.Exit(1)
}

// Wait for response
<-localTxSubmitState.submitResponse
}

func localTxSubmissionAcceptTxHandler() error {
fmt.Print("The transaction was accepted\n")
localTxSubmitState.submitResponse <- true
return nil
}

func localTxSubmissionRejectTxHandler(reasonCbor []byte) error {
fmt.Printf("The transaction was rejected (reason in hex-encoded CBOR): %#v\n", reasonCbor)
os.Exit(1)
return nil
}
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ volumes:

services:
cardano-node:
image: inputoutput/cardano-node:1.33.0
image: ghcr.io/cloudstruct/cardano-node:1.35.4
environment:
NETWORK: ${CARDANO_NETWORK:-testnet}
NETWORK: ${CARDANO_NETWORK:-preview}
ports:
- 8081:3001
volumes:
Expand Down
44 changes: 28 additions & 16 deletions protocol/localtxsubmission/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package localtxsubmission
import (
"fmt"
"github.com/cloudstruct/go-ouroboros-network/protocol"
"sync"
)

type Client struct {
*protocol.Protocol
config *Config
config *Config
busyMutex sync.Mutex
submitResultChan chan error
}

func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
c := &Client{
config: cfg,
config: cfg,
submitResultChan: make(chan error),
}
protoConfig := protocol.ProtocolConfig{
Name: PROTOCOL_NAME,
Expand Down Expand Up @@ -44,28 +48,36 @@ func (c *Client) messageHandler(msg protocol.Message, isResponse bool) error {
}

func (c *Client) SubmitTx(eraId uint16, tx []byte) error {
c.busyMutex.Lock()
defer c.busyMutex.Unlock()
msg := NewMsgSubmitTx(eraId, tx)
return c.SendMessage(msg)
if err := c.SendMessage(msg); err != nil {
return err
}
err := <-c.submitResultChan
return err
}

func (c *Client) Done(tx interface{}) error {
func (c *Client) Stop() error {
c.busyMutex.Lock()
defer c.busyMutex.Unlock()
msg := NewMsgDone()
return c.SendMessage(msg)
if err := c.SendMessage(msg); err != nil {
return err
}
return nil
}

func (c *Client) handleAcceptTx() error {
if c.config.AcceptTxFunc == nil {
return fmt.Errorf("received local-tx-submission AcceptTx message but no callback function is defined")
}
// Call the user callback function
return c.config.AcceptTxFunc()
c.submitResultChan <- nil
return nil
}

func (c *Client) handleRejectTx(msgGeneric protocol.Message) error {
if c.config.RejectTxFunc == nil {
return fmt.Errorf("received local-tx-submission RejectTx message but no callback function is defined")
func (c *Client) handleRejectTx(msg protocol.Message) error {
msgRejectTx := msg.(*MsgRejectTx)
err := TransactionRejectedError{
ReasonCbor: []byte(msgRejectTx.Reason),
}
msg := msgGeneric.(*MsgRejectTx)
// Call the user callback function
return c.config.RejectTxFunc([]byte(msg.Reason))
c.submitResultChan <- err
return nil
}
13 changes: 13 additions & 0 deletions protocol/localtxsubmission/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package localtxsubmission

import (
"fmt"
)

type TransactionRejectedError struct {
ReasonCbor []byte
}

func (e TransactionRejectedError) Error() string {
return fmt.Sprintf("transaction rejected: CBOR reason hex: %x", e.ReasonCbor)
}
6 changes: 0 additions & 6 deletions protocol/localtxsubmission/localtxsubmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,10 @@ type LocalTxSubmission struct {

type Config struct {
SubmitTxFunc SubmitTxFunc
AcceptTxFunc AcceptTxFunc
RejectTxFunc RejectTxFunc
DoneFunc DoneFunc
}

// Callback function types
type SubmitTxFunc func(interface{}) error
type AcceptTxFunc func() error
type RejectTxFunc func([]byte) error
type DoneFunc func() error

func New(protoOptions protocol.ProtocolOptions, cfg *Config) *LocalTxSubmission {
l := &LocalTxSubmission{
Expand Down
6 changes: 1 addition & 5 deletions protocol/localtxsubmission/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,5 @@ func (s *Server) handleSubmitTx(msgGeneric protocol.Message) error {
}

func (s *Server) handleDone() error {
if s.config.DoneFunc == nil {
return fmt.Errorf("received local-tx-submission Done message but no callback function is defined")
}
// Call the user callback function
return s.config.DoneFunc()
return nil
}

0 comments on commit dfb7ac6

Please sign in to comment.