-
Notifications
You must be signed in to change notification settings - Fork 211
/
MVar.hs
238 lines (194 loc) · 8.78 KB
/
MVar.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
-- |
-- Copyright: © 2018-2020 IOHK
-- License: Apache-2.0
--
-- Dummy implementation of the database-layer, using 'MVar'. This may be good
-- for testing to compare with an implementation on a real data store, or to use
-- when compiling the wallet for targets which don't have SQLite.
module Cardano.Wallet.DB.MVar
( newDBLayer
) where
import Prelude
import Cardano.Address.Derivation
( XPrv )
import Cardano.Wallet.DB
( DBLayer (..)
, ErrNoSuchWallet (..)
, ErrRemovePendingTx (..)
, ErrWalletAlreadyExists (..)
, PrimaryKey (..)
)
import Cardano.Wallet.DB.Model
( Database
, Err (..)
, ErrErasePendingTx (..)
, ModelOp
, emptyDatabase
, mCheckWallet
, mInitializeWallet
, mIsStakeKeyRegistered
, mListCheckpoints
, mListWallets
, mPutCheckpoint
, mPutDelegationCertificate
, mPutDelegationRewardBalance
, mPutPrivateKey
, mPutProtocolParameters
, mPutTxHistory
, mPutWalletMeta
, mReadCheckpoint
, mReadDelegationRewardBalance
, mReadPrivateKey
, mReadProtocolParameters
, mReadTxHistory
, mReadWalletMeta
, mRemovePendingTx
, mRemoveWallet
, mRollbackTo
)
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..) )
import Cardano.Wallet.Primitive.Types
( Hash, SortOrder (..), TransactionInfo (..), WalletId, wholeRange )
import Control.Concurrent.MVar
( MVar, modifyMVar, newMVar, withMVar )
import Control.DeepSeq
( NFData, deepseq )
import Control.Exception
( Exception, throwIO )
import Control.Monad.Trans.Except
( ExceptT (..) )
-- | 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 k. (NFData (k 'RootK XPrv), NFData s)
=> IO (DBLayer IO s k)
newDBLayer = do
lock <- newMVar ()
db <- newMVar (emptyDatabase :: Database (PrimaryKey WalletId) s (k 'RootK XPrv, Hash "encryption"))
return $ DBLayer
{-----------------------------------------------------------------------
Wallets
-----------------------------------------------------------------------}
{ initializeWallet = \pk cp meta txs txp -> ExceptT $ do
cp `deepseq` meta `deepseq`
alterDB errWalletAlreadyExists db (mInitializeWallet pk cp meta txs txp)
, removeWallet = ExceptT . alterDB errNoSuchWallet db . mRemoveWallet
, listWallets = readDB db mListWallets
{-----------------------------------------------------------------------
Checkpoints
-----------------------------------------------------------------------}
, putCheckpoint = \pk cp -> ExceptT $ do
cp `deepseq` alterDB errNoSuchWallet db (mPutCheckpoint pk cp)
, readCheckpoint = readDB db . mReadCheckpoint
, listCheckpoints = readDB db . mListCheckpoints
, rollbackTo = \pk pt -> ExceptT $
alterDB errNoSuchWallet db (mRollbackTo pk pt)
, prune = \_ -> error "MVar.prune: not implemented"
{-----------------------------------------------------------------------
Wallet Metadata
-----------------------------------------------------------------------}
, putWalletMeta = \pk meta -> ExceptT $ do
meta `deepseq` alterDB errNoSuchWallet db (mPutWalletMeta pk meta)
, readWalletMeta = readDB db . mReadWalletMeta
, putDelegationCertificate = \pk cert sl -> ExceptT $ do
cert `deepseq` sl `deepseq`
alterDB errNoSuchWallet db (mPutDelegationCertificate pk cert sl)
, isStakeKeyRegistered =
ExceptT . alterDB errNoSuchWallet db . mIsStakeKeyRegistered
{-----------------------------------------------------------------------
Tx History
-----------------------------------------------------------------------}
, putTxHistory = \pk txh -> ExceptT $ do
txh `deepseq` alterDB errNoSuchWallet db (mPutTxHistory pk txh)
, readTxHistory = \pk order range mstatus ->
readDB db (mReadTxHistory pk order range mstatus)
, getTx = \pk tid -> ExceptT $
alterDB errNoSuchWallet db (mCheckWallet pk) >>= \case
Left err -> pure $ Left err
Right _ -> do
txInfos <- readDB db (mReadTxHistory pk Descending wholeRange Nothing)
let txPresent (TransactionInfo{..}) = txInfoId == tid
case filter txPresent txInfos of
[] -> pure $ Right Nothing
t:_ -> pure $ Right $ Just t
{-----------------------------------------------------------------------
Keystore
-----------------------------------------------------------------------}
, putPrivateKey = \pk prv -> ExceptT $ do
prv `deepseq` alterDB errNoSuchWallet db (mPutPrivateKey pk prv)
, readPrivateKey = readDB db . mReadPrivateKey
{-----------------------------------------------------------------------
Pending Tx
-----------------------------------------------------------------------}
, removePendingTx = \pk tid -> ExceptT $ do
alterDB errCannotRemovePendingTx db (mRemovePendingTx pk tid)
{-----------------------------------------------------------------------
Protocol Parameters
-----------------------------------------------------------------------}
, putProtocolParameters = \pk txp -> ExceptT $ do
txp `deepseq`
alterDB errNoSuchWallet db (mPutProtocolParameters pk txp)
, readProtocolParameters = readDB db . mReadProtocolParameters
{-----------------------------------------------------------------------
Delegation Rewards
-----------------------------------------------------------------------}
, putDelegationRewardBalance = \pk amt -> ExceptT $
alterDB errNoSuchWallet db (mPutDelegationRewardBalance pk amt)
, readDelegationRewardBalance =
readDB db . mReadDelegationRewardBalance
{-----------------------------------------------------------------------
Execution
-----------------------------------------------------------------------}
, atomically = \action -> withMVar lock $ \() -> action
}
-- | Apply an operation to the model database, then update the mutable variable.
alterDB
:: (Err (PrimaryKey WalletId) -> Maybe err)
-- ^ Error type converter
-> MVar (Database (PrimaryKey WalletId) s xprv)
-- ^ The database variable
-> ModelOp (PrimaryKey WalletId) s xprv a
-- ^ Operation to run on the database
-> IO (Either err a)
alterDB convertErr db op = modifyMVar db (bubble . op)
where
bubble (Left e, db') = case convertErr e of
Just e' -> pure (db', Left e')
Nothing -> throwIO $ MVarDBError e
bubble (Right a, db') = pure (db', Right a)
-- | Run a query operation on the model database. Any error results are turned
-- into a runtime exception.
readDB
:: MVar (Database (PrimaryKey WalletId) s xprv)
-- ^ The database variable
-> ModelOp (PrimaryKey WalletId) s xprv a
-- ^ Operation to run on the database
-> IO a
readDB db op = alterDB Just db op >>= either (throwIO . MVarDBError) pure
errNoSuchWallet :: Err (PrimaryKey WalletId) -> Maybe ErrNoSuchWallet
errNoSuchWallet (NoSuchWallet (PrimaryKey wid)) = Just (ErrNoSuchWallet wid)
errNoSuchWallet _ = Nothing
errCannotRemovePendingTx :: Err (PrimaryKey WalletId) -> Maybe ErrRemovePendingTx
errCannotRemovePendingTx (CannotRemovePendingTx (ErrErasePendingTxNoSuchWallet (PrimaryKey wid))) =
Just (ErrRemovePendingTxNoSuchWallet (ErrNoSuchWallet wid))
errCannotRemovePendingTx (CannotRemovePendingTx (ErrErasePendingTxNoTx tid)) =
Just (ErrRemovePendingTxNoSuchTransaction tid)
errCannotRemovePendingTx (CannotRemovePendingTx (ErrErasePendingTxNoPendingTx tid)) =
Just (ErrRemovePendingTxTransactionNoMorePending tid)
errCannotRemovePendingTx _ = Nothing
errWalletAlreadyExists
:: Err (PrimaryKey WalletId)
-> Maybe ErrWalletAlreadyExists
errWalletAlreadyExists (WalletAlreadyExists (PrimaryKey wid)) =
Just (ErrWalletAlreadyExists wid)
errWalletAlreadyExists _ = Nothing
-- | Error which happens when model returns an unexpected value.
newtype MVarDBError = MVarDBError (Err (PrimaryKey WalletId))
deriving (Show)
instance Exception MVarDBError