diff --git a/lib/core/cardano-wallet-core.cabal b/lib/core/cardano-wallet-core.cabal index 59790489c33..d9f572a5b0d 100644 --- a/lib/core/cardano-wallet-core.cabal +++ b/lib/core/cardano-wallet-core.cabal @@ -91,7 +91,6 @@ library Cardano.Pool.DB.Model Cardano.Pool.DB.Sqlite Cardano.Pool.DB.Sqlite.TH - Cardano.Pool.DB.Sqlite.Types Cardano.Pool.Metrics Cardano.Wallet Cardano.Wallet.Api diff --git a/lib/core/src/Cardano/Pool/DB/Sqlite.hs b/lib/core/src/Cardano/Pool/DB/Sqlite.hs index 01df492f0a6..b2762de9caa 100644 --- a/lib/core/src/Cardano/Pool/DB/Sqlite.hs +++ b/lib/core/src/Cardano/Pool/DB/Sqlite.hs @@ -38,7 +38,7 @@ import Cardano.DB.Sqlite ) import Cardano.Pool.DB ( DBLayer (..), ErrPointAlreadyExists (..) ) -import Cardano.Pool.DB.Sqlite.Types +import Cardano.Wallet.DB.Sqlite.Types ( BlockId (..) ) import Cardano.Wallet.Primitive.Types ( BlockHeader (..), EpochNo (..), PoolId, SlotId (..) ) diff --git a/lib/core/src/Cardano/Pool/DB/Sqlite/TH.hs b/lib/core/src/Cardano/Pool/DB/Sqlite/TH.hs index 415ffed05fb..937ed234850 100644 --- a/lib/core/src/Cardano/Pool/DB/Sqlite/TH.hs +++ b/lib/core/src/Cardano/Pool/DB/Sqlite/TH.hs @@ -20,7 +20,7 @@ module Cardano.Pool.DB.Sqlite.TH where import Prelude -import Cardano.Pool.DB.Sqlite.Types +import Cardano.Wallet.DB.Sqlite.Types ( sqlSettings' ) import Data.Word ( Word32, Word64 ) @@ -31,7 +31,7 @@ import Database.Persist.TH import GHC.Generics ( Generic (..) ) -import qualified Cardano.Pool.DB.Sqlite.Types as W +import qualified Cardano.Wallet.DB.Sqlite.Types as W import qualified Cardano.Wallet.Primitive.Types as W share diff --git a/lib/core/src/Cardano/Pool/DB/Sqlite/Types.hs b/lib/core/src/Cardano/Pool/DB/Sqlite/Types.hs deleted file mode 100644 index 5e5fea2b168..00000000000 --- a/lib/core/src/Cardano/Pool/DB/Sqlite/Types.hs +++ /dev/null @@ -1,158 +0,0 @@ -{-# LANGUAGE DataKinds #-} -{-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE TypeApplications #-} -{-# OPTIONS_GHC -fno-warn-orphans #-} - --- | --- Copyright: © 2018-2019 IOHK --- License: Apache-2.0 --- --- This module contains instances and types necessary for storing wallets in a --- SQL database with Persistent. --- --- It's in a separate module due to the GHC stage restriction. --- --- The ToJSON/FromJSON and Read instance orphans exist due to class constraints --- on Persistent functions. - -module Cardano.Pool.DB.Sqlite.Types - ( sqlSettings' - , BlockId (..) - ) where - -import Prelude - -import Cardano.Wallet.Primitive.Types - ( EpochLength (..) - , EpochNo - , Hash (..) - , PoolId - , SlotId (..) - , SlotNo - , flatSlot - , fromFlatSlot - ) -import Control.Monad - ( (>=>) ) -import Data.Aeson - ( FromJSON, ToJSON, genericParseJSON, genericToJSON ) -import Data.Bifunctor - ( first ) -import Data.Proxy - ( Proxy (..) ) -import Data.Text - ( Text ) -import Data.Text.Class - ( FromText (..) - , TextDecodingError (..) - , ToText (..) - , fromTextMaybe - , getTextDecodingError - ) -import Data.Word - ( Word32, Word64 ) -import Database.Persist.Sqlite - ( PersistField (..), PersistFieldSql (..), PersistValue ) -import Database.Persist.TH - ( MkPersistSettings (..), sqlSettings ) -import GHC.Generics - ( Generic ) -import Web.HttpApiData - ( FromHttpApiData (..), ToHttpApiData (..) ) -import Web.PathPieces - ( PathPiece (..) ) - -import qualified Data.Aeson as Aeson -import qualified Data.Aeson.Types as Aeson -import qualified Data.Text as T - ----------------------------------------------------------------------------- - --- | Settings for generating the Persistent types. -sqlSettings' :: MkPersistSettings -sqlSettings' = sqlSettings { mpsPrefixFields = False } - ----------------------------------------------------------------------------- --- Helper functions - --- | 'fromText' but with a simpler error type. -fromText' :: FromText a => Text -> Either Text a -fromText' = first (T.pack . getTextDecodingError) . fromText - --- | 'fromPersistValue' defined in terms of 'fromText' -fromPersistValueFromText :: FromText a => PersistValue -> Either Text a -fromPersistValueFromText = fromPersistValue >=> fromTextWithErr - where fromTextWithErr = first ("not a valid value: " <>) . fromText' - --- | Aeson parser defined in terms of 'fromText' -aesonFromText :: FromText a => String -> Aeson.Value -> Aeson.Parser a -aesonFromText what = Aeson.withText what $ either (fail . show) pure . fromText - ----------------------------------------------------------------------------- --- PoolId - -instance PersistField PoolId where - toPersistValue = toPersistValue . toText - fromPersistValue = fromPersistValueFromText - -instance PersistFieldSql PoolId where - sqlType _ = sqlType (Proxy @Text) - -instance Read PoolId where - readsPrec _ = error "readsPrec stub needed for persistent" - -instance PathPiece PoolId where - fromPathPiece = fromTextMaybe - toPathPiece = toText - -instance ToJSON PoolId where toJSON = Aeson.String . toText -instance FromJSON PoolId where parseJSON = aesonFromText "PoolId" - ----------------------------------------------------------------------------- --- BlockId - --- Wraps Hash "BlockHeader" because the persistent dsl doesn't like it raw. -newtype BlockId = BlockId { getBlockId :: Hash "BlockHeader" } - deriving (Show, Eq, Ord, Generic) - -instance PersistField BlockId where - toPersistValue = toPersistValue . toText . getBlockId - fromPersistValue = fmap BlockId <$> fromPersistValueFromText - -instance PersistFieldSql BlockId where - sqlType _ = sqlType (Proxy @Text) - -instance Read BlockId where - readsPrec _ = error "readsPrec stub needed for persistent" - ----------------------------------------------------------------------------- --- SlotId - --- | As a short-to-medium term solution of persisting 'SlotId', we use --- 'flatSlot' with an artificial epochLength. I.e. /not the same epochLength as --- the blockchain/. This is just for the sake of storing the 64 bit epoch and --- the 16 bit slot inside a single 64-bit field. -artificialEpochLength :: EpochLength -artificialEpochLength = EpochLength $ fromIntegral (maxBound :: Word32) - -instance PersistFieldSql SlotId where - sqlType _ = sqlType (Proxy @Word64) - -instance PersistField SlotId where - toPersistValue = toPersistValue . flatSlot artificialEpochLength - fromPersistValue = fmap (fromFlatSlot artificialEpochLength) . fromPersistValue - -instance ToJSON SlotId where toJSON = genericToJSON Aeson.defaultOptions -instance FromJSON SlotId where parseJSON = genericParseJSON Aeson.defaultOptions -instance ToJSON SlotNo where toJSON = genericToJSON Aeson.defaultOptions -instance FromJSON SlotNo where parseJSON = genericParseJSON Aeson.defaultOptions -instance ToJSON EpochNo where toJSON = genericToJSON Aeson.defaultOptions -instance FromJSON EpochNo where parseJSON = genericParseJSON Aeson.defaultOptions -instance ToHttpApiData SlotId where - toUrlPiece = error "toUrlPiece stub needed for persistent" -instance FromHttpApiData SlotId where - parseUrlPiece = error "parseUrlPiece stub needed for persistent" -instance PathPiece SlotId where - toPathPiece = error "toPathPiece stub needed for persistent" - fromPathPiece = error "fromPathPiece stub needed for persistent" diff --git a/lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs b/lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs index b1af344c31b..e6d78f203bb 100644 --- a/lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs +++ b/lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs @@ -262,9 +262,15 @@ instance PersistFieldSql SlotId where artificialEpochLength :: EpochLength artificialEpochLength = EpochLength maxBound +persistSlotId :: SlotId -> PersistValue +persistSlotId = toPersistValue . flatSlot artificialEpochLength + +unPersistSlotId :: PersistValue -> Either Text SlotId +unPersistSlotId = fmap (fromFlatSlot artificialEpochLength) . fromPersistValue + instance PersistField SlotId where - toPersistValue = toPersistValue . flatSlot artificialEpochLength - fromPersistValue = fmap (fromFlatSlot artificialEpochLength) . fromPersistValue + toPersistValue = persistSlotId + fromPersistValue = unPersistSlotId instance ToJSON SlotId where toJSON = genericToJSON defaultOptions @@ -284,6 +290,15 @@ instance ToJSON EpochNo where instance FromJSON EpochNo where parseJSON = fmap EpochNo . parseJSON +instance ToHttpApiData SlotId where + toUrlPiece = error "toUrlPiece stub needed for persistent" +instance FromHttpApiData SlotId where + parseUrlPiece = error "parseUrlPiece stub needed for persistent" +instance PathPiece SlotId where + toPathPiece = error "toPathPiece stub needed for persistent" + fromPathPiece = error "fromPathPiece stub needed for persistent" + + ---------------------------------------------------------------------------- -- SyncProgress