|
5 | 5 | module Cardano.CLI.EraIndependent.Debug.CheckNodeConfiguration.Run (runCheckNodeConfig) where |
6 | 6 |
|
7 | 7 | import Cardano.Api |
8 | | -import Cardano.Api.Byron qualified as Byron |
9 | 8 |
|
10 | 9 | import Cardano.CLI.Compatible.Exception |
11 | 10 | import Cardano.CLI.EraIndependent.Debug.CheckNodeConfiguration.Command |
12 | | -import Cardano.CLI.Read qualified as Read |
13 | 11 | import Cardano.CLI.Type.Error.DebugCmdError |
14 | | -import Cardano.Crypto.Hash qualified as Crypto |
15 | 12 |
|
16 | | -import Control.Monad |
17 | | -import Data.Foldable (for_) |
18 | | -import Data.Text qualified as Text |
19 | | -import Data.Yaml qualified as Yaml |
20 | | -import System.FilePath |
| 13 | +import System.IO qualified as IO |
21 | 14 |
|
| 15 | +-- | Check a node configuration file by reading it the way 'readNodeConfig' |
| 16 | +-- does: every genesis file it names must exist, decode, and match the hash the |
| 17 | +-- configuration gives for it. |
22 | 18 | runCheckNodeConfig :: CheckNodeConfigCmdArgs -> CIO e () |
23 | 19 | runCheckNodeConfig (CheckNodeConfigCmdArgs configFile) = do |
24 | | - nodeConfig :: NodeConfig <- liftIO $ Yaml.decodeFileThrow configFilePath |
25 | | - checkNodeGenesisConfiguration configFile nodeConfig |
26 | | - liftIO $ putStrLn $ "Successfully checked node configuration file: " <> configFilePath |
| 20 | + _ <- |
| 21 | + fromExceptTCli . modifyError (DebugNodeConfigReadCmdError configFilePath) $ |
| 22 | + readNodeConfig configFile |
| 23 | + liftIO $ |
| 24 | + IO.hPutStrLn IO.stderr $ |
| 25 | + "Successfully checked node configuration file: " <> configFilePath |
27 | 26 | where |
28 | 27 | configFilePath = unFile configFile |
29 | | - |
30 | | -checkNodeGenesisConfiguration |
31 | | - :: NodeConfigFile 'In |
32 | | - -- ^ The node configuration file path. It's not read by this function, but used for producing error messages. |
33 | | - -> NodeConfig |
34 | | - -- ^ The parsed node configuration file |
35 | | - -> CIO e () |
36 | | -checkNodeGenesisConfiguration configFile nodeConfig = do |
37 | | - let byronGenFile = adjustFilepath $ ncByronGenesisFile nodeConfig |
38 | | - alonzoGenFile = adjustFilepath $ ncAlonzoGenesisFile nodeConfig |
39 | | - shelleyGenFile = adjustFilepath $ ncShelleyGenesisFile nodeConfig |
40 | | - conwayGenFile = adjustFilepath $ ncConwayGenesisFile nodeConfig |
41 | | - |
42 | | - liftIO $ putStrLn $ "Checking byron genesis file: " <> byronGenFile |
43 | | - |
44 | | - let mExpectedByronHash = unGenesisHashByron <$> ncByronGenesisHash nodeConfig |
45 | | - mExpectedAlonzoHash = Crypto.hashToTextAsHex . unGenesisHashAlonzo <$> ncAlonzoGenesisHash nodeConfig |
46 | | - mExpectedShelleyHash = Crypto.hashToTextAsHex . unGenesisHashShelley <$> ncShelleyGenesisHash nodeConfig |
47 | | - mExpectedConwayHash = Crypto.hashToTextAsHex . unGenesisHashConway <$> ncConwayGenesisHash nodeConfig |
48 | | - |
49 | | - (_, Byron.GenesisHash byronHash) <- fromExceptTCli $ Byron.readGenesisData byronGenFile |
50 | | - let actualByronHash = Text.pack $ show byronHash |
51 | | - actualAlonzoHash <- Crypto.hashToTextAsHex <$> Read.readShelleyOnwardsGenesisAndHash alonzoGenFile |
52 | | - actualShelleyHash <- Crypto.hashToTextAsHex <$> Read.readShelleyOnwardsGenesisAndHash shelleyGenFile |
53 | | - actualConwayHash <- Crypto.hashToTextAsHex <$> Read.readShelleyOnwardsGenesisAndHash conwayGenFile |
54 | | - |
55 | | - -- check only hashes which were specified for the genesis |
56 | | - for_ |
57 | | - [ (mExpectedByronHash, actualByronHash, byronGenFile) |
58 | | - , (mExpectedShelleyHash, actualShelleyHash, shelleyGenFile) |
59 | | - , (mExpectedAlonzoHash, actualAlonzoHash, alonzoGenFile) |
60 | | - , (mExpectedConwayHash, actualConwayHash, conwayGenFile) |
61 | | - ] |
62 | | - $ \(mExpected, actual, genFile) -> |
63 | | - for_ mExpected $ \expected -> |
64 | | - when (actual /= expected) $ |
65 | | - throwCliError $ |
66 | | - DebugNodeConfigWrongGenesisHashCmdError configFilePath genFile actual expected |
67 | | - where |
68 | | - configFilePath = unFile configFile |
69 | | - -- We make the genesis filepath relative to the node configuration file, like the node does: |
70 | | - -- https://github.com/IntersectMBO/cardano-node/blob/9671e7b6a1b91f5a530722937949b86deafaad43/cardano-node/src/Cardano/Node/Configuration/POM.hs#L668 |
71 | | - -- Note that, if the genesis filepath is absolute, the node configuration file's directory is ignored (by property of </>) |
72 | | - adjustFilepath (File f) = takeDirectory configFilePath </> f |
0 commit comments