This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathQuickCheck.hs
166 lines (137 loc) · 6.39 KB
/
QuickCheck.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
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Cardano.Wallet.WalletLayer.QuickCheck
( bracketPassiveWallet
, bracketActiveWallet
) where
import Universum
import Cardano.Wallet.Kernel.Diffusion (WalletDiffusion (..))
import Cardano.Wallet.WalletLayer (ActiveWalletLayer (..),
DeleteAccountError (..), DeleteWalletError (..),
GetAccountError (..), GetAccountsError (..),
GetUtxosError (..), GetWalletError (..),
ImportWalletError (..), PassiveWalletLayer (..),
UpdateAccountError (..), UpdateWalletError (..),
UpdateWalletPasswordError (..), ValidateAddressError (..))
import Pos.Chain.Update (ConfirmedProposalState)
import Pos.Core ()
import Test.Pos.Chain.Txp.Arbitrary ()
import Test.QuickCheck (Arbitrary (..), arbitrary, generate, oneof)
-- | Initialize the passive wallet.
-- The passive wallet cannot send new transactions.
bracketPassiveWallet
:: forall m n a. (MonadMask m, MonadIO n)
=> (PassiveWalletLayer n -> m a) -> m a
bracketPassiveWallet =
bracket
(pure passiveWalletLayer)
(\_ -> return ())
where
passiveWalletLayer :: PassiveWalletLayer n
passiveWalletLayer = PassiveWalletLayer
{ createWallet = \_ -> liftedGen
, getWallets = liftedGen
, getWallet = \_ -> liftedGen
, updateWallet = \_ _ -> liftedGen
, updateWalletPassword = \_ _ -> liftedGen
, deleteWallet = \_ -> liftedGen
, getUtxos = \_ -> liftedGen
, createAccount = \_ _ -> liftedGen
, getAccounts = \_ -> liftedGen
, getAccount = \_ _ -> liftedGen
, getAccountBalance = \_ _ -> liftedGen
, getAccountAddresses = \_ _ _ _ -> liftedGen
, updateAccount = \_ _ _ -> liftedGen
, deleteAccount = \_ _ -> liftedGen
, createAddress = \_ -> liftedGen
, getAddresses = \_ -> liftedGen
, validateAddress = \_ -> liftedGen
, getTransactions = \_ _ _ _ _ _ -> liftedGen
, getTxFromMeta = \_ -> liftedGen
, applyBlocks = \_ -> liftedGen
, rollbackBlocks = \_ -> liftedGen
, getNodeSettings = liftedGen
, nextUpdate = liftedGen
, applyUpdate = liftedGen
, postponeUpdate = liftedGen
, resetWalletState = liftedGen
, importWallet = \_ -> liftedGen
, waitForUpdate = liftedGen
, addUpdate = \_ -> liftedGen
}
-- | A utility function.
liftedGen :: forall b n. (MonadIO n, Arbitrary b) => n b
liftedGen = liftIO . generate $ arbitrary
-- | Initialize the active wallet.
-- The active wallet is allowed all.
bracketActiveWallet
:: forall m n a. (MonadMask m)
=> PassiveWalletLayer n
-> WalletDiffusion
-> (ActiveWalletLayer n -> m a) -> m a
bracketActiveWallet walletPassiveLayer _walletDiffusion =
bracket
(return activeWalletLayer)
(\_ -> return ())
where
activeWalletLayer :: ActiveWalletLayer n
activeWalletLayer = ActiveWalletLayer {
walletPassiveLayer = walletPassiveLayer
, pay = \_ _ _ -> error "unimplemented"
, estimateFees = \_ _ _ -> error "unimplemented"
, redeemAda = \_ -> error "unimplemented"
, getNodeInfo = error "unimplemented"
}
{-----------------------------------------------------------------------------
Orphan instances, in preparation for rehoming them.
Note that most of them are bonkers -- they were put here just for the sake
of the code to compile, as we are not actually using this particular layer
anywhere.
------------------------------------------------------------------------------}
instance Arbitrary GetAccountError where
arbitrary = oneof [ GetAccountError <$> arbitrary
, GetAccountWalletIdDecodingFailed <$> arbitrary
]
instance Arbitrary GetAccountsError where
arbitrary = oneof [ GetAccountsError <$> arbitrary
, GetAccountsWalletIdDecodingFailed <$> arbitrary
]
instance Arbitrary UpdateAccountError where
arbitrary = oneof [ UpdateAccountError <$> arbitrary
, UpdateAccountWalletIdDecodingFailed <$> arbitrary
]
instance Arbitrary DeleteAccountError where
arbitrary = oneof [ DeleteAccountError <$> arbitrary
, DeleteAccountWalletIdDecodingFailed <$> arbitrary
]
instance Arbitrary GetWalletError where
arbitrary = oneof [ GetWalletWalletIdDecodingFailed <$> arbitrary
, GetWalletError <$> arbitrary
]
instance Arbitrary GetUtxosError where
arbitrary = oneof [ pure (GetUtxosWalletIdDecodingFailed "foobar")
, GetUtxosGetAccountsError <$> arbitrary
, GetUtxosCurrentAvailableUtxoError <$> arbitrary
]
instance Arbitrary UpdateWalletPasswordError where
arbitrary = oneof [ UpdateWalletPasswordError <$> arbitrary
, UpdateWalletPasswordWalletIdDecodingFailed <$> arbitrary
]
instance Arbitrary DeleteWalletError where
arbitrary = oneof [ DeleteWalletWalletIdDecodingFailed <$> arbitrary ]
instance Arbitrary UpdateWalletError where
arbitrary = oneof [ UpdateWalletWalletIdDecodingFailed <$> arbitrary ]
instance Arbitrary ValidateAddressError where
arbitrary = oneof [ ValidateAddressDecodingFailed <$> arbitrary
]
instance Arbitrary ImportWalletError where
arbitrary = oneof [ ImportWalletFileNotFound <$> arbitrary
, ImportWalletNoWalletFoundInBackup <$> arbitrary
, ImportWalletCreationFailed <$> arbitrary
]
-- This is obviously not a valid 'Arbitrary' instance, but one will be provided
-- when we will be start using this 'WalletLayer' implementation. Note how the
-- core layer already provides one, it's just a matter of exposing it to other
-- components and use it.
instance Arbitrary ConfirmedProposalState where
arbitrary = oneof []