-
Notifications
You must be signed in to change notification settings - Fork 155
/
TestChain.hs
242 lines (226 loc) · 7.43 KB
/
TestChain.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
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Test.Shelley.Spec.Ledger.Rules.TestChain
( -- TestPoolReap
constantSumPots,
nonNegativeDeposits,
removedAfterPoolreap,
-- TestNewEpoch
adaPreservationChain,
rewardStkCredSync,
)
where
import Cardano.Crypto.Hash (ShortHash)
import Control.Monad (join)
import Control.State.Transition.Extended (TRC (TRC), applySTS)
import Control.State.Transition.Trace
( SourceSignalTarget (..),
Trace (..),
sourceSignalTargets,
)
import Control.State.Transition.Trace.Generator.QuickCheck (forAllTraceFromInitState)
import Data.Foldable (foldl')
import Data.Proxy
import qualified Data.Set as Set
import Data.Word (Word64)
import Shelley.Spec.Ledger.BlockChain (Block (..), TxSeq (..), bbody, bhbody, bheaderSlotNo)
import Shelley.Spec.Ledger.Coin
import Shelley.Spec.Ledger.Core
import Shelley.Spec.Ledger.LedgerState
import Shelley.Spec.Ledger.STS.Chain (ChainState (..), totalAda)
import Shelley.Spec.Ledger.STS.PoolReap (PoolreapState (..))
import Shelley.Spec.Ledger.STS.Tick (TickEnv (TickEnv))
import Shelley.Spec.Ledger.Tx
import Shelley.Spec.Ledger.TxData
import Test.QuickCheck
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (CHAIN, POOLREAP, TICK)
import Test.Shelley.Spec.Ledger.Generator.Core (GenEnv (geConstants))
import qualified Test.Shelley.Spec.Ledger.Generator.Presets as Preset (genEnv)
import Test.Shelley.Spec.Ledger.Generator.Trace.Chain (mkGenesisChainState)
import qualified Test.Shelley.Spec.Ledger.Rules.TestPoolreap as TestPoolreap
import Test.Shelley.Spec.Ledger.Utils (epochFromSlotNo, runShelleyBase, testGlobals)
------------------------------
-- Constants for Properties --
------------------------------
numberOfTests :: Word64
numberOfTests = 300
traceLen :: Word64
traceLen = 100
----------------------------------------------------------------------
-- Properties for Chain
---------------------------------------------------------------------
-- | Verify that the domains for '_rewards' and '_srkCreds' remain in sync.
rewardStkCredSync :: Property
rewardStkCredSync =
forAllChainTrace $ \tr ->
conjoin $
map checkSync $
sourceSignalTargets tr
where
checkSync SourceSignalTarget {source, signal, target} =
let ds =
_dstate
. _delegationState
. esLState
. nesEs
. chainNes
$ target
in counterexample
( mconcat
[ "source\n",
show source,
"signal\n",
show signal,
"target\n",
show target
]
)
$ dom
(_stkCreds ds)
=== (Set.map getRwdCred $ dom (_rewards ds))
adaPreservationChain :: Property
adaPreservationChain =
forAllChainTrace $ \tr ->
conjoin . join $
map (\x -> [checkPreservation x, checkWithdrawlBound x]) $
sourceSignalTargets tr
where
checkPreservation SourceSignalTarget {source, signal, target} =
counterexample
( mconcat
[ "source\n",
show source,
"signal\n",
show signal,
"target\n",
show target
]
)
$ totalAda source === totalAda target
checkWithdrawlBound SourceSignalTarget {source, signal, target} =
epoch source == epoch target ==> rewardDelta === withdrawls
where
epoch s = nesEL . chainNes $ s
sum_ :: Foldable f => f Coin -> Coin
sum_ = foldl' (+) (Coin 0)
withdrawls :: Coin
withdrawls =
foldl'
( \c tx ->
let wdrls =
unWdrl . _wdrls . _body $
tx
in c + sum_ wdrls
)
(Coin 0)
$ txSeqTxns' . bbody $ signal
rewardDelta :: Coin
rewardDelta =
sum_
( _rewards . _dstate
. _delegationState
. esLState
. nesEs
. chainNes
$ source
)
- sum_
( _rewards . _dstate
. _delegationState
. esLState
. nesEs
. chainNes
$ target
)
----------------------------------------------------------------------
-- Properties for PoolReap (using the CHAIN Trace) --
----------------------------------------------------------------------
constantSumPots :: Property
constantSumPots =
forAllChainTrace $ \tr ->
let sst = map chainToPoolreapSst (sourceSignalTargets tr)
in TestPoolreap.constantSumPots sst
nonNegativeDeposits :: Property
nonNegativeDeposits =
forAllChainTrace $ \tr ->
let sst = map chainToPoolreapSst (sourceSignalTargets tr)
in TestPoolreap.nonNegativeDeposits sst
removedAfterPoolreap :: Property
removedAfterPoolreap =
forAllChainTrace $ \tr ->
let ssts = map chainToPoolreapSst (chainSstWithTick tr)
in TestPoolreap.removedAfterPoolreap ssts
---------------------------
-- Utils --
---------------------------
forAllChainTrace ::
(Testable prop) =>
(Trace (CHAIN ShortHash) -> prop) ->
Property
forAllChainTrace prop =
withMaxSuccess (fromIntegral numberOfTests) . property $
forAllTraceFromInitState testGlobals traceLen (Preset.genEnv p) (Just $ mkGenesisChainState (geConstants (Preset.genEnv p))) prop
where
p :: Proxy ShortHash
p = Proxy
-- | Transform CHAIN `sourceSignalTargets`s to POOLREAP ones.
chainToPoolreapSst ::
SourceSignalTarget (CHAIN ShortHash) ->
SourceSignalTarget (POOLREAP ShortHash)
chainToPoolreapSst
( SourceSignalTarget
ChainState {chainNes = nes}
ChainState {chainNes = nes'}
(Block bh _)
) =
SourceSignalTarget
( PoolreapState
(utxoSt nes)
(accountSt nes)
dstate
pstate
)
( PoolreapState
(utxoSt nes')
(accountSt nes')
dstate'
pstate'
)
(epochFromSlotNo s)
where
s = (bheaderSlotNo . bhbody) bh
DPState dstate pstate = (_delegationState . esLState . nesEs) nes
DPState dstate' pstate' = (_delegationState . esLState . nesEs) nes'
utxoSt = _utxoState . esLState . nesEs
accountSt = esAccountState . nesEs
-- | Transform the [(source, signal, target)] of a CHAIN Trace
-- by manually applying the Chain TICK Rule to each source and producing
-- [(source, signal, target')].
--
-- This allows for testing properties on traces that exclude effects of the
-- "UTXO branches" of the STS Rule tree.
chainSstWithTick :: Trace (CHAIN ShortHash) -> [SourceSignalTarget (CHAIN ShortHash)]
chainSstWithTick ledgerTr =
map applyTick ssts
where
ssts = sourceSignalTargets ledgerTr
applyTick ::
SourceSignalTarget (CHAIN ShortHash) ->
SourceSignalTarget (CHAIN ShortHash)
applyTick
( SourceSignalTarget
chainSt@ChainState {chainNes = nes}
_
b@(Block bh _)
) =
case runShelleyBase (applySTS @(TICK ShortHash) (TRC (TickEnv (getGKeys nes), nes, (bheaderSlotNo . bhbody) bh))) of
Left pf ->
error ("chainSstWithTick.applyTick Predicate failure " <> show pf)
Right nes' ->
SourceSignalTarget
chainSt
(chainSt {chainNes = nes'})
b