Skip to content

Commit

Permalink
Use File type for config
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Mar 27, 2023
1 parent c9d4a1f commit 93b974f
Show file tree
Hide file tree
Showing 33 changed files with 458 additions and 410 deletions.
Expand Up @@ -19,7 +19,7 @@ import Cardano.Node.Configuration.POM
import Cardano.Node.Handlers.Shutdown (ShutdownConfig (..))
import Cardano.Node.Protocol.Cardano
import Cardano.Node.Protocol.Types (SomeConsensusProtocol (..))
import Cardano.Node.Types (ConfigYamlFilePath (..), GenesisFile,
import Cardano.Node.Types (ConfigYamlFilePath (..), File (..), GenesisFile,
NodeProtocolConfiguration (..), NodeShelleyProtocolConfiguration (..),
ProtocolFilepaths (..))
import Cardano.TxGenerator.Types
Expand Down Expand Up @@ -63,7 +63,7 @@ mkNodeConfig configFp_
$ first (TxGenError . ("mkNodeConfig: " ++))
$! makeNodeConfiguration (configYamlPc <> filesPc)
where
configFp = ConfigYamlFilePath configFp_
configFp = ConfigYamlFilePath $ File configFp_

filesPc :: PartialNodeConfiguration
filesPc = defaultPartialNodeConfiguration
Expand Down
101 changes: 53 additions & 48 deletions cardano-api/src/Cardano/Api/LedgerState.hs
Expand Up @@ -101,6 +101,7 @@ import Cardano.Api.Block
import Cardano.Api.Certificate
import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.IO (File (..), In, fileMap)
import Cardano.Api.IPC (ConsensusModeParams (..),
LocalChainSyncClient (LocalChainSyncClientPipelined),
LocalNodeClientProtocols (..), LocalNodeClientProtocolsInMode,
Expand Down Expand Up @@ -147,6 +148,7 @@ 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 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 @@ -724,7 +726,7 @@ rollBackLedgerStateHist hist maxInc = Seq.dropWhileL ((> maxInc) . (\(x,_,_) ->
--------------------------------------------------------------------------------

genesisConfigToEnv
:: GenesisConfig
:: GenesisConfig In
-> Either GenesisConfigError Env
genesisConfigToEnv
-- enp
Expand Down Expand Up @@ -752,25 +754,25 @@ genesisConfigToEnv
, envProtocolConfig = Consensus.topLevelConfigProtocol topLevelConfig
}

readNetworkConfig :: NetworkConfigFile -> ExceptT Text IO NodeConfig
readNetworkConfig :: NetworkConfigFile -> ExceptT Text IO (NodeConfig In)
readNetworkConfig (NetworkConfigFile ncf) = do
ncfg <- (except . parseNodeConfig) =<< readByteString ncf "node"
return ncfg
{ ncByronGenesisFile = adjustGenesisFilePath (mkAdjustPath ncf) (ncByronGenesisFile ncfg)
, ncShelleyGenesisFile = adjustGenesisFilePath (mkAdjustPath ncf) (ncShelleyGenesisFile ncfg)
, ncAlonzoGenesisFile = adjustGenesisFilePath (mkAdjustPath ncf) (ncAlonzoGenesisFile ncfg)
, ncConwayGenesisFile = adjustGenesisFilePath (mkAdjustPath ncf) (ncConwayGenesisFile ncfg)
{ ncByronGenesisFile = adjustGenesisFilePath (fileMap (mkAdjustPath ncf)) (ncByronGenesisFile ncfg)
, ncShelleyGenesisFile = adjustGenesisFilePath (fileMap (mkAdjustPath ncf)) (ncShelleyGenesisFile ncfg)
, ncAlonzoGenesisFile = adjustGenesisFilePath (fileMap (mkAdjustPath ncf)) (ncAlonzoGenesisFile ncfg)
, ncConwayGenesisFile = adjustGenesisFilePath (fileMap (mkAdjustPath ncf)) (ncConwayGenesisFile ncfg)
}

data NodeConfig = NodeConfig
data NodeConfig direction = NodeConfig
{ ncPBftSignatureThreshold :: !(Maybe Double)
, ncByronGenesisFile :: !GenesisFile
, ncByronGenesisFile :: !(GenesisFile direction)
, ncByronGenesisHash :: !GenesisHashByron
, ncShelleyGenesisFile :: !GenesisFile
, ncShelleyGenesisFile :: !(GenesisFile direction)
, ncShelleyGenesisHash :: !GenesisHashShelley
, ncAlonzoGenesisFile :: !GenesisFile
, ncAlonzoGenesisFile :: !(GenesisFile direction)
, ncAlonzoGenesisHash :: !GenesisHashAlonzo
, ncConwayGenesisFile :: !GenesisFile
, ncConwayGenesisFile :: !(GenesisFile direction)
, ncConwayGenesisHash :: !GenesisHashConway
, ncRequiresNetworkMagic :: !Cardano.Crypto.RequiresNetworkMagic
, ncByronSoftwareVersion :: !Cardano.Chain.Update.SoftwareVersion
Expand All @@ -788,11 +790,11 @@ data NodeConfig = NodeConfig
, ncBabbageToConway :: !Consensus.TriggerHardFork
}

instance FromJSON NodeConfig where
instance FromJSON (NodeConfig direction) where
parseJSON =
Aeson.withObject "NodeConfig" parse
where
parse :: Object -> Parser NodeConfig
parse :: Object -> Parser (NodeConfig direction)
parse o =
NodeConfig
<$> o .:? "PBftSignatureThreshold"
Expand Down Expand Up @@ -876,13 +878,13 @@ instance FromJSON NodeConfig where
-- intra-era fork, then the numbering is not consecutive.
----------------------------------------------------------------------

parseNodeConfig :: ByteString -> Either Text NodeConfig
parseNodeConfig :: ByteString -> Either Text (NodeConfig In)
parseNodeConfig bs =
case Yaml.decodeEither' bs of
Left err -> Left $ "Error parsing node config: " <> textShow err
Right nc -> Right nc

adjustGenesisFilePath :: (FilePath -> FilePath) -> GenesisFile -> GenesisFile
adjustGenesisFilePath :: (File direction -> File direction) -> GenesisFile direction -> GenesisFile direction
adjustGenesisFilePath f (GenesisFile p) = GenesisFile (f p)

mkAdjustPath :: FilePath -> (FilePath -> FilePath)
Expand All @@ -894,7 +896,7 @@ readByteString fp cfgType = ExceptT $
return $ Left $ mconcat
[ "Cannot read the ", cfgType, " configuration file at : ", Text.pack fp ]

initLedgerStateVar :: GenesisConfig -> LedgerState
initLedgerStateVar :: GenesisConfig In -> LedgerState
initLedgerStateVar genesisConfig = LedgerState
{ clsState = Ledger.ledgerState $ Consensus.pInfoInitLedger protocolInfo
}
Expand Down Expand Up @@ -954,9 +956,9 @@ toLedgerStateEvents lr = (ledgerState, ledgerEvents)


-- Usually only one constructor, but may have two when we are preparing for a HFC event.
data GenesisConfig
data GenesisConfig direction
= GenesisCardano
!NodeConfig
!(NodeConfig direction)
!Cardano.Chain.Genesis.Config
!ShelleyConfig
!AlonzoGenesis
Expand All @@ -967,8 +969,8 @@ data ShelleyConfig = ShelleyConfig
, scGenesisHash :: !GenesisHashShelley
}

newtype GenesisFile = GenesisFile
{ unGenesisFile :: FilePath
newtype GenesisFile direction = GenesisFile
{ unGenesisFile :: File direction
} deriving Show

newtype GenesisHashByron = GenesisHashByron
Expand Down Expand Up @@ -1003,12 +1005,12 @@ newtype SocketPath = SocketPath
{ unSocketPath :: FilePath
} deriving Show

mkProtocolInfoCardano ::
GenesisConfig ->
Consensus.ProtocolInfo
IO
(HFC.HardForkBlock
(Consensus.CardanoEras Consensus.StandardCrypto))
mkProtocolInfoCardano
:: GenesisConfig In
-> Consensus.ProtocolInfo
IO
(HFC.HardForkBlock
(Consensus.CardanoEras Consensus.StandardCrypto))
mkProtocolInfoCardano (GenesisCardano dnc byronGenesis shelleyGenesis alonzoGenesis conwayGenesis)
= Consensus.protocolInfoCardano
Consensus.ProtocolParamsByron
Expand Down Expand Up @@ -1058,16 +1060,16 @@ mkProtocolInfoCardano (GenesisCardano dnc byronGenesis shelleyGenesis alonzoGene
shelleyPraosNonce :: ShelleyConfig -> Shelley.Spec.Nonce
shelleyPraosNonce sCfg = Shelley.Spec.Nonce (Cardano.Crypto.Hash.Class.castHash . unGenesisHashShelley $ scGenesisHash sCfg)

shelleyProtVer :: NodeConfig -> Shelley.Spec.ProtVer
shelleyProtVer :: NodeConfig In -> Shelley.Spec.ProtVer
shelleyProtVer dnc =
let bver = ncByronProtocolVersion dnc in
Shelley.Spec.ProtVer
(fromIntegral $ Cardano.Chain.Update.pvMajor bver)
(fromIntegral $ Cardano.Chain.Update.pvMinor bver)

readCardanoGenesisConfig
:: NodeConfig
-> ExceptT GenesisConfigError IO GenesisConfig
:: NodeConfig In
-> ExceptT GenesisConfigError IO (GenesisConfig In)
readCardanoGenesisConfig enc =
GenesisCardano enc
<$> readByronGenesisConfig enc
Expand Down Expand Up @@ -1122,48 +1124,49 @@ data LookupFail
deriving (Eq, Show)

readByronGenesisConfig
:: NodeConfig
-> ExceptT GenesisConfigError IO Cardano.Chain.Genesis.Config
:: NodeConfig In
-> ExceptT GenesisConfigError IO Cardano.Chain.Genesis.Config
readByronGenesisConfig enc = do
let file = unGenesisFile $ ncByronGenesisFile enc
genHash <- firstExceptT NEError
. hoistEither
$ Cardano.Crypto.Hashing.decodeAbstractHash (unGenesisHashByron $ ncByronGenesisHash enc)
firstExceptT (NEByronConfig file)
$ Cardano.Chain.Genesis.mkConfigFromFile (ncRequiresNetworkMagic enc) file genHash
firstExceptT (NEByronConfig (unFile file))
$ Cardano.Chain.Genesis.mkConfigFromFile (ncRequiresNetworkMagic enc) (unFile file) genHash

readShelleyGenesisConfig
:: NodeConfig
:: NodeConfig In
-> ExceptT GenesisConfigError IO ShelleyConfig
readShelleyGenesisConfig enc = do
let file = unGenesisFile $ ncShelleyGenesisFile enc
firstExceptT (NEShelleyConfig file . renderShelleyGenesisError)
firstExceptT (NEShelleyConfig (unFile file) . renderShelleyGenesisError)
$ readShelleyGenesis (GenesisFile file) (ncShelleyGenesisHash enc)

readAlonzoGenesisConfig
:: NodeConfig
:: NodeConfig In
-> ExceptT GenesisConfigError IO AlonzoGenesis
readAlonzoGenesisConfig enc = do
let file = unGenesisFile $ ncAlonzoGenesisFile enc
firstExceptT (NEAlonzoConfig file . renderAlonzoGenesisError)
firstExceptT (NEAlonzoConfig (unFile file) . renderAlonzoGenesisError)
$ readAlonzoGenesis (GenesisFile file) (ncAlonzoGenesisHash enc)

readConwayGenesisConfig
:: NodeConfig
:: NodeConfig In
-> ExceptT GenesisConfigError IO (ConwayGenesis Shelley.StandardCrypto)
readConwayGenesisConfig enc = do
let file = unGenesisFile $ ncConwayGenesisFile enc
firstExceptT (NEConwayConfig file . renderConwayGenesisError)
firstExceptT (NEConwayConfig (unFile file) . renderConwayGenesisError)
$ readConwayGenesis (GenesisFile file) (ncConwayGenesisHash enc)

readShelleyGenesis
:: GenesisFile -> GenesisHashShelley
:: GenesisFile In
-> GenesisHashShelley
-> ExceptT ShelleyGenesisError IO ShelleyConfig
readShelleyGenesis (GenesisFile file) expectedGenesisHash = do
content <- handleIOExceptT (ShelleyGenesisReadError file . textShow) $ BS.readFile file
content <- handleIOExceptT (ShelleyGenesisReadError (unFile file) . textShow) $ BS.readFile (unFile file)
let genesisHash = GenesisHashShelley (Cardano.Crypto.Hash.Class.hashWith id content)
checkExpectedGenesisHash genesisHash
genesis <- firstExceptT (ShelleyGenesisDecodeError file . Text.pack)
genesis <- firstExceptT (ShelleyGenesisDecodeError (unFile file) . Text.pack)
. hoistEither
$ Aeson.eitherDecodeStrict' content
pure $ ShelleyConfig genesis genesisHash
Expand Down Expand Up @@ -1202,13 +1205,14 @@ renderShelleyGenesisError sge =
]

readAlonzoGenesis
:: GenesisFile -> GenesisHashAlonzo
:: GenesisFile In -> GenesisHashAlonzo
-> ExceptT AlonzoGenesisError IO AlonzoGenesis
readAlonzoGenesis (GenesisFile file) expectedGenesisHash = do
content <- handleIOExceptT (AlonzoGenesisReadError file . textShow) $ BS.readFile file
content <- BS.readFile (unFile file)
& handleIOExceptT (AlonzoGenesisReadError (unFile file) . textShow)
let genesisHash = GenesisHashAlonzo (Cardano.Crypto.Hash.Class.hashWith id content)
checkExpectedGenesisHash genesisHash
firstExceptT (AlonzoGenesisDecodeError file . Text.pack)
firstExceptT (AlonzoGenesisDecodeError (unFile file) . Text.pack)
. hoistEither
$ Aeson.eitherDecodeStrict' content
where
Expand Down Expand Up @@ -1246,13 +1250,14 @@ renderAlonzoGenesisError sge =
]

readConwayGenesis
:: GenesisFile -> GenesisHashConway
:: GenesisFile In
-> GenesisHashConway
-> ExceptT ConwayGenesisError IO (ConwayGenesis Shelley.StandardCrypto)
readConwayGenesis (GenesisFile file) expectedGenesisHash = do
content <- handleIOExceptT (ConwayGenesisReadError file . textShow) $ BS.readFile file
content <- handleIOExceptT (ConwayGenesisReadError (unFile file) . textShow) $ BS.readFile (unFile file)
let genesisHash = GenesisHashConway (Cardano.Crypto.Hash.Class.hashWith id content)
checkExpectedGenesisHash genesisHash
firstExceptT (ConwayGenesisDecodeError file . Text.pack)
firstExceptT (ConwayGenesisDecodeError (unFile file) . Text.pack)
. hoistEither
$ Aeson.eitherDecodeStrict' content
where
Expand Down
28 changes: 14 additions & 14 deletions cardano-cli/src/Cardano/CLI/Byron/Commands.hs
Expand Up @@ -35,16 +35,16 @@ data ByronCommand =
GenesisParameters

| PrintGenesisHash
GenesisFile
(GenesisFile In)

--- Key Related Commands ---
| Keygen
NewSigningKeyFile
(NewSigningKeyFile Out)

| ToVerification
ByronKeyFormat
(SigningKeyFile In)
NewVerificationKeyFile
(NewVerificationKeyFile Out)

| PrettySigningKeyPublic
ByronKeyFormat
Expand All @@ -53,7 +53,7 @@ data ByronCommand =
| MigrateDelegateKeyFrom
(SigningKeyFile In)
-- ^ Old key
NewSigningKeyFile
(NewSigningKeyFile Out)
-- ^ New Key

| PrintSigningKeyAddress
Expand All @@ -72,10 +72,10 @@ data ByronCommand =
-- ^ Filepath of transaction to submit.

| SpendGenesisUTxO
GenesisFile
(GenesisFile In)
NetworkId
ByronKeyFormat
NewTxFile
(NewTxFile Out)
-- ^ Filepath of the newly created transaction.
(SigningKeyFile In)
-- ^ Signing key of genesis UTxO owner.
Expand All @@ -86,7 +86,7 @@ data ByronCommand =
| SpendUTxO
NetworkId
ByronKeyFormat
NewTxFile
(NewTxFile Out)
-- ^ Filepath of the newly created transaction.
(SigningKeyFile In)
-- ^ Signing key of Tx underwriter.
Expand All @@ -102,35 +102,35 @@ data ByronCommand =
| ValidateCBOR
CBORObject
-- ^ Type of the CBOR object
FilePath
(File In)

| PrettyPrintCBOR
FilePath
(File In)
deriving Show


data NodeCmd = CreateVote
NetworkId
(SigningKeyFile In)
FilePath -- filepath to update proposal
(File In) -- filepath to update proposal
Bool
FilePath
(File Out)
| UpdateProposal
NetworkId
(SigningKeyFile In)
ProtocolVersion
SoftwareVersion
SystemTag
InstallerHash
FilePath
(File Out)
ByronProtocolParametersUpdate
| SubmitUpdateProposal
NetworkId
FilePath
(File In)
-- ^ Update proposal filepath.
| SubmitVote
NetworkId
FilePath
(File In)
-- ^ Vote filepath.
deriving Show

Expand Down

0 comments on commit 93b974f

Please sign in to comment.