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

Add default path for GenesisFile and make path relative to configuration yaml file #648

Merged
merged 1 commit into from
Mar 19, 2020
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
2 changes: 2 additions & 0 deletions cardano-config/cardano-config.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ library
, cardano-shell
, cborg
, contra-tracer
, directory
, file-embed
, filepath
, generic-monoid
, iohk-monitoring
, lobemo-backend-aggregation
Expand Down
20 changes: 16 additions & 4 deletions cardano-config/src/Cardano/Config/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import qualified Data.IP as IP
import qualified Data.Text as T
import Data.Yaml (decodeFileThrow)
import Network.Socket (PortNumber)
import System.Directory (doesFileExist)
import System.FilePath ((</>), takeDirectory)

import qualified Cardano.Chain.Update as Update
import Cardano.BM.Data.Tracer (TracingVerbosity (..))
Expand Down Expand Up @@ -161,7 +163,7 @@ instance FromJSON NodeConfiguration where
parseJSON = withObject "NodeConfiguration" $ \v -> do
nId <- v .:? "NodeId"
ptcl <- v .: "Protocol" .!= RealPBFT
genFile <- v .: "GenesisFile"
genFile <- v .: "GenesisFile" .!= "configuration/genesis/genesis.json"
numCoreNode <- v .:? "NumCoreNodes"
rNetworkMagic <- v .:? "RequiresNetworkMagic" .!= RequiresNoMagic
pbftSignatureThresh <- v .:? "PBftSignatureThreshold"
Expand Down Expand Up @@ -241,9 +243,19 @@ instance FromJSON YamlSocketPath where
parseJSON invalid = panic $ "Parsing of SocketPath failed due to type mismatch. "
<> "Encountered: " <> (T.pack $ show invalid)

parseNodeConfigurationFP :: FilePath -> IO NodeConfiguration
parseNodeConfigurationFP fp = decodeFileThrow fp

parseNodeConfigurationFP :: ConfigYamlFilePath -> IO NodeConfiguration
parseNodeConfigurationFP (ConfigYamlFilePath fp) = do
nc <- decodeFileThrow fp
let genFile = unGenesisFile $ ncGenesisFile nc
exists <- doesFileExist genFile
case exists of
-- Genesis file is an absolute path
True -> pure nc
-- Genesis file is a relative path (relative to configuration yaml filepath)
False -> do let d = takeDirectory fp
pure $ nc { ncGenesisFile = GenesisFile $ d </> genFile }

-- TODO: Make genesisfile relative to configuration file as above.
Comment on lines +246 to +258
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unnecessary. All we need to do is takeDirectory fp </> genFile. That will make it relative to the config file's dir if the file is relative, but if it's absolute it'll remain unchanged.

There is no need to do this probing to see if files exist.

parseNodeConfiguration :: NodeProtocolMode -> IO NodeConfiguration
parseNodeConfiguration npm =
case npm of
Expand Down
2 changes: 1 addition & 1 deletion cardano-node/app/chairman.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ main = withIOManager $ \iocp -> do
, caDelegationCertFp
} <- execParser opts

nc <- liftIO . parseNodeConfigurationFP $ unConfigPath caConfigYaml
nc <- liftIO $ parseNodeConfigurationFP caConfigYaml
frmPtclRes <- runExceptT $ fromProtocol
(ncNodeId nc)
(ncNumCoreNodes nc)
Expand Down
2 changes: 1 addition & 1 deletion cardano-node/src/Cardano/CLI/Ops.hs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ getLocalTip
-> AssociateWithIOCP
-> IO ()
getLocalTip configFp mSockPath iocp = do
nc <- parseNodeConfigurationFP $ unConfigPath configFp
nc <- parseNodeConfigurationFP configFp
sockPath <- return $ chooseSocketPath (ncSocketPath nc) mSockPath

frmPtclRes <- runExceptT . firstExceptT ProtocolError
Expand Down
16 changes: 8 additions & 8 deletions cardano-node/src/Cardano/CLI/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ runCommand (ToVerification ptcl skFp (NewVerificationKeyFile vkFp)) = do
ensureNewFile TL.writeFile vkFp vKey

runCommand (IssueDelegationCertificate configFp epoch issuerSK delegateVK cert) = do
nc <- liftIO . parseNodeConfigurationFP $ unConfigPath configFp
nc <- liftIO $ parseNodeConfigurationFP configFp
vk <- readVerificationKey delegateVK
sk <- readSigningKey (ncProtocol nc) issuerSK
pmId <- readProtocolMagicId $ ncGenesisFile nc
Expand All @@ -271,14 +271,14 @@ runCommand (IssueDelegationCertificate configFp epoch issuerSK delegateVK cert)
ensureNewFileLBS (nFp cert) sCert

