Python-Cardano / pycardano

A lightweight Cardano library in Python
https://pycardano.readthedocs.io
MIT License
215 stars 67 forks source link

FR: Final type check in TxBuilder #205

Closed nielstron closed 1 year ago

nielstron commented 1 year ago

Is your feature request related to a problem? Please describe. A lot of issues when developing arise when trying to build a transaction and accidentally passing values into the TxBuilder that are invalid. Most of the time this error can be easily caught in the TxBuilder, as it has all types already annotated but since python does not enforce these types, they are not always caught. The builder generates a bogus CBOR which is rejected by backends like ogmios with non-helpful error messages, complaining about invalid CBOR - which is hard to debug for the developer.

Describe the solution you'd like Before building the transaction, the TxBuilder does a final typecheck on all internal attributes, raising an informative error message if anything does not match.

Describe alternatives you've considered Maybe we can simply reload the transaction locally using from_cbor and already raise a (less informative) error there that some types don't fit?

Additional context None

cffls commented 1 year ago

Thanks! Will work on this. I really like the alternative idea btw. 🤯

nielstron commented 1 year ago

The alternative solution leverages the type checks that we already have. Maybe we can just make the errors in from_json and from_cbor more informative, this would generally also help with debugging plutus issues.

I.e. "Field X of class Y is expected to be of type V but found W"

cffls commented 1 year ago

PR: https://github.com/Python-Cardano/pycardano/pull/208

I eventually didn't choose the deserializing solution, because I'd like to make validation as independent as possible, and also, less computation :)