Skip to content

Fix supplemental datum handling in experimental API#1112

Merged
Jimbo4350 merged 3 commits intomasterfrom
jordan/supplemental-datum-fix
Feb 27, 2026
Merged

Fix supplemental datum handling in experimental API#1112
Jimbo4350 merged 3 commits intomasterfrom
jordan/supplemental-datum-fix

Conversation

@Jimbo4350
Copy link
Copy Markdown
Contributor

@Jimbo4350 Jimbo4350 commented Feb 25, 2026

Changelog

- description: |
    Fix supplemental datum handling in experimental API. toLedgerDatum now correctly
    produces DatumHash for supplemental datums instead of inline Datum representation.
    getDatums uses new explicit txSupplementalDatums field instead of extracting from
    outputs, which incorrectly included inline datums in TxDats.
  type:
    - bugfix
  projects:
    - cardano-api

Context

The previous implementation had two bugs:

  1. toLedgerDatum converted supplemental datums to inline datum representation (L.Datum), but per the Alonzo ledger spec they should only set a DatumHash on the output with the actual data going into the witness set (TxDats).
  2. getDatums extracted datums from all transaction outputs using L.dataTxOutL, which couldn't distinguish inline datums from supplemental datums, causing inline datums to also be added to TxDats.

This PR introduces a txSupplementalDatums field on TxBodyContent so supplemental datums can be provided explicitly, and fixes toLedgerDatum to produce L.DatumHash for supplemental datums. It also adds a supplementalDatumFromLegacy helper and updates fromLegacyTxOut to return supplemental datum data alongside the TxOut.

How to trust this PR

  • Two focused commits: the first adds the new field and refactors getDatums, the second fixes toLedgerDatum and updates fromLegacyTxOut
  • Changes are limited to 3 files in Cardano.Api.Experimental.Tx
  • Verify supplemental datums produce DatumHash in outputs (not inline Datum)
  • Verify inline datums are not included in TxDats
  • cabal build all and cabal test cardano-api:cardano-api-test should pass

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

Copilot AI review requested due to automatic review settings February 25, 2026 13:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes critical bugs in the handling of supplemental datums in the experimental transaction API. Supplemental datums are now correctly converted to DatumHash references (instead of inline Datum representations) and explicitly tracked in TxBodyContent, ensuring proper separation from inline datums in the witness set.

Changes:

  • Fixed toLedgerDatum to convert supplemental datums to DatumHash instead of inline Datum
  • Added txSupplementalDatums field to TxBodyContent for explicit tracking of supplemental datum data
  • Refactored getDatums to use the new explicit supplemental datums field instead of extracting all datums from outputs

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
cardano-api/src/Cardano/Api/Experimental/Tx/Internal/BodyContent/New.hs Core fix: updated datum conversion logic, added supplemental datums tracking to TxBodyContent, and refactored datum collection
cardano-api/src/Cardano/Api/Experimental/Tx.hs Exported new setTxSupplementalDatums and supplementalDatumFromLegacy functions
cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Fee.hs Added wildcard pattern to match new txSupplementalDatums field

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +368 to +371
:: forall era
. IsEra era
=> OldApi.TxOut CtxTx era
-> Either DatumDecodingError (TxOut (LedgerEra era), Map L.DataHash (L.Data (LedgerEra era)))
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The function signature change to return a tuple including supplemental datums is significant but lacks documentation explaining the new return value. Add a docstring explaining that the second element of the tuple contains supplemental datum data extracted from the output, or an empty map for non-supplemental datums.

Copilot uses AI. Check for mistakes.
Comment on lines +381 to +384
supplementalDatumFromLegacy
:: L.Era (LedgerEra era)
=> OldApi.TxOutDatum CtxTx era
-> Map L.DataHash (L.Data (LedgerEra era))
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The function has a comment header but the signature lacks a proper docstring. Consider adding a Haddock docstring that explains the function's purpose, parameters, and return value to complement the existing comment at line 379-380.

Copilot uses AI. Check for mistakes.
L.TxDats $
fromList $
refInDatums <> txOutsDats
L.TxDats $ fromList refInDatums <> supplementalDats
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The expression fromList refInDatums <> supplementalDats combines a list-to-map conversion with map concatenation. This could be clearer as fromList (refInDatums <> toList supplementalDats) or by converting both to maps first, making the data type transformations more explicit.

Suggested change
L.TxDats $ fromList refInDatums <> supplementalDats
L.TxDats $ fromList (refInDatums <> toList supplementalDats)

Copilot uses AI. Check for mistakes.
@carbolymer
Copy link
Copy Markdown
Contributor

This PR description is not following the template.

@Jimbo4350 Jimbo4350 force-pushed the jordan/supplemental-datum-fix branch from 3642778 to 97e322e Compare February 27, 2026 13:28
@Jimbo4350 Jimbo4350 enabled auto-merge February 27, 2026 13:28
getDatums previously extracted datums from transaction outputs using
L.dataTxOutL, which could not distinguish between inline datums and
supplemental datums, causing both to be added to TxDats.

Per the Alonzo ledger spec, supplemental datums are only those whose
hashes correspond to output DatumHash fields. Inline datums should
never appear in TxDats.

This commit introduces a txSupplementalDatums field on TxBodyContent
so supplemental datums can be provided explicitly, and refactors
getDatums to use this field instead of extracting from outputs.
toLedgerDatum was incorrectly converting TxOutSupplementalDatum to
L.Datum (inline representation). Per the Alonzo ledger spec,
supplemental datums should produce L.DatumHash on the output, with
the full datum data going into TxDats separately.

Also updates fromLegacyTxOut to return supplemental datum data
alongside the TxOut, so callers can populate txSupplementalDatums.
Adds supplementalDatumFromLegacy helper for this purpose.
@Jimbo4350 Jimbo4350 force-pushed the jordan/supplemental-datum-fix branch from 97e322e to 59e5004 Compare February 27, 2026 13:39
@Jimbo4350 Jimbo4350 added this pull request to the merge queue Feb 27, 2026
Merged via the queue into master with commit 52c9854 Feb 27, 2026
30 of 32 checks passed
@Jimbo4350 Jimbo4350 deleted the jordan/supplemental-datum-fix branch February 27, 2026 17:33
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.

3 participants