Skip to content

Added IndefiniteDecoder for round trip plutusdata serialization #431

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

Merged
merged 6 commits into from
Mar 25, 2025

Conversation

theeldermillenial
Copy link
Contributor

This PR adds support for IndefiniteDecoder, a special CBOR decoder class that returns indefinite lists as an IndefiniteList rather than a list as a way to support roundtrip, reproducible PlutusData serialization.

There are two new unit tests that demonstrate the need for this PR in the serialization tests.

The first is test_datum_round_trip, that creates a custom PlutusData class, serializes to CBOR and deserializes. Without the IndefiniteDecoder, this test fails.

The second test is test_datum_raw_round_trip, that creates a custom Plutusdata class, serializes to CBOR and deserializes as RawPlutusData. This test is currently marked as an expected fail test, since this PR does not resolve this issue. Attempts to resolve this problem causes issues with other round trip serialization tests, and warrants further investigation.

closes #331

Copy link

codecov bot commented Mar 16, 2025

Codecov Report

Attention: Patch coverage is 80.95238% with 4 lines in your changes missing coverage. Please review.

Project coverage is 88.79%. Comparing base (a312c08) to head (a243faa).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pycardano/serialization.py 80.95% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #431      +/-   ##
==========================================
- Coverage   88.95%   88.79%   -0.16%     
==========================================
  Files          33       33              
  Lines        4744     4757      +13     
  Branches     1134     1136       +2     
==========================================
+ Hits         4220     4224       +4     
- Misses        369      374       +5     
- Partials      155      159       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@theeldermillenial
Copy link
Contributor Author

Fixed QA issues.

Not entirely sure why but sometimes a test for roundtrip transaction serialization fails. It does not appear to be reproducible.

@cffls
Copy link
Collaborator

cffls commented Mar 23, 2025

Looks like some non-determinism is introduced, which caused the test to fail randomly.

@cffls
Copy link
Collaborator

cffls commented Mar 23, 2025

Also, it seems that the customized function decode_array is never used in test (see code-cov report).

@cffls
Copy link
Collaborator

cffls commented Mar 23, 2025

The reason why it failed non-deterministically was that the order of the elements in a set isn't preserved when deserializing, which led to mismatched order when compared with the original data.
I figured out that this was due to the library was only using the c-extension of cbor2, which isn't customizable at all. This also explained why your code wasn't even covered by any test before. I added a commit that enforces the library to install cbor2 in pure python, and now your function is being used, but some relevant tests are failing, which is expected because we were expecting list but got IndefinitelyList after your change. It would be great if you could help fix the failed tests. Thanks a lot!

@cffls cffls force-pushed the feat/indefinite branch from 5eca3d9 to 9f457c4 Compare March 23, 2025 19:00
@theeldermillenial
Copy link
Contributor Author

@cffls Wait...so does this mean that the c-extension was also causing the issues related to sets?

I'll try to fix the other outstanding issues tonight.

@cffls
Copy link
Collaborator

cffls commented Mar 23, 2025

so does this mean that the c-extension was also causing the issues related to sets?

Yes, it is forcing the code to use c-extension and making it impossible to monkey patch any default behavior, e.g. decoding tag.258 into a set, which doesn't preserve the order.

@theeldermillenial
Copy link
Contributor Author

I wonder if that explains some of the other occasional behavior I would have that cause hash errors. I could never reproduce them and they happened infrequently.

Sigh...

Copy link
Collaborator

@cffls cffls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@cffls cffls merged commit 36483dc into Python-Cardano:main Mar 25, 2025
13 of 15 checks passed
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