Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce constraint usage with eons #299

Merged
merged 10 commits into from
Oct 9, 2023

Conversation

newhoggy
Copy link
Collaborator

@newhoggy newhoggy commented Oct 5, 2023

Changelog

- description: |
    Reduce constraint usage with eons
# uncomment types applicable to the change:
  type:
  # - feature        # introduces a new feature
  # - breaking       # the API has changed in a breaking way
  - compatible     # the API has changed but is non-breaking
  # - optimisation   # measurable performance improvements
  - improvement    # QoL changes e.g. refactoring
  # - bugfix         # fixes a defect
  # - test           # fixes/modifies tests
  # - maintenance    # not directly related to the code
  # - release        # related to a new release preparation
  # - documentation  # change in code docs, haddocks...

Context

By using eons to summon constraints our functions can have fewer or no constraints in their type signatures which simplifies code here as well as later in cardano-cli.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • The change log section in the PR description has been filled in
  • New tests are added if needed and existing tests are updated. These may include:
    • golden tests
    • property tests
    • round trip tests
    • integration tests
      See Running tests for more details
  • The version bounds in .cabal files are updated
  • CI passes. See note on CI. The following CI checks are required:
    • Code is linted with hlint. See .github/workflows/check-hlint.yml to get the hlint version
    • Code is formatted with stylish-haskell. See .github/workflows/stylish-haskell.yml to get the stylish-haskell version
    • Code builds on Linux, MacOS and Windows for ghc-8.10.7 and ghc-9.2.7
  • Self-reviewed the diff

(toLedgerUTxO sbe utxo)
txbody

evalAdaOnly :: ShelleyToAllegraEra era -> TxOutValue era
Copy link
Contributor

Choose a reason for hiding this comment

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

This has changed from ByronToAllegraEra. Was that intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. The original code had ShelleyLedgerEra era in the constraints so you could not call it in the Byron era anyway.

@newhoggy newhoggy force-pushed the newhoggy/reduce-constraint-usage-with-eons branch from 9fe07d7 to fcae06e Compare October 7, 2023 03:18
case txProtocolParams of
BuildTxWith Nothing -> SNothing
BuildTxWith (Just (LedgerProtocolParameters pp)) ->
Alonzo.hashScriptIntegrity (Set.map (L.getLanguageView pp) languages) redeemers datums
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

convPParamsToScriptIntegrityHash simplified a lot for three reasons:

  • The function was returning and Either when Left is never returned. This imposed on callers to handle the Left case as well, which is unnecessary.
  • The function is called with ShelleyBasedEra but the constraint makes it not possible for eras before alonzo anyway. Changing this to AlonzoEraOnwards means we don't have to case on those earlier eras that never happen.
  • alonzoEraOnwardsConstraints removes the need to handle the remaining cases.

IsShelleyBasedEra era
=> L.AlonzoEraTxOut ledgerera
=> Ledger.EraCrypto ledgerera ~ StandardCrypto
=> Ledger.Value ledgerera ~ MaryValue StandardCrypto
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removing constraints like these means the caller no longer needs to supply them.

<> CBOR.encCBOR (txScriptValidityToScriptValidity scriptValidity)
<> CBOR.encodeNullMaybe CBOR.encCBOR txmetadata
ShelleyBasedEraConway ->
CBOR.serialize' (L.eraProtVerLow @L.Babbage)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This looks like a bug. We say Babbage when doing Conway stuff.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this was implemented when there was no Conway era available.

, CBOR.encodeNullMaybe CBOR.encCBOR txmetadata
]
)
(const $ CBOR.serialize' (L.eraProtVerLow @(ShelleyLedgerEra era)) $ mconcat
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using the era parameter means we can't get this wrong:

https://github.com/input-output-hk/cardano-api/pull/299/files#r1349452875

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can actually delete this function (or we should) because we no longer use the intermediate tx body format.

case sData of
TxBodyNoScriptData -> SNothing
TxBodyScriptData w datums redeemers ->
convPParamsToScriptIntegrityHash w apiProtocolParameters redeemers datums languages
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now that convPParamsToScriptIntegrityHash takes the more appropriate AlonzoEraOnwards witness (as opposed to ShelleyBasedEra we can make use of the witness supplied by TxBodyScriptData.

=> IsShelleyBasedEra era
=> ShelleyLedgerEra era ~ ledgerera
=> Ledger.EraCrypto ledgerera ~ StandardCrypto
=> Ledger.Value ledgerera ~ MaryValue StandardCrypto
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removing constraints relieves the caller of the burden of providing them.

=> Ledger.Value ledgerera ~ MaryValue StandardCrypto
=> MaryEraOnwards era
-> AlonzoEraOnwards era
-> BabbageEraOnwards era
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We were previously demanding callers provide three witnesses. We only need the BabbageEraOnwards because that's the most strict and the others are redundant. Demanding fewer witness relieves the called of the burden of providing so many.

@newhoggy newhoggy force-pushed the newhoggy/reduce-constraint-usage-with-eons branch from fcae06e to 47847fb Compare October 9, 2023 11:38
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.

Nice 👍

<> CBOR.encCBOR (txScriptValidityToScriptValidity scriptValidity)
<> CBOR.encodeNullMaybe CBOR.encCBOR txmetadata
ShelleyBasedEraConway ->
CBOR.serialize' (L.eraProtVerLow @L.Babbage)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this was implemented when there was no Conway era available.

, CBOR.encodeNullMaybe CBOR.encCBOR txmetadata
]
)
(const $ CBOR.serialize' (L.eraProtVerLow @(ShelleyLedgerEra era)) $ mconcat
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can actually delete this function (or we should) because we no longer use the intermediate tx body format.

@newhoggy newhoggy force-pushed the newhoggy/reduce-constraint-usage-with-eons branch from 47847fb to 11efd28 Compare October 9, 2023 13:47
@newhoggy newhoggy added this pull request to the merge queue Oct 9, 2023
Merged via the queue into main with commit 9184dff Oct 9, 2023
20 checks passed
@newhoggy newhoggy deleted the newhoggy/reduce-constraint-usage-with-eons branch October 9, 2023 14:18
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.

None yet

2 participants