Skip to content

Commit 8d6eb7a

Browse files
authored
Merge pull request #150 from cloudstruct/test/localtxsubmission-message-unit-tests
test: encode/decode unit tests for localtxsubmission messages
2 parents 2f85253 + 3636e4d commit 8d6eb7a

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

cmd/go-ouroboros-network/localtxsubmission.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ import (
1313
)
1414

1515
type localTxSubmissionFlags struct {
16-
flagset *flag.FlagSet
17-
txFile string
16+
flagset *flag.FlagSet
17+
txFile string
18+
rawTxFile string
1819
}
1920

2021
func newLocalTxSubmissionFlags() *localTxSubmissionFlags {
2122
f := &localTxSubmissionFlags{
2223
flagset: flag.NewFlagSet("local-tx-submission", flag.ExitOnError),
2324
}
2425
f.flagset.StringVar(&f.txFile, "tx-file", "", "path to the JSON transaction file to submit")
26+
f.flagset.StringVar(&f.rawTxFile, "raw-tx-file", "", "path to the raw transaction file to submit")
2527
return f
2628
}
2729

@@ -36,6 +38,10 @@ func testLocalTxSubmission(f *globalFlags) {
3638
fmt.Printf("failed to parse subcommand args: %s\n", err)
3739
os.Exit(1)
3840
}
41+
if localTxSubmissionFlags.txFile == "" && localTxSubmissionFlags.rawTxFile == "" {
42+
fmt.Printf("you must specify -tx-file or -raw-tx-file\n")
43+
os.Exit(1)
44+
}
3945

4046
conn := createClientConnection(f)
4147
errorChan := make(chan error)
@@ -60,23 +66,32 @@ func testLocalTxSubmission(f *globalFlags) {
6066
}
6167
o.LocalTxSubmission.Client.Start()
6268

63-
txData, err := ioutil.ReadFile(localTxSubmissionFlags.txFile)
64-
if err != nil {
65-
fmt.Printf("Failed to load transaction file: %s\n", err)
66-
os.Exit(1)
67-
}
69+
var txBytes []byte
70+
if localTxSubmissionFlags.txFile != "" {
71+
txData, err := ioutil.ReadFile(localTxSubmissionFlags.txFile)
72+
if err != nil {
73+
fmt.Printf("Failed to load transaction file: %s\n", err)
74+
os.Exit(1)
75+
}
6876

69-
var jsonData map[string]string
70-
err = json.Unmarshal(txData, &jsonData)
71-
if err != nil {
72-
fmt.Printf("failed to parse transaction file: %s\n", err)
73-
os.Exit(1)
74-
}
77+
var jsonData map[string]string
78+
err = json.Unmarshal(txData, &jsonData)
79+
if err != nil {
80+
fmt.Printf("failed to parse transaction file: %s\n", err)
81+
os.Exit(1)
82+
}
7583

76-
txBytes, err := hex.DecodeString(jsonData["cborHex"])
77-
if err != nil {
78-
fmt.Printf("failed to decode transaction: %s\n", err)
79-
os.Exit(1)
84+
txBytes, err = hex.DecodeString(jsonData["cborHex"])
85+
if err != nil {
86+
fmt.Printf("failed to decode transaction: %s\n", err)
87+
os.Exit(1)
88+
}
89+
} else {
90+
txBytes, err = ioutil.ReadFile(localTxSubmissionFlags.rawTxFile)
91+
if err != nil {
92+
fmt.Printf("Failed to load transaction file: %s\n", err)
93+
os.Exit(1)
94+
}
8095
}
8196

8297
if err = o.LocalTxSubmission.Client.SubmitTx(ledger.TX_TYPE_ALONZO, txBytes); err != nil {

protocol/localtxsubmission/messages_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package localtxsubmission
22

33
import (
44
"encoding/hex"
5+
"fmt"
6+
"github.com/cloudstruct/go-cardano-ledger"
57
"github.com/cloudstruct/go-ouroboros-network/protocol"
68
"github.com/cloudstruct/go-ouroboros-network/utils"
79
"reflect"
10+
"strings"
811
"testing"
912
)
1013

@@ -14,8 +17,41 @@ type testDefinition struct {
1417
MessageType uint
1518
}
1619

17-
// TODO: implement tests for more messages
20+
// Helper function to allow inline hex decoding without capturing the error
21+
func hexDecode(data string) []byte {
22+
// Strip off any leading/trailing whitespace in hex string
23+
data = strings.TrimSpace(data)
24+
decoded, err := hex.DecodeString(data)
25+
if err != nil {
26+
panic(fmt.Sprintf("error decoding hex: %s", err))
27+
}
28+
return decoded
29+
}
30+
31+
// Valid CBOR that serves as a placeholder for real TX content in the tests
32+
// [h'DEADBEEF']
33+
var placeholderTx = hexDecode("8144DEADBEEF")
34+
35+
// Valid CBOR that serves as a placeholder for TX rejection errors
36+
// [2, 4]
37+
var placeholderRejectError = hexDecode("820204")
38+
1839
var tests = []testDefinition{
40+
{
41+
CborHex: fmt.Sprintf("82008204d81846%x", placeholderTx),
42+
MessageType: MESSAGE_TYPE_SUBMIT_TX,
43+
Message: NewMsgSubmitTx(ledger.TX_TYPE_ALONZO, placeholderTx),
44+
},
45+
{
46+
CborHex: "8101",
47+
Message: NewMsgAcceptTx(),
48+
MessageType: MESSAGE_TYPE_ACCEPT_TX,
49+
},
50+
{
51+
CborHex: fmt.Sprintf("8202%x", placeholderRejectError),
52+
MessageType: MESSAGE_TYPE_REJECT_TX,
53+
Message: NewMsgRejectTx(placeholderRejectError),
54+
},
1955
{
2056
CborHex: "8103",
2157
Message: NewMsgDone(),

0 commit comments

Comments
 (0)