Skip to content

Commit

Permalink
Add ApiErrorNodeNotYetInRecentEra
Browse files Browse the repository at this point in the history
Example error from the integration tests:
```
           ( ClientError
               ( Object
                   ( fromList
                       [
                           ( "code"
                           , String "node_not_yet_in_recent_era"
                           )
                       ,
                           ( "info"
                           , Object
                               ( fromList
                                   [
                                       ( "node_era"
                                       , String "alonzo"
                                       )
                                   ,
                                       ( "supported_recent_eras"
                                       , Array
                                           [ String "babbage"
                                           , String "conway"
                                           ]
                                       )
                                   ]
                               )
                           )
                       ,
                           ( "message"
                           , String "This operation requires the node to be synchronised to a recent era, but the node is currently only synchronised to the AlonzoEra era. Please wait until the node is fully synchronised and try again."
                           )
                       ]
                   )
               )
           )
```
  • Loading branch information
Anviking committed May 25, 2023
1 parent 5cde4f7 commit 1a267b7
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 8 deletions.
16 changes: 14 additions & 2 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs
Original file line number Diff line number Diff line change
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 @@ -471,13 +477,19 @@ instance IsServerError ErrWriteTxEra where
, "compatible with a recent era."
]
ErrNodeNotYetInRecentEra (Cardano.AnyCardanoEra era) ->
apiError err403 NodeNotYetInRecentEra $ T.unwords
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
Original file line number Diff line number Diff line change
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
13 changes: 12 additions & 1 deletion lib/wallet/api/http/Cardano/Wallet/Api/Types/Error.hs
Original file line number Diff line number Diff line change
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 @@ -122,6 +123,7 @@ data ApiErrorInfo
| NetworkQueryFailed
| NetworkUnreachable
| NodeNotYetInRecentEra
!ApiErrorNodeNotYetInRecentEra
| NoRootKey
| NoSuchPool
| NoSuchTransaction
Expand Down Expand Up @@ -224,3 +226,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/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,7 +2057,7 @@ buildAndSignTransactionPure
, builtSealedTx = signedTx
}
where
anyCardanoEra = Write.fromAnyRecentEra era
anyCardanoEra = Write.toAnyCardanoEra era

buildTransaction
:: forall s era.
Expand Down
35 changes: 32 additions & 3 deletions lib/wallet/src/Cardano/Wallet/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ module Cardano.Wallet.Write.Tx
, AnyRecentEra (..)
, InAnyRecentEra (..)
, asAnyRecentEra
, fromAnyRecentEra
, toAnyCardanoEra
, fromAnyCardanoEra
, withInAnyRecentEra
, withRecentEra

Expand Down Expand Up @@ -390,15 +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

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

fromAnyRecentEra :: AnyRecentEra -> Cardano.AnyCardanoEra
fromAnyRecentEra (AnyRecentEra era) = Cardano.AnyCardanoEra (fromRecentEra era)
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

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

6 changes: 6 additions & 0 deletions lib/wallet/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ import Cardano.Wallet.Api.Types.Error
, ApiErrorBalanceTxUnderestimatedFee (..)
, ApiErrorInfo (..)
, ApiErrorMessage (..)
, ApiErrorNodeNotYetInRecentEra (..)
, ApiErrorSharedWalletNoSuchCosigner (..)
, ApiErrorTxOutputLovelaceInsufficient (..)
)
Expand Down Expand Up @@ -620,6 +621,7 @@ spec = do
jsonTest @ApiErrorSharedWalletNoSuchCosigner
jsonTest @ApiErrorTxOutputLovelaceInsufficient
jsonTest @ApiErrorBalanceTxUnderestimatedFee
jsonTest @ApiErrorNodeNotYetInRecentEra
jsonTest @ApiFee
jsonTest @ApiHealthCheck
jsonTest @ApiIncompleteSharedWallet
Expand Down Expand Up @@ -2217,6 +2219,10 @@ instance Arbitrary ApiErrorBalanceTxUnderestimatedFee where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary ApiErrorNodeNotYetInRecentEra where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary ApiTxMetadata where
arbitrary = genericArbitrary
shrink = genericShrink
Expand Down

0 comments on commit 1a267b7

Please sign in to comment.