Skip to content

Commit

Permalink
Rename/improve ErrNodeNotYetInRecentEra error
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed May 26, 2023
1 parent ec56aad commit ef73ee3
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 41 deletions.
25 changes: 20 additions & 5 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs
Expand Up @@ -93,12 +93,17 @@ import Cardano.Wallet.Address.Discovery.Shared
import Cardano.Wallet.Api.Hex
( hexText )
import Cardano.Wallet.Api.Types
( ApiCosignerIndex (..), ApiCredentialType (..), Iso8601Time (..) )
( ApiCosignerIndex (..)
, ApiCredentialType (..)
, Iso8601Time (..)
, toApiEra
)
import Cardano.Wallet.Api.Types.Error
( ApiError (..)
, ApiErrorBalanceTxUnderestimatedFee (..)
, ApiErrorInfo (..)
, ApiErrorMessage (..)
, ApiErrorNodeNotYetInRecentEra (..)
, ApiErrorSharedWalletNoSuchCosigner (..)
, ApiErrorTxOutputLovelaceInsufficient (..)
)
Expand Down Expand Up @@ -162,6 +167,7 @@ import qualified Cardano.Api as Cardano
import qualified Cardano.Wallet.Primitive.Types.Coin as Coin
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Cardano.Wallet.Write.Tx as Write
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
Expand Down Expand Up @@ -457,11 +463,20 @@ instance IsServerError ErrGetPolicyId where

instance IsServerError ErrWriteTxEra where
toServerError = \case
ErrOldEraNotSupported (Cardano.AnyCardanoEra era) ->
apiError err403 BalanceTxEraNotSupported $ T.unwords
[ "Balancing in ", showT era, " "
, "is not supported."
ErrNodeNotYetInRecentEra (Cardano.AnyCardanoEra era) ->
apiError err403 (NodeNotYetInRecentEra info) $ T.unwords
[ "This operation requires the node to be synchronised to a"
, "recent era, but the node is currently only synchronised to the"
, showT era
, "era. Please wait until the node is fully synchronised and"
, "try again."
]
where
info = ApiErrorNodeNotYetInRecentEra
{ nodeEra = toApiEra $ Cardano.AnyCardanoEra era
, supportedRecentEras =
map (toApiEra . Write.toAnyCardanoEra) [minBound .. maxBound]
}

instance IsServerError ErrBalanceTx where
toServerError = \case
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/api/http/Cardano/Wallet/Api/Types.hs
Expand Up @@ -1426,7 +1426,7 @@ data ApiEra
| ApiAlonzo
| ApiBabbage
| ApiConway
deriving (Show, Eq, Generic, Enum, Ord, Bounded)
deriving (Data, Show, Eq, Generic, Enum, Ord, Bounded)
deriving anyclass NFData

toApiEra :: AnyCardanoEra -> ApiEra
Expand Down
15 changes: 13 additions & 2 deletions lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs
Expand Up @@ -24,6 +24,7 @@ module Cardano.Wallet.Api.Types.Error
, ApiErrorSharedWalletNoSuchCosigner (..)
, ApiErrorTxOutputLovelaceInsufficient (..)
, ApiErrorBalanceTxUnderestimatedFee (..)
, ApiErrorNodeNotYetInRecentEra (..)
)
where

Expand All @@ -32,7 +33,7 @@ import Prelude
import Cardano.Wallet.Api.Lib.Options
( DefaultRecord (..), defaultSumTypeOptions )
import Cardano.Wallet.Api.Types
( ApiCosignerIndex (..), ApiCredentialType (..) )
( ApiCosignerIndex (..), ApiCredentialType (..), ApiEra )
import Control.DeepSeq
( NFData (..) )
import Data.Aeson
Expand Down Expand Up @@ -85,7 +86,6 @@ data ApiErrorInfo
| AssetNotPresent
| BadRequest
| BalanceTxConflictingNetworks
| BalanceTxEraNotSupported
| BalanceTxExistingCollateral
| BalanceTxExistingKeyWitnesses
| BalanceTxExistingReturnCollateral
Expand Down Expand Up @@ -120,6 +120,8 @@ data ApiErrorInfo
| NetworkMisconfigured
| NetworkQueryFailed
| NetworkUnreachable
| NodeNotYetInRecentEra
!ApiErrorNodeNotYetInRecentEra
| NoRootKey
| NoSuchPool
| NoSuchTransaction
Expand Down Expand Up @@ -222,3 +224,12 @@ data ApiErrorBalanceTxUnderestimatedFee = ApiErrorBalanceTxUnderestimatedFee
deriving (FromJSON, ToJSON)
via DefaultRecord ApiErrorBalanceTxUnderestimatedFee
deriving anyclass NFData

