/
Properties.hs
427 lines (376 loc) · 16.6 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
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | Properties of the delegation traces induced by the transition systems
-- associated with this aspect of the ledger.
module Ledger.Delegation.Properties
( dcertsAreTriggered
, dcertsAreNotReplayed
, rejectDupSchedDelegs
, tracesAreClassified
, dblockTracesAreClassified
, relevantCasesAreCovered
, onlyValidSignalsAreGenerated
, invalidSignalsAreGenerated
)
where
import Control.Arrow (first, (***))
import Control.Lens (makeLenses, to, view, (&), (.~), (^.))
import Data.Bimap (Bimap)
import qualified Data.Bimap as Bimap
import Data.Data (Data, Typeable)
import Data.List (foldl', last)
import Data.List.Unique (repeated)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Hedgehog (Gen, MonadTest, Property, assert, cover, forAll, property, success,
withTests, (===))
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Control.State.Transition (Embed, Environment, IRC (IRC), PredicateFailure, STS,
Signal, State, TRC (TRC), applySTS, initialRules, judgmentContext, trans,
transitionRules, wrapFailed, (?!))
import Control.State.Transition.Generator (HasSizeInfo, HasTrace, SignalGenerator,
TraceProfile (TraceProfile), classifySize, classifyTraceLength, envGen,
failures, isTrivial, nonTrivialTrace, proportionOfValidSignals, sigGen,
suchThatLastState, trace, traceLengthsAreClassified, traceWithProfile)
import qualified Control.State.Transition.Generator as Transition.Generator
import Control.State.Transition.Trace (Trace, TraceOrder (OldestFirst), lastState,
preStatesAndSignals, traceEnv, traceLength, traceSignals)
import Ledger.Core (Epoch (Epoch), Sig (Sig), Slot, SlotCount (SlotCount), VKey,
VKeyGenesis, addSlot, mkVKeyGenesis, owner, unSlot, unSlotCount)
import Ledger.Delegation (DCert, DELEG, DIState (DIState),
DSEnv (DSEnv, _dSEnvEpoch, _dSEnvK), DSState (DSState),
DState (DState, _dStateDelegationMap, _dStateLastDelegation),
PredicateFailure (IsAlreadyScheduled, IsAlreadyScheduled, SDelegFailure, SDelegSFailure),
delegationMap, delegatorDelegate, depoch, emptyDelegationPayloadRatio, epoch,
liveAfter, mkDCert, multipleDelegationsRatio, nextEpochDelegationsRatio,
scheduledDelegations, selfDelegationsRatio, slot, tamperedDcerts,
thisEpochDelegationsRatio, _dIStateDelegationMap, _dIStateKeyEpochDelegations,
_dIStateLastDelegation, _dIStateScheduledDelegations,
_dSStateKeyEpochDelegations, _dSStateScheduledDelegations)
import qualified Ledger.Delegation.Test
import Ledger.Core.Generators (epochGen, slotGen, vkGen)
import qualified Ledger.Core.Generators as CoreGen
import Ledger.GlobalParams (slotsPerEpoch)
--------------------------------------------------------------------------------
-- Delegation certification triggering tests
--------------------------------------------------------------------------------
-- | Initial state for the ADELEG and ADELEGS systems
initADelegsState :: DState
initADelegsState = DState
{ _dStateDelegationMap = Bimap.empty
, _dStateLastDelegation = Map.empty
}
-- | Initial state for the ADELEG and ADELEGS systems
initSDelegsState :: DSState
initSDelegsState = DSState
{ _dSStateScheduledDelegations = []
, _dSStateKeyEpochDelegations = Set.empty
}
-- | Initial state for the DELEG system
initialDIState :: DIState
initialDIState = DIState
{ _dIStateDelegationMap = _dStateDelegationMap initADelegsState
, _dIStateLastDelegation = _dStateLastDelegation initADelegsState
, _dIStateScheduledDelegations = initSDelegsState ^. scheduledDelegations
, _dIStateKeyEpochDelegations = _dSStateKeyEpochDelegations initSDelegsState
}
-- | Delegation blocks. Simple blockchain to test delegation.
data DBLOCK deriving (Data, Typeable)
-- | A delegation block.
data DBlock
= DBlock
{ _blockSlot :: Slot
, _blockCerts :: [DCert]
}
deriving (Show, Eq)
makeLenses ''DBlock
-- | This corresponds to a state-transition rule where blocks with increasing
-- slot-numbers are produced.
instance STS DBLOCK where
type Environment DBLOCK = DSEnv -- The initial environment is only used to bootstrap the initial state.
type State DBLOCK = (DSEnv, DIState)
type Signal DBLOCK = DBlock
data PredicateFailure DBLOCK
= DPF (PredicateFailure DELEG)
| NotIncreasingBlockSlot
deriving (Eq, Show, Data, Typeable)
initialRules
= [ do
IRC env <- judgmentContext
pure (env, initialDIState)
]
transitionRules
= [ do
TRC (_, (env, st), dblock) <- judgmentContext
let nextSlot = dblock ^. blockSlot
env ^. slot < nextSlot ?! NotIncreasingBlockSlot
stNext <- trans @DELEG $ TRC (env, st, dblock ^. blockCerts)
let nextEpoch = if _dSEnvK env == 0
then 0
else Epoch $ unSlot nextSlot `div` slotsPerEpoch (_dSEnvK env)
return (env & slot .~ nextSlot
& epoch .~ nextEpoch
, stNext)
]
instance Embed DELEG DBLOCK where
wrapFailed = DPF
-- | Check that all the delegation certificates in the trace were correctly
-- applied.
dcertsAreTriggeredInTrace :: MonadTest m => Trace DBLOCK -> m ()
dcertsAreTriggeredInTrace tr
= lastDms === trExpectedDms
where
-- | Delegation map at the final state.
lastDms = st ^. delegationMap
-- | Delegation map what we'd expect to see judging by the delegation
-- certificates in the trace' signals.
trExpectedDms
= expectedDms lastSlot
((fromIntegral . unSlotCount . liveAfter) (_dSEnvK env))
slotsAndDcerts
(env, st) = lastState tr
-- | Last slot that was considered for an activation.
lastSlot :: Int
lastSlot = fst . last $ slotsAndDcerts
-- | Slots in which each block was applied. This is simply the result of
-- pairing the slot number in the pre-state of a signal, with the signal
-- itself.
--
-- We make use of integers, since negative numbers are quite handy when
-- computing the slot at which a given certificate should have been
-- activated (see 'expectedDms' and 'activationSlot').
slotsAndDcerts :: [(Int, DBlock)]
slotsAndDcerts
= first (view (to fst . slot . to unSlot . to fromIntegral))
<$> preStatesAndSignals OldestFirst tr
-- | Compute the expected delegation map after applying the sequence of
-- delegation certificates contained in the given blocks.
--
-- Delegation certificates are applied in the order they appear in the within a
-- block, and blocks are considered in the order they appear on the list passed
-- as parameter.
expectedDms
:: Int
-- ^ Last slot that should have been considered for certificate activation.
-> Int
-- ^ Delegation certificate liveness parameter.
-> [(Int, DBlock)]
-- ^ Delegation certificates to apply, and the slot at which these
-- certificates where scheduled.
-> Bimap VKeyGenesis VKey
expectedDms s d sbs =
foldl' insertIfInjective Bimap.empty (fmap delegatorDelegate activeCerts)
where
-- | Insert the key-value pair in the map only if the value is not in the
-- map already.
insertIfInjective
:: Bimap VKeyGenesis VKey
-> (VKeyGenesis, VKey)
-> Bimap VKeyGenesis VKey
insertIfInjective m (k, v) =
if Bimap.memberR v m
then m
else Bimap.insert k v m
-- | Get the active certificates from each block, and concatenate them all
-- together.
activeCerts :: [DCert]
activeCerts = concatMap _blockCerts activeBlocks
-- | We keep all the blocks whose certificates should be active given the
-- current slot.
activeBlocks :: [DBlock]
activeBlocks
= snd
<$> filter ((<= activationSlot) . fst) sbs
-- | Slot at which the certificates should have become active. If this
-- number is negative that means that no certificate can be activated.
activationSlot :: Int
activationSlot = s - d
-- | Check that there are no duplicated certificates in the trace.
dcertsAreNotReplayed :: Property
dcertsAreNotReplayed = withTests 300 $ property $ do
let (thisTraceLength, step) = (100, 10)
sample <- forAll (traceWithProfile @DBLOCK () thisTraceLength profile)
classifyTraceLength sample thisTraceLength step
dcertsAreNotReplayedInTrace sample
where
dcertsAreNotReplayedInTrace
:: MonadTest m
=> Trace DBLOCK
-> m ()
dcertsAreNotReplayedInTrace traceSample
= repeated traceDelegationCertificates === []
where
traceDelegationCertificates = traceSignals OldestFirst traceSample
& fmap _blockCerts
& concat
profile :: TraceProfile DBLOCK
profile
= TraceProfile
{ proportionOfValidSignals = 95
, failures = [(5, invalidDBlockGen)]
}
where
invalidDBlockGen :: SignalGenerator DBLOCK
invalidDBlockGen env (diEnv, diState)
= DBlock
<$> nextSlotGen env
<*> tamperedDcerts diEnv diState -- Gen.list (Range.constant 0 10) (randomDCertGen env)
instance HasTrace DBLOCK where
envGen
chainLength
= DSEnv
<$> allowedDelegators
-- We do not expect the current epoch to have an influence on the tests, so
-- we chose a small value here.
<*> epochGen 0 10
-- As with epochs, the current slot should not have influence in the tests.
<*> slotGen 0 10
-- 2160 the value of @k@ used in production. However, delegation certificates are activated
-- @2*k@ slots from the slot in which they are issued. This means that if we want to see
-- delegation activations, we need to choose a small value for @k@ since we do not want to blow
-- up the testing time by using large trace lengths.
--
-- Choosing a small @k@ value amounts to picking a large number of epochs. Given a trace length
-- of @n@, if we have @10k@ slots per-epoch, we can have at most @n `div` 10@ epochs (by
-- choosing @k == 1@).
--
<*> CoreGen.k chainLength (chainLength `div` 10)
where
-- We scale the number of delegators linearly up to twice the number of genesis keys we use in
-- production. Factor 2 is chosen ad-hoc here.
allowedDelegators = do
n <- Gen.integral (Range.linear 0 13)
pure $! Set.fromAscList $ mkVKeyGenesis <$> [0 .. n]
sigGen _ (env, st) =
DBlock <$> nextSlotGen env <*> sigGen @DELEG env st
-- | Generate a next slot. We want the resulting trace to include a large number of epoch changes,
-- so we generate an epoch change with higher frequency.
nextSlotGen :: DSEnv -> Gen Slot
nextSlotGen env =
incSlot <$> Gen.frequency
[ (1, Gen.integral (Range.constant 1 10))
, (2, pure $! slotsPerEpoch (_dSEnvK env))
]
where
incSlot c = (env ^.slot) `addSlot` SlotCount c
instance HasSizeInfo DBlock where
isTrivial = null . view blockCerts
dcertsAreTriggered :: Property
dcertsAreTriggered = withTests 300 $ property $
-- The number of tests was determined ad-hoc, since the default failed to
-- uncover the presence of errors.
forAll (nonTrivialTrace () 100) >>= dcertsAreTriggeredInTrace
dblockTracesAreClassified :: Property
dblockTracesAreClassified = withTests 200 $ property $ do
let (tl, step) = (100, 10)
tr <- forAll (trace @DBLOCK () tl)
classifyTraceLength tr tl step
-- Classify the traces by the total number of delegation certificates on
-- them.
classifySize "total dcerts" (traceDCerts tr) (fromIntegral . length) tl step
success
-- | Extract the delegation certificates in the blocks, in the order they would
-- have been applied.
traceDCertsByBlock :: Trace DBLOCK -> [[DCert]]
traceDCertsByBlock tr = _blockCerts <$> traceSignals OldestFirst tr
-- | Flattended list of DCerts for the given Trace
traceDCerts :: Trace DBLOCK -> [DCert]
traceDCerts = concat . traceDCertsByBlock
relevantCasesAreCovered :: Property
relevantCasesAreCovered = withTests 400 $ property $ do
let tl = 100
tr <- forAll (trace @DBLOCK () tl)
-- 40% of the traces must contain as many delegation certificates as blocks.
cover 40
"there are at least as many delegation certificates as blocks"
(traceLength tr <= length (traceDCerts tr))
-- 50% of the traces must contain at most 25% of blocks with empty delegation payload.
cover 50
"at most 25% of the blocks can contain empty delegation payload"
(0.25 >= emptyDelegationPayloadRatio (traceDCertsByBlock tr))
-- 50% of the traces must contain at least 30% of delegations to this epoch.
cover 50
"at least 30% of all certificates delegate in this epoch"
(0.3 <= thisEpochDelegationsRatio (epochDelegationEpoch tr))
-- 70% of the traces must contain at least 50% of delegations to the next
-- epoch.
cover 70
"at least 50% of the certificates delegate in the next epoch"
(0.5 <= nextEpochDelegationsRatio (epochDelegationEpoch tr))
-- 30% of the traces must contain at least 30% of self-delegations.
cover 30
"at least 30% of the certificates self delegate"
(0.3 <= selfDelegationsRatio (traceDCerts tr))
-- 15% of the traces must contain at least 10% of delegations to the same
-- delegate.
cover 50
"at least 25% of delegates have multiple delegators"
(0.05 <= multipleDelegationsRatio (traceDCerts tr))
where
-- Get the epoch in which the delegation certificates of the trace were
-- applied, paired with the epoch of the delegation certificate.
epochDelegationEpoch :: Trace DBLOCK -> [(Epoch, Epoch)]
epochDelegationEpoch tr = preStatesAndSignals @DBLOCK OldestFirst tr
& fmap (_dSEnvEpoch . fst *** (fmap depoch . _blockCerts))
& fmap (\(e, es) -> zip (repeat e) es)
& concat
onlyValidSignalsAreGenerated :: Property
onlyValidSignalsAreGenerated =
withTests 300 $ Transition.Generator.onlyValidSignalsAreGenerated @DBLOCK () 100
--------------------------------------------------------------------------------
-- Properties related to the transition rules
--------------------------------------------------------------------------------
-- | Reject delegation certificates where a genesis key tries to delegate in
-- the same slot.
--
-- This property tries to generate a trace where the last state contains a
-- non-empty sequence of scheduled delegations. If such trace cannot be
-- generated, then the test will fail when the heap limit is reached, or
-- hedgehog gives up.
rejectDupSchedDelegs :: Property
rejectDupSchedDelegs = withTests 300 $ property $ do
(tr, dcert) <- forAll $ do
tr <- trace @DELEG () 100
`suchThatLastState` (not . null . view scheduledDelegations)
let vkS =
case lastState tr ^. scheduledDelegations of
(_, (res, _)):_ -> res
_ -> error $ "This should not happen: "
++ "tr is guaranteed to contain a non-empty sequence of scheduled delegations"
vkD <- vkGen
epo <- Epoch <$> Gen.integral (Range.linear 0 100)
let dcert = mkDCert vkS (Sig (vkD, epo) (owner vkS)) vkD epo
return (tr, dcert)
let pfs = case applySTS (TRC (tr ^. traceEnv, lastState tr, [dcert])) of
Left res -> res
Right _ -> []
assert $ SDelegSFailure (SDelegFailure IsAlreadyScheduled) `elem` concat pfs
-- | Classify the traces.
tracesAreClassified :: Property
tracesAreClassified = traceLengthsAreClassified @DELEG () 1000 100
-- | The signal generator generates invalid signals with high probability when
-- invalid signals are requested.
invalidSignalsAreGenerated :: Property
invalidSignalsAreGenerated =
withTests 300
$ Transition.Generator.invalidSignalsAreGenerated
@DBLOCK
()
(failures profile)
100
-- We have 6 failures we're interested in. However demanding an uniform
-- distribution of predicate failures requires precise tweaking, which is
-- difficult to guarantee. For this reason we allow for an order of
-- magnitude deviation from the uniform distribution:
--
-- > 1/6 * 0.1 * 100 ~ 1.67
--
-- which we round up to 2.
(Ledger.Delegation.Test.coverDelegFailures 2)