Skip to content

Commit

Permalink
Replace HasFileMode typeclass with plain functions
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Apr 13, 2023
1 parent 47caf99 commit f7a5d49
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 49 deletions.
11 changes: 10 additions & 1 deletion cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ module Cardano.Api (
-- ** IO
File(..),
MapFile(..),
HasFileMode(..),
Directory(..),
FileDirection(..),

toFileIn,
toFileOut,

writeByteStringFileWithOwnerPermissions,
writeByteStringFile,
writeByteStringOutput,
Expand Down Expand Up @@ -808,6 +810,13 @@ module Cardano.Api (
parseFilePath,
parseFileOut,
parseDirectory,

toGenesisFileIn,
toGenesisFileOut,
toNetworkConfigFileIn,
toNetworkConfigFileOut,
toNodeConfigIn,
toNodeConfigOut,
) where

import Cardano.Api.Address
Expand Down
19 changes: 8 additions & 11 deletions cardano-api/src/Cardano/Api/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ module Cardano.Api.IO

, File(..)
, MapFile(..)
, HasFileMode(..)
, Directory(..)
, FileDirection(..)

, toFileIn
, toFileOut
) where

#if !defined(mingw32_HOST_OS)
Expand Down Expand Up @@ -52,14 +54,14 @@ import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Char8 as BSC
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy as LBSC
import Data.Kind (Type)
import Data.String (IsString)
import Data.Text (Text)
import qualified Data.Text.IO as Text
import qualified System.IO as IO
import System.IO (Handle)

import Cardano.Api.Error (FileError (..))
import Data.Coerce (coerce)

data FileDirection
= In
Expand Down Expand Up @@ -184,13 +186,8 @@ class MapFile a where
instance MapFile (File direction) where
mapFile f = File . f . unFile

class HasFileMode (f :: FileDirection -> Type) where
usingIn :: f 'InOut -> f 'In
usingOut :: f 'InOut -> f 'Out

instance HasFileMode File where
usingIn :: File 'InOut -> File 'In
usingIn = File . unFile
toFileIn :: File 'InOut -> File 'In
toFileIn = coerce

usingOut :: File 'InOut -> File 'Out
usingOut = File . unFile
toFileOut :: File 'InOut -> File 'Out
toFileOut = coerce
36 changes: 31 additions & 5 deletions cardano-api/src/Cardano/Api/LedgerState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ module Cardano.Api.LedgerState
, constructGlobals
, currentEpochEligibleLeadershipSlots
, nextEpochEligibleLeadershipSlots

, toGenesisFileIn
, toGenesisFileOut
, toNetworkConfigFileIn
, toNetworkConfigFileOut
, toNodeConfigIn
, toNodeConfigOut
)
where

Expand All @@ -74,7 +81,9 @@ import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Lazy as LB
import Data.ByteString.Short as BSS
import Data.Coerce (coerce)
import Data.Foldable
import Data.Function ((&))
import Data.IORef
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
Expand All @@ -86,6 +95,7 @@ import Data.Set (Set)
import qualified Data.Set as Set
import Data.Sharing (FromSharedCBOR, Interns, Share)
import Data.SOP.Strict (K (..), NP (..), fn, (:.:) (Comp))
import Data.String (IsString)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
Expand All @@ -102,7 +112,7 @@ import Cardano.Api.Block
import Cardano.Api.Certificate
import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.IO (File (..), FileDirection (..), HasFileMode, MapFile (..))
import Cardano.Api.IO (File (..), FileDirection (..), MapFile (..))
import Cardano.Api.IPC (ConsensusModeParams (..),
LocalChainSyncClient (LocalChainSyncClientPipelined),
LocalNodeClientProtocols (..), LocalNodeClientProtocolsInMode,
Expand Down Expand Up @@ -149,8 +159,6 @@ import Cardano.Slotting.EpochInfo (EpochInfo)
import qualified Cardano.Slotting.EpochInfo.API as Slot
import Cardano.Slotting.Slot (WithOrigin (At, Origin))
import qualified Cardano.Slotting.Slot as Slot
import Data.Function ((&))
import Data.String (IsString)
import qualified Ouroboros.Consensus.Block.Abstract as Consensus
import qualified Ouroboros.Consensus.Byron.Ledger.Block as Byron
import qualified Ouroboros.Consensus.Byron.Ledger.Ledger as Byron
Expand Down Expand Up @@ -792,6 +800,12 @@ data NodeConfig (direction :: FileDirection) = NodeConfig
, ncBabbageToConway :: !Consensus.TriggerHardFork
}

