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

Transaction.hs: reduce boilerplate and move towards ShelleyBasedEra #783

Merged
merged 1 commit into from
Jun 18, 2024

Conversation

smelc
Copy link
Contributor

@smelc smelc commented Jun 7, 2024

Changelog

- description: |
    Transaction.hs: reduce boilerplate and move towards ShelleyBasedEra
# 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
  - refactoring    # QoL changes
  # - 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...

How to trust this PR

  • It doesn't actually change the runtime behavior
  • It's more deletion that additions 🥇

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • Self-reviewed the diff

@smelc smelc force-pushed the smelc/reduce-boilerplate-modernize-TxValidationErrors branch from 5a540fb to d53b701 Compare June 7, 2024 09:45
Comment on lines 190 to 163
-- TODO: Because we have separated Byron related transaction
-- construction into separate commands, we can parameterize this
-- on ShelleyBasedEra era and remove Either TxCertificatesValidationError
-- construction into separate commands, we can remove
-- Either TxNotSupportedInEraValidationError
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this comment mean I should just remove the call to conjureWitness?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, sbe == supported in the function body, so this conjureWitness will never fail, so i'd say yes. The same for other places in this PR, where we changed CardanoEra to ShelleyBasedEra.

@smelc smelc marked this pull request as ready for review June 7, 2024 09:52
@smelc smelc force-pushed the smelc/treasury-donations branch from 528f47e to 2096e19 Compare June 12, 2024 15:15
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.

Good job!

-- | First argument is the kind of data that is not supported.
-- Second argument in the era that doesn't support the data.
TxNotSupportedInAnyCardanoEraValidationError T.Text AnyCardanoEra
-- | First argument is the kind of data that is not supported.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this haddock is 100% correct - I think it should be after |, as it references the second constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@carbolymer> nah it's all good:

image

Comment on lines 190 to 163
-- TODO: Because we have separated Byron related transaction
-- construction into separate commands, we can parameterize this
-- on ShelleyBasedEra era and remove Either TxCertificatesValidationError
-- construction into separate commands, we can remove
-- Either TxNotSupportedInEraValidationError
Copy link
Contributor

Choose a reason for hiding this comment

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

Well, sbe == supported in the function body, so this conjureWitness will never fail, so i'd say yes. The same for other places in this PR, where we changed CardanoEra to ShelleyBasedEra.

Comment on lines 104 to 107
validateTxCurrentTreasuryValue sbe mCurrentTreasuryValue =
case mCurrentTreasuryValue of
Nothing -> Right Nothing
Just (TxCurrentTreasuryValue { unTxCurrentTreasuryValue }) ->
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

Suggested change
validateTxCurrentTreasuryValue sbe mCurrentTreasuryValue =
case mCurrentTreasuryValue of
Nothing -> Right Nothing
Just (TxCurrentTreasuryValue { unTxCurrentTreasuryValue }) ->
validateTxCurrentTreasuryValue sbe =
mapM $ \(TxCurrentTreasuryValue { unTxCurrentTreasuryValue }) ->

or you can use forM, ...or neither 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since I rebased this PR on main (instead of #778) this is irrelevant now, but I'll keep it mind when revisiting #778 👍

=> ShelleyBasedEra era
-> Maybe TxTreasuryDonation
-> Either (TxNotSupportedInEraValidationError era) (Maybe (Featured ConwayEraOnwards era L.Coin))
validateTxTreasuryDonation sbe mTreasuryDonation =
Copy link
Contributor

Choose a reason for hiding this comment

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

same suggestion as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since I rebased this PR on main (instead of #778) this is irrelevant now, but I'll keep it mind when revisiting #778 👍

@smelc smelc force-pushed the smelc/treasury-donations branch from 2096e19 to b2b9781 Compare June 13, 2024 09:40
@smelc smelc force-pushed the smelc/reduce-boilerplate-modernize-TxValidationErrors branch from d53b701 to f42b8c6 Compare June 13, 2024 09:42
instance Error TxProtocolParametersValidationError where
prettyError (ProtocolParametersNotSupported e) =
"Transaction protocol parameters are not supported in " <> pretty e

validateProtocolParameters
:: CardanoEra 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 will only fail if we call validateProtocolParameters with ByronEra. However the code won't compile if we call this with ByronEra:

ghci> validateProtocolParameters ByronEra Nothing

<interactive>:22:1: error: [GHC-39999]
    • No instance for ‘IsShelleyBasedEra ByronEra’
        arising from a use of ‘print’
    • In a stmt of an interactive GHCi command: print it

What we should do is change the parameter from CardanoEra era to ShelleyBasedEra era but actually in this case no validation is done!

So we can remove this function and directly use setTxProtocolParams.

Have a look at the other functions that demand CardanoEra era but should be parameterized on ShelleyBasedEra era. If no validation is done, we can just drop them. I'd do that in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK will do in a follow-up PR right away 👍

@Jimbo4350> so can you approve in the meantime?

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't because it would be adding churn. If you do what I explained above it should also result in the removal of the unnecessary error constructors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Jimbo4350> done 👍 Please rereview.

@smelc smelc force-pushed the smelc/reduce-boilerplate-modernize-TxValidationErrors branch from f42b8c6 to bfef03a Compare June 13, 2024 13:54
@smelc smelc changed the base branch from smelc/treasury-donations to main June 13, 2024 14:28
@smelc smelc force-pushed the smelc/reduce-boilerplate-modernize-TxValidationErrors branch from bfef03a to 4f9fda2 Compare June 13, 2024 15:35
@smelc smelc force-pushed the smelc/reduce-boilerplate-modernize-TxValidationErrors branch from cf4e491 to 639d6a0 Compare June 18, 2024 08:22
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! However you can remove the TxNotSupportedInAnyCardanoEraValidationError constructor.

data TxNotSupportedInEraValidationError era =
-- | First argument is the kind of data that is not supported.
-- Second argument is the era that doesn't support the data.
TxNotSupportedInAnyCardanoEraValidationError T.Text AnyCardanoEra
Copy link
Contributor

Choose a reason for hiding this comment

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

This constructor can actually be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, done 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Jimbo4350> not really, because this constructor is passed as a function to conjureWitness and there remains one single caller to conjureWitness that uses a CardanoEra era value.

Once this last caller is eliminated, we will be able to tighten TxNotSupportedInAnyCardanoEraValidationError's type.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could use shelleyBasedToCardanoEra.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Jimbo4350> but this is awkward because conjureWitness expects a function to build the error, to which it applies the CardanoEra. If I want to keep conjureWitness existing type (which I think is nice), I need to do something like this:

image

which is weird, because the value passed by conjureWitness gets ignored when building the error.

@smelc smelc force-pushed the smelc/reduce-boilerplate-modernize-TxValidationErrors branch from 639d6a0 to b843840 Compare June 18, 2024 08:53
@smelc smelc enabled auto-merge June 18, 2024 08:53
@smelc smelc added this pull request to the merge queue Jun 18, 2024
@smelc smelc removed this pull request from the merge queue due to a manual request Jun 18, 2024
@smelc smelc added this pull request to the merge queue Jun 18, 2024
Merged via the queue into main with commit fccfd72 Jun 18, 2024
22 checks passed
@smelc smelc deleted the smelc/reduce-boilerplate-modernize-TxValidationErrors branch June 18, 2024 11:12
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