runCommand (CheckDelegation configFp cert issuerVF delegateVF) = do
nc <- liftIO . parseNodeConfigurationFP $ unConfigPath configFp
nc <- liftIO $ parseNodeConfigurationFP configFp
issuerVK <- readVerificationKey issuerVF
delegateVK <- readVerificationKey delegateVF
pmId <- readProtocolMagicId $ ncGenesisFile nc
checkByronGenesisDelegation cert pmId issuerVK delegateVK

runCommand (SubmitTx fp configFp mCliSockPath) = withIOManagerE $ \iocp -> do
nc <- liftIO . parseNodeConfigurationFP $ unConfigPath configFp
nc <- liftIO $ parseNodeConfigurationFP configFp
-- Default update value
let update = Update (ApplicationName "cardano-sl") 1 $ LastKnownBlockVersion 0 2 0
tx <- readByronTx fp
Expand All @@ -300,7 +300,7 @@ runCommand (SubmitTx fp configFp mCliSockPath) = withIOManagerE $ \iocp -> do
(ncProtocol nc)
tx
runCommand (SpendGenesisUTxO configFp (NewTxFile ctTx) ctKey genRichAddr outs) = do
nc <- liftIO . parseNodeConfigurationFP $ unConfigPath configFp
nc <- liftIO $ parseNodeConfigurationFP configFp
sk <- readSigningKey (ncProtocol nc) ctKey
-- Default update value
let update = Update (ApplicationName "cardano-sl") 1 $ LastKnownBlockVersion 0 2 0
Expand All @@ -323,7 +323,7 @@ runCommand (SpendGenesisUTxO configFp (NewTxFile ctTx) ctKey genRichAddr outs) =
ensureNewFileLBS ctTx $ toCborTxAux tx

runCommand (SpendUTxO configFp (NewTxFile ctTx) ctKey ins outs) = do
nc <- liftIO . parseNodeConfigurationFP $ unConfigPath configFp
nc <- liftIO $ parseNodeConfigurationFP configFp
sk <- readSigningKey (ncProtocol nc) ctKey
-- Default update value
let update = Update (ApplicationName "cardano-sl") 1 $ LastKnownBlockVersion 0 2 0
Expand All @@ -347,7 +347,7 @@ runCommand (SpendUTxO configFp (NewTxFile ctTx) ctKey ins outs) = do
ensureNewFileLBS ctTx $ toCborTxAux gTx

runCommand (GenerateTxs
logConfigFp
configFp
signingKey
delegCert
genFile
Expand All @@ -363,14 +363,14 @@ runCommand (GenerateTxs
sigKeysFiles) = withIOManagerE $ \iocp -> do
-- Default update value
let update = Update (ApplicationName "cardano-sl") 1 $ LastKnownBlockVersion 0 2 0
nc <- liftIO $ parseNodeConfigurationFP logConfigFp
nc <- liftIO . parseNodeConfigurationFP $ ConfigYamlFilePath configFp

-- Logging layer
(loggingLayer, _) <- firstExceptT (\(ConfigErrorFileNotFound fp) -> FileNotFoundError fp) $
createLoggingFeatureCLI
(pack $ showVersion version)
NoEnvironment
(Just logConfigFp)
(Just configFp)
(ncLogMetrics nc)

genHash <- getGenesisHashText genFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Cardano.Config.Protocol
, fromProtocol
)
import Cardano.Config.Types
( ConfigError (..)
( ConfigError (..), ConfigYamlFilePath (..)
, CardanoEnvironment(..), LastKnownBlockVersion(..)
, Protocol, SigningKeyFile(..), Update(..)
, ncLogMetrics, ncReqNetworkMagic, ncProtocol
Expand Down Expand Up @@ -79,7 +79,7 @@ runCommand (GenerateTxs logConfigFp
withIOManagerE $ \iocp -> do
-- Default update value
let update = Update (ApplicationName "cardano-tx-generator") 1 $ LastKnownBlockVersion 0 2 0
nc <- liftIO $ parseNodeConfigurationFP logConfigFp
nc <- liftIO . parseNodeConfigurationFP $ ConfigYamlFilePath logConfigFp

-- Logging layer
(loggingLayer, _) <- firstExceptT (\(ConfigErrorFileNotFound fp) -> FileNotFoundError fp) $
Expand Down