Add FromJSON instance for new experimental TxOut#1179
Conversation
87cfa8a to
bc2431f
Compare
bc2431f to
9734a72
Compare
|
This PR is stale because it has been open 45 days with no activity. |
4c0be1f to
3467b7f
Compare
Add per-era FromJSON instances for the experimental TxOut type, mirroring the ToJSON structure. Pre-Alonzo eras parse address and value only; Alonzo adds datum hash support; Babbage+ adds inline datum (parsed from inlineDatumRaw with hash validation) and reference script support. Supplemental datums are deliberately unsupported as the ledger TxOut does not carry them.
Replace the three separate where-bound helpers (datumFields, inlineDatumFields, refScriptFields) with a single top-level datumAndRefScriptFields function. Simplifies alonzoOnwardsTxOutToJson and documents the per-era field layout in one place.
3467b7f to
223fb95
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds FromJSON support for the experimental TxOut era wrapper (around L.TxOut era), intended to be the inverse of the ToJSON format introduced in #1176, and adds a property test to verify JSON round-tripping for supported Shelley-based eras.
Changes:
- Implement
FromJSONinstances for experimentalTxOutacross Shelley→Conway, including Babbage+ inline datum parsing (frominlineDatumRaw) with hash validation and reference script support. - Refactor datum/reference-script JSON field emission to a shared helper (
datumAndRefScriptFields). - Add a Hedgehog property test for
encode/eitherDecoderound-tripping, plus a Herald changelog fragment.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| cardano-api/test/cardano-api-test/Test/Cardano/Api/Json.hs | Adds a JSON round-trip property for the new experimental TxOut (with Dijkstra currently skipped). |
| cardano-api/src/Cardano/Api/Experimental/Tx/Internal/BodyContent/New.hs | Adds FromJSON parsing for experimental TxOut (base fields, datum hash, inline datum raw+hash validation, reference scripts). |
| .changes/20260420_cardano_api_fromjson_experimental_txout.yml | Adds the required changelog fragment documenting the new feature. |
| -- | Verify that the new experimental 'TxOut' round-trips through JSON | ||
| -- (encode then decode) for all Shelley-based eras. | ||
| prop_new_txout_json_roundtrip :: Property |
| Nothing -> case mDatumHash of | ||
| Just dh -> pure $ L.DatumHash dh | ||
| Nothing -> pure L.NoDatum |
…rage Fail when inlineDatumhash is present without inlineDatumRaw, symmetric with the existing check for the opposite case. Correct the Haddock and add an explanatory comment for the Dijkstra skip in prop_new_txout_json_roundtrip.
palas
left a comment
There was a problem hiding this comment.
Looks good. I just made a couple of small suggestions.
inlineDatumhash is really weird, both the capitalisation and it being omitted on Babbage when it inline datum is not present (instad of just being null). But it is outside of the scope of the PR
| addr <- addrFromJson =<< o .: "address" | ||
| apiVal <- parseJSON =<< o .: "value" | ||
| let mv = toMaryValue apiVal | ||
| val <- case cast mv of |
There was a problem hiding this comment.
I think this usage of cast could use a comment explaining how it works
| Nothing -> case cast (L.coin mv) of | ||
| Just v -> pure v | ||
| Nothing -> fail "txOutBaseParseJson: value is unsupported for this era" |
There was a problem hiding this comment.
| Nothing -> case cast (L.coin mv) of | |
| Just v -> pure v | |
| Nothing -> fail "txOutBaseParseJson: value is unsupported for this era" | |
| Nothing -> do | |
| let L.MaryValue _ ma = mv | |
| unless (ma == mempty) $ | |
| fail "txOutBaseParseJson: ada-only era output cannot carry a multi-asset value" | |
| case cast (L.coin mv) of | |
| Just v -> pure v | |
| Nothing -> fail "txOutBaseParseJson: value is unsupported for this era" |
I think it would be for the best to check there are no non-ada assets in the case of pre-mary eras
| case mDatumHash of | ||
| Just dh -> pure $ L.DatumHash dh | ||
| Nothing -> pure L.NoDatum |
There was a problem hiding this comment.
| case mDatumHash of | |
| Just dh -> pure $ L.DatumHash dh | |
| Nothing -> pure L.NoDatum | |
| maybe L.NoDatum L.DatumHash mDatumHash |
Maybe a good place to use the maybe function.
Context
Implements
FromJSONinstances for the newTxOut eratype (which wrapsL.TxOut era) in the experimental API, matching the JSON format produced by theToJSONinstances added in #1176.Closes #926
Changelog
How to trust this PR
FromJSONinstances for each era (Shelley through Conway), mirroring theToJSONstructure from Add ToJSON instance for new experimental TxOut #1176txOutBaseParseJsonhelperdataHashTxOutLinlineDatumRaw(hex-encoded original CBOR bytes) with hash validation againstinlineDatumhash, and reference script parsing viascriptInAnyLangToLedgerScriptaddrFromJson(reverse ofaddrToJson),scriptInAnyLangToLedgerScript(reverse ofledgerScriptToScriptInAnyLang)fromJSON . toJSON = id) across all Shelley-based erasChecklist
fourmolu