toNodeConfigIn :: NodeConfig 'InOut -> NodeConfig 'In
toNodeConfigIn = coerce

toNodeConfigOut :: NodeConfig 'InOut -> NodeConfig 'Out
toNodeConfigOut = coerce

instance FromJSON (NodeConfig direction) where
parseJSON =
Aeson.withObject "NodeConfig" parse
Expand Down Expand Up @@ -973,7 +987,13 @@ data ShelleyConfig = ShelleyConfig

newtype GenesisFile (direction :: FileDirection) = GenesisFile
{ unGenesisFile :: File direction
} deriving newtype (Eq, Ord, Show, IsString, HasFileMode, MapFile, FromJSON, ToJSON)
} deriving newtype (Eq, Ord, Show, IsString, MapFile, FromJSON, ToJSON)

toGenesisFileIn :: GenesisFile 'InOut -> GenesisFile 'In
toGenesisFileIn = coerce

toGenesisFileOut :: GenesisFile 'InOut -> GenesisFile 'Out
toGenesisFileOut = coerce

newtype GenesisHashByron = GenesisHashByron
{ unGenesisHashByron :: Text
Expand Down Expand Up @@ -1001,7 +1021,13 @@ newtype NetworkName = NetworkName

newtype NetworkConfigFile (direction :: FileDirection) = NetworkConfigFile
{ _unNetworkConfigFile :: File direction
} deriving newtype (Eq, Ord, Show, IsString, HasFileMode, MapFile, FromJSON, ToJSON)
} deriving newtype (Eq, Ord, Show, IsString, MapFile, FromJSON, ToJSON)

toNetworkConfigFileIn :: NetworkConfigFile 'InOut -> NetworkConfigFile 'In
toNetworkConfigFileIn = coerce

toNetworkConfigFileOut :: NetworkConfigFile 'InOut -> NetworkConfigFile 'Out
toNetworkConfigFileOut = coerce

newtype SocketPath = SocketPath
{ unSocketPath :: FilePath
Expand Down
22 changes: 20 additions & 2 deletions cardano-cli/src/Cardano/CLI/Byron/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ module Cardano.CLI.Byron.Key
, readPaymentVerificationKey
, renderByronKeyFailure
, byronWitnessToVerKey

, toNewVerificationKeyFileIn
, toNewVerificationKeyFileOut
, toNewSigningKeyFileIn
, toNewSigningKeyFileOut
)
where

Expand All @@ -23,6 +28,7 @@ import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT
right)
import qualified Data.ByteString as SB
import qualified Data.ByteString.UTF8 as UTF8
import Data.Coerce (coerce)
import Data.String (IsString, fromString)
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -63,11 +69,23 @@ renderByronKeyFailure err =

newtype NewSigningKeyFile (direction :: FileDirection) = NewSigningKeyFile
{ unNewSigningKeyFile :: File direction
} deriving newtype (Eq, Ord, Show, IsString, HasFileMode, MapFile, FromJSON, ToJSON)
} deriving newtype (Eq, Ord, Show, IsString, MapFile, FromJSON, ToJSON)

toNewSigningKeyFileIn :: NewSigningKeyFile 'InOut -> NewSigningKeyFile 'In
toNewSigningKeyFileIn = coerce

toNewSigningKeyFileOut :: NewSigningKeyFile 'InOut -> NewSigningKeyFile 'Out
toNewSigningKeyFileOut = coerce

newtype NewVerificationKeyFile (direction :: FileDirection) = NewVerificationKeyFile
{ unNewVerificationKeyFile :: File direction
} deriving newtype (Eq, Ord, Show, IsString, HasFileMode, MapFile, FromJSON, ToJSON)
} deriving newtype (Eq, Ord, Show, IsString, MapFile, FromJSON, ToJSON)

toNewVerificationKeyFileIn :: NewVerificationKeyFile 'InOut -> NewVerificationKeyFile 'In
toNewVerificationKeyFileIn = coerce

