{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.CLI.Run.Debug.LogEpochState
  ( runLogEpochStateCmd
  )
where

import           Cardano.Api
import qualified Cardano.Api as Api

import           Cardano.CLI.Commands.Debug.LogEpochState
import           Cardano.CLI.Orphans ()

import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import qualified System.IO as IO

runLogEpochStateCmd
  :: LogEpochStateCmdArgs
  -> IO ()
runLogEpochStateCmd :: LogEpochStateCmdArgs -> IO ()
runLogEpochStateCmd
  LogEpochStateCmdArgs
    { SocketPath
nodeSocketPath :: SocketPath
nodeSocketPath :: LogEpochStateCmdArgs -> SocketPath
nodeSocketPath
    , NodeConfigFile 'In
configurationFile :: NodeConfigFile 'In
configurationFile :: LogEpochStateCmdArgs -> NodeConfigFile 'In
configurationFile
    , outputFilePath :: LogEpochStateCmdArgs -> File Configuration 'Out
outputFilePath = File String
outputFilePath
    } = do
    String -> ByteString -> IO ()
LBS.appendFile String
outputFilePath ByteString
""

    Either FoldBlocksError (ConditionResult, ())
result <-
      ExceptT FoldBlocksError IO (ConditionResult, ())
-> IO (Either FoldBlocksError (ConditionResult, ()))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT FoldBlocksError IO (ConditionResult, ())
 -> IO (Either FoldBlocksError (ConditionResult, ())))
-> ExceptT FoldBlocksError IO (ConditionResult, ())
-> IO (Either FoldBlocksError (ConditionResult, ()))
forall a b. (a -> b) -> a -> b
$
        NodeConfigFile 'In
-> SocketPath
-> ValidationMode
-> EpochNo
-> ()
-> (AnyNewEpochState
    -> SlotNo -> BlockNo -> StateT () IO ConditionResult)
-> ExceptT FoldBlocksError IO (ConditionResult, ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) s.
MonadIOTransError FoldBlocksError t m =>
NodeConfigFile 'In
-> SocketPath
-> ValidationMode
-> EpochNo
-> s
-> (AnyNewEpochState
    -> SlotNo -> BlockNo -> StateT s IO ConditionResult)
-> t m (ConditionResult, s)
foldEpochState
          NodeConfigFile 'In
configurationFile
          SocketPath
nodeSocketPath
          ValidationMode
Api.QuickValidation
          (Word64 -> EpochNo
EpochNo Word64
forall a. Bounded a => a
maxBound)
          ()
          ( \(AnyNewEpochState ShelleyBasedEra era
sbe NewEpochState (ShelleyLedgerEra era)
nes) SlotNo
_ BlockNo
_ -> do
              IO () -> StateT () IO ()
forall a. IO a -> StateT () IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> StateT () IO ()) -> IO () -> StateT () IO ()
forall a b. (a -> b) -> a -> b
$
                String -> ByteString -> IO ()
LBS.appendFile String
outputFilePath (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$
                  ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era => ByteString) -> ByteString
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints ShelleyBasedEra era
sbe (NewEpochState (ShelleyLedgerEra era) -> ByteString
forall a. ToJSON a => a -> ByteString
Aeson.encode NewEpochState (ShelleyLedgerEra era)
nes) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\n"
              ConditionResult -> StateT () IO ConditionResult
forall a. a -> StateT () IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ConditionResult
ConditionNotMet
          )

    case Either FoldBlocksError (ConditionResult, ())
result of
      Right (ConditionResult, ())
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Left FoldBlocksError
e -> Handle -> String -> IO ()
IO.hPutStrLn Handle
IO.stderr (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"Error: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> FoldBlocksError -> String
forall a. Show a => a -> String
show FoldBlocksError
e