{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}

module Cardano.CLI.Byron.UpdateProposal
  ( runProposalCreation
  , readByronUpdateProposal
  , submitByronUpdateProposal
  )
where

import Cardano.Api
import Cardano.Api.Byron
  ( AsType (AsByronUpdateProposal)
  , ByronProtocolParametersUpdate
  , ByronUpdateProposal
  , makeByronUpdateProposal
  , toByronLedgerUpdateProposal
  )
import Cardano.Api.Byron qualified as Byron

import Cardano.CLI.Byron.Key (readByronSigningKey)
import Cardano.CLI.Byron.Tx (nodeSubmitTx)
import Cardano.CLI.Compatible.Exception
import Cardano.CLI.Helper (ensureNewFileLBS)
import Cardano.CLI.Orphan ()
import Cardano.CLI.Read
import Cardano.CLI.Type.Common

import Control.Tracer (stdoutTracer, traceWith)

runProposalCreation
  :: NetworkId
  -> SigningKeyFile In
  -> Byron.ProtocolVersion
  -> Byron.SoftwareVersion
  -> Byron.SystemTag
  -> Byron.InstallerHash
  -> FilePath
  -> ByronProtocolParametersUpdate
  -> CIO e ()
runProposalCreation :: forall e.
NetworkId
-> SigningKeyFile 'In
-> ProtocolVersion
-> SoftwareVersion
-> SystemTag
-> InstallerHash
-> FilePath
-> ByronProtocolParametersUpdate
-> CIO e ()
runProposalCreation
  NetworkId
nw
  SigningKeyFile 'In
sKey
  ProtocolVersion
pVer
  SoftwareVersion
sVer
  SystemTag
sysTag
  InstallerHash
insHash
  FilePath
outputFp
  ByronProtocolParametersUpdate
params = do
    SomeByronSigningKey
sK <- ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> RIO e SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile 'In
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
NonLegacyByronKeyFormat SigningKeyFile 'In
sKey
    let proposal :: ByronUpdateProposal
proposal = NetworkId
-> ProtocolVersion
-> SoftwareVersion
-> SystemTag
-> InstallerHash
-> SomeByronSigningKey
-> ByronProtocolParametersUpdate
-> ByronUpdateProposal
makeByronUpdateProposal NetworkId
nw ProtocolVersion
pVer SoftwareVersion
sVer SystemTag
sysTag InstallerHash
insHash SomeByronSigningKey
sK ByronProtocolParametersUpdate
params
    ExceptT HelpersError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO () -> RIO e ())
-> ExceptT HelpersError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$
      FilePath -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS FilePath
outputFp (ByteString -> ExceptT HelpersError IO ())
-> ByteString -> ExceptT HelpersError IO ()
forall a b. (a -> b) -> a -> b
$
        ByronUpdateProposal -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes ByronUpdateProposal
proposal

readByronUpdateProposal :: FilePath -> CIO e ByronUpdateProposal
readByronUpdateProposal :: forall e. FilePath -> CIO e ByronUpdateProposal
readByronUpdateProposal FilePath
fp = do
  ByteString
proposalBs <-
    FilePath -> RIO e ByteString
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
FilePath -> m ByteString
readFileCli FilePath
fp
  let proposalResult :: Either SerialiseAsRawBytesError ByronUpdateProposal
proposalResult = AsType ByronUpdateProposal
-> ByteString
-> Either SerialiseAsRawBytesError ByronUpdateProposal
forall a.
SerialiseAsRawBytes a =>
AsType a -> ByteString -> Either SerialiseAsRawBytesError a
deserialiseFromRawBytes AsType ByronUpdateProposal
AsByronUpdateProposal ByteString
proposalBs
  Either SerialiseAsRawBytesError ByronUpdateProposal
-> RIO e ByronUpdateProposal
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
Either e a -> m a
fromEitherCli Either SerialiseAsRawBytesError ByronUpdateProposal
proposalResult

submitByronUpdateProposal
  :: SocketPath
  -> NetworkId
  -> FilePath
  -> CIO e ()
submitByronUpdateProposal :: forall e. SocketPath -> NetworkId -> FilePath -> CIO e ()
submitByronUpdateProposal SocketPath
nodeSocketPath NetworkId
network FilePath
proposalFp = do
  ByronUpdateProposal
proposal <- FilePath -> CIO e ByronUpdateProposal
forall e. FilePath -> CIO e ByronUpdateProposal
readByronUpdateProposal FilePath
proposalFp
  let genTx :: GenTx ByronBlock
genTx = ByronUpdateProposal -> GenTx ByronBlock
toByronLedgerUpdateProposal ByronUpdateProposal
proposal
  Tracer (RIO e) FilePath -> FilePath -> RIO e ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer (RIO e) FilePath
forall (m :: * -> *). MonadIO m => Tracer m FilePath
stdoutTracer (FilePath -> RIO e ()) -> FilePath -> RIO e ()
forall a b. (a -> b) -> a -> b
$ FilePath
"Update proposal TxId: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ TxId (GenTx ByronBlock) -> FilePath
forall a. Condense a => a -> FilePath
condense (GenTx ByronBlock -> TxId (GenTx ByronBlock)
forall tx. HasTxId tx => tx -> TxId tx
txId GenTx ByronBlock
genTx)
  ExceptT ByronTxError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronTxError IO () -> RIO e ())
-> ExceptT ByronTxError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$ SocketPath
-> NetworkId -> GenTx ByronBlock -> ExceptT ByronTxError IO ()
nodeSubmitTx SocketPath
nodeSocketPath NetworkId
network GenTx ByronBlock
genTx