3
3
{-# LANGUAGE DataKinds #-}
4
4
{-# LANGUAGE DeriveAnyClass #-}
5
5
{-# LANGUAGE DeriveGeneric #-}
6
+ {-# LANGUAGE FlexibleContexts #-}
6
7
{-# LANGUAGE GADTs #-}
7
8
{-# LANGUAGE ScopedTypeVariables #-}
8
9
{-# LANGUAGE TupleSections #-}
@@ -114,6 +115,7 @@ import Cardano.Slotting.Slot (EpochSize (EpochSize))
114
115
import Data.Fixed (Fixed (MkFixed ))
115
116
import qualified Data.Yaml as Yaml
116
117
import Text.JSON.Canonical (parseCanonicalJSON , renderCanonicalJSON )
118
+ import qualified Text.JSON.Canonical (ToJSON )
117
119
118
120
import Data.ListMap (ListMap (.. ))
119
121
@@ -412,8 +414,8 @@ runGenesisCreate (GenesisDir rootdir)
412
414
-- Shelley genesis parameters
413
415
start genDlgs mAmount utxoAddrs mempty (Lovelace 0 ) [] [] template
414
416
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
417
419
-- TODO: rationalise the naming convention on these genesis json files.
418
420
where
419
421
adjustTemplate t = t { sgNetworkMagic = unNetworkMagic (toNetworkMagic network) }
@@ -501,7 +503,7 @@ runGenesisCreateCardano :: GenesisDir
501
503
-> Maybe FilePath
502
504
-> ExceptT ShelleyGenesisCmdError IO ()
503
505
runGenesisCreateCardano (GenesisDir rootdir)
504
- genNumGenesisKeys _genNumUTxOKeys
506
+ genNumGenesisKeys genNumUTxOKeys
505
507
mStart mAmount mSecurity slotLength mSlotCoeff
506
508
network byronGenesisT shelleyGenesisT alonzoGenesisT mNodeCfg = do
507
509
start <- maybe (SystemStart <$> getCurrentTimePlus30) pure mStart
@@ -577,39 +579,22 @@ runGenesisCreateCardano (GenesisDir rootdir)
577
579
writeSecrets deldir " shelley" " opcert.json" toOpCert opCerts
578
580
writeSecrets deldir " shelley" " counter.json" toCounter opCerts
579
581
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
583
585
584
586
liftIO $ do
585
587
case mNodeCfg of
586
588
Nothing -> pure ()
587
589
Just nodeCfg -> do
588
590
nodeConfig <- Yaml. decodeFileThrow nodeCfg
589
591
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
608
593
updateConfig :: Yaml. Value -> Yaml. Value
609
594
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
613
598
obj
614
599
updateConfig x = x
615
600
newConfig :: Yaml. Value
@@ -636,7 +621,7 @@ runGenesisCreateCardano (GenesisDir rootdir)
636
621
(toByronRequiresNetworkMagic network)
637
622
byronBalance = TestnetBalanceOptions
638
623
{ tboRichmen = genNumGenesisKeys
639
- , tboPoors = 1
624
+ , tboPoors = genNumUTxOKeys
640
625
, tboTotalBalance = fromMaybe zeroLovelace $ toByronLovelace (fromMaybe 0 mAmount)
641
626
, tboRichmenShare = 0
642
627
}
@@ -751,7 +736,7 @@ runGenesisCreateStaked (GenesisDir rootdir)
751
736
752
737
liftIO $ LBS. writeFile (rootdir </> " genesis.json" ) $ Aeson. encode shelleyGenesis
753
738
754
- writeFileGenesis (rootdir </> " genesis.alonzo.json" ) alonzoGenesis
739
+ void $ writeFileGenesis (rootdir </> " genesis.alonzo.json" ) $ WritePretty alonzoGenesis
755
740
-- TODO: rationalise the naming convention on these genesis json files.
756
741
757
742
liftIO $ Text. putStrLn $ mconcat $
@@ -1160,13 +1145,25 @@ updateCreateStakedOutputTemplate
1160
1145
unLovelace (Lovelace coin) = fromIntegral coin
1161
1146
1162
1147
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
1168
1152
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
1170
1167
1171
1168
-- ----------------------------------------------------------------------------
1172
1169
0 commit comments