-
Notifications
You must be signed in to change notification settings - Fork 23
/
Examples.hs
363 lines (313 loc) · 13.2 KB
/
Examples.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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Test.Consensus.Cardano.Examples (
-- * Setup
codecConfig
-- * Examples
, exampleApplyTxErrWrongEraByron
, exampleApplyTxErrWrongEraShelley
, exampleEraMismatchByron
, exampleEraMismatchShelley
, exampleQueryAnytimeShelley
, exampleQueryEraMismatchByron
, exampleQueryEraMismatchShelley
, exampleResultAnytimeShelley
, exampleResultEraMismatchByron
, exampleResultEraMismatchShelley
, examples
) where
import Data.Coerce (Coercible)
import Data.SOP.BasicFunctors
import Data.SOP.Counting (Exactly (..))
import Data.SOP.Index (Index (..))
import Data.SOP.Strict
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Byron.Ledger (ByronBlock)
import qualified Ouroboros.Consensus.Byron.Ledger as Byron
import Ouroboros.Consensus.Cardano.Block
import Ouroboros.Consensus.Cardano.CanHardFork ()
import Ouroboros.Consensus.HardFork.Combinator
import Ouroboros.Consensus.HardFork.Combinator.Embed.Nary
import qualified Ouroboros.Consensus.HardFork.Combinator.State as State
import qualified Ouroboros.Consensus.HardFork.History as History
import Ouroboros.Consensus.HeaderValidation (AnnTip)
import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState (..))
import Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr)
import Ouroboros.Consensus.Protocol.Praos.Translate ()
import Ouroboros.Consensus.Protocol.TPraos (TPraos)
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
import qualified Ouroboros.Consensus.Shelley.Ledger as Shelley
import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol ()
import Ouroboros.Consensus.Storage.Serialisation
import Ouroboros.Consensus.TypeFamilyWrappers
import Ouroboros.Network.Block (Serialised (..))
import qualified Test.Consensus.Byron.Examples as Byron
import qualified Test.Consensus.Shelley.Examples as Shelley
import Test.Util.Serialisation.Examples (Examples (..), Labelled,
labelled, prefixExamples)
import Test.Util.Serialisation.SomeResult (SomeResult (..))
type Crypto = StandardCrypto
eraExamples :: NP Examples (CardanoEras Crypto)
eraExamples =
Byron.examples
:* Shelley.examplesShelley
:* Shelley.examplesAllegra
:* Shelley.examplesMary
:* Shelley.examplesAlonzo
:* Shelley.examplesBabbage
:* Shelley.examplesConway
:* Nil
-- | By using this function, we can't forget to update this test when adding a
-- new era to 'CardanoEras'.
combineEras ::
NP Examples (CardanoEras Crypto)
-> Examples (CardanoBlock Crypto)
combineEras = mconcat . hcollapse . hap eraInjections
where
eraInjections :: NP (Examples -.-> K (Examples (CardanoBlock Crypto)))
(CardanoEras Crypto)
eraInjections =
fn (K . injExamples "Byron" IZ)
:* fn (K . injExamples "Shelley" (IS IZ))
:* fn (K . injExamples "Allegra" (IS (IS IZ)))
:* fn (K . injExamples "Mary" (IS (IS (IS IZ))))
:* fn (K . injExamples "Alonzo" (IS (IS (IS (IS IZ)))))
:* fn (K . injExamples "Babbage" (IS (IS (IS (IS (IS IZ))))))
:* fn (K . injExamples "Conway" (IS (IS (IS (IS (IS (IS IZ)))))))
:* Nil
injExamples ::
String
-> Index (CardanoEras Crypto) blk
-> Examples blk
-> Examples (CardanoBlock Crypto)
injExamples eraName idx =
prefixExamples eraName
. inject exampleStartBounds idx
{-------------------------------------------------------------------------------
Inject instances
-------------------------------------------------------------------------------}
-- | In reality, an era tag would be prepended, but we're testing that the
-- encoder doesn't care what the bytes are.
instance Inject Serialised where
inject _ _ (Serialised _) = Serialised "<CARDANO_BLOCK>"
instance Inject SomeResult where
inject _ idx (SomeResult q r) =
SomeResult (QueryIfCurrent (injectQuery idx q)) (Right r)
instance Inject Examples where
inject startBounds (idx :: Index xs x) Examples {..} = Examples {
exampleBlock = inj (Proxy @I) exampleBlock
, exampleSerialisedBlock = inj (Proxy @Serialised) exampleSerialisedBlock
, exampleHeader = inj (Proxy @Header) exampleHeader
, exampleSerialisedHeader = inj (Proxy @SerialisedHeader) exampleSerialisedHeader
, exampleHeaderHash = inj (Proxy @WrapHeaderHash) exampleHeaderHash
, exampleGenTx = inj (Proxy @GenTx) exampleGenTx
, exampleGenTxId = inj (Proxy @WrapGenTxId) exampleGenTxId
, exampleApplyTxErr = inj (Proxy @WrapApplyTxErr) exampleApplyTxErr
, exampleQuery = inj (Proxy @(SomeSecond BlockQuery)) exampleQuery
, exampleResult = inj (Proxy @SomeResult) exampleResult
, exampleAnnTip = inj (Proxy @AnnTip) exampleAnnTip
, exampleLedgerState = inj (Proxy @LedgerState) exampleLedgerState
, exampleChainDepState = inj (Proxy @WrapChainDepState) exampleChainDepState
, exampleExtLedgerState = inj (Proxy @ExtLedgerState) exampleExtLedgerState
, exampleSlotNo = exampleSlotNo
}
where
inj ::
forall f a b.
( Inject f
, Coercible a (f x)
, Coercible b (f (HardForkBlock xs))
)
=> Proxy f -> Labelled a -> Labelled b
inj p = fmap (fmap (inject' p startBounds idx))
{-------------------------------------------------------------------------------
Setup
-------------------------------------------------------------------------------}
byronEraParams :: History.EraParams
byronEraParams = Byron.byronEraParams Byron.ledgerConfig
shelleyEraParams :: History.EraParams
shelleyEraParams = Shelley.shelleyEraParams @StandardCrypto Shelley.testShelleyGenesis
allegraEraParams :: History.EraParams
allegraEraParams = Shelley.shelleyEraParams @StandardCrypto Shelley.testShelleyGenesis
maryEraParams :: History.EraParams
maryEraParams = Shelley.shelleyEraParams @StandardCrypto Shelley.testShelleyGenesis
alonzoEraParams :: History.EraParams
alonzoEraParams = Shelley.shelleyEraParams @StandardCrypto Shelley.testShelleyGenesis
babbageEraParams :: History.EraParams
babbageEraParams = Shelley.shelleyEraParams @StandardCrypto Shelley.testShelleyGenesis
conwayEraParams :: History.EraParams
conwayEraParams = Shelley.shelleyEraParams @StandardCrypto Shelley.testShelleyGenesis
-- | We use 10, 20, 30, 40, ... as the transition epochs
shelleyTransitionEpoch :: EpochNo
shelleyTransitionEpoch = 10
byronStartBound :: History.Bound
byronStartBound = History.initBound
shelleyStartBound :: History.Bound
shelleyStartBound =
History.mkUpperBound
byronEraParams
byronStartBound
shelleyTransitionEpoch
allegraStartBound :: History.Bound
allegraStartBound =
History.mkUpperBound
shelleyEraParams
shelleyStartBound
20
maryStartBound :: History.Bound
maryStartBound =
History.mkUpperBound
allegraEraParams
allegraStartBound
30
alonzoStartBound :: History.Bound
alonzoStartBound =
History.mkUpperBound
maryEraParams
maryStartBound
40
babbageStartBound :: History.Bound
babbageStartBound =
History.mkUpperBound
alonzoEraParams
alonzoStartBound
50
conwayStartBound :: History.Bound
conwayStartBound =
History.mkUpperBound
alonzoEraParams
alonzoStartBound
60
exampleStartBounds :: Exactly (CardanoEras Crypto) History.Bound
exampleStartBounds = Exactly $
( K byronStartBound
:* K shelleyStartBound
:* K allegraStartBound
:* K maryStartBound
:* K alonzoStartBound
:* K babbageStartBound
:* K conwayStartBound
:* Nil
)
cardanoShape :: History.Shape (CardanoEras Crypto)
cardanoShape = History.Shape $ Exactly $
( K byronEraParams
:* K shelleyEraParams
:* K allegraEraParams
:* K maryEraParams
:* K alonzoEraParams
:* K babbageEraParams
:* K conwayEraParams
:* Nil
)
summary :: History.Summary (CardanoEras Crypto)
summary =
State.reconstructSummary
cardanoShape
(State.TransitionKnown shelleyTransitionEpoch)
(hardForkLedgerStatePerEra (ledgerStateByron byronLedger))
where
(_, byronLedger) = head $ exampleLedgerState Byron.examples
eraInfoByron :: SingleEraInfo ByronBlock
eraInfoByron = singleEraInfo (Proxy @ByronBlock)
eraInfoShelley :: SingleEraInfo (ShelleyBlock (TPraos StandardCrypto) StandardShelley)
eraInfoShelley = singleEraInfo (Proxy @(ShelleyBlock (TPraos StandardCrypto) StandardShelley))
codecConfig :: CardanoCodecConfig Crypto
codecConfig =
CardanoCodecConfig
Byron.codecConfig
Shelley.ShelleyCodecConfig
Shelley.ShelleyCodecConfig
Shelley.ShelleyCodecConfig
Shelley.ShelleyCodecConfig
Shelley.ShelleyCodecConfig
Shelley.ShelleyCodecConfig
ledgerStateByron ::
LedgerState ByronBlock
-> LedgerState (CardanoBlock Crypto)
ledgerStateByron stByron =
HardForkLedgerState $ HardForkState $ TZ cur
where
cur = State.Current {
currentStart = History.initBound
, currentState = stByron
}
{-------------------------------------------------------------------------------
Examples
-------------------------------------------------------------------------------}
-- | Multi-era examples, e.g., applying a transaction to the wrong era.
multiEraExamples :: Examples (CardanoBlock Crypto)
multiEraExamples = mempty {
exampleApplyTxErr = labelled [
("WrongEraByron", exampleApplyTxErrWrongEraByron)
, ("WrongEraShelley", exampleApplyTxErrWrongEraShelley)
]
, exampleQuery = labelled [
("AnytimeByron", exampleQueryAnytimeByron)
, ("AnytimeShelley", exampleQueryAnytimeShelley)
, ("HardFork", exampleQueryHardFork)
]
, exampleResult = labelled [
("EraMismatchByron", exampleResultEraMismatchByron)
, ("EraMismatchShelley", exampleResultEraMismatchShelley)
, ("AnytimeByron", exampleResultAnytimeByron)
, ("AnytimeShelley", exampleResultAnytimeShelley)
, ("HardFork", exampleResultHardFork)
]
}
-- | The examples: the examples from each individual era lifted in to
-- 'CardanoBlock' /and/ the multi-era examples.
examples :: Examples (CardanoBlock Crypto)
examples = combineEras eraExamples <> multiEraExamples
-- | Applying a Shelley thing to a Byron ledger
exampleEraMismatchByron :: MismatchEraInfo (CardanoEras Crypto)
exampleEraMismatchByron =
MismatchEraInfo $ MR (Z eraInfoShelley) (LedgerEraInfo eraInfoByron)
-- | Applying a Byron thing to a Shelley ledger
exampleEraMismatchShelley :: MismatchEraInfo (CardanoEras Crypto)
exampleEraMismatchShelley =
MismatchEraInfo $ ML eraInfoByron (Z (LedgerEraInfo eraInfoShelley))
exampleApplyTxErrWrongEraByron :: ApplyTxErr (CardanoBlock Crypto)
exampleApplyTxErrWrongEraByron =
HardForkApplyTxErrWrongEra exampleEraMismatchByron
exampleApplyTxErrWrongEraShelley :: ApplyTxErr (CardanoBlock Crypto)
exampleApplyTxErrWrongEraShelley =
HardForkApplyTxErrWrongEra exampleEraMismatchShelley
exampleQueryEraMismatchByron :: SomeSecond BlockQuery (CardanoBlock Crypto)
exampleQueryEraMismatchByron =
SomeSecond (QueryIfCurrentShelley Shelley.GetLedgerTip)
exampleQueryEraMismatchShelley :: SomeSecond BlockQuery (CardanoBlock Crypto)
exampleQueryEraMismatchShelley =
SomeSecond (QueryIfCurrentByron Byron.GetUpdateInterfaceState)
exampleQueryAnytimeByron :: SomeSecond BlockQuery (CardanoBlock Crypto)
exampleQueryAnytimeByron =
SomeSecond (QueryAnytimeByron GetEraStart)
exampleQueryAnytimeShelley :: SomeSecond BlockQuery (CardanoBlock Crypto)
exampleQueryAnytimeShelley =
SomeSecond (QueryAnytimeShelley GetEraStart)
exampleQueryHardFork :: SomeSecond BlockQuery (CardanoBlock Crypto)
exampleQueryHardFork =
SomeSecond (QueryHardFork GetInterpreter)
exampleResultEraMismatchByron :: SomeResult (CardanoBlock Crypto)
exampleResultEraMismatchByron =
SomeResult
(QueryIfCurrentShelley Shelley.GetLedgerTip)
(Left exampleEraMismatchByron)
exampleResultEraMismatchShelley :: SomeResult (CardanoBlock Crypto)
exampleResultEraMismatchShelley =
SomeResult
(QueryIfCurrentByron Byron.GetUpdateInterfaceState)
(Left exampleEraMismatchShelley)
exampleResultAnytimeByron :: SomeResult (CardanoBlock Crypto)
exampleResultAnytimeByron =
SomeResult (QueryAnytimeByron GetEraStart) (Just byronStartBound)
exampleResultAnytimeShelley :: SomeResult (CardanoBlock Crypto)
exampleResultAnytimeShelley =
SomeResult (QueryAnytimeShelley GetEraStart) (Just shelleyStartBound)
exampleResultHardFork :: SomeResult (CardanoBlock Crypto)
exampleResultHardFork =
SomeResult (QueryHardFork GetInterpreter) (History.mkInterpreter summary)