/
Gen.hs
732 lines (625 loc) · 24.7 KB
/
Gen.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
module Test.Cardano.Api.Typed.Gen
( genAddressByron
, genAddressShelley
, genMaybePraosNonce
, genPraosNonce
, genProtocolParameters
, genValueNestedRep
, genValueNestedBundle
, genByronKeyWitness
, genTxId
, genTxIn
, genTxOut
-- * Scripts
, genScript
, genSimpleScript
, genPlutusScript
, genScriptInAnyLang
, genScriptInEra
, genScriptHash
, genScriptData
, genOperationalCertificate
, genOperationalCertificateIssueCounter
, genShelleyWitness
, genSigningKey
, genStakeAddress
, genTx
, genTxBody
, genValue
, genValueDefault
, genVerificationKey
) where
import Cardano.Api
import Cardano.Api.Byron
import Cardano.Api.Shelley
import Cardano.Prelude
import Control.Monad.Fail (fail)
import qualified Data.Map.Strict as Map
import Data.String
import qualified Data.ByteString as BS
import qualified Data.ByteString.Short as SBS
import qualified Cardano.Binary as CBOR
import qualified Cardano.Crypto.Hash as Crypto
import qualified Cardano.Crypto.Seed as Crypto
import qualified Shelley.Spec.Ledger.TxBody as Ledger (EraIndependentTxBody)
import Hedgehog (Gen, Range)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Test.Cardano.Api.Metadata (genTxMetadata)
import Test.Cardano.Chain.UTxO.Gen (genVKWitness)
import Test.Cardano.Crypto.Gen (genProtocolMagicId)
{- HLINT ignore "Reduce duplication" -}
genAddressByron :: Gen (Address ByronAddr)
genAddressByron = makeByronAddress <$> genNetworkId
<*> genVerificationKey AsByronKey
genAddressShelley :: Gen (Address ShelleyAddr)
genAddressShelley = makeShelleyAddress <$> genNetworkId
<*> genPaymentCredential
<*> genStakeAddressReference
genKESPeriod :: Gen KESPeriod
genKESPeriod = KESPeriod <$> Gen.word Range.constantBounded
genLovelace :: Gen Lovelace
genLovelace = Lovelace <$> Gen.integral (Range.linear 0 5000)
----------------------------------------------------------------------------
-- SimpleScript generators
--
genScript :: ScriptLanguage lang -> Gen (Script lang)
genScript (SimpleScriptLanguage lang) =
SimpleScript lang <$> genSimpleScript lang
genScript (PlutusScriptLanguage lang) =
PlutusScript lang <$> genPlutusScript lang
genSimpleScript :: SimpleScriptVersion lang -> Gen (SimpleScript lang)
genSimpleScript lang =
genTerm
where
genTerm = Gen.recursive Gen.choice nonRecursive recursive
-- Non-recursive generators
nonRecursive =
(RequireSignature . verificationKeyHash <$>
genVerificationKey AsPaymentKey)
: [ RequireTimeBefore supported <$> genSlotNo
| supported <- maybeToList (timeLocksSupported lang) ]
++ [ RequireTimeAfter supported <$> genSlotNo
| supported <- maybeToList (timeLocksSupported lang) ]
-- Recursive generators
recursive =
[ RequireAllOf <$> Gen.list (Range.linear 0 10) genTerm
, RequireAnyOf <$> Gen.list (Range.linear 0 10) genTerm
, do ts <- Gen.list (Range.linear 0 10) genTerm
m <- Gen.integral (Range.constant 0 (length ts))
return (RequireMOf m ts)
]
genPlutusScript :: PlutusScriptVersion lang -> Gen (PlutusScript lang)
genPlutusScript _ =
-- We make no attempt to create a valid script
PlutusScriptSerialised . SBS.toShort <$> Gen.bytes (Range.linear 0 32)
genScriptData :: Gen ScriptData
genScriptData =
Gen.recursive
Gen.choice
[ ScriptDataNumber <$> genInteger
, ScriptDataBytes <$> genByteString
]
-- The Gen.recursive combinator calls these with the size halved
[ ScriptDataConstructor <$> genInteger
<*> genScriptDataList
, ScriptDataList <$> genScriptDataList
, ScriptDataMap <$> genScriptDataMap
]
where
genInteger :: Gen Integer
genInteger = Gen.integral
(Range.linear
(-fromIntegral (maxBound :: Word64) :: Integer)
( fromIntegral (maxBound :: Word64) :: Integer))
genByteString :: Gen ByteString
genByteString = BS.pack <$> Gen.list (Range.linear 0 64)
(Gen.word8 Range.constantBounded)
genScriptDataList :: Gen [ScriptData]
genScriptDataList =
Gen.sized $ \sz ->
Gen.list (Range.linear 0 (fromIntegral sz)) genScriptData
genScriptDataMap :: Gen [(ScriptData, ScriptData)]
genScriptDataMap =
Gen.sized $ \sz ->
Gen.list (Range.linear 0 (fromIntegral sz)) $
(,) <$> genScriptData <*> genScriptData
-- ----------------------------------------------------------------------------
-- Script generators for any language, or any language valid in a specific era
--
genScriptInAnyLang :: Gen ScriptInAnyLang
genScriptInAnyLang =
Gen.choice
[ ScriptInAnyLang lang <$> genScript lang
| AnyScriptLanguage lang <- [minBound..maxBound] ]
genScriptInEra :: CardanoEra era -> Gen (ScriptInEra era)
genScriptInEra era =
Gen.choice
[ ScriptInEra langInEra <$> genScript lang
| AnyScriptLanguage lang <- [minBound..maxBound]
, Just langInEra <- [scriptLanguageSupportedInEra era lang] ]
genScriptHash :: Gen ScriptHash
genScriptHash = do
ScriptInAnyLang _ script <- genScriptInAnyLang
return (hashScript script)
----------------------------------------------------------------------------
-- Multi-asset generators
--
genAssetName :: Gen AssetName
genAssetName =
Gen.frequency
-- mostly from a small number of choices, so we get plenty of repetition
[ (9, Gen.element ["", "a", "b", "c"])
, (1, AssetName <$> Gen.utf8 (Range.singleton 32) Gen.alphaNum)
, (1, AssetName <$> Gen.utf8 (Range.constant 1 31) Gen.alphaNum)
]
genPolicyId :: Gen PolicyId
genPolicyId =
Gen.frequency
-- mostly from a small number of choices, so we get plenty of repetition
[ (9, Gen.element [ fromString (x : replicate 55 '0') | x <- ['a'..'c'] ])
-- and some from the full range of the type
, (1, PolicyId <$> genScriptHash)
]
genAssetId :: Gen AssetId
genAssetId = Gen.choice [ AssetId <$> genPolicyId <*> genAssetName
, return AdaAssetId
]
genQuantity :: Range Integer -> Gen Quantity
genQuantity range = fromInteger <$> Gen.integral range
-- | Generate a positive or negative quantity.
genSignedQuantity :: Gen Quantity
genSignedQuantity = genQuantity (Range.constantFrom 0 (-2) 2)
genUnsignedQuantity :: Gen Quantity
genUnsignedQuantity = genQuantity (Range.constant 0 2)
genValue :: Gen AssetId -> Gen Quantity -> Gen Value
genValue genAId genQuant =
valueFromList <$>
Gen.list (Range.constant 0 10)
((,) <$> genAId <*> genQuant)
-- | Generate a 'Value' with any asset ID and a positive or negative quantity.
genValueDefault :: Gen Value
genValueDefault = genValue genAssetId genSignedQuantity
-- | Generate a 'Value' suitable for minting, i.e. non-ADA asset ID and a
-- positive or negative quantity.
genValueForMinting :: Gen Value
genValueForMinting = genValue genAssetIdNoAda genSignedQuantity
where
genAssetIdNoAda :: Gen AssetId
genAssetIdNoAda = AssetId <$> genPolicyId <*> genAssetName
-- | Generate a 'Value' suitable for usage in a transaction output, i.e. any
-- asset ID and a positive quantity.
genValueForTxOut :: Gen Value
genValueForTxOut = genValue genAssetId genUnsignedQuantity
-- Note that we expect to sometimes generate duplicate policy id keys since we
-- pick 90% of policy ids from a set of just three.
genValueNestedRep :: Gen ValueNestedRep
genValueNestedRep =
ValueNestedRep <$> Gen.list (Range.constant 0 5) genValueNestedBundle
genValueNestedBundle :: Gen ValueNestedBundle
genValueNestedBundle =
Gen.choice
[ ValueNestedBundleAda <$> genSignedQuantity
, ValueNestedBundle <$> genPolicyId
<*> Gen.map (Range.constant 0 5)
((,) <$> genAssetName <*> genSignedQuantity)
]
genNetworkId :: Gen NetworkId
genNetworkId =
Gen.choice
[ pure Mainnet
, Testnet <$> genNetworkMagic
]
genNetworkMagic :: Gen NetworkMagic
genNetworkMagic = NetworkMagic <$> Gen.word32 Range.constantBounded
genOperationalCertificate :: Gen OperationalCertificate
genOperationalCertificate = fst <$> genOperationalCertificateWithCounter
genOperationalCertificateIssueCounter :: Gen OperationalCertificateIssueCounter
genOperationalCertificateIssueCounter = snd <$> genOperationalCertificateWithCounter
genOperationalCertificateWithCounter :: Gen (OperationalCertificate, OperationalCertificateIssueCounter)
genOperationalCertificateWithCounter = do
kesVKey <- genVerificationKey AsKesKey
stkPoolOrGenDelExtSign <- Gen.either (genSigningKey AsStakePoolKey) (genSigningKey AsGenesisDelegateExtendedKey)
kesP <- genKESPeriod
c <- Gen.integral $ Range.linear 0 1000
let stakePoolVer = either getVerificationKey (convert . getVerificationKey) stkPoolOrGenDelExtSign
iCounter = OperationalCertificateIssueCounter c stakePoolVer
case issueOperationalCertificate kesVKey stkPoolOrGenDelExtSign kesP iCounter of
-- This case should be impossible as we clearly derive the verification
-- key from the generated signing key.
Left err -> fail $ displayError err
Right pair -> return pair
where
convert :: VerificationKey GenesisDelegateExtendedKey
-> VerificationKey StakePoolKey
convert = (castVerificationKey :: VerificationKey GenesisDelegateKey
-> VerificationKey StakePoolKey)
. (castVerificationKey :: VerificationKey GenesisDelegateExtendedKey
-> VerificationKey GenesisDelegateKey)
-- TODO: Generate payment credential via script
genPaymentCredential :: Gen PaymentCredential
genPaymentCredential = do
vKey <- genVerificationKey AsPaymentKey
return . PaymentCredentialByKey $ verificationKeyHash vKey
genSigningKey :: Key keyrole => AsType keyrole -> Gen (SigningKey keyrole)
genSigningKey roletoken = do
seed <- genSeed (fromIntegral seedSize)
let sk = deterministicSigningKey roletoken seed
return sk
where
seedSize :: Word
seedSize = deterministicSigningKeySeedSize roletoken
genStakeAddress :: Gen StakeAddress
genStakeAddress = makeStakeAddress <$> genNetworkId <*> genStakeCredential
-- TODO: Generate StakeAddressReference via pointer
genStakeAddressReference :: Gen StakeAddressReference
genStakeAddressReference =
Gen.choice
[ StakeAddressByValue <$> genStakeCredential
, return NoStakeAddress
]
-- TODO: Generate StakeCredential via script
genStakeCredential :: Gen StakeCredential
genStakeCredential = do
vKey <- genVerificationKey AsStakeKey
return . StakeCredentialByKey $ verificationKeyHash vKey
genTxBodyShelley :: Gen (TxBody ShelleyEra)
genTxBodyShelley = do
res <- makeTransactionBody <$> genTxBodyContent ShelleyEra
case res of
Left err -> fail (show err) -- TODO: Render function for TxBodyError
Right txBody -> pure txBody
genByronTxOut :: Gen (TxOut ByronEra)
genByronTxOut =
TxOut <$> (byronAddressInEra <$> genAddressByron)
<*> (TxOutAdaOnly AdaOnlyInByronEra <$> genLovelace)
<*> pure TxOutDatumHashNone -- TODO alonzo replace with generator
genShelleyTxOut :: Gen (TxOut ShelleyEra)
genShelleyTxOut =
TxOut <$> (shelleyAddressInEra <$> genAddressShelley)
<*> (TxOutAdaOnly AdaOnlyInShelleyEra <$> genLovelace)
<*> pure TxOutDatumHashNone -- TODO alonzo replace with generator
genShelleyHash :: Gen (Crypto.Hash Crypto.Blake2b_256 Ledger.EraIndependentTxBody)
genShelleyHash = return . Crypto.castHash $ Crypto.hashWith CBOR.serialize' ()
genSlotNo :: Gen SlotNo
genSlotNo = SlotNo <$> Gen.word64 Range.constantBounded
-- TODO: Should probably have a naive generator that generates no inputs, no outputs etc
genTxBodyByron :: Gen (TxBody ByronEra)
genTxBodyByron = do
res <- makeTransactionBody <$> genTxBodyContent ByronEra
case res of
Left err -> fail (show err)
Right txBody -> pure txBody
genTxIn :: Gen TxIn
genTxIn = TxIn <$> genTxId <*> genTxIndex
genTxId :: Gen TxId
genTxId = TxId <$> genShelleyHash
genTxIndex :: Gen TxIx
genTxIndex = TxIx <$> Gen.word Range.constantBounded
genTxOutValue :: CardanoEra era -> Gen (TxOutValue era)
genTxOutValue era =
case era of
ByronEra -> TxOutAdaOnly AdaOnlyInByronEra <$> genLovelace
ShelleyEra -> TxOutAdaOnly AdaOnlyInShelleyEra <$> genLovelace
AllegraEra -> TxOutAdaOnly AdaOnlyInAllegraEra <$> genLovelace
MaryEra -> TxOutValue MultiAssetInMaryEra <$> genValueForTxOut
AlonzoEra -> TxOutValue MultiAssetInAlonzoEra <$> genValueForTxOut
genTxOut :: CardanoEra era -> Gen (TxOut era)
genTxOut era =
case era of
ByronEra -> genByronTxOut
ShelleyEra -> genShelleyTxOut
AllegraEra ->
TxOut
<$> (shelleyAddressInEra <$> genAddressShelley)
<*> (TxOutAdaOnly AdaOnlyInAllegraEra <$> genLovelace)
<*> pure TxOutDatumHashNone -- TODO alonzo replace with generator
MaryEra ->
TxOut
<$> (shelleyAddressInEra <$> genAddressShelley)
<*> genTxOutValue era
<*> pure TxOutDatumHashNone -- TODO alonzo replace with generator
AlonzoEra ->
TxOut
<$> (shelleyAddressInEra <$> genAddressShelley)
<*> genTxOutValue era
<*> pure TxOutDatumHashNone -- TODO alonzo replace with generator
genTtl :: Gen SlotNo
genTtl = genSlotNo
-- TODO: Accept a range for generating ttl.
genTxValidityLowerBound :: CardanoEra era -> Gen (TxValidityLowerBound era)
genTxValidityLowerBound era =
case era of
ByronEra -> pure TxValidityNoLowerBound
ShelleyEra -> pure TxValidityNoLowerBound
AllegraEra -> TxValidityLowerBound ValidityLowerBoundInAllegraEra <$> genTtl
MaryEra -> TxValidityLowerBound ValidityLowerBoundInMaryEra <$> genTtl
AlonzoEra -> panic "genTxValidityLowerBound: Alonzo not implemented yet "
-- TODO: Accept a range for generating ttl.
genTxValidityUpperBound :: CardanoEra era -> Gen (TxValidityUpperBound era)
genTxValidityUpperBound era =
case era of
ByronEra -> pure (TxValidityNoUpperBound ValidityNoUpperBoundInByronEra)
ShelleyEra -> TxValidityUpperBound ValidityUpperBoundInShelleyEra <$> genTtl
AllegraEra -> TxValidityUpperBound ValidityUpperBoundInAllegraEra <$> genTtl
MaryEra -> TxValidityUpperBound ValidityUpperBoundInMaryEra <$> genTtl
AlonzoEra -> panic "genTxValidityUpperBound: Alonzo not implemented yet "
genTxValidityRange
:: CardanoEra era
-> Gen (TxValidityLowerBound era, TxValidityUpperBound era)
genTxValidityRange era =
(,)
<$> genTxValidityLowerBound era
<*> genTxValidityUpperBound era
genTxMetadataInEra :: CardanoEra era -> Gen (TxMetadataInEra era)
genTxMetadataInEra era =
case era of
ByronEra -> pure TxMetadataNone
ShelleyEra ->
Gen.choice
[ pure TxMetadataNone
, TxMetadataInEra TxMetadataInShelleyEra <$> genTxMetadata
]
AllegraEra ->
Gen.choice
[ pure TxMetadataNone
, TxMetadataInEra TxMetadataInAllegraEra <$> genTxMetadata
]
MaryEra ->
Gen.choice
[ pure TxMetadataNone
, TxMetadataInEra TxMetadataInMaryEra <$> genTxMetadata
]
AlonzoEra -> panic "genTxMetadataInEra: Alonzo not implemented yet"
genTxAuxScripts :: CardanoEra era -> Gen (TxAuxScripts era)
genTxAuxScripts era =
case era of
ByronEra -> pure TxAuxScriptsNone
ShelleyEra -> pure TxAuxScriptsNone
AllegraEra -> TxAuxScripts AuxScriptsInAllegraEra
<$> Gen.list (Range.linear 0 3)
(genScriptInEra AllegraEra)
MaryEra -> TxAuxScripts AuxScriptsInMaryEra
<$> Gen.list (Range.linear 0 3)
(genScriptInEra MaryEra)
AlonzoEra -> panic "genTxAuxScripts: Alonzo not implemented yet"
genTxWithdrawals :: CardanoEra era -> Gen (TxWithdrawals BuildTx era)
genTxWithdrawals era =
case era of
ByronEra -> pure TxWithdrawalsNone
ShelleyEra ->
Gen.choice
[ pure TxWithdrawalsNone
, pure (TxWithdrawals WithdrawalsInShelleyEra mempty) -- TODO: Generate withdrawals
]
AllegraEra ->
Gen.choice
[ pure TxWithdrawalsNone
, pure (TxWithdrawals WithdrawalsInAllegraEra mempty) -- TODO: Generate withdrawals
]
MaryEra ->
Gen.choice
[ pure TxWithdrawalsNone
, pure (TxWithdrawals WithdrawalsInMaryEra mempty) -- TODO: Generate withdrawals
]
AlonzoEra -> panic "genTxWithdrawals: Alonzo not implemented yet"
genTxCertificates :: CardanoEra era -> Gen (TxCertificates BuildTx era)
genTxCertificates era =
case era of
ByronEra -> pure TxCertificatesNone
ShelleyEra ->
Gen.choice
[ pure TxCertificatesNone
, pure (TxCertificates CertificatesInShelleyEra mempty $ BuildTxWith mempty) -- TODO: Generate certificates
]
AllegraEra ->
Gen.choice
[ pure TxCertificatesNone
, pure (TxCertificates CertificatesInAllegraEra mempty $ BuildTxWith mempty) -- TODO: Generate certificates
]
MaryEra ->
Gen.choice
[ pure TxCertificatesNone
, pure (TxCertificates CertificatesInMaryEra mempty $ BuildTxWith mempty) -- TODO: Generate certificates
]
AlonzoEra -> panic "genTxCertificates: Alonzo not implemented yet"
genTxUpdateProposal :: CardanoEra era -> Gen (TxUpdateProposal era)
genTxUpdateProposal era =
case era of
ByronEra -> pure TxUpdateProposalNone
ShelleyEra ->
Gen.choice
[ pure TxUpdateProposalNone
, pure (TxUpdateProposal UpdateProposalInShelleyEra emptyUpdateProposal) -- TODO: Generate proposals
]
AllegraEra ->
Gen.choice
[ pure TxUpdateProposalNone
, pure (TxUpdateProposal UpdateProposalInAllegraEra emptyUpdateProposal) -- TODO: Generate proposals
]
MaryEra ->
Gen.choice
[ pure TxUpdateProposalNone
, pure (TxUpdateProposal UpdateProposalInMaryEra emptyUpdateProposal) -- TODO: Generate proposals
]
AlonzoEra -> panic "genTxUpdateProposal: Alonzo not implemented yet"
where
emptyUpdateProposal :: UpdateProposal
emptyUpdateProposal = UpdateProposal Map.empty (EpochNo 0)
genTxMintValue :: CardanoEra era -> Gen (TxMintValue BuildTx era)
genTxMintValue era =
case era of
ByronEra -> pure TxMintNone
ShelleyEra -> pure TxMintNone
AllegraEra -> pure TxMintNone
MaryEra ->
Gen.choice
[ pure TxMintNone
, TxMintValue MultiAssetInMaryEra <$> genValueForMinting <*> return (BuildTxWith mempty)
]
AlonzoEra -> panic "genTxMintValue: Alonzo not implemented yet"
genTxBodyContent :: CardanoEra era -> Gen (TxBodyContent BuildTx era)
genTxBodyContent era = do
trxIns <- Gen.list (Range.constant 1 10) genTxIn
trxOuts <- Gen.list (Range.constant 1 10) (genTxOut era)
fee <- genTxFee era
validityRange <- genTxValidityRange era
txMd <- genTxMetadataInEra era
auxScripts <- genTxAuxScripts era
mpparams <- Gen.maybe genProtocolParameters
withdrawals <- genTxWithdrawals era
certs <- genTxCertificates era
updateProposal <- genTxUpdateProposal era
mintValue <- genTxMintValue era
pure $ TxBodyContent
{ txIns = map (, BuildTxWith (KeyWitness KeyWitnessForSpending)) trxIns
, txInsCollateral = TxInsCollateralNone --TODO: Alonzo era: Generate collateral inputs.
, txOuts = trxOuts
, txFee = fee
, txValidityRange = validityRange
, txMetadata = txMd
, txAuxScripts = auxScripts
, txAuxScriptData = TxAuxScriptDataNone --TODO: Alonzo era: Generate extra script data
, txExtraKeyWits = TxExtraKeyWitnessesNone --TODO: Alonzo era: Generate witness key hashes
, txProtocolParams = BuildTxWith mpparams
, txWithdrawals = withdrawals
, txCertificates = certs
, txUpdateProposal = updateProposal
, txMintValue = mintValue
}
genTxFee :: CardanoEra era -> Gen (TxFee era)
genTxFee era =
case era of
ByronEra -> pure (TxFeeImplicit TxFeesImplicitInByronEra)
ShelleyEra -> TxFeeExplicit TxFeesExplicitInShelleyEra <$> genLovelace
AllegraEra -> TxFeeExplicit TxFeesExplicitInAllegraEra <$> genLovelace
MaryEra -> TxFeeExplicit TxFeesExplicitInMaryEra <$> genLovelace
AlonzoEra -> panic "genTxFee: Alonzo not implemented yet"
genTxBody :: CardanoEra era -> Gen (TxBody era)
genTxBody era =
case era of
ByronEra -> genTxBodyByron
ShelleyEra -> genTxBodyShelley
AllegraEra -> do
res <- makeTransactionBody <$> genTxBodyContent AllegraEra
case res of
Left err -> fail (show err) -- TODO: Render function for TxBodyError
Right txBody -> pure txBody
MaryEra -> do
res <- makeTransactionBody <$> genTxBodyContent MaryEra
case res of
Left err -> fail (show err) -- TODO: Render function for TxBodyError
Right txBody -> pure txBody
AlonzoEra -> panic "genTxBody: Alonzo not implemented yet"
genTx :: forall era. CardanoEra era -> Gen (Tx era)
genTx era =
makeSignedTransaction
<$> genWitnessList
<*> genTxBody era
where
genWitnessList :: Gen [KeyWitness era]
genWitnessList =
case era of
ByronEra -> Gen.list (Range.constant 1 10) genByronKeyWitness
ShelleyEra -> genShelleyBasedWitnessList
AllegraEra -> genShelleyBasedWitnessList
MaryEra -> genShelleyBasedWitnessList
AlonzoEra -> panic "genTx: Alonzo not implemented yet"
genShelleyBasedWitnessList :: IsShelleyBasedEra era => Gen [KeyWitness era]
genShelleyBasedWitnessList = do
bsWits <- Gen.list (Range.constant 0 10) (genShelleyBootstrapWitness era)
keyWits <- Gen.list (Range.constant 0 10) (genShelleyKeyWitness era)
return $ bsWits ++ keyWits
genVerificationKey :: Key keyrole => AsType keyrole -> Gen (VerificationKey keyrole)
genVerificationKey roletoken = getVerificationKey <$> genSigningKey roletoken
genByronKeyWitness :: Gen (KeyWitness ByronEra)
genByronKeyWitness = do
pmId <- genProtocolMagicId
txinWitness <- genVKWitness pmId
return $ ByronKeyWitness txinWitness
genWitnessNetworkIdOrByronAddress :: Gen WitnessNetworkIdOrByronAddress
genWitnessNetworkIdOrByronAddress =
Gen.choice
[ WitnessNetworkId <$> genNetworkId
, WitnessByronAddress <$> genAddressByron
]
genShelleyBootstrapWitness
:: IsShelleyBasedEra era
=> CardanoEra era
-> Gen (KeyWitness era)
genShelleyBootstrapWitness era =
makeShelleyBootstrapWitness
<$> genWitnessNetworkIdOrByronAddress
<*> genTxBody era
<*> genSigningKey AsByronKey
genShelleyKeyWitness
:: IsShelleyBasedEra era
=> CardanoEra era
-> Gen (KeyWitness era)
genShelleyKeyWitness era =
makeShelleyKeyWitness
<$> genTxBody era
<*> genShelleyWitnessSigningKey
genShelleyWitness
:: IsShelleyBasedEra era
=> CardanoEra era
-> Gen (KeyWitness era)
genShelleyWitness era =
Gen.choice
[ genShelleyKeyWitness era
, genShelleyBootstrapWitness era
]
genShelleyWitnessSigningKey :: Gen ShelleyWitnessSigningKey
genShelleyWitnessSigningKey =
Gen.choice [ WitnessPaymentKey <$> genSigningKey AsPaymentKey
, WitnessPaymentExtendedKey <$> genSigningKey AsPaymentExtendedKey
, WitnessStakeKey <$> genSigningKey AsStakeKey
, WitnessStakePoolKey <$> genSigningKey AsStakePoolKey
, WitnessGenesisDelegateKey <$> genSigningKey AsGenesisDelegateKey
, WitnessGenesisUTxOKey <$> genSigningKey AsGenesisUTxOKey
]
genSeed :: Int -> Gen Crypto.Seed
genSeed n = Crypto.mkSeedFromBytes <$> Gen.bytes (Range.singleton n)
genNat :: Gen Natural
genNat = Gen.integral (Range.linear 0 10)
genRational :: Gen Rational
genRational = Gen.realFrac_ (Range.linearFrac 0 1)
genEpochNo :: Gen EpochNo
genEpochNo = EpochNo <$> Gen.word64 (Range.linear 0 10)
genPraosNonce :: Gen PraosNonce
genPraosNonce = makePraosNonce <$> Gen.bytes (Range.linear 0 32)
genMaybePraosNonce :: Gen (Maybe PraosNonce)
genMaybePraosNonce = Gen.maybe genPraosNonce
genProtocolParameters :: Gen ProtocolParameters
genProtocolParameters =
ProtocolParameters
<$> ((,) <$> genNat <*> genNat)
<*> genRational
<*> genMaybePraosNonce
<*> genNat
<*> genNat
<*> genNat
<*> genNat
<*> genNat
<*> Gen.maybe genLovelace
<*> genLovelace
<*> genLovelace
<*> genLovelace
<*> genEpochNo
<*> genNat
<*> genRational
<*> genRational
<*> genRational
-- TODO alonzo: Add proper support for these generators.
<*> return Nothing
<*> return mempty
<*> return Nothing
<*> return Nothing
<*> return Nothing
<*> return Nothing
<*> return Nothing
<*> return Nothing