From 666413c72f871480bc68640982979cb35ac204e4 Mon Sep 17 00:00:00 2001 From: Johannes Lund Date: Thu, 11 May 2023 13:09:57 +0200 Subject: [PATCH] Drop support balancing in future eras --- .../Cardano/Wallet/Api/Http/Server/Error.hs | 16 +- .../Cardano/Wallet/Api/Http/Shelley/Server.hs | 108 +- .../http/Cardano/Wallet/Api/Types/Error.hs | 2 +- lib/wallet/balance-prof-1.html | 939 ++++++++++++++++++ lib/wallet/src/Cardano/Wallet.hs | 2 +- .../src/Cardano/Wallet/Primitive/Types/Tx.hs | 2 + lib/wallet/src/Cardano/Wallet/Write/Tx.hs | 4 +- lib/wallet/unit.prof.html | 939 ++++++++++++++++++ specifications/api/swagger.yaml | 12 + 9 files changed, 1944 insertions(+), 80 deletions(-) create mode 100644 lib/wallet/balance-prof-1.html create mode 100644 lib/wallet/unit.prof.html diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs index d8aab476a67..c632f2fc6ff 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs @@ -457,14 +457,20 @@ instance IsServerError ErrGetPolicyId 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." + ErrTxNotInNodeEra era -> + apiError err403 TxNotInNodeEra $ T.unwords + [ "The provided transaction could be deserialised, just not in" + , showT era <> "," + , "the era the local node currently is in." + , "If the node is not yet fully synced, try waiting." + , "If you're constructing a transaction for a future era for" + , "testing purposes, try doing so on a testnet in that era" + , "instead." ] ErrOldEraNotSupported (Cardano.AnyCardanoEra era) -> apiError err403 BalanceTxEraNotSupported $ T.unwords - [ "Balancing in ", showT era, " " + [ "Balancing transactions in ", showT era + , "(the era the local node currently is in)" , "is not supported." ] diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs index e428e947726..f5e5421e275 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs @@ -155,10 +155,6 @@ import Cardano.Api , toNetworkMagic , unNetworkMagic ) -import Cardano.Api.Extra - ( inAnyCardanoEra ) -import Cardano.Api.Shelley - ( ShelleyLedgerEra ) import Cardano.BM.Tracing ( HasPrivacyAnnotation (..), HasSeverityAnnotation (..) ) import Cardano.Mnemonic @@ -508,7 +504,7 @@ import Cardano.Wallet.Primitive.Types.Tx , TxChange (..) , TxStatus (..) , UnsignedTx (..) - , cardanoTxIdeallyNoLaterThan + , cardanoTxInExactEra , getSealedTxWitnesses , sealedTxFromCardanoBody ) @@ -549,8 +545,6 @@ import Cardano.Wallet.Unsafe ( unsafeRunExceptT ) import Cardano.Wallet.Write.Tx ( AnyRecentEra (..) ) -import Cardano.Wallet.Write.Tx.Balance - ( constructUTxOIndex ) import Control.Arrow ( second, (&&&) ) import Control.DeepSeq @@ -558,7 +552,7 @@ import Control.DeepSeq import Control.Error.Util ( failWith ) import Control.Monad - ( forM, forever, join, void, when, (<=<), (>=>) ) + ( forM, forever, join, void, when, (>=>) ) import Control.Monad.Error.Class ( throwError ) import Control.Monad.IO.Class @@ -3070,80 +3064,36 @@ balanceTransaction (ApiT wid) body = do -- NOTE: Ideally we'd read @pp@ and @era@ atomically. - pp <- liftIO $ NW.currentProtocolParameters nl - era <- liftIO $ NW.currentNodeEra nl + era <- liftIO $ NW.currentNodeEra netLayer + pp <- liftIO (currentProtocolParameters netLayer) + Write.AnyRecentEra recentEra <- guardIsRecentEra era + withWorkerCtx ctx wid liftE liftE $ \wrk -> do - (walletUTxO, wallet, _txs) <- handler $ W.readWalletUTxO @_ @s wrk + (utxo, wallet, _txs) <- handler $ W.readWalletUTxO wrk timeTranslation <- liftIO $ toTimeTranslation (timeInterpreter netLayer) - let mkPartialTx - :: forall era. Write.IsRecentEra era => Cardano.Tx era - -> Handler (Write.PartialTx era) - mkPartialTx tx = do - utxo <- fmap Write.toCardanoUTxO $ mkLedgerUTxO $ body ^. #inputs - pure $ Write.PartialTx - tx - utxo - (fromApiRedeemer <$> body ^. #redeemers) - where - -- NOTE: There are a couple of spread-out pieces of logic - -- dealing with the choice of era, most prominantly: tx, utxo, - -- pparams / current node era. It /might/ be neater to have a - -- single function dedicated to this choice instead; something - -- like - -- @@ - -- chooseEra - -- :: InRecentEra Tx - -- -> InRecentEra UTxO - -- -> InRecentEra PParams - -- -> (IsRecentEra era - -- => Tx era - -- -> UTxO era - -- -> PParams era - -- -> res) - -- -> res - -- @@ - - mkRecentEra :: Handler (Write.RecentEra era) - mkRecentEra = case Cardano.cardanoEra @era of - Cardano.ConwayEra -> pure Write.RecentEraConway - Cardano.BabbageEra -> pure Write.RecentEraBabbage - _ -> liftHandler $ throwE $ W.ErrOldEraNotSupported era - - mkLedgerUTxO - :: [ApiExternalInput n] - -> Handler (Write.UTxO (ShelleyLedgerEra era)) - mkLedgerUTxO ins = do - recentEra <- mkRecentEra - pure - . Write.utxoFromTxOutsInRecentEra recentEra - . map fromExternalInput - $ ins + + partialTx <- parsePartialTx recentEra let balanceTx :: forall era. Write.IsRecentEra era => Write.PartialTx era -> Handler (Cardano.Tx era) - balanceTx partialTx = + balanceTx ptx = liftHandler $ fst <$> Write.balanceTransaction @_ @IO @s (MsgWallet . W.MsgBalanceTx >$< wrk ^. W.logger) (Write.UTxOAssumptions genInpScripts mScriptTemplate - (txWitnessTagFor @k)) - (Write.unsafeFromWalletProtocolParameters pp) + (txWitnessTagFor @k) + ) + (Write.unsafeFromWalletProtocolParameters @era pp) timeTranslation - (constructUTxOIndex walletUTxO) + (Write.constructUTxOIndex utxo) (W.defaultChangeAddressGen argGenChange) (getState wallet) - partialTx - - anyRecentTx <- maybeToHandler (W.ErrOldEraNotSupported era) - . Write.asAnyRecentEra - . cardanoTxIdeallyNoLaterThan era - . getApiT $ body ^. #transaction + ptx - res <- Write.withInAnyRecentEra anyRecentTx - (fmap inAnyCardanoEra . balanceTx <=< mkPartialTx) + res <- Cardano.InAnyCardanoEra Write.cardanoEra <$> balanceTx partialTx case body ^. #encoding of Just HexEncoded -> @@ -3152,11 +3102,27 @@ balanceTransaction _ -> pure $ ApiSerialisedTransaction (ApiT $ W.sealedTxFromCardano res) Base64Encoded where - nl = ctx ^. networkLayer - - maybeToHandler :: IsServerError e => e -> Maybe a -> Handler a - maybeToHandler _ (Just a) = pure a - maybeToHandler e Nothing = liftHandler $ throwE e + parsePartialTx + :: Write.IsRecentEra era + => Write.RecentEra era + -> Handler (Write.PartialTx era) + parsePartialTx era = do + let externalUTxO = Write.toCardanoUTxO + $ Write.utxoFromTxOutsInRecentEra era + $ map fromExternalInput + $ body ^. #inputs + + tx <- maybe + (liftHandler $ throwE $ W.ErrTxNotInNodeEra $ AnyRecentEra era) + pure + . cardanoTxInExactEra (Write.cardanoEraFromRecentEra era) + . getApiT + $ body ^. #transaction + + return $ Write.PartialTx + tx + externalUTxO + (fromApiRedeemer <$> body ^. #redeemers) decodeTransaction :: forall s n diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs index 351bf6b90b3..c9099bb01be 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs @@ -89,7 +89,7 @@ data ApiErrorInfo | BalanceTxExistingCollateral | BalanceTxExistingKeyWitnesses | BalanceTxExistingReturnCollateral - | BalanceTxTxInUnexpectedEra + | TxNotInNodeEra | BalanceTxExistingTotalCollateral | BalanceTxInternalError | BalanceTxUnderestimatedFee diff --git a/lib/wallet/balance-prof-1.html b/lib/wallet/balance-prof-1.html new file mode 100644 index 00000000000..e9071b3eebe --- /dev/null +++ b/lib/wallet/balance-prof-1.html @@ -0,0 +1,939 @@ + + + + + unit + + + + + + + + + + + + + + + + +
+
+
+
+ +
+ + diff --git a/lib/wallet/src/Cardano/Wallet.hs b/lib/wallet/src/Cardano/Wallet.hs index ed03f945581..9d08e4838b5 100644 --- a/lib/wallet/src/Cardano/Wallet.hs +++ b/lib/wallet/src/Cardano/Wallet.hs @@ -1839,7 +1839,7 @@ type MakeRewardAccountBuilder k = data ErrWriteTxEra = ErrOldEraNotSupported Cardano.AnyCardanoEra - | ErrTxNotInEra Write.AnyRecentEra + | ErrTxNotInNodeEra Write.AnyRecentEra deriving (Show, Eq) -- | Build, Sign, Submit transaction. diff --git a/lib/wallet/src/Cardano/Wallet/Primitive/Types/Tx.hs b/lib/wallet/src/Cardano/Wallet/Primitive/Types/Tx.hs index 3a7607e5d6b..817ea946569 100644 --- a/lib/wallet/src/Cardano/Wallet/Primitive/Types/Tx.hs +++ b/lib/wallet/src/Cardano/Wallet/Primitive/Types/Tx.hs @@ -34,6 +34,7 @@ module Cardano.Wallet.Primitive.Types.Tx -- * Serialisation , SealedTx (serialisedTx) , cardanoTxIdeallyNoLaterThan + , cardanoTxInExactEra , sealedTxFromBytes , sealedTxFromBytes' , sealedTxFromCardano @@ -94,6 +95,7 @@ import Cardano.Wallet.Primitive.Types.Tx.SealedTx , SerialisedTx (..) , SerialisedTxParts (..) , cardanoTxIdeallyNoLaterThan + , cardanoTxInExactEra , getSealedTxBody , getSealedTxWitnesses , getSerialisedTxParts diff --git a/lib/wallet/src/Cardano/Wallet/Write/Tx.hs b/lib/wallet/src/Cardano/Wallet/Write/Tx.hs index 64d12786426..5e531a5e47b 100644 --- a/lib/wallet/src/Cardano/Wallet/Write/Tx.hs +++ b/lib/wallet/src/Cardano/Wallet/Write/Tx.hs @@ -1,8 +1,8 @@ {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE ConstraintKinds #-} -{-# LANGUAGE ExplicitNamespaces #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE EmptyCase #-} +{-# LANGUAGE ExplicitNamespaces #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} @@ -202,7 +202,7 @@ import Data.IntCast import Data.Maybe ( fromMaybe, isJust ) import Data.Type.Equality - ( TestEquality (testEquality), type (:~:) (Refl) ) + ( (:~:) (Refl), TestEquality (testEquality) ) import Data.Typeable ( Typeable ) import Numeric.Natural diff --git a/lib/wallet/unit.prof.html b/lib/wallet/unit.prof.html new file mode 100644 index 00000000000..02c6519f31b --- /dev/null +++ b/lib/wallet/unit.prof.html @@ -0,0 +1,939 @@ + + + + + unit + + + + + + + + + + + + + + + + +
+
+
+
+ +
+ + diff --git a/specifications/api/swagger.yaml b/specifications/api/swagger.yaml index e9cb013303b..6a28f2c3074 100644 --- a/specifications/api/swagger.yaml +++ b/specifications/api/swagger.yaml @@ -4748,6 +4748,17 @@ x-errBalanceTxEraNotSupported: &errBalanceTxEraNotSupported type: string enum: ["balance_tx_era_not_supported"] +x-errTxNotInNodeEra: &errTxNotInNodeEra + <<: *responsesErr + title: tx_not_in_node_era + properties: + message: + type: string + description: The transaction could be deserialised, just not in the era of the local node. + code: + type: string + enum: ["tx_not_in_node_era"] + x-errBalanceTxConflictingNetworks: &errBalanceTxConflictingNetworks <<: *responsesErr title: balance_tx_conflicting_networks @@ -6191,6 +6202,7 @@ x-responsesBalanceTransaction: &responsesBalanceTransaction - <<: *errTransactionAlreadyBalanced - <<: *errTransactionIsTooBig - <<: *errUtxoTooSmall + - <<: *errTxNotInNodeEra <<: *responsesErr404WalletNotFound <<: *responsesErr406