-
Notifications
You must be signed in to change notification settings - Fork 23
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
Conversation
b5538b3
to
561bf20
Compare
(const $ pure id) | ||
sbe | ||
setUpdateProposal <- forShelleyBasedEraInEon sbe (pure id) $ \w -> | ||
(A.updateTxBodyL w .~) <$> convTxUpdateProposal sbe (txUpdateProposal bc) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This includes #333, so I have the same remark here https://github.com/input-output-hk/cardano-api/pull/333/files#r1368451851
txBodyL :: Lens' (TxBody era) (L.TxBody (ShelleyLedgerEra era)) | ||
txBodyL = lens unTxBody (\_ x -> TxBody x) | ||
|
||
invalidBeforeTxBodyL :: AllegraEraOnwards era -> Lens' (TxBody era) (Maybe SlotNo) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 -> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible follow up step.
There was a problem hiding this comment.
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.
561bf20
to
59d7ef4
Compare
59d7ef4
to
f4dc69b
Compare
f4dc69b
to
34a4b16
Compare
34a4b16
to
22414a4
Compare
Changelog
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