Skip to content

Conversation

@newhoggy
Copy link
Collaborator

@newhoggy newhoggy commented Nov 30, 2025

Changelog

- description: |
    Migrates JSON serialization instances from legacy IsShelleyBasedEra/IsCardanoEra
    constraints to the new Exp.IsEra constraint system, modernizing the codebase
    while maintaining backwards compatibility. Includes comprehensive golden tests
    to ensure JSON format preservation.
  type:
  - refactoring    # QoL changes
  - test           # fixes/modifies tests
  projects:
  - cardano-api

Context

This PR completes the migration of JSON instances to the experimental era infrastructure (Exp.IsEra), removing the dependency on legacy constraint systems. The refactoring eliminates significant code duplication while preserving the exact JSON serialization format, which is critical for backwards compatibility with existing tools and integrations.

The changes are part of upgrading the instance implementations to use the more maintainable and unified era conversion system, making the codebase easier to extend for future eras.

How to trust this PR

The PR includes comprehensive golden tests that verify JSON serialization remains unchanged:

Run the golden tests to verify backwards compatibility:

# Run the new golden test suite
cabal test cardano-api-golden --test-options="-p TxOut"

# Verify all existing tests still pass
cabal test cardano-api-test

Key verification points:

  • All golden test files in cardano-api/test/cardano-api-golden/files/TxOut/ demonstrate identical JSON output
  • The refactored code produces the same JSON format as before
  • No breaking changes to public API - only internal implementation changes

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

Changes Made

Refactoring (Commit: 12c860a)

Core API Migrations:

  • Migrated AddressInEra JSON instances from IsCardanoEra/IsShelleyBasedEra to Exp.IsEra
  • Updated ReferenceScript serialization to use new constraints with simplified era handling
  • Refactored TxOut (both CtxTx and CtxUTxO) JSON parsing to use Exp.convert for era witnesses
  • Simplified TxOutValue JSON handling with unified era conversion
  • Consolidated duplicate parsing logic for Babbage/Conway/Dijkstra eras into single implementations

Code Quality Improvements:

  • Eliminated ~700 lines of duplicated era-specific case matching
  • Replaced verbose pattern matching with generic convert function calls
  • Unified error handling across different era contexts
  • Improved maintainability by centralizing era conversion logic

Testing (Commit: f710b2b)

Golden Test Infrastructure:

  • Added comprehensive golden test suite for TxOut serialization across Conway era
  • Created test helpers for constructing consistent test data (addresses, values, datums, scripts)
  • Implemented tests for various TxOut configurations:
    • Simple outputs (address + lovelace only)
    • Outputs with datum hashes
    • Outputs with inline datums
    • Outputs with reference scripts
    • Full outputs with multi-assets, inline datums, and reference scripts

Test Coverage:

  • TxOutValue tests for both lovelace-only and multi-asset values
  • AddressInEra serialization tests
  • ReferenceScript golden tests
  • UTxO serialization with multiple outputs
  • Pretty-printed JSON golden files for human readability

Golden Files Generated:

  • 13 golden test files covering all major serialization scenarios
  • Located in cardano-api/test/cardano-api-golden/files/TxOut/conway/
  • Each file captures the exact JSON format to guard against regression

Technical Details

The refactoring leverages the Exp.convert function to obtain appropriate era witnesses dynamically, eliminating the need for explicit pattern matching on each era. This approach:

  1. Reduces code duplication: Single implementation handles all supported eras
  2. Improves extensibility: Adding new eras requires minimal changes
  3. Maintains type safety: Era constraints are still enforced at compile time
  4. Preserves compatibility: JSON format remains identical (verified by golden tests)

Impact Analysis

  • No breaking changes: All public APIs maintain their signatures
  • Internal only: Changes are confined to instance implementations
  • Performance neutral: No algorithmic changes, only code organization
  • Future-proof: Easier to add support for new eras going forward

…tion

Implements a complete golden test suite for TxOut, TxOutValue, AddressInEra,
and ReferenceScript types to ensure JSON format stability after migrating
from IsShelleyBasedEra/IsCardanoEra constraints to Exp.IsEra constraints.

Test Coverage:
- TxOut variations: simple, with datum hash, inline datum, reference script, and full
- TxOutValue: lovelace-only and multi-asset configurations
- AddressInEra: payment address serialization
- ReferenceScript: simple script reference handling
- UTxO: complete UTxO map serialization with multiple outputs

Golden Test Infrastructure:
- Helper functions for constructing test addresses, values, and scripts
- Pretty JSON encoding for readable golden files
- Comprehensive test data builders with realistic hash values
- Support for ConwayEra and future eras (Dijkstra)

This test suite ensures backwards compatibility of the JSON format is
maintained during the constraint migration, preventing breaking changes
in API consumers that depend on JSON serialization.
@newhoggy newhoggy force-pushed the newhoggy/upgrade-instances branch from 4c21458 to 12c860a Compare November 30, 2025 10:50
Refactors JSON serialization instances for core types to use the experimental
Exp.IsEra constraint system instead of IsShelleyBasedEra/IsCardanoEra. This
unifies the constraint approach across the codebase and simplifies era-based
type class instances.

Key Changes:
- AddressInEra: Migrate ToJSON/FromJSON instances to Exp.IsEra
- ReferenceScript: Update JSON instances using Exp.convert for era witnesses
- TxOut (CtxTx/CtxUTxO): Consolidate era-specific parsing into unified handlers
- TxOutValue: Simplify FromJSON with direct era conversion

Implementation Details:
- Use Exp.useEra for obtaining era witnesses at the type level
- Convert era witnesses to required eon types (AlonzoEraOnwards, BabbageEraOnwards, etc.)
- Consolidate duplicate reconciliation logic into single reconcileDatums function
- Remove redundant era-specific pattern matching in favor of generic handlers

This refactoring maintains full backwards compatibility with existing JSON
formats while reducing code duplication and improving maintainability. The
changes prepare the codebase for easier extension to future eras by
centralizing era-handling logic.
@newhoggy newhoggy force-pushed the newhoggy/upgrade-instances branch from 12c860a to c668629 Compare November 30, 2025 10:59
Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

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

Have you tested this PR in cardano-cli?

@newhoggy newhoggy requested a review from Jimbo4350 December 2, 2025 12:21
@newhoggy
Copy link
Collaborator Author

newhoggy commented Dec 2, 2025

Have you tested this PR in cardano-cli?

Yep, tested locally by referencing cardano-api from cabal.project in cardano-cli. Requires some small changes in cardano-cli.

@newhoggy newhoggy dismissed Jimbo4350’s stale review December 2, 2025 12:22

Comments addressed.

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