Skip to content

Commit

Permalink
Adapt callers outside ProtocolParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Jan 15, 2025
1 parent cd62068 commit bb8f66f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
13 changes: 10 additions & 3 deletions cardano-api/internal/Cardano/Api/Governance/Poll.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}

-- | An API for driving on-chain poll for SPOs.
--
Expand Down Expand Up @@ -37,6 +36,9 @@ module Cardano.Api.Governance.Poll
)
where

import Cardano.Api.Pretty
import Cardano.Api.Error
import Cardano.Api.ProtocolParameters
import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eras
import Cardano.Api.Hash
Expand All @@ -56,7 +58,7 @@ import Cardano.Crypto.Hash (hashFromBytes, hashToBytes, hashWith)
import qualified Cardano.Crypto.Hash as Hash
import Cardano.Ledger.Crypto (HASH, StandardCrypto)

import Control.Arrow (left)
import Control.Arrow (first, left)
import Control.Monad (foldM, when)
import Data.Either.Combinators (maybeToRight)
import Data.Function ((&))
Expand Down Expand Up @@ -279,6 +281,7 @@ data GovernancePollError
| ErrGovernancePollUnauthenticated
| ErrGovernancePollMalformedAnswer DecoderError
| ErrGovernancePollInvalidAnswer GovernancePollInvalidAnswerError
| ErrGovernancePollCostModelNotEnoughParameters CostModelNotEnoughParametersError
deriving Show

data GovernancePollInvalidAnswerError = GovernancePollInvalidAnswerError
Expand Down Expand Up @@ -331,6 +334,9 @@ renderGovernancePollError err =
| (ix, answer) <- invalidAnswerAcceptableAnswers invalidAnswer
]
]
ErrGovernancePollCostModelNotEnoughParameters err ->
-- TODO can be simplified?
Text.pack $ docToString $ prettyError err

-- | Verify a poll against a given transaction and returns the signatories
-- (verification key only) when valid.
Expand All @@ -341,7 +347,8 @@ verifyPollAnswer
:: GovernancePoll
-> InAnyShelleyBasedEra Tx
-> Either GovernancePollError [Hash PaymentKey]
verifyPollAnswer poll (InAnyShelleyBasedEra _era (getTxBody -> TxBody body)) = do
verifyPollAnswer poll (InAnyShelleyBasedEra _era tx) = do
body <- first ErrGovernancePollCostModelNotEnoughParameters $ getTxBodyContent tx
answer <- extractPollAnswer (txMetadata body)
answer `hasMatchingHash` hashGovernancePoll poll
answer `isAmongAcceptableChoices` govPollAnswers poll
Expand Down
70 changes: 33 additions & 37 deletions cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ViewPatterns #-}

