Skip to content

Commit 2706a03

Browse files
committed
Switch SystemStartResult to use math/big.Int
This is the actual data type returned by the API and to make the example roundtrip, big integers (> int64) must be supported.
1 parent d283dcf commit 2706a03

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

cmd/gouroboros/query.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ func testQuery(f *globalFlags) {
124124
os.Exit(1)
125125
}
126126
fmt.Printf(
127-
"system-start: year = %d, day = %d, picoseconds = %d\n",
127+
// REVIEW: %d should work for big/Int, but warns and produces output
128+
// like {%!d(bool=false) [2025]}
129+
"system-start: year = %v, day = %d, picoseconds = %v\n",
128130
systemStart.Year,
129131
systemStart.Day,
130132
systemStart.Picoseconds,

protocol/localstatequery/client_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package localstatequery_test
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
"math/big"
2021
"reflect"
2122
"testing"
2223
"time"
@@ -290,9 +291,9 @@ func TestGetUTxOByAddress(t *testing.T) {
290291
func TestGenesisConfigJSON(t *testing.T) {
291292
genesisConfig := localstatequery.GenesisConfigResult{
292293
Start: localstatequery.SystemStartResult{
293-
Year: 2024,
294+
Year: *big.NewInt(2024),
294295
Day: 35,
295-
Picoseconds: 1234567890123456,
296+
Picoseconds: *big.NewInt(1234567890123456),
296297
},
297298
NetworkMagic: 764824073,
298299
NetworkId: 1,

protocol/localstatequery/messages_test.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package localstatequery
1717
import (
1818
"encoding/hex"
1919
"fmt"
20+
"math/big"
2021
"os"
2122
"reflect"
2223
"testing"
@@ -30,6 +31,7 @@ type testDefinition struct {
3031
CborHex string
3132
Message protocol.Message
3233
MessageType uint
34+
Result interface{}
3335
}
3436

3537
var tests = []testDefinition{
@@ -100,9 +102,19 @@ var tests = []testDefinition{
100102
MessageType: MessageTypeQuery,
101103
},
102104
{
103-
CborHex: string(readFile("../../cardano-blueprint/src/api/examples/getSystemStart/result.cbor")),
104-
Message: NewMsgResult([]byte{5}), // FIXME: not correct and should also check SystemStart decoder
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+
})),
105112
MessageType: MessageTypeResult,
113+
Result: SystemStartResult{
114+
Year: unsafeBigInt([]byte("703941703872597091335551638723343370661404331303175992839224705786473148")),
115+
Day: -4205646576720553090,
116+
Picoseconds: unsafeBigInt([]byte("-554918151390414980540174869115975093799476848534297657333456993160799627")),
117+
},
106118
},
107119
}
108120

@@ -116,6 +128,22 @@ func TestDecode(t *testing.T) {
116128
if err != nil {
117129
t.Fatalf("failed to decode CBOR: %s", err)
118130
}
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+
if !reflect.DeepEqual(decoded.Interface(), test.Result) {
139+
t.Fatalf(
140+
"MsgResult content did not decode to expected Result object\n got: %#v\n wanted: %#v",
141+
decoded.Interface(),
142+
test.Result,
143+
)
144+
}
145+
}
146+
119147
// Set the raw CBOR so the comparison should succeed
120148
test.Message.SetCbor(cborData)
121149
if m, ok := msg.(*MsgQuery); ok {
@@ -148,6 +176,24 @@ func TestEncode(t *testing.T) {
148176
}
149177
}
150178

179+
// Helper function to encode to cbor or panic
180+
func unsafeCbor(data interface{}) []byte {
181+
cborData, err := cbor.Encode(data)
182+
if err != nil {
183+
panic(fmt.Sprintf("error encoding to CBOR: %s", err))
184+
}
185+
return cborData
186+
}
187+
188+
func unsafeBigInt(text []byte) big.Int {
189+
var i big.Int
190+
err := i.UnmarshalText(text)
191+
if err != nil {
192+
panic(fmt.Sprintf("error unmarshalling text to big.Int: %s", err))
193+
}
194+
return i
195+
}
196+
151197
// Helper function to allow inline reading of a file without capturing the error
152198
func readFile(path string) []byte {
153199
data, err := os.ReadFile(path)

protocol/localstatequery/queries.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package localstatequery
1616

1717
import (
1818
"fmt"
19+
"math/big"
1920

2021
"github.com/blinklabs-io/gouroboros/cbor"
2122
"github.com/blinklabs-io/gouroboros/ledger"
@@ -416,9 +417,13 @@ type SystemStartQuery struct {
416417
type SystemStartResult struct {
417418
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
418419
_ struct{} `cbor:",toarray"`
419-
Year int
420+
Year big.Int
420421
Day int
421-
Picoseconds uint64
422+
Picoseconds big.Int
423+
}
424+
425+
func (s SystemStartResult) String() string {
426+
return fmt.Sprintf("SystemStart %s %d %s", s.Year.String(), s.Day, s.Picoseconds.String())
422427
}
423428

424429
type ChainBlockNoQuery struct {

0 commit comments

Comments
 (0)