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

Avoid IsShelleyBasedEra and IsCardanoEra where possible #313

Merged
merged 4 commits into from
Oct 18, 2023

Conversation

newhoggy
Copy link
Collaborator

@newhoggy newhoggy commented Oct 12, 2023

Changelog

- description: |
    Remove `IsShelleyBasedEra` and `IsCardanoEra` from all functions and types.
    Use `ShelleyBasedEra` and `CardanoEra` instead.
# 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

We often use a combination of witness and constraints, sometimes inconsistently.

This PR standardises on the the use of witnesses because those are eon-friendly, more flexible and have better type-inference.

Removal of the constraints means that callers no longer have to satisfy those constraints.

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

[ byronAddressInEra <$> genAddressByron
, shelleyAddressInEra <$> genAddressShelley
[ byronAddressInEra <$> genAddressByron
, shelleyAddressInEra sbe <$> genAddressShelley
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

shelleyAddressInEra is an example of a function that previously demanded a constraint, but now demands a witness instead. It is often the case that the witness is just available somewhere in the context.

parseJSON = withText "AddressInEra" $ \txt -> do
addressAny <- runParsecParser parseAddressAny txt
pure $ anyAddressInShelleyBasedEra addressAny
parseJSON =
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 still retain the use of IsShelleyBasedEra and IsCardanoEra constraints for type class instances for now because we don't yet have a way to express these.


ShelleyBasedEra
:: ShelleyBasedEra era
-> CardanoEraStyle 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.

There is no need to embed both the constraint and the witness in the same constructor. They are convertible to each other.

in
case tx of
ShelleyTx _ tx' ->
let x = shelleyBasedEraConstraints sbe $ tx' ^. L.sizeTxF in Lovelace (a * x + b)
Copy link
Collaborator Author

@newhoggy newhoggy Oct 12, 2023

Choose a reason for hiding this comment

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

Often we need more constraints than just IsShelleyBasedEra so demanding it doesn't really help. We can summon all the constraints we need with just the eon witness.

ShelleyTxBody _ txbody _ _ _ _ -> makeShelleyBasedBootstrapWitness sbe nwOrAddr txbody sk

makeShelleyBasedBootstrapWitness :: forall era. ()
=> ShelleyBasedEra 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.

An example of where the witness is sufficient to summon all the constrains we need.

LegacyByronEra -> makeByronTransactionBody
ShelleyBasedEra sbe -> makeShelleyTransactionBody sbe
createAndValidateTransactionBody era =
case cardanoEraStyle era of
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 witness is less messy because we avoid having to give type-hints.

@newhoggy newhoggy force-pushed the newhoggy/less-reliance-on-IsShelleyBasedEra branch from 97c880e to 099db6e Compare October 12, 2023 13:45
@@ -1840,8 +1840,7 @@ fromConwayPParams :: BabbageEraPParams ledgerera
-> ProtocolParameters
fromConwayPParams = fromBabbagePParams

checkProtocolParameters
:: forall era. IsCardanoEra era
checkProtocolParameters :: ()
=> ShelleyBasedEra 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.

Sometimes we demanded both the witness and constraint and they don't match.

(\w -> TxOutValue w (lovelaceToValue (Lovelace (2^(64 :: Integer)) - 1) <> nonAdaChange))
(cardanoEra @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.

Code that uses constraints are harder to maintain. For example we use cardanoEra @era here despite era' already being defined further down.

@newhoggy newhoggy force-pushed the newhoggy/less-reliance-on-IsShelleyBasedEra branch from 099db6e to 153ac0b Compare October 12, 2023 13:53
@newhoggy newhoggy marked this pull request as ready for review October 12, 2023 13:53
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.

Can you open a PR in cardano-cli before we merge this? I see we only use IsShelleyBasedEra twice in cardano-cli so it should be fine.

@newhoggy newhoggy force-pushed the newhoggy/less-reliance-on-IsShelleyBasedEra branch 2 times, most recently from 3eb911a to 6dd0cce Compare October 14, 2023 14:34
@newhoggy
Copy link
Collaborator Author

Can you open a PR in cardano-cli before we merge this? I see we only use IsShelleyBasedEra twice in cardano-cli so it should be fine.

There is a POC here. Will clean up the POC some more.

IntersectMBO/cardano-cli#376

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.

LGTM!

@newhoggy newhoggy force-pushed the newhoggy/less-reliance-on-IsShelleyBasedEra branch from 6dd0cce to 5eb9017 Compare October 18, 2023 04:59
@newhoggy newhoggy requested a review from a team as a code owner October 18, 2023 05:45
@newhoggy newhoggy force-pushed the newhoggy/less-reliance-on-IsShelleyBasedEra branch from fdf5bad to 43027e5 Compare October 18, 2023 10:59
@newhoggy newhoggy added this pull request to the merge queue Oct 18, 2023
Merged via the queue into main with commit 44ffb48 Oct 18, 2023
20 checks passed
@newhoggy newhoggy deleted the newhoggy/less-reliance-on-IsShelleyBasedEra branch October 18, 2023 21:52
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