-- | Transaction bodies
module Cardano.Api.Tx.Body
( parseTxId

-- * Transaction bodies
, TxBody (.., TxBody)
, createTransactionBody
, createAndValidateTransactionBody
, TxBodyContent (..)
Expand Down Expand Up @@ -2148,12 +2146,8 @@ createAndValidateTransactionBody
-> Either TxBodyError (TxBody era)
createAndValidateTransactionBody = makeShelleyTransactionBody

pattern TxBody :: TxBodyContent ViewTx era -> TxBody era
pattern TxBody txbodycontent <- (getTxBodyContent -> txbodycontent)

{-# COMPLETE TxBody #-}

getTxBodyContent :: TxBody era -> TxBodyContent ViewTx era
getTxBodyContent
:: TxBody era -> Either CostModelNotEnoughParametersError (TxBodyContent ViewTx era)
getTxBodyContent = \case
ShelleyTxBody sbe body _scripts scriptdata mAux scriptValidity ->
fromLedgerTxBody sbe scriptValidity body scriptdata mAux
Expand All @@ -2164,34 +2158,36 @@ fromLedgerTxBody
-> Ledger.TxBody (ShelleyLedgerEra era)
-> TxBodyScriptData era
-> Maybe (L.TxAuxData (ShelleyLedgerEra era))
-> TxBodyContent ViewTx era
-> Either CostModelNotEnoughParametersError (TxBodyContent ViewTx era)
fromLedgerTxBody sbe scriptValidity body scriptdata mAux =
TxBodyContent
{ txIns = fromLedgerTxIns sbe body
, txInsCollateral = fromLedgerTxInsCollateral sbe body
, txInsReference = fromLedgerTxInsReference sbe body
, txOuts = fromLedgerTxOuts sbe body scriptdata
, txTotalCollateral = fromLedgerTxTotalCollateral sbe body
, txReturnCollateral = fromLedgerTxReturnCollateral sbe body
, txFee = fromLedgerTxFee sbe body
, txValidityLowerBound = fromLedgerTxValidityLowerBound sbe (A.TxBody body)
, txValidityUpperBound = fromLedgerTxValidityUpperBound sbe (A.TxBody body)
, txWithdrawals = fromLedgerTxWithdrawals sbe body
, txCertificates = fromLedgerTxCertificates sbe body
, txUpdateProposal = maybeFromLedgerTxUpdateProposal sbe body
, txMintValue = fromLedgerTxMintValue sbe body
, txExtraKeyWits = fromLedgerTxExtraKeyWitnesses sbe body
, txProtocolParams = ViewTx
, txMetadata
, txAuxScripts
, txScriptValidity = scriptValidity
, txProposalProcedures = fromLedgerProposalProcedures sbe body
, txVotingProcedures = fromLedgerVotingProcedures sbe body
, txCurrentTreasuryValue = fromLedgerCurrentTreasuryValue sbe body
, txTreasuryDonation = fromLedgerTreasuryDonation sbe body
}
txUpdateProposal <&> \txup ->
TxBodyContent
{ txIns = fromLedgerTxIns sbe body
, txInsCollateral = fromLedgerTxInsCollateral sbe body
, txInsReference = fromLedgerTxInsReference sbe body
, txOuts = fromLedgerTxOuts sbe body scriptdata
, txTotalCollateral = fromLedgerTxTotalCollateral sbe body
, txReturnCollateral = fromLedgerTxReturnCollateral sbe body
, txFee = fromLedgerTxFee sbe body
, txValidityLowerBound = fromLedgerTxValidityLowerBound sbe (A.TxBody body)
, txValidityUpperBound = fromLedgerTxValidityUpperBound sbe (A.TxBody body)
, txWithdrawals = fromLedgerTxWithdrawals sbe body
, txCertificates = fromLedgerTxCertificates sbe body
, txUpdateProposal = txup
, txMintValue = fromLedgerTxMintValue sbe body
, txExtraKeyWits = fromLedgerTxExtraKeyWitnesses sbe body
, txProtocolParams = ViewTx
, txMetadata
, txAuxScripts
, txScriptValidity = scriptValidity
, txProposalProcedures = fromLedgerProposalProcedures sbe body
, txVotingProcedures = fromLedgerVotingProcedures sbe body
, txCurrentTreasuryValue = fromLedgerCurrentTreasuryValue sbe body
, txTreasuryDonation = fromLedgerTreasuryDonation sbe body
}
where
(txMetadata, txAuxScripts) = fromLedgerTxAuxiliaryData sbe mAux
txUpdateProposal = maybeFromLedgerTxUpdateProposal sbe body

fromLedgerProposalProcedures
:: ShelleyBasedEra era
Expand Down Expand Up @@ -2544,15 +2540,15 @@ maybeFromLedgerTxUpdateProposal
:: ()
=> ShelleyBasedEra era
-> Ledger.TxBody (ShelleyLedgerEra era)
-> TxUpdateProposal era
-> Either CostModelNotEnoughParametersError (TxUpdateProposal era)
maybeFromLedgerTxUpdateProposal sbe body =
caseShelleyToBabbageOrConwayEraOnwards
( \w ->
case body ^. L.updateTxBodyL of
SNothing -> TxUpdateProposalNone
SJust p -> TxUpdateProposal w (fromLedgerUpdate sbe p)
SNothing -> pure TxUpdateProposalNone

Check notice

Code scanning / HLint

Redundant bracket Note

cardano-api/internal/Cardano/Api/Tx/Body.hs:2548:45-68: Suggestion: Redundant bracket
  
Found:
  TxUpdateProposal w <$> (fromLedgerUpdate sbe p)
  
Perhaps:
  TxUpdateProposal w <$> fromLedgerUpdate sbe p
SJust p -> TxUpdateProposal w <$> (fromLedgerUpdate sbe p)
)
(const TxUpdateProposalNone)
(const $ pure TxUpdateProposalNone)
sbe

fromLedgerTxMintValue
Expand Down

0 comments on commit bb8f66f

Please sign in to comment.