Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADP-3359] Simplify construction of API errors relating to recent eras. #4603

Merged
merged 11 commits into from
May 22, 2024
Merged
28 changes: 11 additions & 17 deletions lib/api/src/Cardano/Wallet/Api/Http/Server/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ import Fmt
, pretty
)
import Internal.Cardano.Write.Tx
( KeyWitnessCounts (..)
)
import Network.Wai
( Request (pathInfo)
)
Expand All @@ -189,6 +191,9 @@ import Servant.Server
)

import qualified Cardano.Api as Cardano
import qualified Cardano.Wallet.Api.Types as Api
( allRecentEras
)
import qualified Cardano.Wallet.Api.Types.Amount as ApiAmount
import qualified Cardano.Wallet.Api.Types.WalletAssets as ApiWalletAssets
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
Expand All @@ -199,13 +204,11 @@ import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.Foldable as F
import qualified Data.List as L
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Internal.Cardano.Write.Tx as Write
( IsRecentEra
, serializeTx
, toAnyCardanoEra
)
import qualified Internal.Cardano.Write.Tx as WriteTx
import qualified Internal.Cardano.Write.Tx.Balance as Write
Expand Down Expand Up @@ -483,8 +486,7 @@ instance IsServerError ErrWriteTxEra where
where
info = ApiErrorNodeNotYetInRecentEra
{ nodeEra = toApiEra $ Cardano.AnyCardanoEra era
, supportedRecentEras =
map (toApiEra . Write.toAnyCardanoEra) [minBound .. maxBound]
, supportedRecentEras = Api.allRecentEras
}
ErrPartialTxNotInNodeEra nodeEra ->
apiError err403 TxNotInNodeEra $ T.unwords
Expand Down Expand Up @@ -647,19 +649,11 @@ instance IsServerError ErrPostTx where
, "node because its mempool was full."
]
e@(ErrPostTxEraUnsupported unsupported) ->
apiError err403 error' $ toText e
where
error' =
UnsupportedEra
$ ApiErrorUnsupportedEra
{ unsupportedEra = toApiEra unsupported
, supportedEras =
Set.fromList
( toApiEra
. Write.toAnyCardanoEra
<$> [minBound .. maxBound]
)
}
flip (apiError err403) (toText e) $ UnsupportedEra
ApiErrorUnsupportedEra
{ unsupportedEra = toApiEra unsupported
, supportedEras = Api.allRecentEras
}

instance IsServerError ErrSubmitTransaction where
toServerError = \case
Expand Down
12 changes: 12 additions & 0 deletions lib/api/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ module Cardano.Wallet.Api.Types
, ApiWithdrawalPostData (..)
, ApiRewardAccount (..)
, fromApiEra
, allRecentEras
, Iso8601Time (..)
, KeyFormat (..)
, MaintenanceAction (..)
Expand Down Expand Up @@ -576,6 +577,9 @@ import Data.Proxy
import Data.Quantity
( Quantity (..)
)
import Data.Set
( Set
)
import Data.String
( IsString
)
Expand Down Expand Up @@ -671,13 +675,16 @@ import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as BL
import qualified Data.List as L
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Read as T
import qualified Internal.Cardano.Write.Tx as Write
( DatumHash
, allRecentEras
, datumHashFromBytes
, datumHashToBytes
, toAnyCardanoEra
)

{-------------------------------------------------------------------------------
Expand Down Expand Up @@ -1641,6 +1648,11 @@ fromApiEra ApiAlonzo = AnyCardanoEra AlonzoEra
fromApiEra ApiBabbage = AnyCardanoEra BabbageEra
fromApiEra ApiConway = AnyCardanoEra ConwayEra

-- | The complete set of recent eras.
--
allRecentEras :: Set ApiEra
allRecentEras = Set.map (toApiEra . Write.toAnyCardanoEra) Write.allRecentEras

instance FromJSON ApiEra where
parseJSON = genericParseJSON $ Aeson.defaultOptions
{ constructorTagModifier = drop 4 . camelTo2 '_' }
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/Cardano/Wallet/Api/Types/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ data ApiErrorBalanceTxUnderestimatedFee = ApiErrorBalanceTxUnderestimatedFee

data ApiErrorNodeNotYetInRecentEra = ApiErrorNodeNotYetInRecentEra
{ nodeEra :: ApiEra
, supportedRecentEras :: [ApiEra]
, supportedRecentEras :: Set ApiEra
}
deriving (Data, Eq, Generic, Show, Typeable)
deriving (FromJSON, ToJSON)
Expand Down
16 changes: 16 additions & 0 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module Internal.Cardano.Write.Tx
, toRecentEraGADT
, LatestLedgerEra
, RecentEraConstraints
, allRecentEras

-- ** Key witness counts
, KeyWitnessCounts (..)
Expand Down Expand Up @@ -231,6 +232,9 @@ import Data.ByteString.Short
import Data.Coerce
( coerce
)
import Data.Function
( on
)
import Data.Generics.Internal.VL.Lens
( over
, (^.)
Expand All @@ -248,6 +252,9 @@ import Data.Maybe
( fromMaybe
, isJust
)
import Data.Set
( Set
)
import Data.Type.Equality
( TestEquality (testEquality)
, (:~:) (Refl)
Expand Down Expand Up @@ -289,6 +296,7 @@ import qualified Cardano.Wallet.Primitive.Types.Tx.Constraints as W
( txOutMaxCoin
)
import qualified Data.Map as Map
import qualified Data.Set as Set

--------------------------------------------------------------------------------
-- Eras
Expand Down Expand Up @@ -463,13 +471,21 @@ instance Bounded AnyRecentEra where
minBound = AnyRecentEra RecentEraBabbage
maxBound = AnyRecentEra RecentEraConway

instance Ord AnyRecentEra where
compare = compare `on` fromEnum
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This instance is needed in order to have a Set AnyRecentEra.


instance Show AnyRecentEra where
show (AnyRecentEra era) = "AnyRecentEra " <> show era

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

-- | The complete set of recent eras.
--
allRecentEras :: Set AnyRecentEra
allRecentEras = Set.fromList [minBound .. maxBound]

toAnyCardanoEra :: AnyRecentEra -> CardanoApi.AnyCardanoEra
toAnyCardanoEra (AnyRecentEra era) =
CardanoApi.AnyCardanoEra (fromRecentEra era)
Expand Down
15 changes: 4 additions & 11 deletions lib/unit/test/data/Cardano/Wallet/Api/ApiError.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading