Skip to content

Commit db93ac1

Browse files
committed
Fix bug in runGenesisCreateCardano (cardano-cli)
* The hash that was used for the genesis files was bad. * Also: Use value passed with '--gen-utxo-keys'.
1 parent e299e80 commit db93ac1

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

cardano-cli/src/Cardano/CLI/Shelley/Run/Genesis.hs

+32-35
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE DataKinds #-}
44
{-# LANGUAGE DeriveAnyClass #-}
55
{-# LANGUAGE DeriveGeneric #-}
6+
{-# LANGUAGE FlexibleContexts #-}
67
{-# LANGUAGE GADTs #-}
78
{-# LANGUAGE ScopedTypeVariables #-}
89
{-# LANGUAGE TupleSections #-}
@@ -114,6 +115,7 @@ import Cardano.Slotting.Slot (EpochSize (EpochSize))
114115
import Data.Fixed (Fixed (MkFixed))
115116
import qualified Data.Yaml as Yaml
116117
import Text.JSON.Canonical (parseCanonicalJSON, renderCanonicalJSON)
118+
import qualified Text.JSON.Canonical (ToJSON)
117119

118120
import Data.ListMap (ListMap (..))
119121

@@ -412,8 +414,8 @@ runGenesisCreate (GenesisDir rootdir)
412414
-- Shelley genesis parameters
413415
start genDlgs mAmount utxoAddrs mempty (Lovelace 0) [] [] template
414416

415-
writeFileGenesis (rootdir </> "genesis.json") shelleyGenesis
416-
writeFileGenesis (rootdir </> "genesis.alonzo.json") alonzoGenesis
417+
void $ writeFileGenesis (rootdir </> "genesis.json") $ WritePretty shelleyGenesis
418+
void $ writeFileGenesis (rootdir </> "genesis.alonzo.json") $ WritePretty alonzoGenesis
417419
--TODO: rationalise the naming convention on these genesis json files.
418420
where
419421
adjustTemplate t = t { sgNetworkMagic = unNetworkMagic (toNetworkMagic network) }
@@ -501,7 +503,7 @@ runGenesisCreateCardano :: GenesisDir
501503
-> Maybe FilePath
502504
-> ExceptT ShelleyGenesisCmdError IO ()
503505
runGenesisCreateCardano (GenesisDir rootdir)
504-
genNumGenesisKeys _genNumUTxOKeys
506+
genNumGenesisKeys genNumUTxOKeys
505507
mStart mAmount mSecurity slotLength mSlotCoeff
506508
network byronGenesisT shelleyGenesisT alonzoGenesisT mNodeCfg = do
507509
start <- maybe (SystemStart <$> getCurrentTimePlus30) pure mStart
@@ -577,39 +579,22 @@ runGenesisCreateCardano (GenesisDir rootdir)
577579
writeSecrets deldir "shelley" "opcert.json" toOpCert opCerts
578580
writeSecrets deldir "shelley" "counter.json" toCounter opCerts
579581

580-
LBS.writeFile (rootdir </> "byron-genesis.json") (canonicalEncodePretty byronGenesis)
581-
writeFileGenesis (rootdir </> "shelley-genesis.json") shelleyGenesis
582-
writeFileGenesis (rootdir </> "alonzo-genesis.json") alonzoGenesis
582+
byronGenesisHash <- writeFileGenesis (rootdir </> "byron-genesis.json") $ WriteCanonical byronGenesis
583+
shelleyGenesisHash <- writeFileGenesis (rootdir </> "shelley-genesis.json") $ WritePretty shelleyGenesis
584+
alonzoGenesisHash <- writeFileGenesis (rootdir </> "alonzo-genesis.json") $ WritePretty alonzoGenesis
583585

584586
liftIO $ do
585587
case mNodeCfg of
586588
Nothing -> pure ()
587589
Just nodeCfg -> do
588590
nodeConfig <- Yaml.decodeFileThrow nodeCfg
589591
let
590-
hashShelleyGenesis :: ToJSON genesis => genesis -> Text
591-
hashShelleyGenesis genesis = Crypto.hashToTextAsHex gh
592-
where
593-
content :: ByteString
594-
content = LBS.toStrict $ encodePretty genesis
595-
gh :: Crypto.Hash Crypto.Blake2b_256 ByteString
596-
gh = Crypto.hashWith id content
597-
hashByronGenesis :: Genesis.GenesisData -> Text
598-
hashByronGenesis genesis = Crypto.hashToTextAsHex genesisHash
599-
where
600-
genesisHash :: Crypto.Hash Crypto.Blake2b_256 ByteString
601-
genesisHash = Crypto.hashWith id
602-
. LBS.toStrict
603-
. renderCanonicalJSON
604-
. either (error "error parsing json that was just encoded!?") identity
605-
. parseCanonicalJSON
606-
. canonicalEncodePretty $ genesis
607-
-- TODO, NodeConfig needs a ToJSON instance
592+
setHash field hash = Aeson.insert field $ String $ Crypto.hashToTextAsHex hash
608593
updateConfig :: Yaml.Value -> Yaml.Value
609594
updateConfig (Object obj) = Object
610-
$ (Aeson.insert "ByronGenesisHash" . String . hashByronGenesis) byronGenesis
611-
$ (Aeson.insert "ShelleyGenesisHash" . String . hashShelleyGenesis) shelleyGenesis
612-
$ (Aeson.insert "AlonzoGenesisHash" . String . hashShelleyGenesis) alonzoGenesis
595+
$ setHash "ByronGenesisHash" byronGenesisHash
596+
$ setHash "ShelleyGenesisHash" shelleyGenesisHash
597+
$ setHash "AlonzoGenesisHash" alonzoGenesisHash
613598
obj
614599
updateConfig x = x
615600
newConfig :: Yaml.Value
@@ -636,7 +621,7 @@ runGenesisCreateCardano (GenesisDir rootdir)
636621
(toByronRequiresNetworkMagic network)
637622
byronBalance = TestnetBalanceOptions
638623
{ tboRichmen = genNumGenesisKeys
639-
, tboPoors = 1
624+
, tboPoors = genNumUTxOKeys
640625
, tboTotalBalance = fromMaybe zeroLovelace $ toByronLovelace (fromMaybe 0 mAmount)
641626
, tboRichmenShare = 0
642627
}
@@ -751,7 +736,7 @@ runGenesisCreateStaked (GenesisDir rootdir)
751736

752737
liftIO $ LBS.writeFile (rootdir </> "genesis.json") $ Aeson.encode shelleyGenesis
753738

754-
writeFileGenesis (rootdir </> "genesis.alonzo.json") alonzoGenesis
739+
void $ writeFileGenesis (rootdir </> "genesis.alonzo.json") $ WritePretty alonzoGenesis
755740
--TODO: rationalise the naming convention on these genesis json files.
756741

757742
liftIO $ Text.putStrLn $ mconcat $
@@ -1160,13 +1145,25 @@ updateCreateStakedOutputTemplate
11601145
unLovelace (Lovelace coin) = fromIntegral coin
11611146

11621147
writeFileGenesis
1163-
:: ToJSON genesis
1164-
=> FilePath
1165-
-> genesis
1166-
-> ExceptT ShelleyGenesisCmdError IO ()
1167-
writeFileGenesis fpath genesis =
1148+
:: FilePath
1149+
-> WriteFileGenesis
1150+
-> ExceptT ShelleyGenesisCmdError IO (Crypto.Hash Crypto.Blake2b_256 ByteString)
1151+
writeFileGenesis fpath genesis = do
11681152
handleIOExceptT (ShelleyGenesisCmdGenesisFileError . FileIOError fpath) $
1169-
LBS.writeFile fpath (Aeson.encode genesis)
1153+
BS.writeFile fpath content
1154+
return $ Crypto.hashWith id content
1155+
where
1156+
content = case genesis of
1157+
WritePretty a -> LBS.toStrict $ encodePretty a
1158+
WriteCanonical a -> LBS.toStrict
1159+
. renderCanonicalJSON
1160+
. either (error "error parsing json that was just encoded!?") identity
1161+
. parseCanonicalJSON
1162+
. canonicalEncodePretty $ a
1163+
1164+
data WriteFileGenesis where
1165+
WriteCanonical :: Text.JSON.Canonical.ToJSON Identity genesis => genesis -> WriteFileGenesis
1166+
WritePretty :: ToJSON genesis => genesis -> WriteFileGenesis
11701167

11711168
-- ----------------------------------------------------------------------------
11721169

0 commit comments

Comments
 (0)