@@ -16,6 +16,9 @@ package localstatequery
16
16
17
17
import (
18
18
"encoding/hex"
19
+ "fmt"
20
+ "math/big"
21
+ "os"
19
22
"reflect"
20
23
"testing"
21
24
@@ -28,6 +31,7 @@ type testDefinition struct {
28
31
CborHex string
29
32
Message protocol.Message
30
33
MessageType uint
34
+ Result interface {}
31
35
}
32
36
33
37
var tests = []testDefinition {
@@ -92,6 +96,26 @@ var tests = []testDefinition{
92
96
Message : NewMsgReAcquireVolatileTip (),
93
97
MessageType : MessageTypeReacquireVolatileTip ,
94
98
},
99
+ {
100
+ CborHex : string (readFile ("../../cardano-blueprint/src/api/examples/getSystemStart/query.cbor" )),
101
+ Message : NewMsgQuery (& SystemStartQuery {simpleQueryBase {Type : QueryTypeSystemStart }}),
102
+ MessageType : MessageTypeQuery ,
103
+ },
104
+ {
105
+ CborHex : string (readFile ("../../cardano-blueprint/src/api/examples/getSystemStart/result.cbor" )),
106
+ Message : NewMsgResult (unsafeCbor (
107
+ SystemStartResult {
108
+ Year : unsafeBigInt ([]byte ("703941703872597091335551638723343370661404331303175992839224705786473148" )),
109
+ Day : - 4205646576720553090 ,
110
+ Picoseconds : unsafeBigInt ([]byte ("-554918151390414980540174869115975093799476848534297657333456993160799627" )),
111
+ })),
112
+ MessageType : MessageTypeResult ,
113
+ Result : SystemStartResult {
114
+ Year : unsafeBigInt ([]byte ("703941703872597091335551638723343370661404331303175992839224705786473148" )),
115
+ Day : - 4205646576720553090 ,
116
+ Picoseconds : unsafeBigInt ([]byte ("-554918151390414980540174869115975093799476848534297657333456993160799627" )),
117
+ },
118
+ },
95
119
}
96
120
97
121
func TestDecode (t * testing.T ) {
@@ -104,6 +128,23 @@ func TestDecode(t *testing.T) {
104
128
if err != nil {
105
129
t .Fatalf ("failed to decode CBOR: %s" , err )
106
130
}
131
+ // cast msg to MsgResult and further try to decode cbor
132
+ if m , ok := msg .(* MsgResult ); ok && test .Result != nil {
133
+ var decoded = reflect .New (reflect .TypeOf (test .Result ))
134
+ _ , err := cbor .Decode (m .Result , decoded .Interface ())
135
+ if err != nil {
136
+ t .Fatalf ("failed to decode result: %s" , err )
137
+ }
138
+ var actual = reflect .Indirect (decoded ).Interface ()
139
+ if ! reflect .DeepEqual (actual , test .Result ) {
140
+ t .Fatalf (
141
+ "MsgResult content did not decode to expected Result object\n got: %#v\n wanted: %#v" ,
142
+ actual ,
143
+ test .Result ,
144
+ )
145
+ }
146
+ }
147
+
107
148
// Set the raw CBOR so the comparison should succeed
108
149
test .Message .SetCbor (cborData )
109
150
if m , ok := msg .(* MsgQuery ); ok {
@@ -135,3 +176,30 @@ func TestEncode(t *testing.T) {
135
176
}
136
177
}
137
178
}
179
+
180
+ // Helper function to encode to cbor or panic
181
+ func unsafeCbor (data interface {}) []byte {
182
+ cborData , err := cbor .Encode (data )
183
+ if err != nil {
184
+ panic (fmt .Sprintf ("error encoding to CBOR: %s" , err ))
185
+ }
186
+ return cborData
187
+ }
188
+
189
+ func unsafeBigInt (text []byte ) big.Int {
190
+ var i big.Int
191
+ err := i .UnmarshalText (text )
192
+ if err != nil {
193
+ panic (fmt .Sprintf ("error unmarshalling text to big.Int: %s" , err ))
194
+ }
195
+ return i
196
+ }
197
+
198
+ // Helper function to allow inline reading of a file without capturing the error
199
+ func readFile (path string ) []byte {
200
+ data , err := os .ReadFile (path )
201
+ if err != nil {
202
+ panic (fmt .Sprintf ("error reading file: %s" , err ))
203
+ }
204
+ return data
205
+ }
0 commit comments