Skip to content

Commit

Permalink
Move OldEraNotSupported outside of ErrBalanceTx
Browse files Browse the repository at this point in the history
It doesn't belong there. The new constructur will be used by the next
commit.
  • Loading branch information
Anviking committed May 19, 2023
1 parent 24e5499 commit f8fbc44
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 13 deletions.
12 changes: 11 additions & 1 deletion lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import Cardano.Wallet
, ErrWithdrawalNotBeneficial (..)
, ErrWitnessTx (..)
, ErrWritePolicyPublicKey (..)
, ErrWriteTxEra (..)
, ErrWrongPassphrase (..)
, WalletException (..)
)
Expand Down Expand Up @@ -214,6 +215,7 @@ instance IsServerError WalletException where
ExceptionReadAccountPublicKey e -> toServerError e
ExceptionSignPayment e -> toServerError e
ExceptionBalanceTx e -> toServerError e
ExceptionWriteTxEra e -> toServerError e
ExceptionBalanceTxInternalError e -> toServerError e
ExceptionSubmitTransaction e -> toServerError e
ExceptionConstructTx e -> toServerError e
Expand Down Expand Up @@ -453,13 +455,21 @@ instance IsServerError ErrGetPolicyId where
, "from cosigner#0."
]

instance IsServerError ErrBalanceTx where
instance IsServerError ErrWriteTxEra where
toServerError = \case
ErrTxNotInEra _ ->
apiError err403 BalanceTxTxInUnexpectedEra $ T.unwords
[ "The provided transaction could be deserialised, just not"
, "in the era of the local node tip."
]
ErrOldEraNotSupported (Cardano.AnyCardanoEra era) ->
apiError err403 BalanceTxEraNotSupported $ T.unwords
[ "Balancing in ", showT era, " "
, "is not supported."
]

