Skip to content

Commit

Permalink
Inline function
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Mar 17, 2021
1 parent 533d0ff commit 1ed0c77
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
17 changes: 5 additions & 12 deletions cardano-submit-api/src/Cardano/TxSubmit/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Cardano.TxSubmit.Types
, TxSubmitPort (..)
, EnvSocketError(..)
, TxCmdError(..)
, DecodeCBORError(..)
, RawCborDecodeError(..)
, renderTxSubmitWebApiError
, renderTxCmdError
) where
Expand All @@ -35,18 +35,11 @@ newtype TxSubmitPort = TxSubmitPort Int

-- | The errors that the pure 'TextEnvelope' parsing\/decoding functions can return.
--
data DecodeCBORError
= TextEnvelopeTypeError
| TextEnvelopeDecodeError !DecoderError
| TextEnvelopeAesonDecodeError !String
newtype RawCborDecodeError = RawCborDecodeError DecoderError
deriving (Eq, Show)

instance Error DecodeCBORError where
displayError tee =
case tee of
TextEnvelopeTypeError -> "TextEnvelope type error: "
TextEnvelopeAesonDecodeError decErr -> "TextEnvelope aeson decode error: " <> decErr
TextEnvelopeDecodeError decErr -> "TextEnvelope decode error: " <> show decErr
instance Error RawCborDecodeError where
displayError (RawCborDecodeError decErr) = "TextEnvelope decode error: " <> show decErr

-- | An error that can occur in the transaction submission web API.
data TxSubmitWebApiError
Expand All @@ -61,7 +54,7 @@ newtype EnvSocketError = CliEnvVarLookup Text deriving (Eq, Show)
data TxCmdError
= TxCmdSocketEnvError EnvSocketError
| TxCmdEraConsensusModeMismatch !AnyConsensusMode !AnyCardanoEra
| TxCmdTxReadError !DecodeCBORError
| TxCmdTxReadError !RawCborDecodeError
| TxCmdTxSubmitError !Text
| TxCmdTxSubmitErrorEraMismatch !EraMismatch

Expand Down
40 changes: 16 additions & 24 deletions cardano-submit-api/src/Cardano/TxSubmit/Web.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import Cardano.Api (AllegraEra, AnyCardanoEra (AnyCardanoEra),
CardanoEra (AllegraEra, ByronEra, MaryEra, ShelleyEra), Error (..),
FromSomeType (..), HasTypeProxy (AsType), InAnyCardanoEra (..),
LocalNodeConnectInfo (LocalNodeConnectInfo, localConsensusModeParams, localNodeNetworkId, localNodeSocketPath),
MaryEra, NetworkId, SerialiseAsCBOR (..), ShelleyEra, TextEnvelope (..),
TextEnvelopeType (..), ToJSON, Tx, TxId (..), TxInMode (TxInMode),
MaryEra, NetworkId, SerialiseAsCBOR (..), ShelleyEra, ToJSON, Tx, TxId (..),
TxInMode (TxInMode),
TxValidationErrorInMode (TxValidationEraMismatch, TxValidationErrorInMode),
consensusModeOnly, getTxBody, getTxId, submitTxToNodeLocal, toEraInMode)
import Cardano.BM.Trace (Trace, logInfo)
import Cardano.Binary (DecoderError (..))
import Cardano.TxSubmit.CLI.Types (SocketPath (SocketPath))
import Cardano.TxSubmit.Metrics (TxSubmitMetrics (..))
import Cardano.TxSubmit.Rest.Types (WebserverConfig (..), toWarpSettings)
import Cardano.TxSubmit.Types (DecodeCBORError (..), EnvSocketError (..),
import Cardano.TxSubmit.Types (EnvSocketError (..), RawCborDecodeError (..),
TxCmdError (TxCmdEraConsensusModeMismatch, TxCmdTxReadError, TxCmdTxSubmitError, TxCmdTxSubmitErrorEraMismatch),
TxSubmitApi, TxSubmitApiRecord (..), TxSubmitWebApiError (TxSubmitFail),
renderTxCmdError)
Expand Down Expand Up @@ -102,30 +102,22 @@ readEnvSocketPath =
envName = "CARDANO_NODE_SOCKET_PATH"

deserialiseOne :: forall b. ()
=> TextEnvelope
-> FromSomeType SerialiseAsCBOR b
-> Either DecodeCBORError b
deserialiseOne te (FromSomeType ttoken f) = first TextEnvelopeDecodeError $ f <$> deserialiseFromCBOR ttoken (teRawCBOR te)

deserialiseFromTextEnvelopeAnyOf :: forall b. ()
=> TextEnvelope
-> [FromSomeType SerialiseAsCBOR b]
-> Either DecodeCBORError b
deserialiseFromTextEnvelopeAnyOf te ts = foldr (<!>) defaultError (fmap (deserialiseOne te) ts)
where
defaultError :: Either DecodeCBORError b
defaultError = Left (TextEnvelopeDecodeError DecoderErrorVoid)
=> FromSomeType SerialiseAsCBOR b
-> ByteString
-> Either RawCborDecodeError b
deserialiseOne (FromSomeType ttoken f) bs = first RawCborDecodeError $ f <$> deserialiseFromCBOR ttoken bs

readByteStringTextEnvelopeAnyOf2
:: ByteString
-> [FromSomeType SerialiseAsCBOR b]
-> ExceptT DecodeCBORError IO b
readByteStringTextEnvelopeAnyOf2 content types = hoistEither $ do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' content
deserialiseFromTextEnvelopeAnyOf te types
deserialiseAnyOf :: forall b. ()
=> [FromSomeType SerialiseAsCBOR b]
-> ByteString
-> Either RawCborDecodeError b
deserialiseAnyOf ts te = foldr (<!>) defaultError (fmap (`deserialiseOne` te) ts)
where
defaultError :: Either RawCborDecodeError b
defaultError = Left (RawCborDecodeError DecoderErrorVoid)

readByteStringTx :: ByteString -> ExceptT TxCmdError IO (InAnyCardanoEra Tx)
readByteStringTx bs = firstExceptT TxCmdTxReadError $ readByteStringTextEnvelopeAnyOf2 bs
readByteStringTx = firstExceptT TxCmdTxReadError . hoistEither . deserialiseAnyOf
[ FromSomeType (AsTx AsByronEra) (InAnyCardanoEra ByronEra)
, FromSomeType (AsTx AsShelleyEra) (InAnyCardanoEra ShelleyEra)
, FromSomeType (AsTx AsAllegraEra) (InAnyCardanoEra AllegraEra)
Expand Down

0 comments on commit 1ed0c77

Please sign in to comment.