Skip to content

Cardano blueprint localstate query tests #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ch1bo
Copy link

@ch1bo ch1bo commented Apr 17, 2025

Explores how we could test the LocalStatQuery API that is being created in course of the blueprint initiative.

  • This is adding a cardano-blueprint submodule from which local state query and result cbor examples are taken to test serialization roundtrips.

  • Changes SystemStartResult to hold big.Int for Year and Picoseconds as the CDDL (and at least the Haskell server) are using unbounded integers

    type SystemStartResult struct {
        // Tells the CBOR decoder to convert to/from a struct and a CBOR array
        _           struct{} `cbor:",toarray"`
        Year        big.Int
        Day         int
        Picoseconds big.Int
    }
    • This required a few additional custom methods on SystemStartResult to make roundtrips work. Happy for some feedback in how to improve / avoid those changes.

TODO

Extended TestDecode in the localstatequery/messages_test.go to also assert the decoded Result being equal. This is failing right now though and I'm not sure why in:

// cast msg to MsgResult and further try to decode cbor
if m, ok := msg.(*MsgResult); ok && test.Result != nil {
var decoded = reflect.New(reflect.TypeOf(test.Result))
_, err := cbor.Decode(m.Result, decoded.Interface())
if err != nil {
t.Fatalf("failed to decode result: %s", err)
}
if !reflect.DeepEqual(decoded.Interface(), test.Result) {
t.Fatalf(
"MsgResult content did not decode to expected Result object\n got: %#v\n wanted: %#v",
decoded.Interface(),
test.Result,
)
}
}

--- FAIL: TestDecode (0.00s)
    messages_test.go:139: MsgResult content did not decode to expected Result object
          got:    &localstatequery.SystemStartResult{_:struct {}{}, Year:big.Int{neg:false, abs:big.nat{0x7264a1f8c5026abc, 0x7f99e05018460693, 0xc59141d0ba6cc89, 0x65fea6236047}}, Day:-4205646576720553090, Picoseconds:big.Int{neg:true, abs:big.nat{0x8ba3b6818e7c258b, 0x91db8b1c2237dd91, 0x5e3cc5aadf6619e7, 0x50670ee65e80}}}
          wanted: localstatequery.SystemStartResult{_:struct {}{}, Year:big.Int{neg:false, abs:big.nat{0x7264a1f8c5026abc, 0x7f99e05018460693, 0xc59141d0ba6cc89, 0x65fea6236047}}, Day:-4205646576720553090, Picoseconds:big.Int{neg:true, abs:big.nat{0x8ba3b6818e7c258b, 0x91db8b1c2237dd91, 0x5e3cc5aadf6619e7, 0x50670ee65e80}}}

@agaffney
Copy link
Contributor

That failing test you noted is due to pointer vs. non-pointer in the DeepEqual() comparison

@agaffney
Copy link
Contributor

@ch1bo it took a bit of playing around, but I figured out how to fix the test failure. Reflection is fun!

diff --git a/protocol/localstatequery/messages_test.go b/protocol/localstatequery/messages_test.go
index 4156f5f..cbe1fba 100644
--- a/protocol/localstatequery/messages_test.go
+++ b/protocol/localstatequery/messages_test.go
@@ -135,10 +135,10 @@ func TestDecode(t *testing.T) {
                        if err != nil {
                                t.Fatalf("failed to decode result: %s", err)
                        }
-                       if !reflect.DeepEqual(decoded.Interface(), test.Result) {
+                       if !reflect.DeepEqual(reflect.Indirect(decoded).Interface(), test.Result) {
                                t.Fatalf(
                                        "MsgResult content did not decode to expected Result object\n  got:    %#v\n  wanted: %#v",
-                                       decoded.Interface(),
+                                       reflect.Indirect(decoded).Interface(),
                                        test.Result,
                                )
                        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants