Skip to content

Commit

Permalink
Use new File type for tracking read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Mar 27, 2023
1 parent f56e0a7 commit 6bf2563
Show file tree
Hide file tree
Showing 55 changed files with 641 additions and 645 deletions.
2 changes: 1 addition & 1 deletion bench/tx-generator/src/Cardano/Benchmarking/Script/Core.hs
Expand Up @@ -90,7 +90,7 @@ setProtocolParameters s = case s of
protocolParameters <- liftIO $ readProtocolParametersFile file
setProtoParamMode $ ProtocolParameterLocal protocolParameters

readSigningKey :: String -> SigningKeyFile -> ActionM ()
readSigningKey :: String -> SigningKeyFile In -> ActionM ()
readSigningKey name filePath =
liftIO (readSigningKeyFile filePath) >>= \case
Left err -> liftTxGenError err
Expand Down
Expand Up @@ -50,7 +50,7 @@ data Action where
InitWallet :: !String -> Action
StartProtocol :: !FilePath -> !(Maybe FilePath) -> Action
Delay :: !Double -> Action
ReadSigningKey :: !String -> !SigningKeyFile -> Action
ReadSigningKey :: !String -> !(SigningKeyFile In) -> Action
DefineSigningKey :: !String -> !(SigningKey PaymentKey) -> Action
AddFund :: !AnyCardanoEra -> !String -> !TxIn -> !Lovelace -> !String -> Action
WaitBenchmark :: !String -> Action
Expand Down
10 changes: 5 additions & 5 deletions bench/tx-generator/src/Cardano/TxGenerator/Internal/Orphans.hs
Expand Up @@ -9,15 +9,15 @@ import Data.Aeson

import qualified Ouroboros.Network.Magic as Ouroboros (NetworkMagic (..))

import Cardano.Api (NetworkId (..))
import Cardano.Api (File (..), NetworkId (..))
import Cardano.CLI.Types (SigningKeyFile (..))


instance ToJSON SigningKeyFile where
toJSON (SigningKeyFile a) = toJSON a
instance ToJSON (SigningKeyFile direction) where
toJSON (SigningKeyFile (File a)) = toJSON a

instance FromJSON SigningKeyFile where
parseJSON a = SigningKeyFile <$> parseJSON a
instance FromJSON (SigningKeyFile direction) where
parseJSON a = SigningKeyFile . File <$> parseJSON a


instance ToJSON NetworkId where
Expand Down
Expand Up @@ -23,7 +23,7 @@ import Cardano.CLI.Types (SigningKeyFile (..))
import Cardano.Node.Configuration.NodeAddress (NodeIPv4Address)
import Cardano.Node.Types (AdjustFilePaths (..))

import Cardano.Api (AnyCardanoEra, Lovelace)
import Cardano.Api (AnyCardanoEra, File (..), In, Lovelace)
import Cardano.TxGenerator.Internal.Orphans ()
import Cardano.TxGenerator.Types

Expand All @@ -42,7 +42,7 @@ data NixServiceOptions = NixServiceOptions {
, _nix_plutus :: Maybe TxGenPlutusParams
, _nix_nodeConfigFile :: Maybe FilePath
, _nix_cardanoTracerSocket :: Maybe FilePath
, _nix_sigKey :: SigningKeyFile
, _nix_sigKey :: SigningKeyFile In
, _nix_localNodeSocketPath :: String
, _nix_targetNodes :: NonEmpty NodeIPv4Address
} deriving (Show, Eq)
Expand Down Expand Up @@ -70,7 +70,7 @@ instance AdjustFilePaths NixServiceOptions where
adjustFilePaths f opts
= opts {
_nix_nodeConfigFile = f <$> _nix_nodeConfigFile opts
, _nix_sigKey = SigningKeyFile . f . unSigningKeyFile $ _nix_sigKey opts
, _nix_sigKey = SigningKeyFile . File . f . unFile . unSigningKeyFile $ _nix_sigKey opts
}


Expand Down
Expand Up @@ -37,7 +37,7 @@ parseSigningKeyBase16 k
, teRawCBOR = addr
}

readSigningKeyFile :: SigningKeyFile -> IO (Either TxGenError (SigningKey PaymentKey))
readSigningKeyFile :: SigningKeyFile In -> IO (Either TxGenError (SigningKey PaymentKey))
readSigningKeyFile (SigningKeyFile f)
= first ApiError <$> readFileTextEnvelopeAnyOf acceptedTypes f

Expand Down
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Expand Up @@ -65,6 +65,7 @@ library
Cardano.Api.Eras
Cardano.Api.Error
Cardano.Api.Fees
Cardano.Api.IO
Cardano.Api.GenesisParameters
Cardano.Api.Hash
Cardano.Api.HasTypeProxy
Expand Down
15 changes: 8 additions & 7 deletions cardano-api/src/Cardano/Api/Keys/Read.hs
Expand Up @@ -17,6 +17,7 @@ import Data.List.NonEmpty (NonEmpty)
import Cardano.Api.DeserialiseAnyOf
import Cardano.Api.Error
import Cardano.Api.HasTypeProxy
import Cardano.Api.IO (File (..), In)
import Cardano.Api.SerialiseBech32
import Cardano.Api.SerialiseTextEnvelope
import Cardano.Api.Utils
Expand All @@ -28,25 +29,25 @@ import Cardano.Api.Utils
readKeyFile
:: AsType a
-> NonEmpty (InputFormat a)
-> FilePath
-> File In
-> IO (Either (FileError InputDecodeError) a)
readKeyFile asType acceptedFormats path = do
eContent <- fmap Right (readFileBlocking path) `catches` [Handler handler]
case eContent of
Left e -> return $ Left e
Right content ->
return . first (FileError path) $ deserialiseInput asType acceptedFormats content
return . first (FileError (unFile path)) $ deserialiseInput asType acceptedFormats content
where
handler :: IOException -> IO (Either (FileError InputDecodeError) BS.ByteString)
handler e = return . Left $ FileIOError path e
handler e = return . Left $ FileIOError (unFile path) e

-- | Read a cryptographic key from a file.
--
-- The contents of the file must be in the text envelope format.
readKeyFileTextEnvelope
:: HasTextEnvelope a
=> AsType a
-> FilePath
-> File In
-> IO (Either (FileError InputDecodeError) a)
readKeyFileTextEnvelope asType fp =
first toInputDecodeError <$> readFileTextEnvelope asType fp
Expand All @@ -71,15 +72,15 @@ readKeyFileAnyOf
:: forall b.
[FromSomeType SerialiseAsBech32 b]
-> [FromSomeType HasTextEnvelope b]
-> FilePath
-> File In
-> IO (Either (FileError InputDecodeError) b)
readKeyFileAnyOf bech32Types textEnvTypes path = do
eContent <- fmap Right (readFileBlocking path) `catches` [Handler handler]
case eContent of
Left e -> return $ Left e
Right content ->
return . first (FileError path) $ deserialiseInputAnyOf bech32Types textEnvTypes content
return . first (FileError (unFile path)) $ deserialiseInputAnyOf bech32Types textEnvTypes content
where
handler :: IOException -> IO (Either (FileError InputDecodeError) BS.ByteString)
handler e = return . Left $ FileIOError path e
handler e = return . Left $ FileIOError (unFile path) e

19 changes: 10 additions & 9 deletions cardano-api/src/Cardano/Api/SerialiseLedgerCddl.hs
Expand Up @@ -48,6 +48,7 @@ import qualified Cardano.Binary as CBOR
import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.HasTypeProxy
import Cardano.Api.IO (File (..), In, Out)
import Cardano.Api.SerialiseCBOR
import Cardano.Api.Tx
import Cardano.Api.Utils
Expand Down Expand Up @@ -211,23 +212,23 @@ deserialiseWitnessLedgerCddl era TextEnvelopeCddl{teCddlRawCBOR,teCddlDescriptio

writeTxFileTextEnvelopeCddl
:: IsCardanoEra era
=> FilePath
=> File Out
-> Tx era
-> IO (Either (FileError ()) ())
writeTxFileTextEnvelopeCddl path tx =
runExceptT $ do
handleIOExceptT (FileIOError path) $ LBS.writeFile path txJson
handleIOExceptT (FileIOError (unFile path)) $ LBS.writeFile (unFile path) txJson
where
txJson = encodePretty' textEnvelopeCddlJSONConfig (serialiseTxLedgerCddl tx) <> "\n"

writeTxWitnessFileTextEnvelopeCddl
:: ShelleyBasedEra era
-> FilePath
-> File Out
-> KeyWitness era
-> IO (Either (FileError ()) ())
writeTxWitnessFileTextEnvelopeCddl sbe path w =
runExceptT $ do
handleIOExceptT (FileIOError path) $ LBS.writeFile path txJson
handleIOExceptT (FileIOError (unFile path)) $ LBS.writeFile (unFile path) txJson
where
txJson = encodePretty' textEnvelopeCddlJSONConfig (serialiseWitnessLedgerCddl sbe w) <> "\n"

Expand Down Expand Up @@ -306,20 +307,20 @@ cddlTypeToEra unknownCddlType = Left $ TextEnvelopeCddlErrUnknownType unknownCdd

readFileTextEnvelopeCddlAnyOf
:: [FromSomeTypeCDDL TextEnvelopeCddl b]
-> FilePath
-> File In
-> IO (Either (FileError TextEnvelopeCddlError) b)
readFileTextEnvelopeCddlAnyOf types path =
runExceptT $ do
te <- newExceptT $ readTextEnvelopeCddlFromFile path
firstExceptT (FileError path) $ hoistEither $ do
firstExceptT (FileError (unFile path)) $ hoistEither $ do
deserialiseFromTextEnvelopeCddlAnyOf types te

readTextEnvelopeCddlFromFile
:: FilePath
:: File In
-> IO (Either (FileError TextEnvelopeCddlError) TextEnvelopeCddl)
readTextEnvelopeCddlFromFile path =
runExceptT $ do
bs <- handleIOExceptT (FileIOError path) $
bs <- handleIOExceptT (FileIOError (unFile path)) $
readFileBlocking path
firstExceptT (FileError path . TextEnvelopeCddlAesonDecodeError path)
firstExceptT (FileError (unFile path) . TextEnvelopeCddlAesonDecodeError (unFile path))
. hoistEither $ Aeson.eitherDecodeStrict' bs
38 changes: 19 additions & 19 deletions cardano-api/src/Cardano/Api/SerialiseTextEnvelope.hs
Expand Up @@ -60,6 +60,7 @@ import Cardano.Binary (DecoderError)

import Cardano.Api.Error
import Cardano.Api.HasTypeProxy
import Cardano.Api.IO (File (..), In, Out)
import Cardano.Api.SerialiseCBOR
import Cardano.Api.Utils (readFileBlocking)

Expand Down Expand Up @@ -227,7 +228,7 @@ deserialiseFromTextEnvelopeAnyOf types te =
matching (FromSomeType ttoken _f) = actualType == textEnvelopeType ttoken

writeFileWithOwnerPermissions
:: FilePath
:: File Out
-> LBS.ByteString
-> IO (Either (FileError ()) ())
#ifdef UNIX
Expand All @@ -241,17 +242,17 @@ writeFileWithOwnerPermissions path a = do
-- it will be immediately turned into a Handle (which will be closed when
-- the Handle is closed)
bracketOnError
(openFd path WriteOnly (Just ownerModes) defaultFileFlags)
(openFd (unFile path) WriteOnly (Just ownerModes) defaultFileFlags)
closeFd
(\fd -> setFdOwnerAndGroup fd user (-1) >> pure fd)
case ownedFile of
Left (err :: IOException) -> do
pure $ Left $ FileIOError path err
pure $ Left $ FileIOError (unFile path) err
Right fd -> do
bracket
(fdToHandle fd)
hClose
(\handle -> runExceptT $ handleIOExceptT (FileIOError path) $ LBS.hPut handle a)
(\handle -> runExceptT $ handleIOExceptT (FileIOError (unFile path)) $ LBS.hPut handle a)
#else
-- On something other than unix, we make a _new_ file, and since we created it,
-- we must own it. We then place it at the target location. Unfortunately this
Expand All @@ -272,20 +273,20 @@ writeFileWithOwnerPermissions targetPath a =
#endif

writeFileTextEnvelope :: HasTextEnvelope a
=> FilePath
=> File Out
-> Maybe TextEnvelopeDescr
-> a
-> IO (Either (FileError ()) ())
writeFileTextEnvelope path mbDescr a =
runExceptT $ do
handleIOExceptT (FileIOError path) $ LBS.writeFile path content
handleIOExceptT (FileIOError (unFile path)) $ LBS.writeFile (unFile path) content
where
content = textEnvelopeToJSON mbDescr a


writeFileTextEnvelopeWithOwnerPermissions
:: HasTextEnvelope a
=> FilePath
=> File Out
-> Maybe TextEnvelopeDescr
-> a
-> IO (Either (FileError ()) ())
Expand All @@ -301,45 +302,44 @@ textEnvelopeToJSON mbDescr a =

readFileTextEnvelope :: HasTextEnvelope a
=> AsType a
-> FilePath
-> File In
-> IO (Either (FileError TextEnvelopeError) a)
readFileTextEnvelope ttoken path =
runExceptT $ do
content <- handleIOExceptT (FileIOError path) $ readFileBlocking path
firstExceptT (FileError path) $ hoistEither $ do
content <- handleIOExceptT (FileIOError (unFile path)) $ readFileBlocking path
firstExceptT (FileError (unFile path)) $ hoistEither $ do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' content
deserialiseFromTextEnvelope ttoken te


readFileTextEnvelopeAnyOf :: [FromSomeType HasTextEnvelope b]
-> FilePath
-> File In
-> IO (Either (FileError TextEnvelopeError) b)
readFileTextEnvelopeAnyOf types path =
runExceptT $ do
content <- handleIOExceptT (FileIOError path) $ readFileBlocking path
firstExceptT (FileError path) $ hoistEither $ do
content <- handleIOExceptT (FileIOError (unFile path)) $ readFileBlocking path
firstExceptT (FileError (unFile path)) $ hoistEither $ do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' content
deserialiseFromTextEnvelopeAnyOf types te


readTextEnvelopeFromFile :: FilePath
-> IO (Either (FileError TextEnvelopeError) TextEnvelope)
readTextEnvelopeFromFile :: File In -> IO (Either (FileError TextEnvelopeError) TextEnvelope)
readTextEnvelopeFromFile path =
runExceptT $ do
bs <- handleIOExceptT (FileIOError path) $
bs <- handleIOExceptT (FileIOError (unFile path)) $
readFileBlocking path
firstExceptT (FileError path . TextEnvelopeAesonDecodeError)
firstExceptT (FileError (unFile path) . TextEnvelopeAesonDecodeError)
. hoistEither $ Aeson.eitherDecodeStrict' bs


readTextEnvelopeOfTypeFromFile
:: TextEnvelopeType
-> FilePath
-> File In
-> IO (Either (FileError TextEnvelopeError) TextEnvelope)
readTextEnvelopeOfTypeFromFile expectedType path =
runExceptT $ do
te <- ExceptT (readTextEnvelopeFromFile path)
firstExceptT (FileError path) $ hoistEither $
firstExceptT (FileError (unFile path)) $ hoistEither $
expectTextEnvelopeOfType expectedType te
return te

5 changes: 3 additions & 2 deletions cardano-api/src/Cardano/Api/Utils.hs
Expand Up @@ -52,6 +52,7 @@ import System.Directory (emptyPermissions, readable, setPermissions)
#endif

import Cardano.Api.Eras
import Cardano.Api.IO (File (..), In)
import Options.Applicative (ReadM)
import Options.Applicative.Builder (eitherReader)
import qualified Text.Read as Read
Expand Down Expand Up @@ -104,9 +105,9 @@ writeSecrets outDir prefix suffix secretOp xs =
setPermissions filename (emptyPermissions {readable = True})
#endif

readFileBlocking :: FilePath -> IO BS.ByteString
readFileBlocking :: File In -> IO BS.ByteString
readFileBlocking path = bracket
(openFileBlocking path ReadMode)
(openFileBlocking (unFile path) ReadMode)
hClose
(\fp -> do
-- An arbitrary block size.
Expand Down

0 comments on commit 6bf2563

Please sign in to comment.