-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathUtxos.hs
298 lines (277 loc) · 9.92 KB
/
Utxos.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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Cardano.Ledger.Babbage.Rules.Utxos (
BabbageUTXOS,
utxosTransition,
expectScriptsToPass,
babbageEvalScriptsTxInvalid,
) where
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext)
import Cardano.Ledger.Alonzo.Plutus.Evaluate (
collectPlutusScriptsWithContext,
evalPlutusScripts,
)
import Cardano.Ledger.Alonzo.Rules (
AlonzoUtxosEvent (..),
AlonzoUtxosPredFailure (..),
TagMismatchDescription (..),
invalidBegin,
invalidEnd,
scriptFailureToFailureDescription,
validBegin,
validEnd,
when2Phase,
)
import Cardano.Ledger.Alonzo.UTxO (
AlonzoEraUTxO (..),
AlonzoScriptsNeeded,
)
import Cardano.Ledger.Babbage.Collateral (
collAdaBalance,
collOuts,
)
import Cardano.Ledger.Babbage.Core
import Cardano.Ledger.Babbage.Era (BabbageEra, BabbageUTXOS)
import Cardano.Ledger.Babbage.Rules.Ppup ()
import Cardano.Ledger.Babbage.Tx
import Cardano.Ledger.BaseTypes (
ShelleyBase,
StrictMaybe,
epochInfo,
systemStart,
)
import Cardano.Ledger.Binary (EncCBOR (..))
import Cardano.Ledger.CertState (certDState, dsGenDelegs)
import Cardano.Ledger.Coin (Coin (..), DeltaCoin (..))
import Cardano.Ledger.Plutus.Evaluate (
ScriptFailure (..),
ScriptResult (..),
)
import Cardano.Ledger.Shelley.LedgerState (UTxOState (..), updateStakeDistribution)
import Cardano.Ledger.Shelley.PParams (Update)
import Cardano.Ledger.Shelley.Rules (
PpupEnv (..),
PpupEvent,
ShelleyPPUP,
ShelleyPpupPredFailure,
UtxoEnv (..),
updateUTxOState,
)
import Cardano.Ledger.UTxO (EraUTxO (..), UTxO (..))
import Control.Monad.Trans.Reader (asks)
import Control.State.Transition.Extended
import Data.List.NonEmpty (nonEmpty)
import qualified Data.Map.Strict as Map
import Data.MapExtras (extractKeys)
import qualified Debug.Trace as Debug
import Lens.Micro
type instance EraRuleFailure "UTXOS" BabbageEra = AlonzoUtxosPredFailure BabbageEra
type instance EraRuleEvent "UTXOS" BabbageEra = AlonzoUtxosEvent BabbageEra
instance InjectRuleFailure "UTXOS" AlonzoUtxosPredFailure BabbageEra
instance InjectRuleEvent "UTXOS" AlonzoUtxosEvent BabbageEra
instance InjectRuleFailure "UTXOS" ShelleyPpupPredFailure BabbageEra where
injectFailure = UpdateFailure
-- =====================================================
instance
( AlonzoEraTx era
, AlonzoEraPParams era
, ShelleyEraTxBody era
, BabbageEraTxBody era
, AlonzoEraUTxO era
, EraPlutusContext era
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
, EraGov era
, GovState era ~ ShelleyGovState era
, Embed (EraRule "PPUP" era) (BabbageUTXOS era)
, Environment (EraRule "PPUP" era) ~ PpupEnv era
, Signal (EraRule "PPUP" era) ~ StrictMaybe (Update era)
, State (EraRule "PPUP" era) ~ ShelleyGovState era
, Signal (BabbageUTXOS era) ~ Tx era
, EncCBOR (EraRuleFailure "PPUP" era)
, Eq (EraRuleFailure "PPUP" era)
, Show (EraRuleFailure "PPUP" era)
, InjectRuleFailure "UTXOS" AlonzoUtxosPredFailure era
, InjectRuleEvent "UTXOS" AlonzoUtxosEvent era
, EraRule "UTXOS" era ~ BabbageUTXOS era
) =>
STS (BabbageUTXOS era)
where
type BaseM (BabbageUTXOS era) = ShelleyBase
type Environment (BabbageUTXOS era) = UtxoEnv era
type State (BabbageUTXOS era) = UTxOState era
type Signal (BabbageUTXOS era) = AlonzoTx era
type PredicateFailure (BabbageUTXOS era) = AlonzoUtxosPredFailure era
type Event (BabbageUTXOS era) = AlonzoUtxosEvent era
transitionRules = [utxosTransition]
instance
( Era era
, STS (ShelleyPPUP era)
, EraRuleFailure "PPUP" era ~ ShelleyPpupPredFailure era
, EraRuleEvent "PPUP" era ~ PpupEvent era
) =>
Embed (ShelleyPPUP era) (BabbageUTXOS era)
where
wrapFailed = UpdateFailure
wrapEvent = AlonzoPpupToUtxosEvent
utxosTransition ::
forall era.
( AlonzoEraTx era
, ShelleyEraTxBody era
, BabbageEraTxBody era
, AlonzoEraUTxO era
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
, EraGov era
, GovState era ~ ShelleyGovState era
, Environment (EraRule "PPUP" era) ~ PpupEnv era
, Signal (EraRule "PPUP" era) ~ StrictMaybe (Update era)
, Embed (EraRule "PPUP" era) (BabbageUTXOS era)
, State (EraRule "PPUP" era) ~ ShelleyGovState era
, Signal (BabbageUTXOS era) ~ Tx era
, EncCBOR (EraRuleFailure "PPUP" era)
, Eq (EraRuleFailure "PPUP" era)
, Show (EraRuleFailure "PPUP" era)
, EraPlutusContext era
, EraRule "UTXOS" era ~ BabbageUTXOS era
, InjectRuleFailure "UTXOS" AlonzoUtxosPredFailure era
, InjectRuleEvent "UTXOS" AlonzoUtxosEvent era
) =>
TransitionRule (BabbageUTXOS era)
utxosTransition =
judgmentContext >>= \(TRC (_, _, tx)) -> do
case tx ^. isValidTxL of
IsValid True -> babbageEvalScriptsTxValid
IsValid False -> babbageEvalScriptsTxInvalid
-- ===================================================================
expectScriptsToPass ::
forall era.
( AlonzoEraTx era
, EraPlutusContext era
, AlonzoEraUTxO era
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
, STS (EraRule "UTXOS" era)
, InjectRuleFailure "UTXOS" AlonzoUtxosPredFailure era
, BaseM (EraRule "UTXOS" era) ~ ShelleyBase
, InjectRuleEvent "UTXOS" AlonzoUtxosEvent era
) =>
PParams era ->
Tx era ->
UTxO era ->
Rule (EraRule "UTXOS" era) 'Transition ()
expectScriptsToPass pp tx utxo = do
sysSt <- liftSTS $ asks systemStart
ei <- liftSTS $ asks epochInfo
{- sLst := collectTwoPhaseScriptInputs pp tx utxo -}
case collectPlutusScriptsWithContext ei sysSt pp tx utxo of
Right sLst -> do
{- isValid tx = evalScripts tx sLst = True -}
whenFailureFree $
when2Phase $ case evalPlutusScripts sLst of
Fails _ fs ->
failBecause $
injectFailure $
ValidationTagMismatch
(tx ^. isValidTxL)
(FailedUnexpectedly (scriptFailureToFailureDescription <$> fs))
Passes ps -> mapM_ (tellEvent . injectEvent . SuccessfulPlutusScriptsEvent) (nonEmpty ps)
Left info -> failBecause (injectFailure $ CollectErrors info)
babbageEvalScriptsTxValid ::
forall era.
( AlonzoEraTx era
, AlonzoEraUTxO era
, ShelleyEraTxBody era
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
, STS (BabbageUTXOS era)
, Signal (BabbageUTXOS era) ~ Tx era
, Environment (EraRule "PPUP" era) ~ PpupEnv era
, Signal (EraRule "PPUP" era) ~ StrictMaybe (Update era)
, Embed (EraRule "PPUP" era) (BabbageUTXOS era)
, GovState era ~ ShelleyGovState era
, State (EraRule "PPUP" era) ~ ShelleyGovState era
, EraPlutusContext era
, InjectRuleFailure "UTXOS" AlonzoUtxosPredFailure era
, EraRule "UTXOS" era ~ BabbageUTXOS era
, InjectRuleEvent "UTXOS" AlonzoUtxosEvent era
) =>
TransitionRule (BabbageUTXOS era)
babbageEvalScriptsTxValid = do
TRC (UtxoEnv slot pp certState, utxos@(UTxOState utxo _ _ pup _ _), tx) <-
judgmentContext
let txBody = tx ^. bodyTxL
genDelegs = dsGenDelegs (certDState certState)
-- We intentionally run the PPUP rule before evaluating any Plutus scripts.
-- We do not want to waste computation running plutus scripts if the
-- transaction will fail due to `PPUP`
ppup' <-
trans @(EraRule "PPUP" era) $
TRC (PPUPEnv slot pp genDelegs, pup, txBody ^. updateTxBodyL)
() <- pure $! Debug.traceEvent validBegin ()
expectScriptsToPass pp tx utxo
() <- pure $! Debug.traceEvent validEnd ()
updateUTxOState
pp
utxos
txBody
certState
ppup'
(tellEvent . TotalDeposits (hashAnnotated txBody))
(\a b -> tellEvent $ TxUTxODiff a b)
babbageEvalScriptsTxInvalid ::
forall era.
( AlonzoEraTx era
, BabbageEraTxBody era
, EraPlutusContext era
, AlonzoEraUTxO era
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
, STS (EraRule "UTXOS" era)
, Environment (EraRule "UTXOS" era) ~ UtxoEnv era
, Signal (EraRule "UTXOS" era) ~ Tx era
, State (EraRule "UTXOS" era) ~ UTxOState era
, BaseM (EraRule "UTXOS" era) ~ ShelleyBase
, InjectRuleFailure "UTXOS" AlonzoUtxosPredFailure era
, InjectRuleEvent "UTXOS" AlonzoUtxosEvent era
) =>
TransitionRule (EraRule "UTXOS" era)
babbageEvalScriptsTxInvalid = do
TRC (UtxoEnv _ pp _, us@(UTxOState utxo _ fees _ _ _), tx) <- judgmentContext
{- txb := txbody tx -}
let txBody = tx ^. bodyTxL
sysSt <- liftSTS $ asks systemStart
ei <- liftSTS $ asks epochInfo
() <- pure $! Debug.traceEvent invalidBegin ()
case collectPlutusScriptsWithContext ei sysSt pp tx utxo of
Right sLst ->
{- sLst := collectTwoPhaseScriptInputs pp tx utxo -}
{- isValid tx = evalScripts tx sLst = False -}
whenFailureFree $
when2Phase $ case evalPlutusScripts sLst of
Passes _ ->
failBecause $
injectFailure $
ValidationTagMismatch (tx ^. isValidTxL) PassedUnexpectedly
Fails ps fs -> do
mapM_ (tellEvent . injectEvent @"UTXOS" . SuccessfulPlutusScriptsEvent @era) (nonEmpty ps)
tellEvent (injectEvent $ FailedPlutusScriptsEvent (scriptFailurePlutus <$> fs))
Left info -> failBecause (injectFailure $ CollectErrors info)
() <- pure $! Debug.traceEvent invalidEnd ()
{- utxoKeep = txBody ^. collateralInputsTxBodyL ⋪ utxo -}
{- utxoDel = txBody ^. collateralInputsTxBodyL ◁ utxo -}
let !(utxoKeep, utxoDel) = extractKeys (unUTxO utxo) (txBody ^. collateralInputsTxBodyL)
UTxO collouts = collOuts txBody
DeltaCoin collateralFees = collAdaBalance txBody utxoDel -- NEW to Babbage
pure $!
us {- (collInputs txb ⋪ utxo) ∪ collouts tx -}
{ utxosUtxo = UTxO (Map.union utxoKeep collouts) -- NEW to Babbage
{- fees + collateralFees -}
, utxosFees = fees <> Coin collateralFees -- NEW to Babbage
, utxosStakeDistr = updateStakeDistribution pp (utxosStakeDistr us) (UTxO utxoDel) (UTxO collouts)
}