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

Switch to use lens and eons for txbody construction #334

Merged
merged 6 commits into from
Oct 31, 2023

Conversation

newhoggy
Copy link
Collaborator

@newhoggy newhoggy commented Oct 21, 2023

Changelog

- description: |
    Switch to use lens and eons for txbody construction
# 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

This PR follows on from #333 and includes commits from that PR. Review that PR first.

How to trust this PR

Highlight important bits of the PR that will make the review faster. If there are commands the reviewer can run to observe the new behavior, describe them.

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

@newhoggy newhoggy marked this pull request as ready for review October 21, 2023 04:12
@newhoggy newhoggy force-pushed the newhoggy/switch-to-use-newtype-wrapper branch 4 times, most recently from b5538b3 to 561bf20 Compare October 21, 2023 05:24
(const $ pure id)
sbe
setUpdateProposal <- forShelleyBasedEraInEon sbe (pure id) $ \w ->
(A.updateTxBodyL w .~) <$> convTxUpdateProposal sbe (txUpdateProposal bc)
Copy link
Collaborator Author

@newhoggy newhoggy Oct 21, 2023

Choose a reason for hiding this comment

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

Because the lenses we use take an eon we can use forShelleyBasedEraInEon and not care about what eons the field is valid for.

This is one of the advantages of using eons over constraints.

In a future era the ledger ends support for a field, then only the lens need change. This code will not need to change at all, reducing the blast radius of ledger changes.

Take for instance https://github.com/input-output-hk/cardano-api/pull/334/files#r1367683063

The updateTxBodyL lens takes ShelleyToBabbageEra eon as its argument, but you wouldn't know it from here - nor do you need to because forShelleyBasedEraInEon selects that eon automatically for you and only sets the field in the tx body when the era is in that eon relieving the developer from maintaining that detail.

Copy link
Contributor

@carbolymer carbolymer Oct 25, 2023

Choose a reason for hiding this comment

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

Tbh I'd prefer using:

setUpdateProposal <- forEraInEon (cardanoEra @era) (pure id) $ \w ->
  ...

Removes the need for maintaining multiple specialized functions for each eon.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wasn't intending to create one for each eon. Just CardanoEra and ShelleyBasedEra. We can promote sbe to era as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How does it look now?

s (L.ValidityInterval a _) b = L.ValidityInterval a b

updateTxBodyL :: ShelleyToBabbageEra era -> Lens' (TxBody era) (StrictMaybe (L.Update (ShelleyLedgerEra era)))
updateTxBodyL w = shelleyToBabbageEraConstraints w $ txBodyL . L.updateTxBodyL
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Notice that updateBodyL takes a ShelleyToBabbageEra eon and internally summons the necessary constraint so the caller doesn't have to.

Copy link
Contributor

@carbolymer carbolymer left a comment

Choose a reason for hiding this comment

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

txBodyL :: Lens' (TxBody era) (L.TxBody (ShelleyLedgerEra era))
txBodyL = lens unTxBody (\_ x -> TxBody x)

invalidBeforeTxBodyL :: AllegraEraOnwards era -> Lens' (TxBody era) (Maybe SlotNo)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we making this change that depends more on the eons when we intend to remove them?

Copy link
Collaborator Author

@newhoggy newhoggy Oct 23, 2023

Choose a reason for hiding this comment

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

We hadn't yet planned how much we're going to trim which may affect what we keep and what we don't, so I did the conservative thing and kept all the features as is.

We should have that discussion.

But I think if even if we dropped support for invalidBeforeTxBodyL in eras before Babbage, that doesn't mean eons go it away, it would just mean for example:

invalidBeforeTxBodyL :: AllegraEraOnwards era -> Lens' (TxBody era) (Maybe SlotNo)

becomes

invalidBeforeTxBodyL :: BabbageEraOnwards era -> Lens' (TxBody era) (Maybe SlotNo)

setUpdateProposal <- forShelleyBasedEraInEon sbe (pure id) $ \w ->
(A.updateTxBodyL w .~) <$> convTxUpdateProposal sbe (txUpdateProposal bc)

setInvalidBefore <- forShelleyBasedEraInEon sbe (pure id) $ \w ->
Copy link
Contributor

@carbolymer carbolymer Oct 25, 2023

Choose a reason for hiding this comment

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

There's a lot of repetition of functions like:

forShelleyBasedEraInEon sbe (pure id) $ \w -> ...

It feels that we could use a Monoid (Ap f (Endo a)) here. For example:

whenShelleyBasedEraInEon :: ()
  => Eon eon
  => Monoid a
  => ShelleyBasedEra era
  -> (eon era -> a)
  -> a
whenShelleyBasedEraInEon sbe f = forShelleyBasedEraInEon sbe mempty f

then

let setMint = whenShelleyBasedEraInEon sbe $ Endo $ \w ->
  A.mintTxBodyL w .~ convMintValue apiMintValue

or

  setUpdateProposal <- fmap getAp . whenShelleyBasedEraInEon sbe $ Ap $ \w ->
    (Endo $ A.updateTxBodyL w .~) <$> convTxUpdateProposal sbe (txUpdateProposal bc)

and then when constructing the txbody use <> instead of &.

(This could be simplified further using do notation from Monad f => Monad (Ap f) and we can create specialized funcitons already wrapping arguments into Ap f (Endo a))

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Possible follow up step.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I pushed the suggested change with some slight changes.

@newhoggy newhoggy force-pushed the newhoggy/switch-to-use-newtype-wrapper branch from 561bf20 to 59d7ef4 Compare October 28, 2023 07:21
@newhoggy newhoggy force-pushed the newhoggy/switch-to-use-newtype-wrapper branch from 59d7ef4 to f4dc69b Compare October 30, 2023 11:08
@newhoggy newhoggy force-pushed the newhoggy/switch-to-use-newtype-wrapper branch from f4dc69b to 34a4b16 Compare October 30, 2023 11:52
@newhoggy newhoggy requested a review from smelc October 31, 2023 06:32
@newhoggy newhoggy added this pull request to the merge queue Oct 31, 2023
@newhoggy newhoggy removed this pull request from the merge queue due to a manual request Oct 31, 2023
@newhoggy newhoggy force-pushed the newhoggy/switch-to-use-newtype-wrapper branch from 34a4b16 to 22414a4 Compare October 31, 2023 10:21
@newhoggy newhoggy enabled auto-merge October 31, 2023 10:38
@newhoggy newhoggy added this pull request to the merge queue Oct 31, 2023
Merged via the queue into main with commit 7420d7b Oct 31, 2023
@newhoggy newhoggy deleted the newhoggy/switch-to-use-newtype-wrapper branch October 31, 2023 11:00
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.

4 participants