instance IsServerError ErrBalanceTx where
toServerError = \case
ErrBalanceTxUpdateError (ErrExistingKeyWitnesses n) ->
apiError err403 BalanceTxExistingKeyWitnesses $ mconcat
[ "The transaction could not be balanced, because it contains "
Expand Down
6 changes: 3 additions & 3 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3107,7 +3107,7 @@ balanceTransaction
mkRecentEra = case Cardano.cardanoEra @era of
Cardano.ConwayEra -> pure Write.RecentEraConway
Cardano.BabbageEra -> pure Write.RecentEraBabbage
_ -> liftHandler $ throwE $ Write.ErrOldEraNotSupported era
_ -> liftHandler $ throwE $ W.ErrOldEraNotSupported era

mkLedgerUTxO
:: [ApiExternalInput n]
Expand Down Expand Up @@ -3137,7 +3137,7 @@ balanceTransaction
(getState wallet)
partialTx

anyRecentTx <- maybeToHandler (Write.ErrOldEraNotSupported era)
anyRecentTx <- maybeToHandler (W.ErrOldEraNotSupported era)
. Write.asAnyRecentEra
. cardanoTxIdeallyNoLaterThan era
. getApiT $ body ^. #transaction
Expand Down Expand Up @@ -4224,7 +4224,7 @@ guardIsRecentEra (Cardano.AnyCardanoEra era) = case era of
Cardano.ShelleyEra -> liftE invalidEra
Cardano.ByronEra -> liftE invalidEra
where
invalidEra = Write.ErrOldEraNotSupported $ Cardano.AnyCardanoEra era
invalidEra = W.ErrOldEraNotSupported $ Cardano.AnyCardanoEra era

mkWithdrawal
:: forall n block
Expand Down
1 change: 1 addition & 0 deletions lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ data ApiErrorInfo
| BalanceTxExistingCollateral
| BalanceTxExistingKeyWitnesses
| BalanceTxExistingReturnCollateral
| BalanceTxTxInUnexpectedEra
| BalanceTxExistingTotalCollateral
| BalanceTxInternalError
| BalanceTxUnderestimatedFee
Expand Down
9 changes: 2 additions & 7 deletions lib/wallet/bench/restore-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ import Cardano.BM.Trace
import Cardano.Mnemonic
( SomeMnemonic (..), entropyToMnemonic )
import Cardano.Wallet
( WalletException (..)
, WalletLayer (..)
, WalletWorkerLog (..)
, dummyChangeAddressGen
)
( WalletLayer (..), WalletWorkerLog (..), dummyChangeAddressGen )
import Cardano.Wallet.Address.Derivation
( Depth (..), WalletKey, digest, publicKey )
import Cardano.Wallet.Address.Derivation.Byron
Expand Down Expand Up @@ -241,7 +237,6 @@ import qualified Cardano.Wallet.Primitive.Types.UTxOStatistics as UTxOStatistics
import qualified Cardano.Wallet.Shelley.Compatibility as Cardano
import qualified Cardano.Wallet.Write.ProtocolParameters as Write
import qualified Cardano.Wallet.Write.Tx as Write
import qualified Cardano.Wallet.Write.Tx.Balance as Write
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
Expand Down Expand Up @@ -1041,5 +1036,5 @@ guardIsRecentEra (Cardano.AnyCardanoEra era) = case era of
Cardano.ShelleyEra -> invalidEra
Cardano.ByronEra -> invalidEra
where
invalidEra = throwIO $ ExceptionBalanceTx $ Write.ErrOldEraNotSupported $
invalidEra = throwIO $ W.ExceptionWriteTxEra $ W.ErrOldEraNotSupported $
Cardano.AnyCardanoEra era
7 changes: 7 additions & 0 deletions lib/wallet/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ module Cardano.Wallet
, ErrNoSuchTransaction (..)
, ErrStartTimeLaterThanEndTime (..)
, ErrWitnessTx (..)
, ErrWriteTxEra (..)

-- ** Root Key
, withRootKey
Expand Down Expand Up @@ -1836,6 +1837,11 @@ signTransaction tl preferredLatestEra witCountCtx keyLookup mextraRewardAcc
type MakeRewardAccountBuilder k =
(k 'RootK XPrv, Passphrase "encryption") -> (XPrv, Passphrase "encryption")

data ErrWriteTxEra
= ErrOldEraNotSupported Cardano.AnyCardanoEra
| ErrTxNotInEra Write.AnyRecentEra
deriving (Show, Eq)

-- | Build, Sign, Submit transaction.
--
-- Requires the encryption passphrase in order to decrypt the root private key.
Expand Down Expand Up @@ -3428,6 +3434,7 @@ data WalletException
| ExceptionReadAccountPublicKey ErrReadAccountPublicKey
| ExceptionSignPayment ErrSignPayment
| ExceptionBalanceTx ErrBalanceTx
| ExceptionWriteTxEra ErrWriteTxEra
| ExceptionBalanceTxInternalError ErrBalanceTxInternalError
| ExceptionSubmitTransaction ErrSubmitTransaction
| ExceptionConstructTx ErrConstructTx
Expand Down
18 changes: 18 additions & 0 deletions lib/wallet/src/Cardano/Wallet/Primitive/Types/Tx/SealedTx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

-- |
Expand All @@ -17,6 +18,7 @@ module Cardano.Wallet.Primitive.Types.Tx.SealedTx (
-- * Types
SealedTx (serialisedTx, unsafeCardanoTx)
, cardanoTxIdeallyNoLaterThan
, cardanoTxInExactEra
, sealedTxFromBytes
, sealedTxFromBytes'
, sealedTxFromCardano
Expand Down Expand Up @@ -59,8 +61,12 @@ import Data.ByteArray
( ByteArray, ByteArrayAccess )
import Data.ByteString
( ByteString )
import Data.Data
( Proxy (..) )
import Data.Either
( partitionEithers )
import Data.Either.Extra
( eitherToMaybe )
import Data.Function
( on )
import Data.Text
Expand Down Expand Up @@ -179,6 +185,18 @@ cardanoTxIdeallyNoLaterThan
-> InAnyCardanoEra Cardano.Tx
cardanoTxIdeallyNoLaterThan era = unsafeCardanoTx . ideallyNoLaterThan era

-- | Re-deserialises the bytes 'SealedTx' as a transaction in the provided era
-- exactly.
cardanoTxInExactEra
:: forall era. Cardano.IsCardanoEra era
=> CardanoEra era
-> SealedTx
-> Maybe (Cardano.Tx era)
cardanoTxInExactEra _ tx =
eitherToMaybe
$ deserialiseFromCBOR (Cardano.AsTx (Cardano.proxyToAsType $ Proxy @era))
$ serialisedTx tx

getSealedTxBody :: SealedTx -> InAnyCardanoEra Cardano.TxBody
getSealedTxBody (SealedTx _ (InAnyCardanoEra era tx) _) =
InAnyCardanoEra era (Cardano.getTxBody tx)
Expand Down
15 changes: 14 additions & 1 deletion lib/wallet/src/Cardano/Wallet/Write/Tx.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down Expand Up @@ -199,7 +200,9 @@ import Data.Generics.Product
import Data.IntCast
( intCast )
import Data.Maybe
( fromMaybe )
( fromMaybe, isJust )
import Data.Type.Equality
( TestEquality (testEquality), type (:~:) (Refl) )
import Data.Typeable
( Typeable )
import Numeric.Natural
Expand Down Expand Up @@ -258,6 +261,12 @@ data RecentEra era where
deriving instance Eq (RecentEra era)
deriving instance Show (RecentEra era)

instance TestEquality RecentEra where
testEquality RecentEraBabbage RecentEraBabbage = Just Refl
testEquality RecentEraConway RecentEraConway = Just Refl
testEquality RecentEraBabbage RecentEraConway = Nothing
testEquality RecentEraConway RecentEraBabbage = Nothing

class
( Cardano.IsShelleyBasedEra era
, Typeable era
Expand Down Expand Up @@ -384,6 +393,10 @@ data AnyRecentEra where
instance Show AnyRecentEra where
show (AnyRecentEra era) = "AnyRecentEra " <> show era

instance Eq AnyRecentEra where
AnyRecentEra e1 == AnyRecentEra e2 =
isJust $ testEquality e1 e2

fromAnyRecentEra :: AnyRecentEra -> Cardano.AnyCardanoEra
fromAnyRecentEra (AnyRecentEra era) = Cardano.AnyCardanoEra (fromRecentEra era)

Expand Down
1 change: 0 additions & 1 deletion lib/wallet/src/Cardano/Wallet/Write/Tx/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ data ErrBalanceTx
| ErrBalanceTxInternalError ErrBalanceTxInternalError
| ErrBalanceTxInputResolutionConflicts (NonEmpty (W.TxOut, W.TxOut))
| ErrBalanceTxUnresolvedInputs (NonEmpty W.TxIn)
| ErrOldEraNotSupported Cardano.AnyCardanoEra
deriving (Show, Eq)

-- | A 'PartialTx' is an an unbalanced 'SealedTx' along with the necessary
Expand Down

0 comments on commit f8fbc44

Please sign in to comment.