data ApiErrorNodeNotYetInRecentEra = ApiErrorNodeNotYetInRecentEra
{ nodeEra :: ApiEra
, supportedRecentEras :: [ApiEra]
}
deriving (Data, Eq, Generic, Show, Typeable)
deriving (FromJSON, ToJSON)
via DefaultRecord ApiErrorNodeNotYetInRecentEra
deriving anyclass NFData
2 changes: 1 addition & 1 deletion lib/wallet/bench/restore-bench.hs
Expand Up @@ -1010,7 +1010,7 @@ guardIsRecentEra (Cardano.AnyCardanoEra era) = case era of
Cardano.ShelleyEra -> invalidEra
Cardano.ByronEra -> invalidEra
where
invalidEra = throwIO $ W.ExceptionWriteTxEra $ W.ErrOldEraNotSupported $
invalidEra = throwIO $ W.ExceptionWriteTxEra $ W.ErrNodeNotYetInRecentEra $
Cardano.AnyCardanoEra era

withNonEmptyUTxO :: Wallet s -> Set Tx -> e -> IO a -> IO (Either e a)
Expand Down
5 changes: 3 additions & 2 deletions lib/wallet/src/Cardano/Wallet.hs
Expand Up @@ -1868,7 +1868,8 @@ type MakeRewardAccountBuilder k =
(k 'RootK XPrv, Passphrase "encryption") -> (XPrv, Passphrase "encryption")

data ErrWriteTxEra
= ErrOldEraNotSupported Cardano.AnyCardanoEra
= ErrNodeNotYetInRecentEra Cardano.AnyCardanoEra
-- ^ Node is not synced enough or on an unsupported testnet in an older era.
deriving (Show, Eq)

-- | Build, Sign, Submit transaction.
Expand Down Expand Up @@ -2067,7 +2068,7 @@ buildAndSignTransactionPure
, builtSealedTx = signedTx
}
where
anyCardanoEra = Write.fromAnyRecentEra era
anyCardanoEra = Write.toAnyCardanoEra era

buildTransaction
:: forall s era.
Expand Down
50 changes: 46 additions & 4 deletions lib/wallet/src/Cardano/Wallet/Write/Tx.hs
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
Expand Down Expand Up @@ -57,7 +58,8 @@ module Cardano.Wallet.Write.Tx
, AnyRecentEra (..)
, InAnyRecentEra (..)
, asAnyRecentEra
, fromAnyRecentEra
, toAnyCardanoEra
, fromAnyCardanoEra
, withInAnyRecentEra
, withRecentEra

Expand Down Expand Up @@ -199,7 +201,9 @@ import Data.Generics.Product
import Data.IntCast
( intCast )
import Data.Maybe
( fromMaybe )
( fromMaybe, isJust )
import Data.Type.Equality
( (:~:) (Refl), TestEquality (testEquality) )
import Data.Typeable
( Typeable )
import Numeric.Natural
Expand Down Expand Up @@ -258,6 +262,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 @@ -381,11 +391,43 @@ data AnyRecentEra where
=> RecentEra era -- and explicit value.
-> AnyRecentEra -- and that's it.

instance Enum AnyRecentEra where
-- NOTE: We're not starting at 0! 0 would be Byron, which is not a recent
-- era.
fromEnum = fromEnum . toAnyCardanoEra
toEnum n = fromMaybe err . fromAnyCardanoEra $ toEnum n
where
err = error $ unwords
[ "AnyRecentEra.toEnum:", show n
, "doesn't correspond to a recent era."
]
instance Bounded AnyRecentEra where
minBound = AnyRecentEra RecentEraBabbage
maxBound = AnyRecentEra RecentEraConway

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

fromAnyRecentEra :: AnyRecentEra -> Cardano.AnyCardanoEra
fromAnyRecentEra (AnyRecentEra era) = Cardano.AnyCardanoEra (fromRecentEra era)
instance Eq AnyRecentEra where
AnyRecentEra e1 == AnyRecentEra e2 =
isJust $ testEquality e1 e2

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

fromAnyCardanoEra
:: Cardano.AnyCardanoEra
-> Maybe AnyRecentEra
fromAnyCardanoEra = \case
Cardano.AnyCardanoEra Cardano.ByronEra -> Nothing
Cardano.AnyCardanoEra Cardano.ShelleyEra -> Nothing
Cardano.AnyCardanoEra Cardano.AllegraEra -> Nothing
Cardano.AnyCardanoEra Cardano.MaryEra -> Nothing
Cardano.AnyCardanoEra Cardano.AlonzoEra -> Nothing
Cardano.AnyCardanoEra Cardano.BabbageEra
-> Just $ AnyRecentEra RecentEraBabbage
Cardano.AnyCardanoEra Cardano.ConwayEra
-> Just $ AnyRecentEra RecentEraConway

withRecentEra ::
AnyRecentEra -> (forall era. IsRecentEra era => RecentEra era -> a) -> a
Expand Down
51 changes: 30 additions & 21 deletions lib/wallet/test/data/Cardano/Wallet/Api/ApiError.json

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

0 comments on commit ef73ee3

Please sign in to comment.