toNewVerificationKeyFileOut :: NewVerificationKeyFile 'InOut -> NewVerificationKeyFile 'Out
toNewVerificationKeyFileOut = coerce

-- | Print some invariant properties of a public key:
-- its hash and formatted view.
Expand Down
12 changes: 11 additions & 1 deletion cardano-cli/src/Cardano/CLI/Byron/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module Cardano.CLI.Byron.Tx
, toCborTxAux

, ScriptValidity(..)

, toNewTxFileIn
, toNewTxFileOut
)
where

Expand All @@ -32,6 +35,7 @@ import Data.Bifunctor (Bifunctor (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import Data.Coerce (coerce)
import qualified Data.List as List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -80,7 +84,13 @@ renderByronTxError err =

newtype NewTxFile (direction :: FileDirection) = NewTxFile
{ unNewTxFile :: File direction
} deriving newtype (Eq, Ord, Show, IsString, HasFileMode, MapFile, FromJSON, ToJSON)
} deriving newtype (Eq, Ord, Show, IsString, MapFile, FromJSON, ToJSON)

toNewTxFileIn :: NewTxFile 'InOut -> NewTxFile 'In
toNewTxFileIn = coerce

toNewTxFileOut :: NewTxFile 'InOut -> NewTxFile 'Out
toNewTxFileOut = coerce


-- | Pretty-print an address in its Base58 form, and also
Expand Down
24 changes: 21 additions & 3 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ module Cardano.CLI.Shelley.Commands
, BlockId (..)
, WitnessSigningData (..)
, ColdVerificationKeyOrFile (..)

, toOpCertCounterFileIn
, toOpCertCounterFileOut
, toWitnessFileIn
, toWitnessFileOut
) where

import Prelude

import Cardano.Api.Shelley

import Data.Coerce (coerce)
import Data.String (IsString)
import Data.Text (Text)

import Cardano.CLI.Shelley.Key (PaymentVerifier, StakeIdentifier, StakeVerifier,
Expand All @@ -60,7 +67,6 @@ import Cardano.CLI.Types

import Cardano.Chain.Common (BlockCount)
import Cardano.Ledger.Shelley.TxBody (MIRPot)
import Data.String (IsString)

--
-- Shelley CLI command data types
Expand Down Expand Up @@ -576,15 +582,27 @@ data CardanoAddressKeyType

newtype OpCertCounterFile (direction :: FileDirection) = OpCertCounterFile
{ unOpCertCounterFile :: File direction
} deriving newtype (Eq, Ord, Show, IsString, HasFileMode, MapFile, FromJSON, ToJSON)
} deriving newtype (Eq, Ord, Show, IsString, MapFile, FromJSON, ToJSON)

toOpCertCounterFileIn :: OpCertCounterFile 'InOut -> OpCertCounterFile 'In
toOpCertCounterFileIn = coerce

toOpCertCounterFileOut :: OpCertCounterFile 'InOut -> OpCertCounterFile 'Out
toOpCertCounterFileOut = coerce

newtype PrivKeyFile
= PrivKeyFile FilePath
deriving Show

newtype WitnessFile (direction :: FileDirection) = WitnessFile
{ unWitnessFile :: File direction
} deriving newtype (Eq, Ord, Show, IsString, HasFileMode, MapFile, FromJSON, ToJSON)
} deriving newtype (Eq, Ord, Show, IsString, MapFile, FromJSON, ToJSON)

toWitnessFileIn :: WitnessFile 'InOut -> WitnessFile 'In
toWitnessFileIn = coerce

toWitnessFileOut :: WitnessFile 'InOut -> WitnessFile 'Out
toWitnessFileOut = coerce

-- | A raw verification key given in Base64, and decoded into a ByteString.
newtype VerificationKeyBase64
Expand Down
22 changes: 11 additions & 11 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -827,18 +827,18 @@ createDelegateKeys dir index = do
liftIO $ createDirectoryIfMissing False dir
runGenesisKeyGenDelegate
(VerificationKeyFile $ File $ dir </> "delegate" ++ strIndex ++ ".vkey")
(usingOut @SigningKeyFile coldSK)
(usingOut @OpCertCounterFile opCertCtr)
(toSigningKeyFileOut coldSK)
(toOpCertCounterFileOut opCertCtr)
runGenesisKeyGenDelegateVRF
(VerificationKeyFile $ File $ dir </> "delegate" ++ strIndex ++ ".vrf.vkey")
(SigningKeyFile $ File $ dir </> "delegate" ++ strIndex ++ ".vrf.skey")
firstExceptT ShelleyGenesisCmdNodeCmdError $ do
runNodeKeyGenKES
(usingOut @VerificationKeyFile kesVK)
(toVerificationKeyFileOut kesVK)
(SigningKeyFile $ File $ dir </> "delegate" ++ strIndex ++ ".kes.skey")
runNodeIssueOpCert
(VerificationKeyFilePath (usingIn @VerificationKeyFile kesVK))
(usingIn @SigningKeyFile coldSK)
(VerificationKeyFilePath (toVerificationKeyFileIn kesVK))
(toSigningKeyFileIn coldSK)
opCertCtr
(KESPeriod 0)
(File $ dir </> "opcert" ++ strIndex ++ ".cert")
Expand Down Expand Up @@ -870,18 +870,18 @@ createPoolCredentials dir index = do
liftIO $ createDirectoryIfMissing False dir
firstExceptT ShelleyGenesisCmdNodeCmdError $ do
runNodeKeyGenKES
(usingOut @VerificationKeyFile kesVK)
(toVerificationKeyFileOut kesVK)
(SigningKeyFile $ File $ dir </> "kes" ++ strIndex ++ ".skey")
runNodeKeyGenVRF
(VerificationKeyFile $ File $ dir </> "vrf" ++ strIndex ++ ".vkey")
(SigningKeyFile $ File $ dir </> "vrf" ++ strIndex ++ ".skey")
runNodeKeyGenCold
(VerificationKeyFile $ File $ dir </> "cold" ++ strIndex ++ ".vkey")
(usingOut @SigningKeyFile coldSK)
(usingOut @OpCertCounterFile opCertCtr)
(toSigningKeyFileOut coldSK)
(toOpCertCounterFileOut opCertCtr)
runNodeIssueOpCert
(VerificationKeyFilePath (usingIn @VerificationKeyFile kesVK))
(usingIn @SigningKeyFile coldSK)
(VerificationKeyFilePath (toVerificationKeyFileIn kesVK))
(toSigningKeyFileIn coldSK)
opCertCtr
(KESPeriod 0)
(File $ dir </> "opcert" ++ strIndex ++ ".cert")
Expand Down Expand Up @@ -1005,7 +1005,7 @@ readShelleyGenesisWithDefault
-> (ShelleyGenesis StandardShelley -> ShelleyGenesis StandardShelley)
-> ExceptT ShelleyGenesisCmdError IO (ShelleyGenesis StandardShelley)
readShelleyGenesisWithDefault fpath adjustDefaults = do
newExceptT (readAndDecodeShelleyGenesis (usingIn @File fpath))
newExceptT (readAndDecodeShelleyGenesis (toFileIn fpath))
`catchError` \err ->
case err of
ShelleyGenesisCmdGenesisFileReadError (FileIOError _ ioe)
Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Node.hs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ runNodeIssueOpCert kesVerKeyOrFile

ocertIssueCounter <- firstExceptT ShelleyNodeCmdReadFileError
. newExceptT
$ readFileTextEnvelope AsOperationalCertificateIssueCounter (usingIn @File ocertCtrPath)
$ readFileTextEnvelope AsOperationalCertificateIssueCounter (toFileIn ocertCtrPath)

verKeyKes <- firstExceptT ShelleyNodeCmdReadKeyFileError
. newExceptT
Expand All @@ -221,7 +221,7 @@ runNodeIssueOpCert kesVerKeyOrFile
-- a new cert but without updating the counter.
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeLazyByteStringFile (usingOut @File ocertCtrPath)
$ writeLazyByteStringFile (toFileOut ocertCtrPath)
$ textEnvelopeToJSON (Just $ ocertCtrDesc $ getCounter nextOcertCtr) nextOcertCtr

firstExceptT ShelleyNodeCmdWriteFileError
Expand Down
Loading

0 comments on commit f7a5d49

Please sign in to comment.