Skip to content

Commit

Permalink
Merge pull request #930 from IntersectMBO/smelc/revert-908
Browse files Browse the repository at this point in the history
Revert "create-testnet-data: augment/check node configuration file for genesis hashes and paths"
  • Loading branch information
carbolymer authored Oct 14, 2024
2 parents 746a580 + 0fd2324 commit bf585b8
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 542 deletions.
1 change: 0 additions & 1 deletion cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ test-suite cardano-cli-test
type: exitcode-stdio-1.0
build-depends:
aeson,
aeson-pretty,
base16-bytestring,
bech32 >=1.1.0,
bytestring,
Expand Down
2 changes: 0 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ data GenesisCreateStakedCmdArgs era = GenesisCreateStakedCmdArgs

data GenesisCreateTestNetDataCmdArgs era = GenesisCreateTestNetDataCmdArgs
{ eon :: !(ShelleyBasedEra era)
, specNodeConfig :: !(Maybe FilePath)
-- ^ Path to the node configuration file to use. If unspecified, a default one will be used.
, specShelley :: !(Maybe FilePath)
-- ^ Path to the @genesis-shelley@ file to use. If unspecified, a default one will be used.
, specAlonzo :: !(Maybe FilePath)
Expand Down
7 changes: 1 addition & 6 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ pGenesisCreateTestNetData :: ShelleyBasedEra era -> EnvCli -> Parser (GenesisCmd
pGenesisCreateTestNetData sbe envCli =
fmap GenesisCreateTestNetData $
GenesisCreateTestNetDataCmdArgs sbe
<$> optional pNodeFile
<*> optional (pSpecFile "shelley")
<$> optional (pSpecFile "shelley")
<*> optional (pSpecFile "alonzo")
<*> optional (pSpecFile "conway")
<*> pNumGenesisKeys
Expand All @@ -235,10 +234,6 @@ pGenesisCreateTestNetData sbe envCli =
<*> pMaybeSystemStart
<*> pOutputDir
where
pNodeFile =
parseFilePath
"node-configuration"
"The node configuration file to use. Entries for hashes and paths of genesis files are checked if they exist. Otherwise they are filled in."
pSpecFile eraStr =
parseFilePath
("spec-" <> eraStr)
Expand Down
27 changes: 14 additions & 13 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,17 @@ runGenesisCreateCardanoCmd
writeFileGenesis (rootdir </> "conway-genesis.json") $ WritePretty conwayGenesis

liftIO $ do
forM_ mNodeConfigTemplate $ \nodeCfg -> do
let hashes =
Map.fromList
[ ("ByronGenesisHash", byronGenesisHash)
, ("ShelleyGenesisHash", shelleyGenesisHash)
, ("AlonzoGenesisHash", alonzoGenesisHash)
, ("ConwayGenesisHash", conwayGenesisHash)
]
writeGenesisHashesToNodeConfigFile nodeCfg hashes (rootdir </> "node-config.json")
case mNodeConfigTemplate of
Nothing -> pure ()
Just nodeCfg -> do
let hashes =
Map.fromList
[ ("ByronGenesisHash", byronGenesisHash)
, ("ShelleyGenesisHash", shelleyGenesisHash)
, ("AlonzoGenesisHash", alonzoGenesisHash)
, ("ConwayGenesisHash", conwayGenesisHash)
]
writeGenesisHashesToNodeConfigFile nodeCfg hashes (rootdir </> "node-config.json")
where
convertToShelleyError = withExceptT GenesisCmdByronError
convertGenesisKey :: Byron.SigningKey -> SigningKey GenesisExtendedKey
Expand Down Expand Up @@ -564,19 +566,18 @@ runGenesisCreateCardanoCmd
-- | @writeGenesisHashesToNodeConfigFile src hashes dest@ reads the node configuration file
-- at @src@ and the writes an augmented version of this file at @dest@, with the hashes.
writeGenesisHashesToNodeConfigFile
:: ()
=> MonadIO m
:: MonadIO m
=> FilePath
-- ^ From where to read the node configuration file
-> Map.Map Aeson.Key (Crypto.Hash h a)
-- ^ Key of an era's hash (like "ByronGenesisHash", "ShelleyGenesisHash", etc.), to the hash of its genesis file
-> FilePath
-- ^ Where to write the updated node config file
-> m ()
writeGenesisHashesToNodeConfigFile sourcePath hashes destinationPath = liftIO $ do
writeGenesisHashesToNodeConfigFile sourcePath hashes destinationPath = do
nodeConfig <- Yaml.decodeFileThrow sourcePath
let newConfig = foldr updateConfigHash nodeConfig $ Map.toList hashes
Aeson.encodeFile destinationPath newConfig
liftIO $ Aeson.encodeFile destinationPath newConfig
where
setHash field hash = Aeson.insert field $ Aeson.String $ Crypto.hashToTextAsHex hash
updateConfigHash :: (Aeson.Key, Crypto.Hash h a) -> Yaml.Value -> Yaml.Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,12 @@ import Cardano.CLI.Types.Errors.GenesisCmdError
import Cardano.CLI.Types.Errors.NodeCmdError
import Cardano.CLI.Types.Errors.StakePoolCmdError
import Cardano.CLI.Types.Key
import qualified Cardano.Crypto.Hash as Crypto
import Ouroboros.Consensus.Shelley.Node (ShelleyGenesisStaking (..))

import Control.DeepSeq (NFData, deepseq)
import Control.Monad (forM, forM_, unless, void, when)
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Encode.Pretty as Aeson
import qualified Data.Aeson.Key as Aeson
import qualified Data.Aeson.KeyMap as Aeson
import Data.Bifunctor (Bifunctor (..))
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.ListMap (ListMap (..))
import Data.Map.Strict (Map)
Expand All @@ -70,7 +65,6 @@ import Data.String (fromString)
import qualified Data.Text as Text
import Data.Tuple (swap)
import Data.Word (Word64)
import qualified Data.Yaml as Yaml
import GHC.Exts (IsList (..))
import GHC.Generics (Generic)
import GHC.Num (Natural)
Expand Down Expand Up @@ -176,7 +170,6 @@ runGenesisCreateTestNetDataCmd
Cmd.GenesisCreateTestNetDataCmdArgs
{ eon
, networkId
, specNodeConfig
, specShelley
, specAlonzo
, specConway
Expand Down Expand Up @@ -328,37 +321,9 @@ runGenesisCreateTestNetDataCmd
shelleyGenesis

-- Write genesis.json file to output
let conwayGenesisFilename = "conway-genesis.json"
shelleyGenesisFilename = "shelley-genesis.json"
alonzoGenesisFilename = "alonzo-genesis.json"
conwayGenesisPath = outputDir </> conwayGenesisFilename
shelleyGenesisPath = outputDir </> shelleyGenesisFilename
alonzoGenesisPath = outputDir </> alonzoGenesisFilename
liftIO $ LBS.writeFile conwayGenesisPath $ Aeson.encodePretty conwayGenesis'
liftIO $ LBS.writeFile shelleyGenesisPath $ Aeson.encodePretty shelleyGenesis'
liftIO $ LBS.writeFile alonzoGenesisPath $ Aeson.encodePretty alonzoGenesis

case specNodeConfig of
Nothing -> {- Don't do anything for now -} pure ()
Just inputNodeConfigPath -> do
let outputNodeConfigPath = outputDir </> "configuration.json"
addOrCheckHash k v = addOrCheck inputNodeConfigPath k (Crypto.hashToTextAsHex v)
addOrCheckPath k v = addOrCheck inputNodeConfigPath k (Text.pack v)
conwayGenesisHash <- getShelleyOnwardsGenesisHash conwayGenesisPath
shelleyGenesisHash <- getShelleyOnwardsGenesisHash shelleyGenesisPath
alonzoGenesisHash <- getShelleyOnwardsGenesisHash alonzoGenesisPath
nodeConfig <- Yaml.decodeFileThrow inputNodeConfigPath
nodeConfigToWrite <-
except $
-- Write hashs
addOrCheckHash "ConwayGenesisHash" conwayGenesisHash nodeConfig
>>= addOrCheckHash "ShelleyGenesisHash" shelleyGenesisHash
>>= addOrCheckHash "AlonzoGenesisHash" alonzoGenesisHash
-- Write paths
>>= addOrCheckPath "ConwayGenesisFile" conwayGenesisFilename
>>= addOrCheckPath "ShelleyGenesisFile" shelleyGenesisFilename
>>= addOrCheckPath "AlonzoGenesisFile" alonzoGenesisFilename
liftIO $ LBS.writeFile outputNodeConfigPath $ Aeson.encodePretty nodeConfigToWrite
liftIO $ LBS.writeFile (outputDir </> "conway-genesis.json") $ Aeson.encode conwayGenesis'
liftIO $ LBS.writeFile (outputDir </> "shelley-genesis.json") $ Aeson.encode shelleyGenesis'
liftIO $ LBS.writeFile (outputDir </> "alonzo-genesis.json") $ Aeson.encode alonzoGenesis
where
genesisDir = outputDir </> "genesis-keys"
delegateDir = outputDir </> "delegate-keys"
Expand All @@ -370,30 +335,6 @@ runGenesisCreateTestNetDataCmd
:: Delegation -> (L.KeyHash L.Staking L.StandardCrypto, L.PoolParams L.StandardCrypto)
mkDelegationMapEntry d = (dDelegStaking d, dPoolParams d)

-- @addOrCheck filepath key expectedValue obj @ checks
-- if @obj@ maps @key@. If it does, it checks that the value is @expectedValue@.
-- If @key@ is not mapped, the mapping @key -> expectedValue@ is inserted.
addOrCheck :: FilePath -> Aeson.Key -> Text.Text -> Yaml.Value -> Either GenesisCmdError Yaml.Value
addOrCheck filepath key expectedValue nodeConfig@(Aeson.Object obj) =
case Aeson.lookup key obj of
Nothing ->
-- Key of hash is not there, insert it
pure $ Aeson.Object $ Aeson.insert key (Aeson.String expectedValue) obj
Just (Aeson.String seen)
| seen == expectedValue ->
-- Hash is there and it's correct: no change
pure nodeConfig
Just (Aeson.String seen) ->
-- Hash is there, but it's incorrect: fail
Left $ GenesisCmdWrongGenesisHash filepath (Aeson.toText key) seen expectedValue
_ ->
Left $
GenesisCmdWrongNodeConfigFile
filepath
("Expected a String at key \"" <> Aeson.toText key <> "\", but found something else")
addOrCheck filepath _ _ _ =
Left $ GenesisCmdWrongNodeConfigFile filepath "Expected Object at the top-level"

addDRepsToConwayGenesis
:: [VerificationKey DRepKey]
-> [VerificationKey StakeKey]
Expand Down Expand Up @@ -467,15 +408,6 @@ runGenesisCreateTestNetDataCmd
rest <- mapAccumM f a' t
return $ h' : rest

--- | Read the given file and hashes it using 'Blake2b_256'
getShelleyOnwardsGenesisHash
:: MonadIO m
=> FilePath
-> m (Crypto.Hash Crypto.Blake2b_256 BS.ByteString)
getShelleyOnwardsGenesisHash path = do
content <- liftIO $ BS.readFile path
return $ Crypto.hashWith @Crypto.Blake2b_256 id content

-- | The output format used all along this file
desiredKeyOutputFormat :: KeyOutputFormat
desiredKeyOutputFormat = KeyOutputFormatTextEnvelope
Expand Down
23 changes: 0 additions & 23 deletions cardano-cli/src/Cardano/CLI/Types/Errors/GenesisCmdError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ data GenesisCmdError
!(VerificationKeyFile In)
!Text
!SomeAddressVerificationKey
| -- | @GenesisCmdWrongNodeConfigFile path error@ indicates
-- that the node configuration at @path@ is badly formed. @error@
-- gives details about the error
GenesisCmdWrongNodeConfigFile !FilePath !Text
| -- | @GenesisCmdWrongGenesisHash path key seen expected@ indicates
-- that the node configuration at @path@ has the wrong value @seen@ for @key@.
-- The value should be @expected@ instead.
GenesisCmdWrongGenesisHash !FilePath !Text !Text !Text
deriving Show

instance Error GenesisCmdError where
Expand Down Expand Up @@ -141,18 +133,3 @@ instance Error GenesisCmdError where
<> "."
<> "This is incorrect: the delegated supply should be less or equal to the total supply."
<> " Note that the total supply can either come from --total-supply or from the default template. Please fix what you use."
GenesisCmdWrongNodeConfigFile path err ->
"Node configuration file at "
<> pretty path
<> " is badly formed: "
<> pretty err
GenesisCmdWrongGenesisHash path key seen expected ->
"Hash associated to key \""
<> pretty key
<> "\" in file "
<> pretty path
<> " is wrong. The value in the file is "
<> pretty seen
<> " whereas "
<> pretty expected
<> " is expected."
Loading

0 comments on commit bf585b8

Please sign in to comment.