{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- DUPLICATE -- adapted from: cardano-node/src/Cardano/Node/Protocol/Alonzo.hs

module Cardano.Node.Protocol.Alonzo (
    AlonzoProtocolInstantiationError (..)
    -- * Reusable parts
  , readGenesis
  , validateGenesis
  ) where

import           Cardano.Api.Any
import qualified Cardano.Ledger.Alonzo.Genesis as Alonzo
import           Cardano.Node.Protocol.Shelley (GenesisReadError,
                     readGenesisAny)
import           Cardano.Node.Types
import           Cardano.Prelude
import           Prelude (String)

--
-- Alonzo genesis
--

readGenesis :: GenesisFile
            -> Maybe GenesisHash
            -> ExceptT GenesisReadError IO
                       (Alonzo.AlonzoGenesis, GenesisHash)
readGenesis :: GenesisFile
-> Maybe GenesisHash
-> ExceptT GenesisReadError IO (AlonzoGenesis, GenesisHash)
readGenesis = GenesisFile
-> Maybe GenesisHash
-> ExceptT GenesisReadError IO (AlonzoGenesis, GenesisHash)
forall genesis.
FromJSON genesis =>
GenesisFile
-> Maybe GenesisHash
-> ExceptT GenesisReadError IO (genesis, GenesisHash)
readGenesisAny

validateGenesis :: Alonzo.AlonzoGenesis
                -> ExceptT AlonzoProtocolInstantiationError IO ()
validateGenesis :: AlonzoGenesis -> ExceptT AlonzoProtocolInstantiationError IO ()
validateGenesis AlonzoGenesis
_ = () -> ExceptT AlonzoProtocolInstantiationError IO ()
forall a. a -> ExceptT AlonzoProtocolInstantiationError IO a
forall (m :: * -> *) a. Monad m => a -> m a
return () --TODO alonzo: do the validation

data AlonzoProtocolInstantiationError
  = InvalidCostModelError !FilePath
  | CostModelExtractionError !FilePath
  | AlonzoCostModelFileError !(FileError ())
  | AlonzoCostModelDecodeError !FilePath !String
  deriving Int -> AlonzoProtocolInstantiationError -> ShowS
[AlonzoProtocolInstantiationError] -> ShowS
AlonzoProtocolInstantiationError -> String
(Int -> AlonzoProtocolInstantiationError -> ShowS)
-> (AlonzoProtocolInstantiationError -> String)
-> ([AlonzoProtocolInstantiationError] -> ShowS)
-> Show AlonzoProtocolInstantiationError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AlonzoProtocolInstantiationError -> ShowS
showsPrec :: Int -> AlonzoProtocolInstantiationError -> ShowS
$cshow :: AlonzoProtocolInstantiationError -> String
show :: AlonzoProtocolInstantiationError -> String
$cshowList :: [AlonzoProtocolInstantiationError] -> ShowS
showList :: [AlonzoProtocolInstantiationError] -> ShowS
Show

instance Error AlonzoProtocolInstantiationError where
  displayError :: AlonzoProtocolInstantiationError -> String
displayError (InvalidCostModelError String
fp) =
    String
"Invalid cost model: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ShowS
forall a b. (Show a, ConvertText String b) => a -> b
show String
fp
  displayError (CostModelExtractionError String
fp) =
    String
"Error extracting the cost model at: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ShowS
forall a b. (Show a, ConvertText String b) => a -> b
show String
fp
  displayError (AlonzoCostModelFileError FileError ()
err) =
    FileError () -> String
forall e. Error e => e -> String
displayError FileError ()
err
  displayError (AlonzoCostModelDecodeError String
fp String
err) =
    String
"Error decoding cost model at: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ShowS
forall a b. (Show a, ConvertText String b) => a -> b
show String
fp String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" Error: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
err