-
Notifications
You must be signed in to change notification settings - Fork 211
/
PrimitiveSpec.hs
295 lines (254 loc) · 9.01 KB
/
PrimitiveSpec.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Cardano.Wallet.PrimitiveSpec
( spec
) where
import Prelude
import Cardano.Wallet.Primitive
( Address (..)
, Block (..)
, BlockHeader (..)
, Coin (..)
, Dom (..)
, EpochId (..)
, Hash (..)
, SlotId (..)
, Tx (..)
, TxIn (..)
, TxOut (..)
, UTxO (..)
, balance
, excluding
, isSubsetOf
, isValidCoin
, restrictedBy
, restrictedTo
, updatePending
)
import Data.Set
( Set, (\\) )
import Test.Hspec
( Spec, describe, it )
import Test.QuickCheck
( Arbitrary (..)
, Property
, checkCoverage
, choose
, cover
, oneof
, property
, scale
, vectorOf
, (===)
)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
spec :: Spec
spec = do
describe "Generators are valid" $ do
it "Arbitrary Coin" $ property isValidCoin
describe "Lemma 2.1 - Properties of UTxO operations" $ do
it "2.1.1) ins⊲ u ⊆ u"
(checkCoverage prop_2_1_1)
it "2.1.2) ins⋪ u ⊆ u"
(checkCoverage prop_2_1_2)
it "2.1.3) u ⊳ outs ⊆ u"
(checkCoverage prop_2_1_3)
it "2.1.4) ins⊲ (u ⋃ v) = (ins⊲ u) ⋃ (ins⊲ v)"
(checkCoverage prop_2_1_4)
it "2.1.5) ins⋪ (u ⋃ v) = (ins⋪ u) ⋃ (ins⋪ v)"
(checkCoverage prop_2_1_5)
it "2.1.6) (dom u ⋂ ins) ⊲ u = ins⊲ u"
(checkCoverage prop_2_1_6)
it "2.1.7) (dom u ⋂ ins) ⋪ u = ins⋪ u"
(checkCoverage prop_2_1_7)
it "2.1.8) (dom u ⋃ ins) ⋪ (u ⋃ v) = (ins ⋃ dom u) ⋪ v"
(checkCoverage prop_2_1_8)
it "2.1.9) ins⋪ u = (dom u \\ ins)⊲ u"
(checkCoverage prop_2_1_9)
describe "Lemma 2.6 - Properties of balance" $ do
it "2.6.1) dom u ⋂ dom v ==> balance (u ⋃ v) = balance u + balance v"
(checkCoverage prop_2_6_1)
it "2.6.2) balance (ins⋪ u) = balance u - balance (ins⊲ u)"
(checkCoverage prop_2_6_2)
describe "Lemma 3.3 - Updating the pending set" $ do
it "3.3) updatePending b pending ⊆ pending"
(checkCoverage prop_3_2)
{-------------------------------------------------------------------------------
Wallet Specification - Lemma 2.1 - Properties of UTxO operations
-------------------------------------------------------------------------------}
prop_2_1_1 :: (Set TxIn, UTxO) -> Property
prop_2_1_1 (ins, u) =
cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ dom u `Set.intersection` ins
prop = (u `restrictedBy` ins) `isSubsetOf` u
prop_2_1_2 :: (Set TxIn, UTxO) -> Property
prop_2_1_2 (ins, u) =
cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ dom u `Set.intersection` ins
prop = (u `excluding` ins) `isSubsetOf` u
prop_2_1_3 :: (Set TxOut, UTxO) -> Property
prop_2_1_3 (outs, u) =
cover 50 cond "u ⋂ outs ≠ ∅" (property prop)
where
cond = not $ Set.null $
Set.fromList (Map.elems (getUTxO u)) `Set.intersection` outs
prop = (u `restrictedTo` outs) `isSubsetOf` u
prop_2_1_4 :: (Set TxIn, UTxO, UTxO) -> Property
prop_2_1_4 (ins, u, v) =
cover 50 cond "(dom u ⋃ dom v) ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ Set.union (dom u) (dom v) `Set.intersection` ins
prop =
((u <> v) `restrictedBy` ins)
===
(u `restrictedBy` ins) <> (v `restrictedBy` ins)
prop_2_1_5 :: (Set TxIn, UTxO, UTxO) -> Property
prop_2_1_5 (ins, u, v) =
cover 50 cond "(dom u ⋃ dom v) ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ Set.union (dom u) (dom v) `Set.intersection` ins
prop =
((u <> v) `excluding` ins)
===
(u `excluding` ins) <> (v `excluding` ins)
prop_2_1_6 :: (Set TxIn, UTxO) -> Property
prop_2_1_6 (ins, u) =
cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ dom u `Set.intersection` ins
prop =
(u `restrictedBy` (dom u `Set.intersection` ins))
===
(u `restrictedBy` ins)
prop_2_1_7 :: (Set TxIn, UTxO) -> Property
prop_2_1_7 (ins, u) =
cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ dom u `Set.intersection` ins
prop =
(u `excluding` (dom u `Set.intersection` ins))
===
(u `excluding` ins)
prop_2_1_8 :: (Set TxIn, UTxO, UTxO) -> Property
prop_2_1_8 (ins, u, v) =
cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ dom u `Set.intersection` ins
prop =
((u <> v) `excluding` (dom u <> ins))
===
v `excluding` (ins <> dom u)
prop_2_1_9 :: (Set TxIn, UTxO) -> Property
prop_2_1_9 (ins, u) =
cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ dom u `Set.intersection` ins
prop = (u `excluding` ins) === u `restrictedBy` (dom u \\ ins)
{-------------------------------------------------------------------------------
Wallet Specification - Lemma 2.6 - Properties of Balance
-------------------------------------------------------------------------------}
prop_2_6_1 :: (UTxO, UTxO) -> Property
prop_2_6_1 (u, v) =
cover 50 cond "u ≠ ∅ , v ≠ ∅" (property prop)
where
-- NOTE:
-- A precondition (u ⋂ v = ∅ ) is hard to satisfy because our generators
-- are built in order to not be 'too entropic'. So, we better just create
-- a v' that has no overlap with u.
v' = v `excluding` dom u
cond = not (u `isSubsetOf` mempty || v' `isSubsetOf` mempty)
prop = balance (u <> v') === balance u + balance v'
prop_2_6_2 :: (Set TxIn, UTxO) -> Property
prop_2_6_2 (ins, u) =
cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop)
where
cond = not $ Set.null $ dom u `Set.intersection` ins
prop =
balance (u `excluding` ins)
===
balance u - balance (u `restrictedBy` ins)
{-------------------------------------------------------------------------------
Wallet Specification - Lemma 3.3 - Update the Pending Set
-------------------------------------------------------------------------------}
prop_3_2 :: (Block, Set Tx) -> Property
prop_3_2 (b, pending) =
cover 50 cond0 "pending ≠ ∅ " $
cover 10 cond1 "transactions b ⋂ pending ≠ ∅ " $
property prop
where
cond0 = not $ Set.null pending
cond1 = not $ Set.null $ transactions b `Set.intersection` pending
prop = updatePending b pending `Set.isSubsetOf` pending
{-------------------------------------------------------------------------------
Arbitrary Instances
Arbitrary instances define here aren't necessarily reflecting on real-life
scenario, but they help test the property above by constructing data
structures that don't have much entropy and therefore, allow us to even test
something when checking for intersections and set restrictions!
-------------------------------------------------------------------------------}
instance Arbitrary (Hash "Tx") where
-- No Shrinking
arbitrary = oneof
[ pure $ Hash "TXID01"
, pure $ Hash "TXID02"
, pure $ Hash "TXID03"
]
-- Same for addresses
instance Arbitrary Address where
-- No Shrinking
arbitrary = oneof
[ pure $ Address "ADDR01"
, pure $ Address "ADDR02"
, pure $ Address "ADDR03"
]
instance Arbitrary Coin where
-- No Shrinking
arbitrary = Coin <$> choose (0, 3)
instance Arbitrary EpochId where
arbitrary = EpochId <$> arbitrary
instance Arbitrary SlotId where
arbitrary = SlotId <$> arbitrary
instance Arbitrary TxOut where
-- No Shrinking
arbitrary = TxOut
<$> arbitrary
<*> arbitrary
instance Arbitrary TxIn where
-- No Shrinking
arbitrary = TxIn
<$> arbitrary
<*> scale (`mod` 3) arbitrary -- No need for a crazy high indexes
instance Arbitrary UTxO where
shrink (UTxO utxo) = UTxO <$> shrink utxo
arbitrary = do
n <- choose (0, 10)
utxo <- zip
<$> vectorOf n arbitrary
<*> vectorOf n arbitrary
return $ UTxO $ Map.fromList utxo
instance Arbitrary Tx where
shrink (Tx ins outs) =
(flip Tx outs <$> shrink ins) <> (Tx ins <$> shrink outs)
arbitrary = do
ins <- choose (1, 3) >>= flip vectorOf arbitrary
outs <- choose (1, 3) >>= flip vectorOf arbitrary
return $ Tx ins outs
instance Arbitrary BlockHeader where
-- No Shrinking
arbitrary = BlockHeader
<$> arbitrary
<*> arbitrary
<*> oneof
[ pure $ Hash "BLOCK01"
, pure $ Hash "BLOCK02"
, pure $ Hash "BLOCK03"
]
instance Arbitrary Block where
shrink (Block h txs) = Block h <$> shrink txs
arbitrary = do
txs <- choose (0, 500) >>= flip vectorOf arbitrary
Block <$> arbitrary <*> pure (Set.fromList txs)