-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend DB Layer & Wallet to support wallet metadata #158
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
52dddfa
use a record type instead of a tuple for our mocked db
KtorZ 042806b
add correct support for wallet metadata in the wallet layer
KtorZ f805b06
write a few more property to tests WalletMeta: room for abstracting &…
KtorZ b7db50e
define a generic property 'read after put'
KtorZ fee0feb
add generic property "can't put before wallet exists" for db resources
KtorZ 0f8e1e9
add generic 'isolation' property
KtorZ 5af4423
re-organize generic properties around properties (and not resources)
KtorZ ec6374b
add generic property to test sequential insertions to DB
KtorZ 762f785
add generic property for parallel DB insertion
KtorZ c3b1a93
add extra properties to test db initialization functions
KtorZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
|
||
-- | | ||
-- Copyright: © 2018-2019 IOHK | ||
|
@@ -10,47 +11,78 @@ | |
module Cardano.Wallet.DB | ||
( DBLayer(..) | ||
, PrimaryKey(..) | ||
, ErrPutTxHistory(..) | ||
, ErrNoSuchWallet(..) | ||
, ErrWalletAlreadyExists(..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Primitive.Model | ||
( Wallet ) | ||
import Cardano.Wallet.Primitive.Types | ||
( Hash, Tx, TxMeta, WalletId ) | ||
( Hash, Tx, TxMeta, WalletId, WalletMetadata ) | ||
import Control.Monad.Trans.Except | ||
( ExceptT ) | ||
import Data.Map.Strict | ||
( Map ) | ||
import GHC.TypeLits | ||
( Symbol ) | ||
|
||
|
||
-- | 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 DBLayer m s = DBLayer | ||
{ putCheckpoint | ||
{ createWallet | ||
:: PrimaryKey WalletId | ||
-> Wallet s | ||
-> m () | ||
-> WalletMetadata | ||
-> ExceptT (ErrWalletAlreadyExists "createWallet") m () | ||
-- ^ Initialize a database entry for a given wallet. 'putCheckpoint', | ||
-- 'putWalletMeta' or 'putTxHistory' will actually all fail if they are | ||
-- called _first_ on a wallet. | ||
-- | ||
|
||
, listWallets | ||
:: m [PrimaryKey WalletId] | ||
-- ^ Get the list of all known wallets in the DB, possibly empty. | ||
|
||
, putCheckpoint | ||
:: PrimaryKey WalletId | ||
-> Wallet s | ||
-> ExceptT (ErrNoSuchWallet "putCheckpoint") m () | ||
-- ^ Replace the current checkpoint for a given wallet. We do not handle | ||
-- rollbacks yet, and therefore only stores the latest available | ||
-- checkpoint. | ||
-- | ||
-- If the wallet doesn't exist, this operation returns an error. | ||
|
||
, readCheckpoint | ||
:: PrimaryKey WalletId | ||
-> m (Maybe (Wallet s)) | ||
-- ^ Fetch the most recent checkpoint of a given wallet. Return 'Nothing' | ||
-- if there's no such wallet. | ||
-- ^ Fetch the most recent checkpoint of a given wallet. | ||
-- | ||
-- Return 'Nothing' if there's no such wallet. | ||
|
||
, readWallets | ||
:: m [PrimaryKey WalletId] | ||
-- ^ Get the list of all known wallets in the DB, possibly empty. | ||
, putWalletMeta | ||
:: PrimaryKey WalletId | ||
-> WalletMetadata | ||
-> ExceptT (ErrNoSuchWallet "putWalletMeta") m () | ||
-- ^ Replace an existing wallet metadata with the given one. | ||
-- | ||
-- If the wallet doesn't exist, this operation returns an error | ||
|
||
, readWalletMeta | ||
:: PrimaryKey WalletId | ||
-> m (Maybe WalletMetadata) | ||
-- ^ Fetch a wallet metadata, if they exist. | ||
-- | ||
-- Return 'Nothing' if there's no such wallet. | ||
|
||
, putTxHistory | ||
:: PrimaryKey WalletId | ||
-> Map (Hash "Tx") (Tx, TxMeta) | ||
-> ExceptT ErrPutTxHistory m () | ||
-> ExceptT (ErrNoSuchWallet "putTxHistory") m () | ||
-- ^ Augments the transaction history for a known wallet. | ||
-- | ||
-- If an entry for a particular transaction already exists it is not | ||
|
@@ -61,14 +93,20 @@ data DBLayer m s = DBLayer | |
, readTxHistory | ||
:: PrimaryKey WalletId | ||
-> m (Map (Hash "Tx") (Tx, TxMeta)) | ||
-- ^ Fetch the current transaction history of a known wallet. Returns an | ||
-- empty map if the wallet isn't found. | ||
-- ^ Fetch the current transaction history of a known wallet. | ||
-- | ||
-- Returns an empty map if the wallet isn't found. | ||
} | ||
|
||
-- | Error while trying to insert transaction history in the DB. | ||
newtype ErrPutTxHistory | ||
= ErrNoSuchWallet WalletId | ||
deriving (Show, Eq) | ||
-- | Can't perform given operation because there's no wallet | ||
newtype ErrNoSuchWallet (operation :: Symbol) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat way to encode additional info to which operation the error is related 💯 |
||
= ErrNoSuchWallet WalletId -- Wallet is gone or doesn't exist yet | ||
deriving (Eq, Show) | ||
|
||
-- | Forbidden operation was executed on an already existing wallet | ||
newtype ErrWalletAlreadyExists (operation :: Symbol) | ||
= ErrWalletAlreadyExists WalletId -- Wallet already exists in db | ||
deriving (Eq, Show) | ||
|
||
-- | A primary key which can take many forms depending on the value. This may | ||
-- become a type family as we move forward, but for now, it illustrate that | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also add a note that there is no transaction surrounding
readCheckpoint
andputCheckpoint
because there will be only one thread applying blocks.