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

Condense Read and Validation modules in cardano-cli #4516

Merged
merged 6 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions bench/tx-generator/src/Cardano/Benchmarking/PlutusExample.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Cardano.Benchmarking.PlutusExample
where
import Prelude
import qualified Data.Map as Map
import Prelude

import Control.Monad.Trans.Except
import qualified Data.ByteString.Char8 as BSC

import Cardano.CLI.Shelley.Script (readFileScriptInAnyLang)

import Cardano.Api
import Cardano.Api.Shelley ( ProtocolParameters(..), PlutusScript(..)
, fromAlonzoExUnits, protocolParamCostModels, toPlutusData)
import Cardano.Api.Shelley (PlutusScript (..), ProtocolParameters (..), fromAlonzoExUnits,
protocolParamCostModels, toPlutusData)
import Cardano.Ledger.Alonzo.TxInfo (exBudgetToExUnits)

import qualified Plutus.V1.Ledger.Api as Plutus
import Plutus.V1.Ledger.Contexts (ScriptContext(..), ScriptPurpose(..), TxInfo(..), TxOutRef(..))
import Plutus.V1.Ledger.Contexts (ScriptContext (..), ScriptPurpose (..), TxInfo (..),
TxOutRef (..))

import Cardano.CLI.Shelley.Run.Read

readScript :: FilePath -> IO (Script PlutusScriptV1)
readScript fp = do
Expand Down
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ module Cardano.Api (
-- ** Misc
ScriptLockedTxInsError(..),
TxInsExistError(..),
renderEra,
renderNotScriptLockedTxInsError,
renderTxInsExistError,
txInsExistInUTxO,
Expand Down
13 changes: 13 additions & 0 deletions cardano-api/src/Cardano/Api/Utils.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

#if !defined(mingw32_HOST_OS)
Expand All @@ -17,6 +18,7 @@ module Cardano.Api.Utils
, note
, parseFilePath
, readFileBlocking
, renderEra
, runParsecParser
, textShow
, writeSecrets
Expand Down Expand Up @@ -47,6 +49,8 @@ import System.Posix.Files (ownerReadMode, setFileMode)
import System.Directory (emptyPermissions, readable, setPermissions)
#endif

import Cardano.Api.Eras

(?!) :: Maybe a -> e -> Either e a
Nothing ?! e = Left e
Just x ?! _ = Right x
Expand Down Expand Up @@ -121,3 +125,12 @@ readFileBlocking path = bracket

textShow :: Show a => a -> Text
textShow = Text.pack . show

renderEra :: AnyCardanoEra -> Text
renderEra (AnyCardanoEra ByronEra) = "Byron"
renderEra (AnyCardanoEra ShelleyEra) = "Shelley"
renderEra (AnyCardanoEra AllegraEra) = "Allegra"
renderEra (AnyCardanoEra MaryEra) = "Mary"
renderEra (AnyCardanoEra AlonzoEra) = "Alonzo"
renderEra (AnyCardanoEra BabbageEra) = "Babbage"

3 changes: 2 additions & 1 deletion cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ library
Cardano.CLI.Shelley.Run.StakeAddress
Cardano.CLI.Shelley.Run.TextView
Cardano.CLI.Shelley.Run.Transaction
Cardano.CLI.Shelley.Script
Cardano.CLI.Shelley.Run.Read
Cardano.CLI.Shelley.Run.Validate

Cardano.CLI.TopHandler

Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import Cardano.CLI.Shelley.Key (InputDecodeError, PaymentVerifier (..)
readVerificationKeyTextOrFileAnyOf, renderVerificationKeyTextOrFileError)
import Cardano.CLI.Shelley.Parsers (AddressCmd (..), AddressKeyType (..), OutputFile (..))
import Cardano.CLI.Shelley.Run.Address.Info (ShelleyAddressInfoError, runAddressInfo)
import Cardano.CLI.Shelley.Script
import Cardano.CLI.Shelley.Run.Read
import Cardano.CLI.Types

data ShelleyAddressCmdError
Expand Down
41 changes: 40 additions & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Run/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ module Cardano.CLI.Shelley.Run.Genesis
, readAndDecodeShelleyGenesis
, readAlonzoGenesis
, runGenesisCmd

-- * Protocol Parameters
, ProtocolParamsError(..)
, renderProtocolParamsError
, readProtocolParameters
, readProtocolParametersSourceSpec
) where

import Cardano.Prelude hiding (unlines)
Expand Down Expand Up @@ -104,7 +110,7 @@ import Cardano.Chain.Common (BlockCount (unBlockCount))
import Cardano.Chain.Delegation (delegateVK)
import qualified Cardano.Chain.Delegation as Dlg
import qualified Cardano.Chain.Genesis as Genesis
import Cardano.Chain.Update
import Cardano.Chain.Update hiding (ProtocolParameters)
import Cardano.Slotting.Slot (EpochSize (EpochSize))
import Data.Fixed (Fixed (MkFixed))
import qualified Data.Yaml as Yaml
Expand Down Expand Up @@ -1314,3 +1320,36 @@ readAlonzoGenesis fpath = do
lbs <- handleIOExceptT (ShelleyGenesisCmdGenesisFileError . FileIOError fpath) $ LBS.readFile fpath
firstExceptT (ShelleyGenesisCmdAesonDecodeError fpath . Text.pack)
. hoistEither $ Aeson.eitherDecode' lbs


-- Protocol Parameters

data ProtocolParamsError
= ProtocolParamsErrorFile (FileError ())
| ProtocolParamsErrorJSON !FilePath !Text
| ProtocolParamsErrorGenesis !ShelleyGenesisCmdError

renderProtocolParamsError :: ProtocolParamsError -> Text
renderProtocolParamsError (ProtocolParamsErrorFile fileErr) =
Text.pack $ displayError fileErr
renderProtocolParamsError (ProtocolParamsErrorJSON fp jsonErr) =
"Error while decoding the protocol parameters at: " <> Text.pack fp <> " Error: " <> jsonErr
renderProtocolParamsError (ProtocolParamsErrorGenesis err) =
Text.pack $ displayError err

readProtocolParametersSourceSpec :: ProtocolParamsSourceSpec
-> ExceptT ProtocolParamsError IO ProtocolParameters
readProtocolParametersSourceSpec (ParamsFromGenesis (GenesisFile f)) =
fromShelleyPParams . sgProtocolParams
<$> firstExceptT ProtocolParamsErrorGenesis (readShelleyGenesisWithDefault f id)
readProtocolParametersSourceSpec (ParamsFromFile f) = readProtocolParameters f

--TODO: eliminate this and get only the necessary params, and get them in a more
-- helpful way rather than requiring them as a local file.
readProtocolParameters :: ProtocolParamsFile
-> ExceptT ProtocolParamsError IO ProtocolParameters
readProtocolParameters (ProtocolParamsFile fpath) = do
pparams <- handleIOExceptT (ProtocolParamsErrorFile . FileIOError fpath) $ LBS.readFile fpath
firstExceptT (ProtocolParamsErrorJSON fpath . Text.pack) . hoistEither $
Aeson.eitherDecode' pparams

Loading