Skip to content

Commit

Permalink
[ADP-3359] Move ApiEra type and related functions to separate modul…
Browse files Browse the repository at this point in the history
…e. (#4605)

This PR moves the `ApiEra` type and related functions to a separate
module `Api.Types.Era`.

As a result:
- the `ApiEra` type is co-located only with its related functions;
- the `Api.Types`
[megamodule](https://www.parsonsmatt.org/2019/11/27/keeping_compilation_fast.html)
is now ever-so-slightly smaller.

Additionally:
- functions relating to `ApiEra` are now imported via the `ApiEra`
qualifier;
- the `Api.Types` module does **_not_** re-export symbols from
`Api.Types.Era`.

## Issue

ADP-3359

Follow-on from #4595.
  • Loading branch information
jonathanknowles authored May 23, 2024
2 parents 0f1bdf1 + 5595e4a commit 0d3a9b0
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 85 deletions.
1 change: 1 addition & 0 deletions lib/api/cardano-wallet-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ library
Cardano.Wallet.Api.Types.Amount
Cardano.Wallet.Api.Types.BlockHeader
Cardano.Wallet.Api.Types.Certificate
Cardano.Wallet.Api.Types.Era
Cardano.Wallet.Api.Types.Error
Cardano.Wallet.Api.Types.Key
Cardano.Wallet.Api.Types.MintBurn
Expand Down
14 changes: 7 additions & 7 deletions lib/api/src/Cardano/Wallet/Api/Http/Server/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ import Cardano.Wallet.Api.Types
( ApiCosignerIndex (..)
, ApiCredentialType (..)
, Iso8601Time (..)
, toApiEra
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorBalanceTxUnderestimatedFee (..)
Expand Down Expand Up @@ -191,10 +190,11 @@ import Servant.Server
)

import qualified Cardano.Api as Cardano
import qualified Cardano.Wallet.Api.Types as Api
import qualified Cardano.Wallet.Api.Types.Amount as ApiAmount
import qualified Cardano.Wallet.Api.Types.Era as ApiEra
( allRecentEras
, fromAnyCardanoEra
)
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
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
Expand Down Expand Up @@ -485,8 +485,8 @@ instance IsServerError ErrWriteTxEra where
]
where
info = ApiErrorNodeNotYetInRecentEra
{ nodeEra = toApiEra $ Cardano.AnyCardanoEra era
, supportedRecentEras = Api.allRecentEras
{ nodeEra = ApiEra.fromAnyCardanoEra $ Cardano.AnyCardanoEra era
, supportedRecentEras = ApiEra.allRecentEras
}
ErrPartialTxNotInNodeEra nodeEra ->
apiError err403 TxNotInNodeEra $ T.unwords
Expand Down Expand Up @@ -651,8 +651,8 @@ instance IsServerError ErrPostTx where
e@(ErrPostTxEraUnsupported unsupported) ->
flip (apiError err403) (toText e) $ UnsupportedEra
ApiErrorUnsupportedEra
{ unsupportedEra = toApiEra unsupported
, supportedEras = Api.allRecentEras
{ unsupportedEra = ApiEra.fromAnyCardanoEra unsupported
, supportedEras = ApiEra.allRecentEras
}

instance IsServerError ErrSubmitTransaction where
Expand Down
6 changes: 4 additions & 2 deletions lib/api/src/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ import Cardano.Wallet.Api.Types
, getApiMnemonicT
, toApiAsset
, toApiDecodeTransactionPostData
, toApiEra
, toApiNetworkParameters
, toApiUtxoStatistics
)
Expand Down Expand Up @@ -911,6 +910,9 @@ import qualified Cardano.Wallet.Address.Discovery.Sequential as Seq
import qualified Cardano.Wallet.Address.Discovery.Shared as Shared
import qualified Cardano.Wallet.Api.Types as Api
import qualified Cardano.Wallet.Api.Types.Amount as ApiAmount
import qualified Cardano.Wallet.Api.Types.Era as ApiEra
( fromAnyCardanoEra
)
import qualified Cardano.Wallet.Api.Types.WalletAssets as ApiWalletAssets
import qualified Cardano.Wallet.DB as W
import qualified Cardano.Wallet.Delegation as WD
Expand Down Expand Up @@ -4406,7 +4408,7 @@ getNetworkInformation nid
, Api.nextEpoch = snd <$> nowInfo
, Api.nodeTip = apiNodeTip
, Api.networkTip = fst <$> nowInfo
, Api.nodeEra = toApiEra nodeEra
, Api.nodeEra = ApiEra.fromAnyCardanoEra nodeEra
, Api.networkInfo =
Api.ApiNetworkInfo
( case nid of
Expand Down
58 changes: 4 additions & 54 deletions lib/api/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ module Cardano.Wallet.Api.Types
, ApiDecodedTransaction (..)
, ApiDelegationAction (..)
, ApiDeregisterPool (..)
, ApiEra (..)
, ApiEraInfo (..)
, ApiExternalCertificate (..)
, ApiExternalInput (..)
Expand Down Expand Up @@ -167,8 +166,6 @@ module Cardano.Wallet.Api.Types
, ApiWithdrawalGeneral (..)
, ApiWithdrawalPostData (..)
, ApiRewardAccount (..)
, fromApiEra
, allRecentEras
, Iso8601Time (..)
, KeyFormat (..)
, MaintenanceAction (..)
Expand All @@ -180,7 +177,6 @@ module Cardano.Wallet.Api.Types
, SettingsPutData (..)
, toApiAsset
, toApiAssetMetadata
, toApiEra
, toApiNetworkParameters
, toApiUtxoStatistics
, VerificationKeyHashing (..)
Expand Down Expand Up @@ -272,9 +268,7 @@ import Cardano.Address.Script
, ValidationLevel (..)
)
import Cardano.Api
( AnyCardanoEra (..)
, CardanoEra (..)
, StakeAddress
( StakeAddress
, deserialiseFromBech32
, proxyToAsType
, serialiseToBech32
Expand Down Expand Up @@ -360,6 +354,9 @@ import Cardano.Wallet.Api.Types.Certificate
, ApiRegisterPool (..)
, ApiRewardAccount (..)
)
import Cardano.Wallet.Api.Types.Era
( ApiEra (..)
)
import Cardano.Wallet.Api.Types.Key
( ApiAccountKey (..)
, ApiAccountKeyShared (..)
Expand Down Expand Up @@ -577,9 +574,6 @@ import Data.Proxy
import Data.Quantity
( Quantity (..)
)
import Data.Set
( Set
)
import Data.String
( IsString
)
Expand Down Expand Up @@ -675,16 +669,13 @@ 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 @@ -1619,47 +1610,6 @@ newtype ApiBlockInfo = ApiBlockInfo
deriving anyclass NFData
deriving Show via (Quiet ApiBlockInfo)

data ApiEra
= ApiByron
| ApiShelley
| ApiAllegra
| ApiMary
| ApiAlonzo
| ApiBabbage
| ApiConway
deriving (Data, Show, Eq, Generic, Enum, Ord, Bounded)
deriving anyclass NFData

toApiEra :: AnyCardanoEra -> ApiEra
toApiEra (AnyCardanoEra ByronEra) = ApiByron
toApiEra (AnyCardanoEra ShelleyEra) = ApiShelley
toApiEra (AnyCardanoEra AllegraEra) = ApiAllegra
toApiEra (AnyCardanoEra MaryEra) = ApiMary
toApiEra (AnyCardanoEra AlonzoEra) = ApiAlonzo
toApiEra (AnyCardanoEra BabbageEra) = ApiBabbage
toApiEra (AnyCardanoEra ConwayEra) = ApiConway

fromApiEra :: ApiEra -> AnyCardanoEra
fromApiEra ApiByron = AnyCardanoEra ByronEra
fromApiEra ApiShelley = AnyCardanoEra ShelleyEra
fromApiEra ApiAllegra = AnyCardanoEra AllegraEra
fromApiEra ApiMary = AnyCardanoEra MaryEra
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 '_' }
instance ToJSON ApiEra where
toJSON = genericToJSON $ Aeson.defaultOptions
{ constructorTagModifier = drop 4 . camelTo2 '_' }

data ApiNetworkInfo = ApiNetworkInfo
{ networkId :: !Text
, protocolMagic :: !Integer
Expand Down
120 changes: 120 additions & 0 deletions lib/api/src/Cardano/Wallet/Api/Types/Era.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

-- |
-- Copyright:
-- © 2018-2023 IOHK
-- © 2023-2024 Cardano Foundation
-- License:
-- Apache-2.0
--
-- This module provides API types and functions relating to eras.
--
module Cardano.Wallet.Api.Types.Era
( ApiEra (..)
, fromAnyCardanoEra
, toAnyCardanoEra
, allRecentEras
)
where

import Cardano.Api
( AnyCardanoEra (AnyCardanoEra)
, CardanoEra (..)
)
import Control.DeepSeq
( NFData
)
import Data.Aeson
( FromJSON (parseJSON)
, Options (constructorTagModifier)
, ToJSON (toJSON)
, camelTo2
, genericParseJSON
, genericToJSON
)
import Data.Data
( Data
)
import Data.Eq
( Eq
)
import Data.Function
( ($)
, (.)
)
import Data.List
( drop
)
import Data.Ord
( Ord
)
import Data.Set
( Set
)
import GHC.Generics
( Generic
)
import Prelude
( Bounded
, Enum
)
import Text.Show
( Show
)

import qualified Data.Aeson as Aeson
import qualified Data.Set as Set
import qualified Internal.Cardano.Write.Tx as Write
( allRecentEras
, toAnyCardanoEra
)

data ApiEra
= ApiByron
| ApiShelley
| ApiAllegra
| ApiMary
| ApiAlonzo
| ApiBabbage
| ApiConway
deriving (Data, Show, Eq, Generic, Enum, Ord, Bounded)
deriving anyclass NFData

instance FromJSON ApiEra where
parseJSON = genericParseJSON $ Aeson.defaultOptions
{ constructorTagModifier = drop 4 . camelTo2 '_' }

instance ToJSON ApiEra where
toJSON = genericToJSON $ Aeson.defaultOptions
{ constructorTagModifier = drop 4 . camelTo2 '_' }

fromAnyCardanoEra :: AnyCardanoEra -> ApiEra
fromAnyCardanoEra = \case
AnyCardanoEra ByronEra -> ApiByron
AnyCardanoEra ShelleyEra -> ApiShelley
AnyCardanoEra AllegraEra -> ApiAllegra
AnyCardanoEra MaryEra -> ApiMary
AnyCardanoEra AlonzoEra -> ApiAlonzo
AnyCardanoEra BabbageEra -> ApiBabbage
AnyCardanoEra ConwayEra -> ApiConway

toAnyCardanoEra :: ApiEra -> AnyCardanoEra
toAnyCardanoEra = \case
ApiByron -> AnyCardanoEra ByronEra
ApiShelley -> AnyCardanoEra ShelleyEra
ApiAllegra -> AnyCardanoEra AllegraEra
ApiMary -> AnyCardanoEra MaryEra
ApiAlonzo -> AnyCardanoEra AlonzoEra
ApiBabbage -> AnyCardanoEra BabbageEra
ApiConway -> AnyCardanoEra ConwayEra

-- | The complete set of recent eras.
--
allRecentEras :: Set ApiEra
allRecentEras =
Set.map (fromAnyCardanoEra . Write.toAnyCardanoEra) Write.allRecentEras
4 changes: 3 additions & 1 deletion lib/api/src/Cardano/Wallet/Api/Types/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ import Cardano.Wallet.Api.Lib.Options
import Cardano.Wallet.Api.Types
( ApiCosignerIndex (..)
, ApiCredentialType (..)
, ApiEra
)
import Cardano.Wallet.Api.Types.Amount
( ApiAmount
)
import Cardano.Wallet.Api.Types.Era
( ApiEra
)
import Cardano.Wallet.Api.Types.WalletAssets
( ApiWalletAssets
)
Expand Down
4 changes: 3 additions & 1 deletion lib/benchmarks/exe/latency-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import Cardano.Wallet.Api.Http.Shelley.Server
)
import Cardano.Wallet.Api.Types
( AddressAmount (..)
, ApiEra
, ApiMnemonicT (..)
, ApiT (..)
, ApiTransaction
Expand All @@ -61,6 +60,9 @@ import Cardano.Wallet.Api.Types
import Cardano.Wallet.Api.Types.Amount
( ApiAmount (..)
)
import Cardano.Wallet.Api.Types.Era
( ApiEra
)
import Cardano.Wallet.Api.Types.WalletAssets
( ApiWalletAssets (..)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Cardano.Address
import Cardano.CLI
( Port (..)
)
import Cardano.Wallet.Api.Types
import Cardano.Wallet.Api.Types.Era
( ApiEra
)
import Cardano.Wallet.Faucet
Expand Down
4 changes: 3 additions & 1 deletion lib/integration/framework/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ import Cardano.Wallet.Api.Types
, ApiByronWallet
, ApiCoinSelection
, ApiConstructTransaction
, ApiEra (..)
, ApiFee (..)
, ApiMaintenanceAction (..)
, ApiNetworkInformation
Expand All @@ -334,6 +333,9 @@ import Cardano.Wallet.Api.Types
import Cardano.Wallet.Api.Types.Amount
( ApiAmount (ApiAmount)
)
import Cardano.Wallet.Api.Types.Era
( ApiEra (..)
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorInfo (..)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ where

import Prelude

import Cardano.Wallet.Api.Types
import Cardano.Wallet.Api.Types.Era
( ApiEra (..)
)
import Test.Hspec.Core.Spec
Expand Down
Loading

0 comments on commit 0d3a9b0

Please sign in to comment.