/
Consensus.hs
191 lines (180 loc) · 7.18 KB
/
Consensus.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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Test.Cardano.Ledger.Babbage.Examples.Consensus where
import Cardano.Ledger.Alonzo.Data
( AlonzoAuxiliaryData (..),
AuxiliaryDataHash (..),
Data (..),
dataToBinaryData,
hashData,
)
import Cardano.Ledger.Alonzo.Language (Language (..))
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..), ExUnits (..))
import qualified Cardano.Ledger.Alonzo.Scripts as Tag (Tag (..))
import Cardano.Ledger.Alonzo.Translation ()
import Cardano.Ledger.Alonzo.Tx (AlonzoTx (..), IsValid (..))
import Cardano.Ledger.Alonzo.TxWitness (RdmrPtr (..), Redeemers (..), TxDats (..), TxWitness (..))
import Cardano.Ledger.Babbage (BabbageEra)
import Cardano.Ledger.Babbage.PParams (BabbagePParamsHKD (..), emptyPParams, emptyPParamsUpdate)
import Cardano.Ledger.Babbage.Translation ()
import Cardano.Ledger.Babbage.TxBody (BabbageTxBody (..), BabbageTxOut (..), Datum (..))
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core (EraScript (hashScript), TxBody)
import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Keys (asWitness)
import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..))
import Cardano.Ledger.SafeHash (hashAnnotated)
import Cardano.Ledger.Serialization (mkSized)
import Cardano.Ledger.Shelley.API
( ApplyTxError (..),
Credential (..),
Network (..),
NewEpochState (..),
ProposedPPUpdates (..),
RewardAcnt (..),
TxId (..),
Update (..),
Wdrl (..),
)
import Cardano.Ledger.Shelley.Rules.Delegs (DelegsPredicateFailure (..))
import Cardano.Ledger.Shelley.Rules.Ledger (LedgerPredicateFailure (..))
import Cardano.Ledger.Shelley.Tx (ShelleyTx (..))
import Cardano.Ledger.Shelley.UTxO (makeWitnessesVKey)
import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..), ValidityInterval (..))
import Cardano.Ledger.TxIn (mkTxInPartial)
import Cardano.Slotting.Slot (EpochNo (..), SlotNo (..))
import Data.Default.Class (def)
import qualified Data.Map.Strict as Map
import Data.Proxy (Proxy (..))
import qualified Data.Sequence.Strict as StrictSeq
import qualified Data.Set as Set
import qualified PlutusTx as Plutus
import qualified Test.Cardano.Ledger.Alonzo.Examples.Consensus as AlonzoLE
import Test.Cardano.Ledger.Alonzo.Scripts (alwaysFails, alwaysSucceeds)
import qualified Test.Cardano.Ledger.Mary.Examples.Consensus as MarySLE
import qualified Test.Cardano.Ledger.Shelley.Examples.Consensus as SLE
import Test.Cardano.Ledger.Shelley.Orphans ()
import Test.Cardano.Ledger.Shelley.Utils (mkAddr)
type StandardBabbage = BabbageEra StandardCrypto
-- | ShelleyLedgerExamples for Babbage era
ledgerExamplesBabbage :: SLE.ShelleyLedgerExamples StandardBabbage
ledgerExamplesBabbage =
SLE.ShelleyLedgerExamples
{ SLE.sleBlock = SLE.exampleShelleyLedgerBlock exampleTransactionInBlock,
SLE.sleHashHeader = SLE.exampleHashHeader (Proxy @StandardBabbage),
SLE.sleTx = exampleTransactionInBlock,
SLE.sleApplyTxError =
ApplyTxError $
pure $
DelegsFailure $
DelegateeNotRegisteredDELEG @StandardBabbage (SLE.mkKeyHash 1),
SLE.sleRewardsCredentials =
Set.fromList
[ Left (Coin 100),
Right (ScriptHashObj (SLE.mkScriptHash 1)),
Right (KeyHashObj (SLE.mkKeyHash 2))
],
SLE.sleResultExamples = resultExamples,
SLE.sleNewEpochState = exampleBabbageNewEpochState,
SLE.sleChainDepState = SLE.exampleLedgerChainDepState 1,
SLE.sleTranslationContext = AlonzoLE.exampleAlonzoGenesis
}
where
resultExamples =
SLE.ShelleyResultExamples
{ SLE.srePParams = def,
SLE.sreProposedPPUpdates = examplePPPU,
SLE.srePoolDistr = SLE.examplePoolDistr,
SLE.sreNonMyopicRewards = SLE.exampleNonMyopicRewards,
SLE.sreShelleyGenesis = SLE.testShelleyGenesis
}
examplePPPU =
ProposedPPUpdates $
Map.singleton
(SLE.mkKeyHash 0)
(emptyPParamsUpdate {_collateralPercentage = SJust 150})
collateralOutput :: BabbageTxOut StandardBabbage
collateralOutput =
BabbageTxOut
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
(MaryValue 8675309 (MultiAsset mempty))
NoDatum
SNothing
exampleTxBodyBabbage :: TxBody StandardBabbage
exampleTxBodyBabbage =
BabbageTxBody
(Set.fromList [mkTxInPartial (TxId (SLE.mkDummySafeHash Proxy 1)) 0]) -- spending inputs
(Set.fromList [mkTxInPartial (TxId (SLE.mkDummySafeHash Proxy 2)) 1]) -- collateral inputs
(Set.fromList [mkTxInPartial (TxId (SLE.mkDummySafeHash Proxy 1)) 3]) -- reference inputs
( StrictSeq.fromList
[ mkSized $
BabbageTxOut
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
(MarySLE.exampleMultiAssetValue 2)
(Datum $ dataToBinaryData datumExample) -- inline datum
(SJust $ alwaysSucceeds PlutusV2 3) -- reference script
]
)
(SJust $ mkSized collateralOutput) -- collateral return
(SJust $ Coin 8675309) -- collateral tot
SLE.exampleCerts -- txcerts
( Wdrl $
Map.singleton
(RewardAcnt Testnet (SLE.keyToCredential SLE.exampleStakeKey))
(Coin 100) -- txwdrls
)
(Coin 999) -- txfee
(ValidityInterval (SJust (SlotNo 2)) (SJust (SlotNo 4))) -- txvldt
( SJust $
Update
( ProposedPPUpdates $
Map.singleton
(SLE.mkKeyHash 1)
(emptyPParamsUpdate {_maxBHSize = SJust 4000})
)
(EpochNo 0)
) -- txUpdates
(Set.singleton $ SLE.mkKeyHash 212) -- reqSignerHashes
(MarySLE.exampleMultiAssetValue 3) -- mint
(SJust $ SLE.mkDummySafeHash (Proxy @StandardCrypto) 42) -- scriptIntegrityHash
(SJust . AuxiliaryDataHash $ SLE.mkDummySafeHash (Proxy @StandardCrypto) 42) -- adHash
(SJust Mainnet) -- txnetworkid
datumExample :: Data StandardBabbage
datumExample = Data (Plutus.I 191)
redeemerExample :: Data StandardBabbage
redeemerExample = Data (Plutus.I 919)
exampleTx :: ShelleyTx StandardBabbage
exampleTx =
ShelleyTx
exampleTxBodyBabbage
( TxWitness
(makeWitnessesVKey (hashAnnotated exampleTxBodyBabbage) [asWitness SLE.examplePayKey]) -- vkey
mempty -- bootstrap
( Map.singleton
(hashScript @StandardBabbage $ alwaysSucceeds PlutusV1 3)
(alwaysSucceeds PlutusV1 3) -- txscripts
)
(TxDats $ Map.singleton (hashData datumExample) datumExample)
( Redeemers $
Map.singleton (RdmrPtr Tag.Spend 0) (redeemerExample, ExUnits 5000 5000)
) -- redeemers
)
( SJust $
AlonzoAuxiliaryData
SLE.exampleMetadataMap -- metadata
( StrictSeq.fromList
[alwaysFails PlutusV1 2, TimelockScript $ RequireAllOf mempty] -- Scripts
)
)
exampleTransactionInBlock :: AlonzoTx StandardBabbage
exampleTransactionInBlock = AlonzoTx b w (IsValid True) a
where
(ShelleyTx b w a) = exampleTx
exampleBabbageNewEpochState :: NewEpochState StandardBabbage
exampleBabbageNewEpochState =
SLE.exampleNewEpochState
(MarySLE.exampleMultiAssetValue 1)
emptyPParams
(emptyPParams {_coinsPerUTxOByte = Coin 1})