{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

module Test.Cardano.Chain.Config (
  readMainetCfg,
) where

import qualified Cardano.Chain.Genesis as Genesis
import Cardano.Crypto.Hashing (Hash, decodeHash)
import Cardano.Crypto.ProtocolMagic (RequiresNetworkMagic (..))
import Cardano.Crypto.Raw (Raw)
import Cardano.Prelude
import Paths_cardano_ledger_byron (getDataFileName)

-- | Read the test mainnet configuration file from the @test@ directory.
--
-- An error is thrown if it is not possible to elaborate a genesis
-- configuration from the genesis file.
--
-- We use `RequiresNoMagic`, as it indicates mainnet
readMainetCfg :: MonadIO m => m Genesis.Config
readMainetCfg :: forall (m :: * -> *). MonadIO m => m Config
readMainetCfg = do
  mainnetGenesisJson <- IO String -> m String
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> m String) -> IO String -> m String
forall a b. (a -> b) -> a -> b
$ String -> IO String
getDataFileName String
"test/mainnet-genesis.json"
  let genHash =
        (Text -> Hash Raw)
-> (Hash Raw -> Hash Raw) -> Either Text (Hash Raw) -> Hash Raw
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
          (Text -> Hash Raw
forall a. HasCallStack => Text -> a
panic (Text -> Hash Raw) -> (Text -> Text) -> Text -> Hash Raw
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ConfigurationError -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show (ConfigurationError -> Text)
-> (Text -> ConfigurationError) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> ConfigurationError
Genesis.GenesisHashDecodeError)
          Hash Raw -> Hash Raw
forall (cat :: * -> * -> *) a. Category cat => cat a a
identity
          ( Text -> Either Text (Hash Raw)
forall a. Text -> Either Text (Hash a)
decodeHash
              Text
"5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb"
          ) ::
          Hash Raw

  either (panic . show) identity
    <$> runExceptT
      (Genesis.mkConfigFromFile RequiresNoMagic mainnetGenesisJson genHash)