-
Notifications
You must be signed in to change notification settings - Fork 212
/
Properties.hs
837 lines (784 loc) · 28.9 KB
/
Properties.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
-- TODO: https://input-output.atlassian.net/browse/ADP-2841
{-# OPTIONS_GHC -fno-warn-ambiguous-fields #-}
module Cardano.Wallet.DB.Properties
( properties
) where
import Prelude
import Cardano.Wallet.DB
( DBLayer (..), ErrWalletAlreadyExists (..), cleanDB )
import Cardano.Wallet.DB.Arbitrary
( GenState
, GenTxHistory (..)
, InitialCheckpoint (..)
, KeyValPairs (..)
, MockChain (..)
)
import Cardano.Wallet.DB.Pure.Implementation
( filterTxHistory )
import Cardano.Wallet.DB.WalletState
( ErrNoSuchWallet (..) )
import Cardano.Wallet.DummyTarget.Primitive.Types
( dummyGenesisParameters )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey (..) )
import Cardano.Wallet.Primitive.Model
( Wallet, applyBlock, currentTip )
import Cardano.Wallet.Primitive.Types
( BlockHeader (..)
, ChainPoint (..)
, GenesisParameters
, ProtocolParameters
, Slot
, SlotId (..)
, SlotNo (..)
, SortOrder (..)
, WalletId (..)
, WalletMetadata (..)
, WithOrigin (..)
, chainPointFromBlockHeader
, toSlot
, wholeRange
)
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.Tx
( Direction (..)
, TransactionInfo (..)
, Tx (..)
, TxMeta (..)
, TxStatus (..)
, isPending
, toTxHistory
)
import Cardano.Wallet.Unsafe
( unsafeRunExceptT )
import Cardano.Wallet.Util
( ShowFmt (..) )
import Control.Monad
( forM, forM_, void )
import Control.Monad.IO.Class
( liftIO )
import Control.Monad.Trans.Class
( lift )
import Control.Monad.Trans.Except
( ExceptT, mapExceptT, runExceptT )
import Control.Monad.Trans.State.Strict
( evalStateT, get, modify' )
import Data.Bifunctor
( bimap )
import Data.Function
( (&) )
import Data.Functor.Identity
( Identity (..) )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
import Data.Generics.Labels
()
import Data.List
( unfoldr )
import Data.Maybe
( catMaybes, isNothing, mapMaybe )
import Data.Quantity
( Quantity (..) )
import Data.Set
( Set )
import Data.Word
( Word32, Word64 )
import Fmt
( Buildable, blockListF, pretty )
import Test.Hspec
( SpecWith, describe, it, shouldBe, shouldReturn )
import Test.QuickCheck
( Arbitrary (..)
, Gen
, Property
, checkCoverage
, choose
, conjoin
, counterexample
, cover
, elements
, forAll
, label
, property
, suchThat
, (.&&.)
, (===)
, (==>)
)
import Test.QuickCheck.Monadic
( PropertyM, assert, monadicIO, monitor, pick, run )
import UnliftIO.Async
( forConcurrently_ )
import qualified Data.List as L
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
properties
:: (GenState s, Eq s)
=> SpecWith (DBLayer IO s ShelleyKey)
properties = do
describe "Extra Properties about DB initialization" $ do
it "createWallet . listWallets yields expected results"
(property . prop_createListWallet)
it "creating same wallet twice yields an error"
(property . prop_createWalletTwice)
it "removing the same wallet twice yields an error"
(property . prop_removeWalletTwice)
describe "put . read yields a result" $ do
it "Checkpoint" $
property . prop_readAfterPut
(\DBLayer{..} a0 -> mapExceptT atomically . putCheckpoint a0)
(\DBLayer{..} -> atomically . readCheckpoint)
it "Wallet Metadata" $
property . prop_readAfterPut
(\DBLayer{..} a0 -> mapExceptT atomically . putWalletMeta a0)
(\DBLayer{..} -> atomically . fmap (fmap fst) . readWalletMeta)
it "Tx History" $
property . prop_readAfterPut
putTxHistory_
readTxHistory_
it "Private Key" $
property . prop_readAfterPut
(\DBLayer{..} a0 -> mapExceptT atomically . putPrivateKey a0)
(\DBLayer{..} -> atomically . readPrivateKey)
describe "getTx properties" $ do
it "can read after putting tx history for valid tx id" $
property . prop_getTxAfterPutValidTxId
it "cannot read after putting tx history for invalid tx id" $
property . prop_getTxAfterPutInvalidTxId
it "cannot read after putting tx history for invalid wallet id" $
property . prop_getTxAfterPutInvalidWalletId
describe "can't put before wallet exists" $ do
it "Checkpoint" $
property . prop_putBeforeInit
(\DBLayer{..} a0 -> mapExceptT atomically . putCheckpoint a0)
(\DBLayer{..} -> atomically . readCheckpoint)
Nothing
it "Wallet Metadata" $
property . prop_putBeforeInit
(\DBLayer{..} a0 -> mapExceptT atomically . putWalletMeta a0)
(\DBLayer{..} -> atomically . fmap (fmap fst) . readWalletMeta)
Nothing
it "Tx History" $
property . prop_putBeforeInit
putTxHistory_
readTxHistory_
(pure mempty)
it "Private Key" $
property . prop_putBeforeInit
(\DBLayer{..} a0 -> mapExceptT atomically . putPrivateKey a0)
(\DBLayer{..} -> atomically . readPrivateKey)
Nothing
describe "put doesn't affect other resources" $ do
it "Checkpoint vs Wallet Metadata & Tx History & Private Key" $
property . prop_isolation
(\DBLayer{..} a0 -> mapExceptT atomically . putCheckpoint a0)
(\DBLayer{..} -> atomically . readWalletMeta)
readTxHistory_
(\DBLayer{..} -> atomically . readPrivateKey)
it "Wallet Metadata vs Tx History & Checkpoint & Private Key" $
property . prop_isolation
(\DBLayer{..} a0 -> mapExceptT atomically . putWalletMeta a0)
readTxHistory_
(\DBLayer{..} -> atomically . readCheckpoint)
(\DBLayer{..} -> atomically . readPrivateKey)
it "Tx History vs Checkpoint & Wallet Metadata & Private Key" $
property . prop_isolation
putTxHistory_
(\DBLayer{..} -> atomically . readCheckpoint)
(\DBLayer{..} -> atomically . readWalletMeta)
(\DBLayer{..} -> atomically . readPrivateKey)
describe "can't read after delete" $ do
it "Checkpoint" $
property . prop_readAfterDelete
(\DBLayer{..} -> atomically . readCheckpoint)
Nothing
it "Wallet Metadata" $
property . prop_readAfterDelete
(\DBLayer{..} -> atomically . readWalletMeta)
Nothing
it "Tx History" $
property . prop_readAfterDelete
readTxHistory_
(pure mempty)
it "Private Key" $
property . prop_readAfterDelete
(\DBLayer{..} -> atomically . readPrivateKey)
Nothing
describe "sequential puts replace values in order" $ do
it "Checkpoint" $
checkCoverage . prop_sequentialPut
(\DBLayer{..} a0 -> mapExceptT atomically . putCheckpoint a0)
(\DBLayer{..} -> atomically . readCheckpoint)
lrp
it "Wallet Metadata" $
checkCoverage . prop_sequentialPut
(\DBLayer{..} a0 -> mapExceptT atomically . putWalletMeta a0)
(\DBLayer{..} -> atomically . fmap (fmap fst) . readWalletMeta)
lrp
it "Tx History" $
checkCoverage . prop_sequentialPut
putTxHistory_
readTxHistory_
sortedUnions
it "Private Key" $
checkCoverage . prop_sequentialPut
(\DBLayer{..} a0 -> mapExceptT atomically . putPrivateKey a0)
(\DBLayer{..} -> atomically . readPrivateKey)
lrp
describe "parallel puts replace values in _any_ order" $ do
it "Checkpoint" $
checkCoverage . prop_parallelPut
(\DBLayer{..} a0 -> mapExceptT atomically . putCheckpoint a0)
(\DBLayer{..} -> atomically . readCheckpoint)
(length . lrp @Maybe)
it "Wallet Metadata" $
checkCoverage . prop_parallelPut
(\DBLayer{..} a0 -> mapExceptT atomically . putWalletMeta a0)
(\DBLayer{..} -> atomically . fmap (fmap fst) . readWalletMeta)
(length . lrp @Maybe)
it "Tx History" $
checkCoverage . prop_parallelPut
putTxHistory_
readTxHistory_
(length . sortedUnions)
it "Private Key" $
checkCoverage . prop_parallelPut
(\DBLayer{..} a0 -> mapExceptT atomically . putPrivateKey a0)
(\DBLayer{..} -> atomically . readPrivateKey)
(length . lrp @Maybe)
describe "rollback" $ do
it "Can rollback to any arbitrary known checkpoint"
(property . prop_rollbackCheckpoint)
it "Correctly re-construct tx history on rollbacks"
(checkCoverage . prop_rollbackTxHistory)
-- | Wrap the result of 'readTransactions' in an arbitrary identity Applicative
readTxHistory_
:: Functor m
=> DBLayer m s ShelleyKey
-> WalletId
-> m (Identity GenTxHistory)
readTxHistory_ DBLayer{..} wid =
(Identity . GenTxHistory . fmap toTxHistory)
<$> atomically
(readTransactions wid Nothing Descending wholeRange Nothing Nothing)
putTxHistory_
:: DBLayer m s ShelleyKey
-> WalletId
-> GenTxHistory
-> ExceptT ErrNoSuchWallet m ()
putTxHistory_ DBLayer{..} wid =
mapExceptT atomically . putTxHistory wid . unGenTxHistory
{-------------------------------------------------------------------------------
Utils
-------------------------------------------------------------------------------}
-- | Keep only the
-- (L)ast (R)ecently (P)ut entry
lrp :: (Applicative f, Ord k) => [(k, v)] -> [f v]
lrp =
fmap snd
. L.sortOn fst
. Map.toList
. foldl (\m (k, v) -> Map.insert k (pure v) m) mempty
-- | Keep the unions (right-biaised) of all entry
unions :: (Monoid v, Ord k) => [(k, v)] -> [Identity v]
unions =
fmap Identity
. Map.elems
. foldl (\m (k, v) -> Map.unionWith (<>) (Map.fromList [(k, v)]) m) mempty
-- | Keep the unions (right-biased) of all transactions, and sort them in the
-- default order for readTransactions.
sortedUnions :: Ord k => [(k, GenTxHistory)] -> [Identity GenTxHistory]
sortedUnions = map (Identity . sort' . runIdentity) . unions
where
sort' = GenTxHistory
. filterTxHistory Nothing Descending wholeRange
. unGenTxHistory
-- | Execute an action once per key @k@ present in the given list
once :: (Ord k, Monad m) => [(k,v)] -> ((k,v) -> m a) -> m [a]
once xs action = fmap catMaybes $ flip evalStateT mempty $
forM xs $ \(k, v) -> do
s <- get
modify' (Set.insert k)
if Set.member k s
then pure Nothing
else Just <$> lift (action (k, v))
-- | Like 'once', but discards the result
once_ :: (Ord k, Monad m) => [(k,v)] -> ((k,v) -> m a) -> m ()
once_ xs = void . once xs
-- | Filter a transaction list according to the given predicate, returns their
-- ids.
filterTxs
:: (TxMeta -> Bool)
-> [(Tx, TxMeta)]
-> [Hash "Tx"]
filterTxs predicate = mapMaybe fn
where
fn (tx, meta) = if predicate meta then Just (txId tx) else Nothing
-- | Pick an arbitrary element from a monadic property, and label it in the
-- counterexample:
--
-- >>> ShowFmt meta <- namedPick "Wallet Metadata" arbitrary
--
-- If failing, the following line will be added to the counter example:
--
-- @
-- Wallet Metadata:
-- squirtle (still restoring (94%)), created at 1963-10-09 06:50:11 UTC, not delegating
-- @
namedPick :: Show a => String -> Gen a -> PropertyM IO a
namedPick lbl gen =
monitor (counterexample ("\n" <> lbl <> ":")) *> pick gen
-- | Like 'assert', but allow giving a label / title before running a assertion
assertWith :: String -> Bool -> PropertyM IO ()
assertWith lbl condition = do
let flag = if condition then "✓" else "✗"
monitor (counterexample $ lbl <> " " <> flag)
assert condition
{-------------------------------------------------------------------------------
Properties
-------------------------------------------------------------------------------}
-- | Can list created wallets
prop_createListWallet
:: DBLayer IO s ShelleyKey
-> KeyValPairs WalletId (InitialCheckpoint s, WalletMetadata)
-> Property
prop_createListWallet db@DBLayer{..} (KeyValPairs pairs) =
monadicIO (setup >> prop)
where
setup = liftIO (cleanDB db)
prop = liftIO $ do
res <- once pairs $ \(k, (InitialCheckpoint cp0, meta)) ->
atomically $ unsafeRunExceptT $
initializeWallet k cp0 meta mempty gp
(length <$> atomically listWallets) `shouldReturn` length res
-- | Trying to create a same wallet twice should yield an error
prop_createWalletTwice
:: DBLayer IO s ShelleyKey
-> ( WalletId
, InitialCheckpoint s
, WalletMetadata
)
-> Property
prop_createWalletTwice db@DBLayer{..} (wid, InitialCheckpoint cp0, meta) =
monadicIO (setup >> prop)
where
setup = liftIO (cleanDB db)
prop = liftIO $ do
let err = ErrWalletAlreadyExists wid
atomically (runExceptT $ initializeWallet wid cp0 meta mempty gp)
`shouldReturn` Right ()
atomically (runExceptT $ initializeWallet wid cp0 meta mempty gp)
`shouldReturn` Left err
-- | Trying to remove a same wallet twice should yield an error
prop_removeWalletTwice
:: DBLayer IO s ShelleyKey
-> ( WalletId
, InitialCheckpoint s
, WalletMetadata
)
-> Property
prop_removeWalletTwice db@DBLayer{..} (wid, InitialCheckpoint cp0, meta) =
monadicIO (setup >> prop)
where
setup = liftIO $ do
cleanDB db
atomically $ unsafeRunExceptT $ initializeWallet wid cp0 meta mempty gp
prop = liftIO $ do
let err = ErrNoSuchWallet wid
atomically (runExceptT $ removeWallet wid) `shouldReturn` Right ()
atomically (runExceptT $ removeWallet wid) `shouldReturn` Left err
-- | Checks that a given resource can be read after having been inserted in DB.
prop_readAfterPut
:: ( Buildable (f a), Eq (f a), Applicative f, GenState s )
=> ( DBLayer IO s ShelleyKey
-> WalletId
-> a
-> ExceptT ErrNoSuchWallet IO ()
) -- ^ Put Operation
-> ( DBLayer IO s ShelleyKey
-> WalletId
-> IO (f a)
) -- ^ Read Operation
-> DBLayer IO s ShelleyKey
-> (WalletId, a)
-- ^ Property arguments
-> Property
prop_readAfterPut putOp readOp db@DBLayer{..} (wid, a) =
monadicIO (setup >> prop)
where
setup = do
run $ cleanDB db
(InitialCheckpoint cp0, meta) <- pick arbitrary
run $ atomically $ unsafeRunExceptT $
initializeWallet wid cp0 meta mempty gp
prop = do
run $ unsafeRunExceptT $ putOp db wid a
res <- run $ readOp db wid
let fa = pure a
monitor $ counterexample $ "\nInserted\n" <> pretty fa
monitor $ counterexample $ "\nRead\n" <> pretty res
assertWith "Inserted == Read" (res == fa)
prop_getTxAfterPutValidTxId
:: GenState s
=> DBLayer IO s ShelleyKey
-> WalletId
-> GenTxHistory
-> Property
prop_getTxAfterPutValidTxId db@DBLayer{..} wid txGen =
monadicIO (setup >> prop)
where
setup = do
run $ cleanDB db
(InitialCheckpoint cp0, meta) <- pick arbitrary
run $ atomically $ unsafeRunExceptT $
initializeWallet wid cp0 meta mempty gp
prop = do
let txs = unGenTxHistory txGen
run $ unsafeRunExceptT $ mapExceptT atomically $ putTxHistory wid txs
forM_ txs $ \(Tx {txId}, txMeta) -> do
(Just (TransactionInfo {txInfoId, txInfoMeta})) <-
run $ atomically $ unsafeRunExceptT $ getTx wid txId
monitor $ counterexample $ "\nInserted\n"
<> pretty txMeta <> " for txId: " <> pretty txId
monitor $ counterexample $ "\nRead\n"
<> pretty txInfoMeta <> " for txId: " <> pretty txInfoId
assertWith "Inserted is included in Read"
(txMeta == txInfoMeta && txId == txInfoId)
prop_getTxAfterPutInvalidTxId
:: GenState s
=> DBLayer IO s ShelleyKey
-> WalletId
-> GenTxHistory
-> (Hash "Tx")
-> Property
prop_getTxAfterPutInvalidTxId db@DBLayer{..} wid txGen txId' =
monadicIO (setup >> prop)
where
setup = do
run $ cleanDB db
(InitialCheckpoint cp0, meta) <- pick arbitrary
run $ atomically $ unsafeRunExceptT $
initializeWallet wid cp0 meta mempty gp
prop = do
let txs = unGenTxHistory txGen
run $ unsafeRunExceptT $ mapExceptT atomically $ putTxHistory wid txs
res <- run $ atomically $ unsafeRunExceptT $ getTx wid txId'
assertWith "Irrespective of Inserted, Read is Nothing for invalid tx id"
(isNothing res)
prop_getTxAfterPutInvalidWalletId
:: DBLayer IO s ShelleyKey
-> ( WalletId
, InitialCheckpoint s
, WalletMetadata
)
-> GenTxHistory
-> WalletId
-> Property
prop_getTxAfterPutInvalidWalletId db@DBLayer{..}
(wid, InitialCheckpoint cp0, meta) txGen wid'
= wid /= wid' ==> monadicIO (setup >> prop)
where
setup = liftIO $ do
cleanDB db
atomically $ unsafeRunExceptT $ initializeWallet wid cp0 meta mempty gp
prop = liftIO $ do
let txs = unGenTxHistory txGen
atomically (runExceptT $ putTxHistory wid txs) `shouldReturn` Right ()
forM_ txs $ \(Tx {txId}, _) -> do
let err = ErrNoSuchWallet wid'
atomically (runExceptT $ getTx wid' txId) `shouldReturn` Left err
-- | Can't put resource before a wallet has been initialized
prop_putBeforeInit
:: (Buildable (f a), Eq (f a), GenState s)
=> ( DBLayer IO s ShelleyKey
-> WalletId
-> a
-> ExceptT ErrNoSuchWallet IO ()
) -- ^ Put Operation
-> ( DBLayer IO s ShelleyKey
-> WalletId
-> IO (f a)
) -- ^ Read Operation
-> f a
-- ^ An 'empty' value for the 'Applicative' f
-> DBLayer IO s ShelleyKey
-> (WalletId, a)
-- ^ Property arguments
-> Property
prop_putBeforeInit putOp readOp empty db (wid, a) =
monadicIO (setup >> prop)
where
setup = liftIO (cleanDB db)
prop = liftIO $ do
runExceptT (putOp db wid a) >>= \case
Right _ ->
fail "expected put operation to fail but it succeeded!"
Left err ->
err `shouldBe` ErrNoSuchWallet wid
(ShowFmt <$> readOp db wid) `shouldReturn` (ShowFmt empty)
-- | Modifying one resource leaves the other untouched
prop_isolation
:: ( Buildable (f b), Eq (f b)
, Buildable (g c), Eq (g c)
, Buildable (h d), Eq (h d)
, GenState s
, Show s
)
=> ( DBLayer IO s ShelleyKey
-> WalletId
-> a
-> ExceptT ErrNoSuchWallet IO ()
) -- ^ Put Operation
-> ( DBLayer IO s ShelleyKey
-> WalletId
-> IO (f b)
) -- ^ Read Operation for another resource
-> ( DBLayer IO s ShelleyKey
-> WalletId
-> IO (g c)
) -- ^ Read Operation for another resource
-> ( DBLayer IO s ShelleyKey
-> WalletId
-> IO (h d)
) -- ^ Read Operation for another resource
-> DBLayer IO s ShelleyKey
-> (ShowFmt WalletId, ShowFmt a)
-- ^ Properties arguments
-> Property
prop_isolation putA readB readC readD db@DBLayer{..} (ShowFmt wid, ShowFmt a) =
monadicIO (setup >>= prop)
where
setup = do
run $ cleanDB db
(InitialCheckpoint cp0, meta) <- pick arbitrary
(GenTxHistory txs) <- pick arbitrary
run $ atomically $ do
unsafeRunExceptT $ initializeWallet wid cp0 meta mempty gp
unsafeRunExceptT $ putTxHistory wid txs
run $ (,,)
<$> readB db wid
<*> readC db wid
<*> readD db wid
prop (b, c, d) = liftIO $ do
unsafeRunExceptT $ putA db wid a
(ShowFmt <$> readB db wid) `shouldReturn` ShowFmt b
(ShowFmt <$> readC db wid) `shouldReturn` ShowFmt c
(ShowFmt <$> readD db wid) `shouldReturn` ShowFmt d
-- | Can't read back data after delete
prop_readAfterDelete
:: (Buildable (f a), Eq (f a), GenState s)
=> ( DBLayer IO s ShelleyKey
-> WalletId
-> IO (f a)
) -- ^ Read Operation
-> f a
-- ^ An 'empty' value for the 'Applicative' f
-> DBLayer IO s ShelleyKey
-> ShowFmt WalletId
-> Property
prop_readAfterDelete readOp empty db@DBLayer{..} (ShowFmt wid) =
monadicIO (setup >> prop)
where
setup = do
run $ cleanDB db
(InitialCheckpoint cp0, meta) <- pick arbitrary
run $ atomically $ unsafeRunExceptT $
initializeWallet wid cp0 meta mempty gp
prop = liftIO $ do
atomically $ unsafeRunExceptT $ removeWallet wid
(ShowFmt <$> readOp db wid) `shouldReturn` ShowFmt empty
-- | Check that the DB supports multiple sequential puts for a given resource
prop_sequentialPut
:: (Buildable (f a), Eq (f a), GenState s)
=> ( DBLayer IO s ShelleyKey
-> WalletId
-> a
-> ExceptT ErrNoSuchWallet IO ()
) -- ^ Put Operation
-> ( DBLayer IO s ShelleyKey
-> WalletId
-> IO (f a)
) -- ^ Read Operation
-> (forall k. Ord k => [(k, a)] -> [f a])
-- ^ How do we expect operations to resolve
-> DBLayer IO s ShelleyKey
-> KeyValPairs (ShowFmt WalletId) (ShowFmt a)
-- ^ Property arguments
-> Property
prop_sequentialPut putOp readOp resolve db@DBLayer{..} kv =
cover 25 cond "conflicting db entries" $ monadicIO (setup >> prop)
where
pairs = (\(KeyValPairs xs) -> bimap unShowFmt unShowFmt <$> xs) kv
-- Make sure that we have some conflicting insertion to actually test the
-- semantic of the DB Layer.
cond = L.length (L.nub ids) /= L.length ids
where
ids = map fst pairs
setup = do
run $ cleanDB db
(InitialCheckpoint cp0, meta) <- pick arbitrary
run $ atomically $ unsafeRunExceptT $ once_ pairs $ \(k, _) ->
initializeWallet k cp0 meta mempty gp
prop = do
run $ unsafeRunExceptT $ forM_ pairs $ uncurry (putOp db)
res <- run $ once pairs (readOp db . fst)
let resolved = resolve pairs
monitor $ counterexample $ "\nResolved\n" <> pretty resolved
monitor $ counterexample $ "\nRead\n" <> pretty res
assertWith "Resolved == Read" (res == resolved)
-- | Check that the DB supports multiple sequential puts for a given resource
prop_parallelPut
:: (GenState s)
=> ( DBLayer IO s ShelleyKey
-> WalletId
-> a
-> ExceptT ErrNoSuchWallet IO ()
) -- ^ Put Operation
-> ( DBLayer IO s ShelleyKey
-> WalletId
-> IO (f a)
) -- ^ Read Operation
-> (forall k. Ord k => [(k, a)] -> Int)
-- ^ How many entries to we expect in the end
-> DBLayer IO s ShelleyKey
-> KeyValPairs WalletId a
-- ^ Property arguments
-> Property
prop_parallelPut putOp readOp resolve db@DBLayer{..} (KeyValPairs pairs) =
cover 25 cond "conflicting db entries" $ monadicIO (setup >> prop)
where
-- Make sure that we have some conflicting insertion to actually test the
-- semantic of the DB Layer.
cond = L.length (L.nub ids) /= L.length ids
where
ids = map fst pairs
setup = do
run $ cleanDB db
(InitialCheckpoint cp0, meta) <- pick arbitrary
run $ atomically $ unsafeRunExceptT $ once_ pairs $ \(k, _) ->
initializeWallet k cp0 meta mempty gp
prop = liftIO $ do
forConcurrently_ pairs $ unsafeRunExceptT . uncurry (putOp db)
res <- once pairs (readOp db . fst)
length res `shouldBe` resolve pairs
-- | Can rollback to any particular checkpoint previously stored
prop_rollbackCheckpoint
:: forall s k. (GenState s, Eq s)
=> DBLayer IO s k
-> InitialCheckpoint s
-> MockChain
-> Property
prop_rollbackCheckpoint db@DBLayer{..} (InitialCheckpoint cp0) (MockChain chain) = do
monadicIO $ do
ShowFmt wid <- namedPick "Wallet ID" arbitrary
ShowFmt meta <- namedPick "Wallet Metadata" arbitrary
ShowFmt point <- namedPick "Rollback target" (elements $ ShowFmt <$> cps)
setup wid meta >> prop wid point
where
cps :: [Wallet s]
cps = flip unfoldr (chain, cp0) $ \case
([], _) -> Nothing
(b:q, cp) -> let cp' = snd . snd $ applyBlock b cp in Just (cp', (q, cp'))
setup wid meta = run $ do
cleanDB db
atomically $ do
unsafeRunExceptT $ initializeWallet wid cp0 meta mempty gp
unsafeRunExceptT $ forM_ cps (putCheckpoint wid)
prop wid point = do
let tip = currentTip point
point' <- run $ atomically $ unsafeRunExceptT $
rollbackTo wid (toSlot $ chainPointFromBlockHeader tip)
cp <- run $ atomically $ readCheckpoint wid
let str = maybe "∅" pretty cp
monitor $ counterexample ("Checkpoint after rollback: \n" <> str)
assert (ShowFmt cp == ShowFmt (pure point))
assert (ShowFmt point' == ShowFmt (chainPointFromBlockHeader tip))
-- | Re-schedule pending transaction on rollback, i.e.:
--
-- (PoR = Point of Rollback)
--
-- - There's no transaction beyond the PoR
-- - Any transaction after the PoR is forgotten
--
-- FIXME LATER: This function only tests slot numbers to roll back to,
-- not Slot. See note [PointSlotNo] for the difference.
-- The reason for this restriction is that the 'rollbackTo' function
-- from the DBLayer currently does not roll the TxHistory back correctly
-- if there is a rollback to genesis.
prop_rollbackTxHistory
:: forall s k. ()
=> DBLayer IO s k
-> InitialCheckpoint s
-> GenTxHistory
-> Property
prop_rollbackTxHistory db@DBLayer{..} (InitialCheckpoint cp0) (GenTxHistory txs0) = do
monadicIO $ do
ShowFmt wid <- namedPick "Wallet ID" arbitrary
ShowFmt meta <- namedPick "Wallet Metadata" arbitrary
ShowFmt slot <- namedPick "Requested Rollback slot" arbitrary
let ixs = forgotten slot
monitor $ label ("Forgotten tx after point: " <> show (L.length ixs))
monitor $ cover 50 (not $ null ixs) "rolling back something"
setup wid meta >> prop wid slot
where
setup wid meta = run $ do
cleanDB db
atomically $ do
unsafeRunExceptT $ initializeWallet wid cp0 meta mempty gp
unsafeRunExceptT $ putTxHistory wid txs0
prop wid requestedPoint = do
point <- run $ unsafeRunExceptT $ mapExceptT atomically $
rollbackTo wid (At requestedPoint)
txs <- run $ atomically $ fmap toTxHistory
<$> readTransactions wid Nothing Descending wholeRange Nothing
Nothing
monitor $ counterexample $ "\n" <> "Actual Rollback Point:\n" <> (pretty point)
monitor $ counterexample $ "\nOriginal tx history:\n" <> (txsF txs0)
monitor $ counterexample $ "\nNew tx history:\n" <> (txsF txs)
let slot = pseudoSlotNo point
assertWith "All txs before are still known" $
L.sort (knownAfterRollback slot) == L.sort (txId . fst <$> txs)
assertWith "All txs are now before the point of rollback" $
all (isBefore slot . snd) txs
where
txsF :: [(Tx, TxMeta)] -> String
txsF =
L.intercalate "\n"
. map (\(tx, meta) -> unwords
[ "- "
, pretty (txId tx)
, pretty (meta ^. #slotNo)
, pretty (meta ^. #status)
, pretty (meta ^. #direction)
, pretty (meta ^. #amount)
])
pseudoSlotNo ChainPointAtGenesis = SlotNo 0
pseudoSlotNo (ChainPoint slot _) = slot
isBefore :: SlotNo -> TxMeta -> Bool
isBefore slot meta = (slotNo :: TxMeta -> SlotNo) meta <= slot
forgotten :: SlotNo -> [Hash "Tx"]
forgotten slot =
let isAfter meta = meta ^. #slotNo > slot
in filterTxs isAfter txs0
knownAfterRollback :: SlotNo -> [Hash "Tx"]
knownAfterRollback slot =
filterTxs (isBefore slot) txs0
gp :: GenesisParameters
gp = dummyGenesisParameters