-
Notifications
You must be signed in to change notification settings - Fork 157
/
TwoPhaseValidation.hs
1844 lines (1678 loc) · 57.1 KB
/
TwoPhaseValidation.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
module Test.Cardano.Ledger.Examples.TwoPhaseValidation where
import Cardano.Crypto.DSIGN.Class (Signable)
import qualified Cardano.Crypto.Hash as CH
import Cardano.Crypto.Hash.Class (sizeHash)
import qualified Cardano.Crypto.KES.Class as KES
import Cardano.Crypto.VRF (evalCertified)
import qualified Cardano.Crypto.VRF.Class as VRF
import Cardano.Ledger.Address (Addr (..))
import Cardano.Ledger.Alonzo (AlonzoEra)
import Cardano.Ledger.Alonzo.Data (Data (..), hashData)
import Cardano.Ledger.Alonzo.Language (Language (..))
import Cardano.Ledger.Alonzo.PParams (PParams' (..))
import Cardano.Ledger.Alonzo.PlutusScriptApi (CollectError (..), collectTwoPhaseScriptInputs)
import Cardano.Ledger.Alonzo.Rules.Bbody (AlonzoBBODY, AlonzoBbodyPredFail (..))
import Cardano.Ledger.Alonzo.Rules.Utxo (UtxoPredicateFailure (..))
import Cardano.Ledger.Alonzo.Rules.Utxos (UtxosPredicateFailure (..))
import Cardano.Ledger.Alonzo.Rules.Utxow (AlonzoPredFail (..), AlonzoUTXOW)
import Cardano.Ledger.Alonzo.Scripts
( CostModel (..),
ExUnits (..),
)
import qualified Cardano.Ledger.Alonzo.Scripts as Tag (Tag (..))
import Cardano.Ledger.Alonzo.Tx
( IsValidating (..),
ScriptPurpose (..),
ValidatedTx (..),
hashWitnessPPData,
)
import Cardano.Ledger.Alonzo.TxInfo (txInfo, valContext)
import Cardano.Ledger.Alonzo.TxWitness (RdmrPtr (..), Redeemers (..), TxDats (..))
import Cardano.Ledger.BaseTypes (Network (..), Seed, StrictMaybe (..), textToUrl)
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core (EraRule)
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Credential
( Credential (..),
StakeCredential,
StakeReference (..),
)
import qualified Cardano.Ledger.Crypto as CC
import Cardano.Ledger.Era (Era (..), SupportsSegWit (..), ValidateScript (hashScript))
import Cardano.Ledger.Hashes (EraIndependentTxBody, ScriptHash)
import Cardano.Ledger.Keys
( GenDelegs (..),
KeyHash,
KeyPair (..),
KeyRole (..),
asWitness,
coerceKeyRole,
hashKey,
hashVerKeyVRF,
signedDSIGN,
signedKES,
)
import Cardano.Ledger.Mary.Value (PolicyID (..))
import Cardano.Ledger.SafeHash (hashAnnotated)
import Cardano.Ledger.Serialization (ToCBORGroup)
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval (..))
import Cardano.Ledger.Slot (BlockNo (..))
import Cardano.Ledger.Val (inject, (<+>))
import Cardano.Slotting.EpochInfo (EpochInfo, fixedEpochInfo)
import Cardano.Slotting.Slot (EpochSize (..), SlotNo (..))
import Cardano.Slotting.Time (SystemStart (..), mkSlotLength)
import Control.State.Transition.Extended hiding (Assertion)
import Control.State.Transition.Trace (checkTrace, (.-), (.->))
import qualified Data.ByteString as BS (replicate)
import Data.Coerce (coerce)
import Data.Default.Class (Default (..))
import Data.Functor.Identity (Identity, runIdentity)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (fromJust)
import qualified Data.Sequence.Strict as StrictSeq
import qualified Data.Set as Set
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Numeric.Natural (Natural)
import Plutus.V1.Ledger.Api (defaultCostModelParams)
import qualified Plutus.V1.Ledger.Api as Plutus
import Shelley.Spec.Ledger.API
( BHBody (..),
BHeader (..),
Block (..),
DPState (..),
DState (..),
KESPeriod (..),
LedgerState (..),
Nonce (NeutralNonce),
OCert (..),
PoolParams (..),
PrevHash (GenesisHash),
ProtVer (..),
UTxO (..),
)
import Shelley.Spec.Ledger.BlockChain (bBodySize, mkSeed, seedEta, seedL)
import Shelley.Spec.Ledger.EpochBoundary (BlocksMade (..))
import Shelley.Spec.Ledger.LedgerState (UTxOState (..), WitHashes (..))
import Shelley.Spec.Ledger.OCert (OCertSignable (..))
import Shelley.Spec.Ledger.STS.Bbody (BbodyEnv (..), BbodyPredicateFailure (..), BbodyState (..))
import Shelley.Spec.Ledger.STS.Delegs (DelegsPredicateFailure (..))
import Shelley.Spec.Ledger.STS.Delpl (DelplPredicateFailure (..))
import Shelley.Spec.Ledger.STS.Ledger (LedgerPredicateFailure (..))
import Shelley.Spec.Ledger.STS.Ledgers (LedgersPredicateFailure (..))
import Shelley.Spec.Ledger.STS.Pool (PoolPredicateFailure (..))
import Shelley.Spec.Ledger.STS.Utxo (UtxoEnv (..))
import Shelley.Spec.Ledger.STS.Utxow (UtxowPredicateFailure (..))
import Shelley.Spec.Ledger.TxBody
( DCert (..),
DelegCert (..),
PoolCert (..),
PoolMetadata (..),
RewardAcnt (..),
TxIn (..),
Wdrl (..),
)
import Shelley.Spec.Ledger.UTxO (makeWitnessVKey, txid)
import Test.Cardano.Ledger.Generic.Indexed (theKeyPair)
import Test.Cardano.Ledger.Generic.Proof
import Test.Cardano.Ledger.Generic.Updaters
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (C_Crypto)
import Test.Shelley.Spec.Ledger.Generator.EraGen (genesisId)
import Test.Shelley.Spec.Ledger.Utils
( RawSeed (..),
applySTSTest,
mkKESKeyPair,
mkKeyPair,
mkVRFKeyPair,
runShelleyBase,
unsafeBoundRational,
)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (Assertion, testCase, (@?=))
-- =======================
-- Setup the initial state
-- =======================
testEpochInfo :: EpochInfo Identity
testEpochInfo = fixedEpochInfo (EpochSize 100) (mkSlotLength 1)
testSystemStart :: SystemStart
testSystemStart = SystemStart $ posixSecondsToUTCTime 0
-- | A cost model that sets everything as being free
freeCostModel :: CostModel
freeCostModel = CostModel $ 0 <$ fromJust defaultCostModelParams
pp :: Proof era -> Core.PParams era
pp pf =
newPParams
pf
[ Costmdls $ Map.singleton PlutusV1 freeCostModel,
MaxValSize 1000000000,
MaxTxExUnits $ ExUnits 1000000 1000000,
MaxBlockExUnits $ ExUnits 1000000 1000000,
ProtocolVersion $ ProtVer 5 0
]
utxoEnv :: Proof era -> UtxoEnv era
utxoEnv pf =
UtxoEnv
(SlotNo 0)
(pp pf)
mempty
(GenDelegs mempty)
-- | Create an address with a given payment script.
-- The proof here is used only as a Proxy.
scriptAddr :: forall era. (Scriptic era) => Core.Script era -> Proof era -> Addr (Crypto era)
scriptAddr s _pf = Addr Testnet pCred sCred
where
pCred = ScriptHashObj . hashScript @era $ s
(_ssk, svk) = mkKeyPair @(Crypto era) (RawSeed 0 0 0 0 0)
sCred = StakeRefBase . KeyHashObj . hashKey $ svk
someKeys :: forall era. Era era => Proof era -> KeyPair 'Payment (Crypto era)
someKeys _pf = KeyPair vk sk
where
(sk, vk) = mkKeyPair @(Crypto era) (RawSeed 1 1 1 1 1)
someAddr :: forall era. Era era => Proof era -> Addr (Crypto era)
someAddr pf = Addr Testnet pCred sCred
where
(_ssk, svk) = mkKeyPair @(Crypto era) (RawSeed 0 0 0 0 2)
pCred = KeyHashObj . hashKey . vKey $ someKeys pf
sCred = StakeRefBase . KeyHashObj . hashKey $ svk
someOutput :: Scriptic era => Proof era -> Core.TxOut era
someOutput pf =
newTxOut Override pf [Address $ someAddr pf, Amount (inject $ Coin 1000)]
collateralOutput :: Scriptic era => Proof era -> Core.TxOut era
collateralOutput pf =
newTxOut Override pf [Address $ someAddr pf, Amount (inject $ Coin 1000)]
alwaysSucceedsHash ::
forall era.
Scriptic era =>
Natural ->
Proof era ->
ScriptHash (Crypto era)
alwaysSucceedsHash n pf = hashScript @era $ (always n pf)
alwaysFailsHash :: forall era. Scriptic era => Natural -> Proof era -> ScriptHash (Crypto era)
alwaysFailsHash n pf = hashScript @era $ (never n pf)
timelockScript :: PostShelley era => Int -> Proof era -> Core.Script era
timelockScript s pf = allOf [matchkey 1, after (100 + s)] $ pf
timelockHash ::
forall era.
PostShelley era =>
Int ->
Proof era ->
ScriptHash (Crypto era)
timelockHash n pf = hashScript @era $ (timelockScript n pf)
timelockAddr :: forall era. PostShelley era => Proof era -> Addr (Crypto era)
timelockAddr pf = Addr Testnet pCred sCred
where
(_ssk, svk) = mkKeyPair @(Crypto era) (RawSeed 0 0 0 0 2)
pCred = ScriptHashObj (timelockHash 0 pf)
sCred = StakeRefBase . KeyHashObj . hashKey $ svk
timelockOut :: PostShelley era => Proof era -> Core.TxOut era
timelockOut pf =
newTxOut Override pf [Address $ timelockAddr pf, Amount (inject $ Coin 1)]
-- | This output is unspendable since it is locked by a plutus script,
-- but has no datum hash.
unspendableOut :: forall era. (Scriptic era) => Proof era -> Core.TxOut era
unspendableOut pf =
newTxOut
Override
pf
[ Address (scriptAddr (always 3 pf) pf),
Amount (inject $ Coin 5000)
]
initUTxO :: PostShelley era => Proof era -> UTxO era
initUTxO pf =
UTxO $
Map.fromList $
[ (TxIn genesisId 1, alwaysSucceedsOutput pf),
(TxIn genesisId 2, alwaysFailsOutput pf)
]
++ map (\i -> (TxIn genesisId i, someOutput pf)) [3 .. 8]
++ map (\i -> (TxIn genesisId i, collateralOutput pf)) [11 .. 18]
++ [ (TxIn genesisId 100, timelockOut pf),
(TxIn genesisId 101, unspendableOut pf)
]
initialUtxoSt ::
( Default (State (EraRule "PPUP" era)),
PostShelley era
) =>
Proof era ->
UTxOState era
initialUtxoSt pf = UTxOState (initUTxO pf) (Coin 0) (Coin 0) def
-- | This is a helper type for the expectedUTxO function.
-- ExpectSuccess indicates that we created a valid transaction
-- where the IsValidating flag is true.
data Expect era = ExpectSuccess (Core.TxBody era) (Core.TxOut era) | ExpectFailure
-- | In each of our main eight examples, the UTxO map obtained
-- by applying the transaction is straightforward. This function
-- captures the logic.
--
-- Each example transaction (given a number i) will use
-- (TxIn genesisId (10+i), someOutput) for its' single input,
-- and (TxIn genesisId i, collateralOutput) for its' single collateral output.
--
-- If we expect the transaction script to validate, then
-- the UTxO for (TxIn genesisId i) will be consumed and a UTxO will be created.
-- Otherwise, the UTxO for (TxIn genesisId (10+i)) will be consumed.
expectedUTxO :: forall era. (PostShelley era) => Proof era -> Expect era -> Natural -> UTxO era
expectedUTxO pf ex idx = UTxO utxo
where
utxo = case ex of
ExpectSuccess txb newOut ->
Map.insert (TxIn (txid @era txb) 0) newOut (filteredUTxO idx)
ExpectFailure -> filteredUTxO (10 + idx)
filteredUTxO :: Natural -> Map.Map (TxIn (Crypto era)) (Core.TxOut era)
filteredUTxO x = Map.filterWithKey (\(TxIn _ i) _ -> i /= x) (unUTxO . initUTxO $ pf)
keyBy :: Ord k => (a -> k) -> [a] -> Map k a
keyBy f xs = Map.fromList $ (\x -> (f x, x)) <$> xs
-- =========================================================================
-- Example 1: Process a SPEND transaction with a succeeding Plutus script.
-- =========================================================================
datumExample1 :: Data era
datumExample1 = Data (Plutus.I 123)
redeemerExample1 :: Data era
redeemerExample1 = Data (Plutus.I 42)
txDatsExample1 :: Era era => TxDats era
txDatsExample1 = TxDats $ keyBy hashData $ [datumExample1]
alwaysSucceedsOutput :: forall era. (Scriptic era) => Proof era -> Core.TxOut era
alwaysSucceedsOutput pf =
newTxOut
Override
pf
[ Address (scriptAddr (always 3 pf) pf),
Amount (inject $ Coin 5000),
DHash [hashData $ datumExample1 @era]
]
validatingRedeemersEx1 :: Era era => Redeemers era
validatingRedeemersEx1 =
Redeemers $
Map.singleton (RdmrPtr Tag.Spend 0) (redeemerExample1, ExUnits 5000 5000)
outEx1 :: Scriptic era => Proof era -> Core.TxOut era
outEx1 pf = newTxOut Override pf [Address (someAddr pf), Amount (inject $ Coin 4995)]
validatingBody :: Scriptic era => Proof era -> Core.TxBody era
validatingBody pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 1],
Collateral [TxIn genesisId 11],
Outputs [outEx1 pf],
Txfee (Coin 5),
WppHash (newWppHash pf (pp pf) [PlutusV1] validatingRedeemersEx1 txDatsExample1)
]
type SignBody era =
( Signable
(CC.DSIGN (Crypto era))
(CH.Hash (CC.HASH (Crypto era)) EraIndependentTxBody)
)
validatingTx ::
forall era.
( Scriptic era,
SignBody era
) =>
Proof era ->
Core.Tx era
validatingTx pf =
newTx
Override
pf
[ Body (validatingBody pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (validatingBody pf)) (someKeys pf)],
ScriptWits [always 3 pf],
DataWits [datumExample1],
RdmrWits validatingRedeemersEx1
]
]
utxoEx1 :: forall era. PostShelley era => Proof era -> UTxO era
utxoEx1 pf = expectedUTxO pf (ExpectSuccess (validatingBody pf) (outEx1 pf)) 1
utxoStEx1 ::
forall era.
(Default (State (EraRule "PPUP" era)), PostShelley era) =>
Proof era ->
UTxOState era
utxoStEx1 pf = UTxOState (utxoEx1 pf) (Coin 0) (Coin 5) def
-- ======================================================================
-- Example 2: Process a SPEND transaction with a failing Plutus script.
-- ======================================================================
datumExample2 :: Data era
datumExample2 = Data (Plutus.I 0)
redeemerExample2 :: Data era
redeemerExample2 = Data (Plutus.I 1)
txDatsExample2 :: Era era => TxDats era
txDatsExample2 = TxDats $ keyBy hashData $ [datumExample2]
notValidatingRedeemers :: Era era => Redeemers era
notValidatingRedeemers =
Redeemers
( Map.fromList
[ ( RdmrPtr Tag.Spend 0,
(redeemerExample2, ExUnits 5000 5000)
)
]
)
alwaysFailsOutput :: forall era. (Scriptic era) => Proof era -> Core.TxOut era
alwaysFailsOutput pf =
newTxOut
Override
pf
[ Address (scriptAddr (never 0 pf) pf),
Amount (inject $ Coin 3000),
DHash [hashData $ datumExample2 @era]
]
outEx2 :: (Scriptic era) => Proof era -> Core.TxOut era
outEx2 pf = newTxOut Override pf [Address (someAddr pf), Amount (inject $ Coin 2995)]
notValidatingBody :: Scriptic era => Proof era -> Core.TxBody era
notValidatingBody pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 2],
Collateral [TxIn genesisId 12],
Outputs [outEx2 pf],
Txfee (Coin 5),
WppHash (newWppHash pf (pp pf) [PlutusV1] notValidatingRedeemers txDatsExample2)
]
notValidatingTx ::
( Scriptic era,
SignBody era
) =>
Proof era ->
Core.Tx era
notValidatingTx pf =
newTx
Override
pf
[ Body (notValidatingBody pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (notValidatingBody pf)) (someKeys pf)],
ScriptWits [never 0 pf],
DataWits [datumExample2],
RdmrWits notValidatingRedeemers
]
]
utxoEx2 :: PostShelley era => Proof era -> UTxO era
utxoEx2 pf = expectedUTxO pf ExpectFailure 2
utxoStEx2 ::
(Default (State (EraRule "PPUP" era)), PostShelley era) =>
Proof era ->
UTxOState era
utxoStEx2 pf = UTxOState (utxoEx2 pf) (Coin 0) (Coin 1000) def
-- =========================================================================
-- Example 3: Process a CERT transaction with a succeeding Plutus script.
-- =========================================================================
outEx3 :: Era era => Proof era -> Core.TxOut era
outEx3 pf = newTxOut Override pf [Address (someAddr pf), Amount (inject $ Coin 995)]
redeemerExample3 :: Data era
redeemerExample3 = Data (Plutus.I 42)
validatingRedeemersEx3 :: Era era => Redeemers era
validatingRedeemersEx3 =
Redeemers $
Map.singleton (RdmrPtr Tag.Cert 0) (redeemerExample3, ExUnits 5000 5000)
scriptStakeCredSuceed :: Scriptic era => Proof era -> StakeCredential (Crypto era)
scriptStakeCredSuceed pf = ScriptHashObj (alwaysSucceedsHash 2 pf)
validatingBodyWithCert :: Scriptic era => Proof era -> Core.TxBody era
validatingBodyWithCert pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 3],
Collateral [TxIn genesisId 13],
Outputs [outEx3 pf],
Certs [DCertDeleg (DeRegKey $ scriptStakeCredSuceed pf)],
Txfee (Coin 5),
WppHash (newWppHash pf (pp pf) [PlutusV1] validatingRedeemersEx3 mempty)
]
validatingTxWithCert ::
forall era.
( Scriptic era,
SignBody era
) =>
Proof era ->
Core.Tx era
validatingTxWithCert pf =
newTx
Override
pf
[ Body (validatingBodyWithCert pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (validatingBodyWithCert pf)) (someKeys pf)],
ScriptWits [always 2 pf],
RdmrWits validatingRedeemersEx3
]
]
utxoEx3 :: PostShelley era => Proof era -> UTxO era
utxoEx3 pf = expectedUTxO pf (ExpectSuccess (validatingBodyWithCert pf) (outEx3 pf)) 3
utxoStEx3 ::
(Default (State (EraRule "PPUP" era)), PostShelley era) =>
Proof era ->
UTxOState era
utxoStEx3 pf = UTxOState (utxoEx3 pf) (Coin 0) (Coin 5) def
-- =====================================================================
-- Example 4: Process a CERT transaction with a failing Plutus script.
-- =====================================================================
outEx4 :: (Scriptic era) => Proof era -> Core.TxOut era
outEx4 pf = newTxOut Override pf [Address (someAddr pf), Amount (inject $ Coin 995)]
redeemerExample4 :: Data era
redeemerExample4 = Data (Plutus.I 0)
notValidatingRedeemersEx4 :: Era era => Redeemers era
notValidatingRedeemersEx4 =
Redeemers $
Map.singleton (RdmrPtr Tag.Cert 0) (redeemerExample4, ExUnits 5000 5000)
scriptStakeCredFail :: Scriptic era => Proof era -> StakeCredential (Crypto era)
scriptStakeCredFail pf = ScriptHashObj (alwaysFailsHash 1 pf)
notValidatingBodyWithCert :: Scriptic era => Proof era -> Core.TxBody era
notValidatingBodyWithCert pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 4],
Collateral [TxIn genesisId 14],
Outputs [outEx4 pf],
Certs [DCertDeleg (DeRegKey $ scriptStakeCredFail pf)],
Txfee (Coin 5),
WppHash (newWppHash pf (pp pf) [PlutusV1] notValidatingRedeemersEx4 mempty)
]
notValidatingTxWithCert ::
forall era.
( Scriptic era,
SignBody era
) =>
Proof era ->
Core.Tx era
notValidatingTxWithCert pf =
newTx
Override
pf
[ Body (notValidatingBodyWithCert pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (notValidatingBodyWithCert pf)) (someKeys pf)],
ScriptWits [never 1 pf],
RdmrWits notValidatingRedeemersEx4
]
]
utxoEx4 :: PostShelley era => Proof era -> UTxO era
utxoEx4 pf = expectedUTxO pf ExpectFailure 4
utxoStEx4 ::
(Default (State (EraRule "PPUP" era)), PostShelley era) =>
Proof era ->
UTxOState era
utxoStEx4 pf = UTxOState (utxoEx4 pf) (Coin 0) (Coin 1000) def
-- ==============================================================================
-- Example 5: Process a WITHDRAWAL transaction with a succeeding Plutus script.
-- ==============================================================================
outEx5 :: (Scriptic era) => Proof era -> Core.TxOut era
outEx5 pf = newTxOut Override pf [Address (someAddr pf), Amount (inject $ Coin 1995)]
redeemerExample5 :: Data era
redeemerExample5 = Data (Plutus.I 42)
validatingRedeemersEx5 :: Era era => Redeemers era
validatingRedeemersEx5 =
Redeemers $
Map.singleton (RdmrPtr Tag.Rewrd 0) (redeemerExample5, ExUnits 5000 5000)
validatingBodyWithWithdrawal :: Scriptic era => Proof era -> Core.TxBody era
validatingBodyWithWithdrawal pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 5],
Collateral [TxIn genesisId 15],
Outputs [outEx5 pf],
Txfee (Coin 5),
Wdrls
( Wdrl $
Map.singleton
(RewardAcnt Testnet (scriptStakeCredSuceed pf))
(Coin 1000)
),
WppHash (newWppHash pf (pp pf) [PlutusV1] validatingRedeemersEx5 mempty)
]
validatingTxWithWithdrawal ::
forall era.
( Scriptic era,
SignBody era
) =>
Proof era ->
Core.Tx era
validatingTxWithWithdrawal pf =
newTx
Override
pf
[ Body (validatingBodyWithWithdrawal pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (validatingBodyWithWithdrawal pf)) (someKeys pf)],
ScriptWits [always 2 pf],
RdmrWits validatingRedeemersEx5
]
]
utxoEx5 :: PostShelley era => Proof era -> UTxO era
utxoEx5 pf = expectedUTxO pf (ExpectSuccess (validatingBodyWithWithdrawal pf) (outEx5 pf)) 5
utxoStEx5 ::
(Default (State (EraRule "PPUP" era)), PostShelley era) =>
Proof era ->
UTxOState era
utxoStEx5 pf = UTxOState (utxoEx5 pf) (Coin 0) (Coin 5) def
-- ===========================================================================
-- Example 6: Process a WITHDRAWAL transaction with a failing Plutus script.
-- ===========================================================================
outEx6 :: (Scriptic era) => Proof era -> Core.TxOut era
outEx6 pf = newTxOut Override pf [Address (someAddr pf), Amount (inject $ Coin 1995)]
redeemerExample6 :: Data era
redeemerExample6 = Data (Plutus.I 0)
notValidatingRedeemersEx6 :: Era era => Redeemers era
notValidatingRedeemersEx6 =
Redeemers $
Map.singleton (RdmrPtr Tag.Rewrd 0) (redeemerExample6, ExUnits 5000 5000)
notValidatingBodyWithWithdrawal :: Scriptic era => Proof era -> Core.TxBody era
notValidatingBodyWithWithdrawal pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 6],
Collateral [TxIn genesisId 16],
Outputs [outEx6 pf],
Txfee (Coin 5),
Wdrls
( Wdrl $
Map.singleton
(RewardAcnt Testnet (scriptStakeCredFail pf))
(Coin 1000)
),
WppHash (newWppHash pf (pp pf) [PlutusV1] notValidatingRedeemersEx6 mempty)
]
notValidatingTxWithWithdrawal ::
forall era.
( Scriptic era,
SignBody era
) =>
Proof era ->
Core.Tx era
notValidatingTxWithWithdrawal pf =
newTx
Override
pf
[ Body (notValidatingBodyWithWithdrawal pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (notValidatingBodyWithWithdrawal pf)) (someKeys pf)],
ScriptWits [never 1 pf],
RdmrWits notValidatingRedeemersEx6
]
]
utxoEx6 :: PostShelley era => Proof era -> UTxO era
utxoEx6 pf = expectedUTxO pf ExpectFailure 6
utxoStEx6 ::
(Default (State (EraRule "PPUP" era)), PostShelley era) =>
Proof era ->
UTxOState era
utxoStEx6 pf = UTxOState (utxoEx6 pf) (Coin 0) (Coin 1000) def
-- =============================================================================
-- Example 7: Process a MINT transaction with a succeeding Plutus script.
-- =============================================================================
mintEx7 :: forall era. (Scriptic era, HasTokens era) => Proof era -> Core.Value era
mintEx7 pf = forge @era 1 (always 2 pf)
outEx7 :: (HasTokens era, Scriptic era) => Proof era -> Core.TxOut era
outEx7 pf = newTxOut Override pf [Address (someAddr pf), Amount (mintEx7 pf <+> inject (Coin 995))]
redeemerExample7 :: Data era
redeemerExample7 = Data (Plutus.I 42)
validatingRedeemersEx7 :: Era era => Redeemers era
validatingRedeemersEx7 =
Redeemers $
Map.singleton (RdmrPtr Tag.Mint 0) (redeemerExample7, ExUnits 5000 5000)
validatingBodyWithMint :: (HasTokens era, Scriptic era) => Proof era -> Core.TxBody era
validatingBodyWithMint pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 7],
Collateral [TxIn genesisId 17],
Outputs [outEx7 pf],
Txfee (Coin 5),
Mint (mintEx7 pf),
WppHash (newWppHash pf (pp pf) [PlutusV1] validatingRedeemersEx7 mempty)
]
validatingTxWithMint ::
forall era.
( Scriptic era,
HasTokens era,
SignBody era
) =>
Proof era ->
Core.Tx era
validatingTxWithMint pf =
newTx
Override
pf
[ Body (validatingBodyWithMint pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (validatingBodyWithMint pf)) (someKeys pf)],
ScriptWits [always 2 pf],
RdmrWits validatingRedeemersEx7
]
]
utxoEx7 :: forall era. (HasTokens era, PostShelley era) => Proof era -> UTxO era
utxoEx7 pf = expectedUTxO pf (ExpectSuccess (validatingBodyWithMint pf) (outEx7 pf)) 7
utxoStEx7 ::
forall era.
(Default (State (EraRule "PPUP" era)), PostShelley era, HasTokens era) =>
Proof era ->
UTxOState era
utxoStEx7 pf = UTxOState (utxoEx7 pf) (Coin 0) (Coin 5) def
-- ==============================================================================
-- Example 8: Process a MINT transaction with a failing Plutus script.
-- ==============================================================================
mintEx8 :: forall era. (Scriptic era, HasTokens era) => Proof era -> Core.Value era
mintEx8 pf = forge @era 1 (never 1 pf)
outEx8 :: (HasTokens era, Scriptic era) => Proof era -> Core.TxOut era
outEx8 pf = newTxOut Override pf [Address (someAddr pf), Amount (mintEx8 pf <+> inject (Coin 995))]
redeemerExample8 :: Data era
redeemerExample8 = Data (Plutus.I 0)
notValidatingRedeemersEx8 :: Era era => Redeemers era
notValidatingRedeemersEx8 =
Redeemers $
Map.singleton (RdmrPtr Tag.Mint 0) (redeemerExample8, ExUnits 5000 5000)
notValidatingBodyWithMint :: (HasTokens era, Scriptic era) => Proof era -> Core.TxBody era
notValidatingBodyWithMint pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 8],
Collateral [TxIn genesisId 18],
Outputs [outEx8 pf],
Txfee (Coin 5),
Mint (mintEx8 pf),
WppHash (newWppHash pf (pp pf) [PlutusV1] notValidatingRedeemersEx8 mempty)
]
notValidatingTxWithMint ::
forall era.
( Scriptic era,
HasTokens era,
SignBody era
) =>
Proof era ->
Core.Tx era
notValidatingTxWithMint pf =
newTx
Override
pf
[ Body (notValidatingBodyWithMint pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (notValidatingBodyWithMint pf)) (someKeys pf)],
ScriptWits [never 1 pf],
RdmrWits notValidatingRedeemersEx8
]
]
utxoEx8 :: PostShelley era => Proof era -> UTxO era
utxoEx8 pf = expectedUTxO pf ExpectFailure 8
utxoStEx8 ::
(Default (State (EraRule "PPUP" era)), PostShelley era) =>
Proof era ->
UTxOState era
utxoStEx8 pf = UTxOState (utxoEx8 pf) (Coin 0) (Coin 1000) def
-- ====================================================================================
-- Example 9: Process a transaction with a succeeding script in every place possible,
-- and also with succeeding timelock scripts.
-- ====================================================================================
validatingRedeemersEx9 :: Era era => Redeemers era
validatingRedeemersEx9 =
Redeemers . Map.fromList $
[ (RdmrPtr Tag.Spend 0, (Data (Plutus.I 101), ExUnits 5000 5000)),
(RdmrPtr Tag.Cert 1, (Data (Plutus.I 102), ExUnits 5000 5000)),
(RdmrPtr Tag.Rewrd 0, (Data (Plutus.I 103), ExUnits 5000 5000)),
(RdmrPtr Tag.Mint 1, (Data (Plutus.I 104), ExUnits 5000 5000))
]
mintEx9 :: forall era. (PostShelley era, HasTokens era) => Proof era -> Core.Value era
mintEx9 pf = forge @era 1 (always 2 pf) <+> forge @era 1 (timelockScript 1 pf)
outEx9 :: (HasTokens era, PostShelley era) => Proof era -> Core.TxOut era
outEx9 pf =
newTxOut
Override
pf
[ Address (someAddr pf),
Amount (mintEx9 pf <+> inject (Coin 4996))
]
timelockStakeCred :: PostShelley era => Proof era -> StakeCredential (Crypto era)
timelockStakeCred pf = ScriptHashObj (timelockHash 2 pf)
validatingBodyManyScripts ::
(HasTokens era, PostShelley era) =>
Proof era ->
Core.TxBody era
validatingBodyManyScripts pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 1, TxIn genesisId 100],
Collateral [TxIn genesisId 11],
Outputs [outEx9 pf],
Txfee (Coin 5),
Certs
[ DCertDeleg (DeRegKey $ timelockStakeCred pf),
DCertDeleg (DeRegKey $ scriptStakeCredSuceed pf)
],
Wdrls
( Wdrl $
Map.fromList
[ (RewardAcnt Testnet (scriptStakeCredSuceed pf), Coin 0),
(RewardAcnt Testnet (timelockStakeCred pf), Coin 0)
]
),
Mint (mintEx9 pf),
WppHash (newWppHash pf (pp pf) [PlutusV1] validatingRedeemersEx9 txDatsExample1),
Vldt (ValidityInterval SNothing (SJust $ SlotNo 1))
]
validatingTxManyScripts ::
forall era.
( PostShelley era,
HasTokens era,
SignBody era
) =>
Proof era ->
Core.Tx era
validatingTxManyScripts pf =
newTx
Override
pf
[ Body (validatingBodyManyScripts pf),
Witnesses'
[ AddrWits $
map
(makeWitnessVKey . hashAnnotated . validatingBodyManyScripts $ pf)
[someKeys pf, theKeyPair 1],
ScriptWits
[ always 2 pf,
always 3 pf,
timelockScript 0 pf,
timelockScript 1 pf,
timelockScript 2 pf
],
DataWits [datumExample1],
RdmrWits validatingRedeemersEx9
]
]
utxoEx9 :: forall era. (PostShelley era, HasTokens era) => Proof era -> UTxO era
utxoEx9 pf = UTxO utxo
where
utxo =
Map.insert (TxIn (txid @era (validatingBodyManyScripts pf)) 0) (outEx9 pf) $
Map.filterWithKey
(\k _ -> k /= (TxIn genesisId 1) && k /= (TxIn genesisId 100))
(unUTxO $ initUTxO pf)
utxoStEx9 ::
forall era.
(Default (State (EraRule "PPUP" era)), PostShelley era, HasTokens era) =>
Proof era ->
UTxOState era
utxoStEx9 pf = UTxOState (utxoEx9 pf) (Coin 0) (Coin 5) def
-- ====================================================================================
-- Example 10: A transaction with an acceptable supplimentary datum
-- ====================================================================================
outEx10 :: forall era. (Scriptic era) => Proof era -> Core.TxOut era
outEx10 pf =
newTxOut
Override
pf
[ Address (scriptAddr (always 3 pf) pf),
Amount (inject $ Coin 995),
DHash [hashData $ datumExample1 @era]
]
okSupplimentaryDatumTxBody :: Scriptic era => Proof era -> Core.TxBody era
okSupplimentaryDatumTxBody pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 3],
Outputs [outEx10 pf],
Txfee (Coin 5),
WppHash (newWppHash pf (pp pf) [] (Redeemers mempty) txDatsExample1)
]
okSupplimentaryDatumTx ::
forall era.
( Scriptic era,
SignBody era
) =>
Proof era ->
Core.Tx era
okSupplimentaryDatumTx pf =
newTx
Override
pf
[ Body (okSupplimentaryDatumTxBody pf),
Witnesses'
[ AddrWits [makeWitnessVKey (hashAnnotated (okSupplimentaryDatumTxBody pf)) (someKeys pf)],
DataWits [datumExample1]
]
]
utxoEx10 :: forall era. PostShelley era => Proof era -> UTxO era
utxoEx10 pf = expectedUTxO pf (ExpectSuccess (okSupplimentaryDatumTxBody pf) (outEx10 pf)) 3
utxoStEx10 ::
forall era.
(Default (State (EraRule "PPUP" era)), PostShelley era) =>
Proof era ->
UTxOState era
utxoStEx10 pf = UTxOState (utxoEx10 pf) (Coin 0) (Coin 5) def
-- =======================
-- Invalid Transactions
-- =======================
incorrectNetworkIDTxBody :: Era era => Proof era -> Core.TxBody era
incorrectNetworkIDTxBody pf =
newTxBody
Override
pf
[ Inputs [TxIn genesisId 3],
Outputs [outEx3 pf],
Txfee (Coin 5),
Txnetworkid (SJust Mainnet)
]
incorrectNetworkIDTx :: (Era era, SignBody era) => Proof era -> Core.Tx era
incorrectNetworkIDTx pf =
newTx
Override
pf