Skip to content

Unify cert indexing via indexCertificatesWith#1140

Open
Jimbo4350 wants to merge 3 commits intomasterfrom
unify-cert-indexing-1138
Open

Unify cert indexing via indexCertificatesWith#1140
Jimbo4350 wants to merge 3 commits intomasterfrom
unify-cert-indexing-1138

Conversation

@Jimbo4350
Copy link
Contributor

@Jimbo4350 Jimbo4350 commented Mar 18, 2026

Changelog

- description: |
    Unify cert indexing via shared indexCertificatesWith helper. Fix
    indexTxCertificates in all three call sites (legacy, experimental,
    compatible) to preserve unwitnessed certificates. Fix
    mapScriptWitnessesCertificates in both APIs to handle Nothing-witness
    certs. Fix collectTxBodyScriptWitnesses to collect simple script
    witnesses from certs, not just Plutus.
  type:
  - bugfix
  - refactoring
  - test
  projects:
  - cardano-api

Context

Follow-up to #1136 based on review feedback from @carbolymer.

  • Extract shared indexCertificatesWith so cert-indexing logic lives in one place
  • Fix indexTxCertificates in all three locations to return Maybe witnesses instead of filtering out Nothing
  • Fix mapScriptWitnessesCertificates in both legacy and experimental APIs — previously silently dropped unwitnessed certs during fee balancing
  • Fix scriptWitnessesCertificates in collectTxBodyScriptWitnesses to collect all script witnesses (simple + Plutus), not just Plutus
  • Expand cert-preservation property tests to cover 4 cert types with key, simple script, and no-witness variations
  • Add properties for collectTxBodyScriptWitnesses and createCompatibleTx cert preservation

Closes #1138

Checklist

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

…itnessed certs

Introduce indexCertificatesWith, a shared helper that associates each
certificate with its position-based ScriptWitnessIndex while preserving
unwitnessed certificates (those stored with a Nothing witness).

Previously indexTxCertificates filtered out Nothing-witness certs,
silently dropping them during fee balancing. The return type now
includes Maybe so callers must handle both cases.

Fixes #1138
Update indexTxCertificates in the experimental API and Compatible/Tx
to use the shared indexCertificatesWith helper. Fix
mapScriptWitnessesCertificates in both the legacy and experimental
APIs to handle Nothing-witness certs instead of silently dropping them.

Also fix scriptWitnessesCertificates in collectTxBodyScriptWitnesses
to collect all script witnesses (simple and Plutus), not just Plutus.
- Cover ConwayRegCert (unwitnessed), ConwayUnRegCert (key-witnessed),
  ConwayDelegCert (simple-script-witnessed), and ConwayRegDelegCert
  (key-witnessed) in the substituteExecutionUnits property.
- Randomize cert ordering via Gen.shuffle for meaningful variation.
- Add property: collectTxBodyScriptWitnesses returns exactly the
  script-witnessed certs and ignores unwitnessed/key-witnessed ones.
- Add property: createCompatibleTx preserves all certs (witnessed and
  unwitnessed) in the resulting ledger transaction body.
- Extract shared genShuffledCertsWithCount generator.
@Jimbo4350 Jimbo4350 force-pushed the unify-cert-indexing-1138 branch from 6f39374 to e33e202 Compare March 18, 2026 18:37
@Jimbo4350 Jimbo4350 requested a review from Copilot March 18, 2026 19:08
@Jimbo4350 Jimbo4350 marked this pull request as ready for review March 18, 2026 19:08
Copy link
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 centralizes certificate indexing across the legacy, experimental, and compatible transaction-building paths to ensure certificate ordering/indexing matches ledger semantics and to prevent silently dropping certificates that have no witness (Nothing).

Changes:

  • Introduces a shared indexCertificatesWith helper and updates all indexTxCertificates implementations to preserve unwitnessed certs via Maybe witnesses.
  • Fixes fee-balancing and script-witness collection to handle Nothing-witness certs correctly and to collect simple-script cert witnesses (not only Plutus).
  • Expands/extends property tests to cover certificate preservation and witness collection across the affected code paths.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
cardano-api/src/Cardano/Api/Tx/Internal/Body.hs Adds indexCertificatesWith, updates indexTxCertificates to include unwitnessed certs, and adjusts cert script-witness collection accordingly.
cardano-api/src/Cardano/Api/Tx/Internal/Fee.hs Updates substituteExecutionUnits certificate handling to preserve certs with Nothing witnesses when rebuilding TxCertificates.
cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Fee.hs Reuses the unified indexing approach; fixes cert mapping and witness collection to include simple scripts and preserve unwitnessed certs.
cardano-api/src/Cardano/Api/Compatible/Tx.hs Updates compatible transaction construction to use the new Maybe witness form while preserving certs and correct indices.
cardano-api/test/cardano-api-test/Test/Cardano/Api/Experimental/Fee.hs Adds/updates regression properties for cert preservation, script witness collection from certs, and compatible tx cert preservation.

-- store as @Nothing@ in the OMap (no witness required) and some as @Just@ (witness
-- required) — then verify that all survive 'substituteExecutionUnits' unchanged.
prop_substituteExecutionUnits_preserves_certs :: Property
prop_substituteExecutionUnits_preserves_certs = H.property $ do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm still leaning towards property.

Copy link
Contributor

@palas palas left a comment

Choose a reason for hiding this comment

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

Looks good 👍

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.

Unify cert indexing via indexTxCertificates and expand substituteExecutionUnits cert tests

3 participants