Skip to content
Permalink
Browse files

Fix the PersistState class

  • Loading branch information...
rvl committed May 16, 2019
1 parent 80bcb7f commit 449606c4047b716c4dc1ad7cd5c60aa4b0c26991
@@ -12,7 +12,6 @@ module Cardano.Wallet.DB
( -- * Interface
DBLayer(..)
, PrimaryKey(..)
, PersistState(..)

-- * Errors
, ErrNoSuchWallet(..)
@@ -31,8 +30,6 @@ import Cardano.Wallet.Primitive.Types
( Hash, Tx, TxId, TxMeta, WalletId, WalletMetadata )
import Control.DeepSeq
( NFData )
import Control.Monad.IO.Class
( MonadIO (..) )
import Control.Monad.Trans.Except
( ExceptT )
import Data.Map.Strict
@@ -42,7 +39,7 @@ import Data.Map.Strict
-- | A Database interface for storing various things in a DB. In practice,
-- we'll need some extra contraints on the wallet state that allows us to
-- serialize and unserialize it (e.g. @forall s. (Serialize s) => ...@)
data (IsOurs s, NFData s, Show s, PersistState s, TxId t) => DBLayer m s t = DBLayer
data (IsOurs s, NFData s, Show s, TxId t) => DBLayer m s t = DBLayer
{ createWallet
:: PrimaryKey WalletId
-> Wallet s t
@@ -153,13 +150,3 @@ newtype ErrWalletAlreadyExists
-- (like for instance, the last known network tip).
newtype PrimaryKey key = PrimaryKey key
deriving (Eq, Ord)

----------------------------------------------------------------------------

class PersistState s where
putState :: MonadIO m => Wallet s t -> s -> m ()
readState :: MonadIO m => Wallet s t -> m (Maybe s)
addressScheme :: Wallet s t -> Int
-- putState :: MonadIO m => (WalletId, SlotId) -> s -> ReaderT SqlBackend m ()
-- readState :: MonadIO m => (WalletId, SlotId) -> ReaderT SqlBackend m (Maybe s)
-- addressScheme :: s -> AddressScheme
@@ -21,7 +21,6 @@ import Cardano.Wallet.DB
, ErrNoSuchWallet (..)
, ErrWalletAlreadyExists (..)
, PrimaryKey (..)
, PersistState (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..), Key, XPrv )
@@ -54,7 +53,7 @@ data Database s t = Database
-- | Instantiate a new in-memory "database" layer that simply stores data in
-- a local MVar. Data vanishes if the software is shut down.
newDBLayer
:: forall s t. (IsOurs s, PersistState s, NFData s, Show s, TxId t)
:: forall s t. (IsOurs s, NFData s, Show s, TxId t)
=> IO (DBLayer IO s t)
newDBLayer = do
lock <- newMVar ()
@@ -18,6 +18,7 @@

module Cardano.Wallet.DB.Sqlite
( newDBLayer
, PersistState(..)
) where

import Prelude
@@ -28,7 +29,6 @@ import Cardano.Wallet.DB
( DBLayer (..)
, ErrNoSuchWallet (..)
, ErrWalletAlreadyExists (..)
, PersistState (..)
, PrimaryKey (..)
)
import Cardano.Wallet.DB.SqliteTypes
@@ -63,6 +63,8 @@ import Data.Generics.Internal.VL.Lens
( (^.) )
import Data.List
( sortOn )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Text
@@ -701,3 +703,24 @@ selectTxHistory wid = do
outs <- fmap entityVal <$> selectList [TxOutputTableTxId <-. txids]
[Asc TxOutputTableTxId, Asc TxOutputTableIndex]
pure $ txHistoryFromEntity metas ins outs



----------------------------------------------------------------------------

checkpointId :: Checkpoint -> (W.WalletId, W.SlotId)
checkpointId cp = (checkpointTableWalletId cp, checkpointTableSlot cp)

class PersistState s where
insertState :: MonadIO m => (W.WalletId, W.SlotId) -> s -> ReaderT SqlBackend m ()
selectState :: MonadIO m => (W.WalletId, W.SlotId) -> ReaderT SqlBackend m (Maybe s)
deleteState :: MonadIO m => Proxy s -> W.WalletId -> ReaderT SqlBackend m ()
addressScheme :: Proxy s -> AddressScheme

instance PersistState (W.SeqState t) where
insertState (_wid, _sl) _st = do
pure ()
selectState (_wid, _sl) = do
pure Nothing
deleteState _ wid = deleteCascadeWhere [SeqStateTableWalletId ==. wid]
addressScheme _ = Sequential
@@ -23,10 +23,6 @@ import Cardano.Wallet.Primitive.Types
)
import Control.Monad
( (>=>) )
import Control.Monad.IO.Class
( MonadIO (..) )
import Control.Monad.Trans.Reader
( ReaderT (..) )
import Data.Aeson
( FromJSON (..)
, ToJSON (..)
@@ -56,7 +52,7 @@ import Data.Text.Class
import Data.Word
( Word64, Word8 )
import Database.Persist.Sqlite
( PersistField (..), PersistFieldSql (..), PersistValue, SqlBackend )
( PersistField (..), PersistFieldSql (..), PersistValue )
import GHC.Generics
( Generic )
import Web.HttpApiData

0 comments on commit 449606c

Please sign in to comment.
You can’t perform that action at this time.