Skip to content
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 10 commits into from
Apr 9, 2019

Conversation

KtorZ
Copy link
Member

@KtorZ KtorZ commented Apr 5, 2019

Issue Number

#145

Overview

  • I have reviewed a bit the interface of the DB layer to be more consistent across various "resources"
  • I have extended the db layer to handle wallet metadata, and fixed the MVar implementation accordingly
  • I have updated tests and wrote a few more for the metadata operations

Comments

This is still very drafty, the MVar tests are a lot of the same thing. I'd like to spend some time refactoring them in order to abstract away the core properties we are testing on each resource, and make them more readable.

@KtorZ KtorZ changed the title Ktor z/145/wallet metadata Extend DB Layer & Wallet to support wallet metadata Apr 5, 2019
@KtorZ KtorZ self-assigned this Apr 5, 2019
This slightly increases the readability and makes future (incoming) extensions easier
@KtorZ KtorZ marked this pull request as ready for review April 8, 2019 15:34
@KtorZ KtorZ force-pushed the KtorZ/145/wallet-metadata branch from d163142 to 5b3d203 Compare April 8, 2019 15:35
@KtorZ KtorZ mentioned this pull request Apr 8, 2019
3 tasks
Copy link
Contributor

@paweljakubas paweljakubas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - really fond of the construct :

newtype ErrSome (operation :: Symbol)

@@ -62,13 +94,18 @@ data DBLayer m s = DBLayer
:: 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.
-- empty map if the wallet istn't found.
Copy link
Contributor

@paweljakubas paweljakubas Apr 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was not necessary amendment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Erf. Seems like a merge resolution gone wrong.

= ErrNoSuchWallet WalletId
deriving (Show, Eq)
-- | Can't perform given operation because there's no wallet
newtype ErrNoSuchWallet (operation :: Symbol)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 💯

data Database s = Database
{ wallet :: Wallet s
, metadata :: WalletMetadata
, txHistory :: Map (Hash "Tx") (Tx, TxMeta)
Copy link
Contributor

@paweljakubas paweljakubas Apr 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to these days cannot understand why we need TxMeta (not related to this PR)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need them because our users need them ;)
Those pieces of information (direction, timestamp, total amount etc..) are very valuable to API consumers.

Wallet Metadata
-----------------------------------------------------------------------}

, putWalletMeta = \key@(PrimaryKey wid) meta -> ExceptT $ do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if fine-tuning type with "Symbol" would not do the trick here to embrace one impl of putCheckpoint and readCheckpoint for both checkpoint and metadata - the implementations are very close ....

putCheckpoint db key cp1
txs' <- readTxHistory db key
txs' `shouldBe` txs
dbPutCheckpointIsolationProp db key (cp,meta,txs) cp' = monadicIO $ liftIO $ do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a number of tests that are permutations of put{TxHistory, WalletMeta, Checkpoint}. Basically, we have :

  1. CreateWallet
  2. Two puts
  3. Two reads where one does not overlap with one of 2.
    I wonder if we can replace all those tests with one, where we create wallet, than shuffle list of pairs (operation and arg like txs, cp meta). Moreover, we can make it more general. For instance, generate :
([Wallet DummyState], [WalletMetadata], [Map (Hash "Tx") (Tx, TxMeta)])

and even make sure the lengths of those three lists are different. then construct operations with generated values using those. Then shuffle. In that case we will test a number of what should be independent operations, some of them updating and at the end we expect last values of each list to be returned upon corresponding reads ...

Of course this can be a separate PR - but I think it would be valuable to have those tests, mainly because we will end up with one sequential test and one parallel test for a lot of permutations and multiple updates

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! That's what I mentioned to you on Slack. I am not yet fully satisfied with the testing situation here, although it tests things, we can feel the duplication and weight of the tests here.
Although it's tricky to come up with a unified approach for operation agnostic properties here, i think it's valuable in the long run.

@KtorZ
Copy link
Member Author

KtorZ commented Apr 9, 2019

Merging as-is for now. Will tackle tests refactoring as debts (adding this to the blackboard).

Copy link
Contributor

@rvl rvl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

putCheckpoint db (PrimaryKey wid) cp'
unsafeRunExceptT $ putTxHistory db (PrimaryKey wid) txs -- Safe after ^
-- FIXME
-- Note that, the two calls below are _safe_ under the assumption that
Copy link
Contributor

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 and putCheckpoint because there will be only one thread applying blocks.

@KtorZ KtorZ force-pushed the KtorZ/145/wallet-metadata branch from 5b3d203 to 19e9502 Compare April 9, 2019 10:39
@KtorZ KtorZ force-pushed the KtorZ/145/wallet-metadata branch from 19e9502 to c3b1a93 Compare April 9, 2019 10:48
@KtorZ
Copy link
Member Author

KtorZ commented Apr 9, 2019

@paweljakubas I did take a shot at refactoring the tests to make them easier to read and be less-redundant. I actually caught an implementation bug doing so :)
It's kinda nice now, and it'll nicely extend with new DB operations.

@KtorZ KtorZ merged commit eb50d4d into master Apr 9, 2019
@KtorZ KtorZ deleted the KtorZ/145/wallet-metadata branch April 9, 2019 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants