/
StateMachine.hs
1097 lines (949 loc) · 32.7 KB
/
StateMachine.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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Copyright: © 2018-2020 IOHK
-- License: Apache-2.0
--
-- These are state machine model tests for the 'DBLayer' implementations.
--
-- The basic principle is to define the simplest possible model, without real
-- types or crypto. Then generate a test case, which is a sequence of
-- actions. Run the actions on both the model and the actual implementation, and
-- check that they are "equivalent" at every step.
--
-- There is an excellent article about this testing method available at:
-- https://iohk.io/blog/an-in-depth-look-at-quickcheck-state-machine/
--
-- The example code for the article is here:
-- https://github.com/well-typed/qsm-in-depth
--
-- This module follows the article and example code (Version1.hs), pretty much
-- exactly.
module Cardano.Wallet.DB.StateMachine
( prop_sequential
, validateGenerators
, TestConstraints
) where
import Prelude
import Cardano.Address.Script
( ScriptTemplate (..) )
import Cardano.Pool.Types
( PoolId (..) )
import Cardano.Wallet.Address.Book
( AddressBookIso )
import Cardano.Wallet.Address.Derivation
( Depth (..), DerivationPrefix, Index, KeyFingerprint, Role (..) )
import Cardano.Wallet.Address.Derivation.Shared
()
import Cardano.Wallet.Address.Derivation.SharedKey
( SharedKey )
import Cardano.Wallet.Address.Derivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Address.Discovery
( IsOurs, PendingIxs )
import Cardano.Wallet.Address.Discovery.Random
( RndState )
import Cardano.Wallet.Address.Discovery.Sequential
( AddressPoolGap, SeqState (..) )
import Cardano.Wallet.Address.Discovery.Shared
( Readiness
, SharedAddressPool (..)
, SharedAddressPools (..)
, SharedState (..)
)
import Cardano.Wallet.DB
( DBLayer (..), DBLayerParams (..) )
import Cardano.Wallet.DB.Arbitrary
( GenState, GenTxHistory (..), InitialCheckpoint (..) )
import Cardano.Wallet.DB.Pure.Implementation
( Database (..)
, Err (..)
, TxHistory
, WalletDatabase (..)
, mInitializeWallet
, mListCheckpoints
, mPutCheckpoint
, mPutDelegationCertificate
, mPutDelegationRewardBalance
, mPutTxHistory
, mReadCheckpoint
, mReadDelegationRewardBalance
, mReadGenesisParameters
, mReadTxHistory
, mRollbackTo
)
import Cardano.Wallet.DummyTarget.Primitive.Types
( dummyGenesisParameters, dummyTimeInterpreter )
import Cardano.Wallet.Primitive.Model
( Wallet )
import Cardano.Wallet.Primitive.Types
( BlockHeader (..)
, ChainPoint
, DecentralizationLevel
, DelegationCertificate
, EpochNo (..)
, ExecutionUnits (..)
, FeePolicy
, GenesisParameters (..)
, LinearFunction
, Range (..)
, Slot
, SlotNo (..)
, SortOrder (..)
, StakeKeyCertificate
, TokenBundleMaxSize
, TxParameters (..)
, WalletId (..)
, WalletMetadata (..)
)
import Cardano.Wallet.Primitive.Types.Address
( Address, AddressState )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount (..) )
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle )
import Cardano.Wallet.Primitive.Types.TokenMap
( TokenMap )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenName, TokenPolicyId )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity )
import Cardano.Wallet.Primitive.Types.Tx
( Direction (..)
, LocalTxSubmissionStatus (..)
, SealedTx (..)
, TransactionInfo (..)
, Tx (..)
, TxCBOR
, TxMeta
, TxMetadata
, TxScriptValidity
, TxStatus
, inputs
)
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( TxSize (..) )
import Cardano.Wallet.Primitive.Types.Tx.TxIn
( TxIn (..) )
import Cardano.Wallet.Primitive.Types.Tx.TxOut
( TxOut (..) )
import Cardano.Wallet.Primitive.Types.UTxO
( UTxO (..) )
import Cardano.Wallet.Read.Eras.EraValue
( eraValueSerialize )
import Cardano.Wallet.Read.NetworkId
( NetworkDiscriminant (..) )
import Control.DeepSeq
( NFData )
import Control.Foldl
( Fold (..) )
import Control.Monad
( forM_, replicateM, void, when )
import Control.Monad.IO.Unlift
( MonadIO )
import Crypto.Hash
( Blake2b_160, Digest, digestFromByteString, hash )
import Data.Bifunctor
( first )
import Data.Foldable
( foldl', toList )
import Data.Functor.Classes
( Eq1, Show1 )
import Data.Generics.Internal.VL
( build )
import Data.List.Extra
( enumerate )
import Data.Map
( Map )
import Data.Map.Strict.NonEmptyMap.Internal
( NonEmptyMap )
import Data.Maybe
( catMaybes, fromJust )
import Data.Quantity
( Percentage (..), Quantity (..) )
import Data.Set
( Set )
import Data.Time.Clock
( NominalDiffTime, diffUTCTime, getCurrentTime )
import Data.TreeDiff
( ToExpr (..), defaultExprViaShow, genericToExpr )
import Fmt
( Buildable )
import GHC.Generics
( Generic, Generic1 )
import GHC.Stack
( HasCallStack, callStack )
import Test.Hspec
( SpecWith, describe, expectationFailure, it )
import Test.QuickCheck
( Arbitrary (..)
, Gen
, Property
, applyArbitrary2
, arbitraryBoundedEnum
, frequency
, generate
, resize
, (===)
)
import Test.QuickCheck.Monadic
( monadicIO, run )
import Test.StateMachine
( CommandNames (..)
, Concrete
, GenSym
, Logic (..)
, Reason (..)
, Reference
, StateMachine
, Symbolic
, forAllCommands
, forall
, member
, prettyCommands
, runCommands
, (.==)
)
import Test.StateMachine.Types
( Commands (..) )
import UnliftIO.Async
( race_ )
import UnliftIO.Concurrent
( threadDelay )
import UnliftIO.Exception
( evaluate )
import qualified Cardano.Crypto.Wallet as CC
import qualified Cardano.Wallet.Address.Pool as AddressPool
import qualified Control.Foldl as Foldl
import qualified Data.ByteArray as BA
import qualified Data.ByteString.Char8 as B8
import qualified Data.List as L
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.TreeDiff as Expr
import qualified Data.TreeDiff.OMap as Expr
import qualified Test.QuickCheck as QC
import qualified Test.StateMachine.Types as QSM
import qualified Test.StateMachine.Types.Rank2 as Rank2
{-------------------------------------------------------------------------------
Mock implementation
-------------------------------------------------------------------------------}
-- | The mock state type uses the model database with mock wallet ID and key
-- types.
type Mock s = Database MWid s MPrivKey
-- | Mock wallet ID -- simple and easy to read.
newtype MWid = MWid String
deriving (Show, Eq, Ord, Generic)
-- | Convert a mock wallet ID to a real one by hashing it, then splicing the
-- mock ID in front so that both ID types are sorted in the same order.
unMockWid :: MWid -> WalletId
unMockWid (MWid wid) = WalletId m
where
m = fromJust $ digestFromByteString spliced
spliced = wid' <> B8.drop (B8.length wid') hashed
hashed = BA.convert (hash wid' :: Digest Blake2b_160)
wid' = B8.pack wid
-- | Represent (XPrv, Hash) as a string.
type MPrivKey = String
{-------------------------------------------------------------------------------
Language
-------------------------------------------------------------------------------}
data Cmd s wid
= CreateWallet
| PutCheckpoint (Wallet s)
| ReadCheckpoint
| ListCheckpoints
| PutTxHistory TxHistory
| ReadTxHistory
(Maybe Coin)
SortOrder
(Range SlotNo)
(Maybe TxStatus)
| GetTx (Hash "Tx")
| ReadGenesisParameters
| RollbackTo wid Slot
| PutDelegationCertificate DelegationCertificate SlotNo
| PutDelegationRewardBalance wid Coin
| ReadDelegationRewardBalance
deriving stock (Show, Generic1, Eq, Functor, Foldable, Traversable)
deriving anyclass (CommandNames)
data Success s wid
= Unit ()
| NewWallet wid
| WalletId' wid
| Checkpoint (Wallet s)
| Metadata WalletMetadata
| TxHistory [TransactionInfo]
| LocalTxSubmission [LocalTxSubmissionStatus (Hash "Tx")]
| GenesisParams (Maybe GenesisParameters)
| ChainPoints [ChainPoint]
| Point ChainPoint
| DelegationRewardBalance Coin
| StakeKeyStatus Bool
deriving stock (Show, Generic1, Eq, Functor, Foldable, Traversable)
newtype Resp s wid
= Resp (Either Err (Success s wid))
deriving (Show, Eq)
instance Functor (Resp s) where
fmap f (Resp r) = Resp (fmap (fmap f) r)
instance Foldable (Resp s) where
foldMap f (Resp r) = either mempty (foldMap f) r
instance Traversable (Resp s) where
traverse f (Resp (Right r)) = Resp . Right <$> traverse f r
traverse _f (Resp (Left e)) = pure $ Resp . Left $ e
{-------------------------------------------------------------------------------
Interpreter: mock implementation
-------------------------------------------------------------------------------}
runMock :: HasCallStack => Cmd s MWid -> Mock s -> (Resp s MWid, Mock s)
runMock = \case
CreateWallet -> \db@(Database wid _ _) -> (Resp $ Right $ NewWallet wid, db)
PutCheckpoint wal ->
first (Resp . fmap Unit) . mPutCheckpoint wal
ListCheckpoints ->
first (Resp . fmap ChainPoints) . mListCheckpoints
ReadCheckpoint ->
first (Resp . fmap Checkpoint) . mReadCheckpoint
PutDelegationCertificate cert sl ->
first (Resp . fmap Unit) . mPutDelegationCertificate cert sl
PutTxHistory txs ->
first (Resp . fmap Unit) . mPutTxHistory txs
ReadTxHistory minW order range status ->
first (Resp . fmap TxHistory)
. mReadTxHistory timeInterpreter minW order range status
GetTx _tid ->
first (Resp . fmap (TxHistory . maybe [] pure))
-- TODO: Implement mGetTx
-- . mGetTx wid tid
. (Right Nothing,)
ReadGenesisParameters ->
first (Resp . fmap GenesisParams) . mReadGenesisParameters
PutDelegationRewardBalance _wid amt ->
first (Resp . fmap Unit) . mPutDelegationRewardBalance amt
ReadDelegationRewardBalance ->
first (Resp . fmap DelegationRewardBalance)
. mReadDelegationRewardBalance
RollbackTo _wid point ->
first (Resp . fmap Point) . mRollbackTo point
where
_requireCallStack = callStack
timeInterpreter = dummyTimeInterpreter
{-------------------------------------------------------------------------------
Interpreter: real I/O
-------------------------------------------------------------------------------}
runIO
:: forall m s
. MonadIO m
=> DBLayer m s
-> Cmd s WalletId
-> m (Resp s WalletId)
runIO DBLayer{..} = fmap Resp . go
where
wid = walletId_
runDBSuccess
:: (stm a -> m a)
-> (a -> Success s WalletId)
-> stm a
-> m (Either Err (Success s WalletId))
runDBSuccess atomically' s action = do
r <- atomically' action
pure $ Right $ s r
go
:: Cmd s WalletId
-> m (Either Err (Success s WalletId))
go = \case
CreateWallet -> pure $ Right $ NewWallet wid
PutCheckpoint wal -> runDBSuccess atomically Unit $ putCheckpoint wal
ReadCheckpoint -> Right . Checkpoint <$>
atomically readCheckpoint
ListCheckpoints -> Right . ChainPoints <$>
atomically listCheckpoints
PutDelegationCertificate pool sl ->
runDBSuccess atomically Unit $ putDelegationCertificate pool sl
PutTxHistory txs -> runDBSuccess atomically Unit $ putTxHistory txs
ReadTxHistory minWith order range status ->
fmap (Right . TxHistory) $
atomically $
readTransactions minWith order range status Nothing
GetTx tid ->
runDBSuccess atomically (TxHistory . maybe [] pure) $ getTx tid
ReadGenesisParameters -> Right . GenesisParams <$>
atomically readGenesisParameters
PutDelegationRewardBalance _wid amt -> runDBSuccess atomically Unit $
putDelegationRewardBalance amt
ReadDelegationRewardBalance -> Right . DelegationRewardBalance <$>
atomically readDelegationRewardBalance
RollbackTo _wid sl -> runDBSuccess atomically Point $ rollbackTo sl
{-------------------------------------------------------------------------------
Working with references
-------------------------------------------------------------------------------}
-- | Shortcut for instantiating a Cmd/Resp with references to return values
-- which are either Concrete or Symbolic. Concrete references are actual
-- values. Symbolic references correspond to the return value of a command.
-- The Functor f will be Cmd/Resp and reference type r will be
-- Concrete/Symbolic.
newtype At f r
= At (f (Reference WalletId r))
deriving (Generic)
deriving instance
Show (f (Reference WalletId r)) => Show (At f r)
type f :@ r = At f r
type RefEnv k a r = [(Reference k r, a)]
(!) :: (Eq1 r, Eq k) => RefEnv k a r -> Reference k r -> a
env ! r = fromJust (lookup r env)
{-------------------------------------------------------------------------------
Relating the mock model to the real implementation
-------------------------------------------------------------------------------}
type WidRefs r =
RefEnv WalletId MWid r
data Model s r
= Model (Mock s) (WidRefs r)
deriving (Generic)
deriving instance (Show1 r, Show s) => Show (Model s r)
initModel :: MWid -> DBLayerParams s -> Model s r
initModel mwid params = Model (mInitializeWallet mwid params) []
toMock :: (Functor (f s), Eq1 r) => Model s r -> f s :@ r -> f s MWid
toMock (Model _ wids) (At fr) = fmap (wids !) fr
step :: Eq1 r => Model s r -> Cmd s :@ r -> (Resp s MWid, Mock s)
step m@(Model mock _) c = runMock (toMock m c) mock
{-------------------------------------------------------------------------------
Events
-------------------------------------------------------------------------------}
data Event s r = Event
{ before :: Model s r
, cmd :: Cmd s :@ r
, after :: Model s r
, mockResp :: Resp s MWid
}
deriving instance (Show1 r, Show s) => Show (Event s r)
lockstep
:: forall s r. Eq1 r
=> Model s r
-> Cmd s :@ r
-> Resp s :@ r
-> Event s r
lockstep m@(Model _ ws) c (At resp) = Event
{ before = m
, cmd = c
, after = Model mock' (ws <> ws')
, mockResp = resp'
}
where
(resp', mock') = step m c
ws' :: WidRefs r
ws' = zip (toList resp) (toList resp')
{-------------------------------------------------------------------------------
Generator
-------------------------------------------------------------------------------}
{- HLINT ignore generator "Use ++" -}
generator
:: forall s. Arbitrary (Wallet s)
=> Model s Symbolic
-> Maybe (Gen (Cmd s :@ Symbolic))
generator (Model _ wids) = Just $ frequency $ fmap (fmap At) . snd <$> concat
[ generatorWithoutId
, if null wids then [] else generatorWithWid (fst <$> wids)
]
declareGenerator
:: String -- ^ A readable name
-> Int -- ^ Frequency
-> Gen cmd -- ^ Generator
-> (String, (Int, Gen cmd))
declareGenerator name f gen = (name, (f, gen))
generatorWithoutId
:: [(String, (Int, Gen (Cmd s (Reference WalletId r))))]
generatorWithoutId =
[ declareGenerator "CreateWallet" 5 $ pure CreateWallet
]
generatorWithWid
:: forall s r. Arbitrary (Wallet s)
=> [Reference WalletId r]
-> [(String, (Int, Gen (Cmd s (Reference WalletId r))))]
generatorWithWid wids =
[ declareGenerator "PutCheckpoints" 5
$ PutCheckpoint <$> arbitrary
, declareGenerator "ReadCheckpoint" 5
$ pure ReadCheckpoint
, declareGenerator "ListCheckpoints" 5
$ pure ListCheckpoints
, declareGenerator "PutDelegationCertificate" 5
$ PutDelegationCertificate <$> arbitrary <*> arbitrary
, declareGenerator "PutTxHistory" 5
$ PutTxHistory <$> fmap unGenTxHistory arbitrary
, declareGenerator "ReadTxHistory" 5
$ ReadTxHistory
<$> genMinWithdrawal
<*> genSortOrder
<*> genRange
<*> arbitrary
, declareGenerator "RollbackTo" 1
$ RollbackTo <$> genId <*> arbitrary
-- TODO: Implement mPrune
-- , declareGenerator "Prune" 1
-- $ Prune <$> genId <*> arbitrary
, declareGenerator "ReadGenesisParameters" 1
$ pure ReadGenesisParameters
]
where
genId :: Gen (Reference WalletId r)
genId = QC.elements wids
genSortOrder :: Gen SortOrder
genSortOrder = arbitraryBoundedEnum
genRange :: Gen (Range SlotNo)
genRange = applyArbitrary2 Range
genMinWithdrawal :: Gen (Maybe Coin)
genMinWithdrawal = frequency
[ (10, pure Nothing)
, (1, Just <$> arbitrary)
]
isUnordered :: Ord x => [x] -> Bool
isUnordered xs = xs /= L.sort xs
shrinker
:: Arbitrary (Wallet s)
=> Cmd s :@ r
-> [Cmd s :@ r]
shrinker (At cmd) = case cmd of
PutCheckpoint wal ->
[ At $ PutCheckpoint wal'
| wal' <- shrink wal ]
PutTxHistory h ->
[ At $ PutTxHistory h'
| h' <- map unGenTxHistory . shrink . GenTxHistory $ h
]
RollbackTo wid sid ->
[ At $ RollbackTo wid sid'
| sid' <- shrink sid
]
ReadTxHistory minW so range status ->
[ At $ ReadTxHistory minW so range' status
| range' <- shrink range
]
_ -> []
{-------------------------------------------------------------------------------
The state machine proper
-------------------------------------------------------------------------------}
transition :: Eq1 r => Model s r -> Cmd s :@ r -> Resp s :@ r -> Model s r
transition m c = after . lockstep m c
precondition :: Model s Symbolic -> Cmd s :@ Symbolic -> Logic
precondition (Model _ wids) (At c) =
forall (toList c) (`member` map fst wids)
postcondition
:: (Eq s, Show s)
=> Model s Concrete -> Cmd s :@ Concrete -> Resp s :@ Concrete -> Logic
postcondition m c r =
toMock (after e) r .== mockResp e
where
e = lockstep m c r
semantics
:: MonadIO m
=> DBLayer m s
-> Cmd s :@ Concrete
-> m (Resp s :@ Concrete)
semantics db (At c) =
(At . fmap QSM.reference) <$>
runIO db (fmap QSM.concrete c)
symbolicResp :: Model s Symbolic -> Cmd s :@ Symbolic -> GenSym (Resp s :@ Symbolic)
symbolicResp m c =
At <$> traverse (const QSM.genSym) resp
where
(resp, _mock') = step m c
type TestConstraints s =
( Eq s
, GenState s
, Arbitrary (Wallet s)
, ToExpr s
)
sm
:: (MonadIO m, TestConstraints s)
=> MWid
-> DBLayerParams s
-> DBLayer m s
-> StateMachine (Model s) (At (Cmd s)) m (At (Resp s))
sm mwid params db = QSM.StateMachine
{ initModel = initModel mwid params
, transition = transition
, precondition = precondition
, postcondition = postcondition
, invariant = Nothing
, generator = generator
, shrinker = const shrinker
, semantics = semantics db
, mock = symbolicResp
, cleanup = const $ pure ()
}
{-------------------------------------------------------------------------------
Additional type class instances required to run the QSM tests
-------------------------------------------------------------------------------}
instance Functor f => Rank2.Functor (At f) where
fmap = \f (At x) -> At $ fmap (lift f) x
where
lift :: (r x -> r' x) -> QSM.Reference x r -> QSM.Reference x r'
lift f (QSM.Reference x) = QSM.Reference (f x)
instance Foldable f => Rank2.Foldable (At f) where
foldMap = \f (At x) -> foldMap (lift f) x
where
lift :: (r x -> m) -> QSM.Reference x r -> m
lift f (QSM.Reference x) = f x
instance Traversable t => Rank2.Traversable (At t) where
traverse = \f (At x) -> At <$> traverse (lift f) x
where
lift
:: Functor f
=> (r x -> f (r' x))
-> QSM.Reference x r
-> f (QSM.Reference x r')
lift f (QSM.Reference x) = QSM.Reference <$> f x
deriving instance ToExpr s => ToExpr (Model s Concrete)
instance ToExpr s => ToExpr (Mock s) where
toExpr = genericToExpr
instance (ToExpr k, ToExpr v) => ToExpr (NonEmptyMap k v) where
toExpr = genericToExpr
instance ToExpr WalletId where
toExpr = defaultExprViaShow
instance ToExpr s => ToExpr (Wallet s) where
toExpr = genericToExpr
instance ToExpr BlockHeader where
toExpr = genericToExpr
instance ToExpr (Hash purpose) where
toExpr = genericToExpr
instance ToExpr b => ToExpr (Quantity a b) where
toExpr = genericToExpr
instance ToExpr GenesisParameters where
toExpr = defaultExprViaShow
instance ToExpr Slot where
toExpr = genericToExpr
instance ToExpr SlotNo where
toExpr = genericToExpr
instance ToExpr EpochNo where
toExpr = defaultExprViaShow
instance ToExpr TxStatus where
toExpr = genericToExpr
instance ToExpr PoolId where
toExpr = defaultExprViaShow
instance ToExpr AddressState where
toExpr = genericToExpr
instance (ToExpr addr, ToExpr ix) => ToExpr (AddressPool.Pool addr ix) where
toExpr pool = Expr.Rec "Pool" $ Expr.fromList
[ ("gap", toExpr $ AddressPool.gap pool)
, ("addresses", toExpr $ AddressPool.addresses pool)
]
instance ToExpr DerivationPrefix where
toExpr = defaultExprViaShow
instance ToExpr (Index ix typ) where
toExpr = defaultExprViaShow
instance ToExpr (SeqState 'Mainnet ShelleyKey) where
toExpr = defaultExprViaShow
instance ToExpr (RndState 'Mainnet) where
toExpr = defaultExprViaShow
instance ToExpr a => ToExpr (Readiness a) where
toExpr = genericToExpr
instance ToExpr AddressPoolGap where
toExpr = genericToExpr
instance ToExpr ScriptTemplate where
toExpr = defaultExprViaShow
instance ToExpr (SharedKey 'AccountK CC.XPub) where
toExpr = defaultExprViaShow
instance ToExpr (KeyFingerprint "payment" SharedKey) where
toExpr = defaultExprViaShow
instance ToExpr (PendingIxs 'CredFromScriptK) where
toExpr = genericToExpr
instance ToExpr (SharedAddressPool 'UtxoExternal SharedKey) where
toExpr = genericToExpr
instance ToExpr (SharedAddressPool 'UtxoInternal SharedKey) where
toExpr = genericToExpr
instance ToExpr (SharedAddressPools SharedKey) where
toExpr = genericToExpr
instance ToExpr (SharedState 'Mainnet SharedKey) where
toExpr = genericToExpr
instance (ToExpr s, ToExpr xprv) => ToExpr (WalletDatabase s xprv) where
toExpr = genericToExpr
instance ToExpr UTxO where
toExpr = genericToExpr
instance ToExpr WalletMetadata where
toExpr = defaultExprViaShow
instance ToExpr TxCBOR where
toExpr = toExpr . fst . build eraValueSerialize
instance ToExpr Tx where
toExpr = genericToExpr
instance ToExpr TxScriptValidity where
toExpr = genericToExpr
instance ToExpr TxIn where
toExpr = genericToExpr
instance ToExpr TxMetadata where
toExpr = defaultExprViaShow
instance ToExpr Coin where
toExpr = genericToExpr
instance ToExpr TxOut where
toExpr = genericToExpr
instance ToExpr TokenBundle where
toExpr = genericToExpr
instance ToExpr TokenMap where
toExpr = genericToExpr
instance ToExpr TokenName where
toExpr = genericToExpr
instance ToExpr TokenPolicyId where
toExpr = genericToExpr
instance ToExpr TokenQuantity where
toExpr = genericToExpr
instance ToExpr Address where
toExpr = genericToExpr
instance ToExpr TxMeta where
toExpr = genericToExpr
instance ToExpr SealedTx where
toExpr = defaultExprViaShow
instance ToExpr Percentage where
toExpr = genericToExpr
instance ToExpr DecentralizationLevel where
toExpr = genericToExpr
instance ToExpr TxSize where
toExpr = genericToExpr
instance ToExpr TokenBundleMaxSize where
toExpr = genericToExpr
instance ToExpr TxParameters where
toExpr = genericToExpr
instance ToExpr ExecutionUnits where
toExpr = genericToExpr
instance ToExpr a => ToExpr (LinearFunction a) where
toExpr = genericToExpr
instance ToExpr FeePolicy where
toExpr = genericToExpr
instance ToExpr Direction where
toExpr = genericToExpr
instance ToExpr MWid where
toExpr = defaultExprViaShow
instance ToExpr StakeKeyCertificate where
toExpr = genericToExpr
instance ToExpr RewardAccount where
toExpr = genericToExpr
{-------------------------------------------------------------------------------
Tagging
-------------------------------------------------------------------------------}
-- | Interesting combinations of commands.
data Tag
= SuccessfulReadTxHistory
| UnsuccessfulReadTxHistory
| TxUnsortedInputs
-- ^ Putting a transaction with unsorted inputs.
| TxUnsortedOutputs
| PutCheckpointTwice
-- ^ Multiple checkpoints are successfully saved to a wallet.
| RolledBackOnce
-- ^ We have rolled back at least once
deriving (Bounded, Enum, Eq, Ord, Show)
-- | The list of all possible 'Tag' values.
allTags :: [Tag]
allTags = enumerate
tag :: forall s. [Event s Symbolic] -> [Tag]
tag = Foldl.fold $ catMaybes <$> sequenceA
[ readTransactions (not . null) SuccessfulReadTxHistory
, readTransactions null UnsuccessfulReadTxHistory
, txUnsorted inputs TxUnsortedInputs
, txUnsorted outputs TxUnsortedOutputs
, countAction PutCheckpointTwice (>= 2) isPutCheckpointSuccess
, countAction RolledBackOnce (>= 1) isRollbackSuccess
]
where
isRollbackSuccess :: Event s Symbolic -> Maybe MWid
isRollbackSuccess ev = case (cmd ev, mockResp ev, before ev) of
(At (RollbackTo wid _), Resp (Right Point{}), Model _ wids ) ->
Just (wids ! wid)
_otherwise ->
Nothing
countAction
:: forall k. Ord k => Tag -> (Int -> Bool)
-> (Event s Symbolic -> Maybe k)
-> Fold (Event s Symbolic) (Maybe Tag)
countAction res enough match = Fold update mempty extract
where
update :: Map k Int -> Event s Symbolic -> Map k Int
update matches ev =
case match ev of
Just wid ->
Map.insertWith (+) wid 1 matches
_otherwise ->
matches
extract :: Map k Int -> Maybe Tag
extract matches
| any enough matches = Just res
| otherwise = Nothing
readTransactions
:: ([TransactionInfo] -> Bool)
-> Tag
-> Fold (Event s Symbolic) (Maybe Tag)
readTransactions check res = Fold update False (extractf res)
where
update :: Bool -> Event s Symbolic -> Bool
update didRead ev = didRead || case (cmd ev, mockResp ev) of
(At ReadTxHistory {}, Resp (Right (TxHistory h))) ->
check h
_otherwise ->
False
txUnsorted
:: Ord a
=> (Tx -> [a])
-> Tag
-> Fold (Event s Symbolic) (Maybe Tag)
txUnsorted sel res = Fold update False (extractf res)
where
update :: Bool -> Event s Symbolic -> Bool
update didRead ev = didRead ||
case (cmd ev, mockResp ev) of
(At (PutTxHistory h), Resp (Right _)) ->
any (isUnordered . sel . fst) h
_otherwise ->
False
isPutCheckpointSuccess :: Event s Symbolic -> Maybe ()
isPutCheckpointSuccess ev = case (cmd ev, mockResp ev, before ev) of
(At (PutCheckpoint _wal)
, Resp (Right (Unit ()))
, Model _ _ )
-> Just ()
_otherwise
-> Nothing
extractf :: a -> Bool -> Maybe a
extractf a t = if t then Just a else Nothing
execCmd
:: Model s Symbolic
-> QSM.Command (At (Cmd s)) (At (Resp s))
-> Event s Symbolic
execCmd model (QSM.Command cmd resp _vars) =
lockstep model cmd resp
execCmds
:: MWid
-> DBLayerParams s
-> QSM.Commands (At (Cmd s)) (At (Resp s))
-> [Event s Symbolic]
execCmds mwid params = \(QSM.Commands cs) -> go (initModel mwid params) cs
where
go
:: Model s Symbolic
-> [QSM.Command (At (Cmd s)) (At (Resp s))]
-> [Event s Symbolic]
go _ [] = []
go m (c : cs) = e : go (after e) cs where e = execCmd m c
{-------------------------------------------------------------------------------
Top-level tests
-------------------------------------------------------------------------------}
testMWid :: MWid
testMWid = MWid "test"
testWid :: WalletId
testWid = unMockWid testMWid
genDBParams
:: ( AddressBookIso s
, Arbitrary s
, Buildable s
, Eq s
, IsOurs s Address
, IsOurs s RewardAccount
, NFData s