-
Notifications
You must be signed in to change notification settings - Fork 155
/
Utxow.hs
404 lines (369 loc) · 14.8 KB
/
Utxow.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Cardano.Ledger.Babbage.Rules.Utxow (
BabbageUTXOW,
BabbageUtxowPredFailure (..),
babbageMissingScripts,
validateFailedBabbageScripts,
validateScriptsWellFormed,
babbageUtxowTransition,
)
where
import Cardano.Crypto.DSIGN.Class (Signable)
import Cardano.Crypto.Hash.Class (Hash)
import Cardano.Ledger.Alonzo.Rules (
AlonzoUtxowEvent (WrappedShelleyEraEvent),
AlonzoUtxowPredFailure (ShelleyInAlonzoUtxowPredFailure),
hasExactSetOfRedeemers,
missingRequiredDatums,
ppViewHashesMatch,
requiredSignersAreWitnessed,
)
import Cardano.Ledger.Alonzo.Rules as Alonzo (AlonzoUtxoEvent)
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript)
import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx)
import Cardano.Ledger.Alonzo.TxInfo (ExtendedUTxO (..), validScript)
import Cardano.Ledger.Alonzo.UTxO (AlonzoScriptsNeeded)
import Cardano.Ledger.Babbage.Era (BabbageUTXOW)
import Cardano.Ledger.Babbage.Rules.Utxo (BabbageUTXO, BabbageUtxoPredFailure (..))
import Cardano.Ledger.Babbage.Tx (refScripts)
import Cardano.Ledger.Babbage.TxBody (
BabbageEraTxBody (..),
BabbageEraTxOut (..),
)
import Cardano.Ledger.BaseTypes (ShelleyBase, quorum, strictMaybeToMaybe)
import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
import Cardano.Ledger.Binary.Coders (
Decode (From, Invalid, SumD, Summands),
Encode (Sum, To),
decode,
encode,
(!>),
(<!),
)
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (DSIGN, HASH)
import Cardano.Ledger.Rules.ValidationMode (Inject (..), Test, runTest, runTestOnSignal)
import Cardano.Ledger.Shelley.LedgerState (UTxOState (..), witsFromTxWitnesses)
import Cardano.Ledger.Shelley.Rules (
ShelleyUtxowEvent (UtxoEvent),
ShelleyUtxowPredFailure,
UtxoEnv (..),
validateNeededWitnesses,
)
import qualified Cardano.Ledger.Shelley.Rules as Shelley
import Cardano.Ledger.UTxO (EraUTxO (..), UTxO (..))
import Control.Monad.Trans.Reader (asks)
import Control.State.Transition.Extended (
Embed (..),
STS (..),
TRC (..),
TransitionRule,
judgmentContext,
liftSTS,
trans,
)
import Data.Foldable (sequenceA_, toList)
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe)
import Data.Maybe.Strict (StrictMaybe (..))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable
import Lens.Micro
import Lens.Micro.Extras (view)
import NoThunks.Class (InspectHeapNamed (..), NoThunks (..))
import Validation (failureUnless)
data BabbageUtxowPredFailure era
= AlonzoInBabbageUtxowPredFailure !(AlonzoUtxowPredFailure era)
| -- | Embed UTXO rule failures
UtxoFailure !(PredicateFailure (EraRule "UTXO" era))
| -- | the set of malformed script witnesses
MalformedScriptWitnesses
!(Set (ScriptHash (EraCrypto era)))
| -- | the set of malformed script witnesses
MalformedReferenceScripts
!(Set (ScriptHash (EraCrypto era)))
deriving instance
( Era era
, Show (ShelleyUtxowPredFailure era)
, Show (PredicateFailure (EraRule "UTXO" era))
, Show (PredicateFailure (EraRule "UTXOS" era))
, Show (Script era)
, Show (TxOut era)
, Show (TxCert era)
, Show (Value era)
) =>
Show (BabbageUtxowPredFailure era)
deriving instance
( Era era
, Eq (ShelleyUtxowPredFailure era)
, Eq (PredicateFailure (EraRule "UTXO" era))
, Eq (PredicateFailure (EraRule "UTXOS" era))
, Eq (TxOut era)
, Eq (TxCert era)
, Eq (Script era)
) =>
Eq (BabbageUtxowPredFailure era)
instance Inject (AlonzoUtxowPredFailure era) (BabbageUtxowPredFailure era) where
inject = AlonzoInBabbageUtxowPredFailure
instance Inject (ShelleyUtxowPredFailure era) (BabbageUtxowPredFailure era) where
inject = AlonzoInBabbageUtxowPredFailure . ShelleyInAlonzoUtxowPredFailure
instance
( Era era
, EncCBOR (TxOut era)
, EncCBOR (TxCert era)
, EncCBOR (Value era)
, EncCBOR (PredicateFailure (EraRule "UTXOS" era))
, EncCBOR (PredicateFailure (EraRule "UTXO" era))
, EncCBOR (Script era)
, Typeable (TxAuxData era)
) =>
EncCBOR (BabbageUtxowPredFailure era)
where
encCBOR = encode . work
where
work (AlonzoInBabbageUtxowPredFailure x) = Sum AlonzoInBabbageUtxowPredFailure 1 !> To x
work (UtxoFailure x) = Sum UtxoFailure 2 !> To x
work (MalformedScriptWitnesses x) = Sum MalformedScriptWitnesses 3 !> To x
work (MalformedReferenceScripts x) = Sum MalformedReferenceScripts 4 !> To x
instance
( Era era
, DecCBOR (TxOut era)
, DecCBOR (TxCert era)
, DecCBOR (Value era)
, DecCBOR (PredicateFailure (EraRule "UTXOS" era))
, DecCBOR (PredicateFailure (EraRule "UTXO" era))
, Typeable (Script era)
, Typeable (TxAuxData era)
) =>
DecCBOR (BabbageUtxowPredFailure era)
where
decCBOR = decode (Summands "BabbageUtxowPred" work)
where
work 1 = SumD AlonzoInBabbageUtxowPredFailure <! From
work 2 = SumD UtxoFailure <! From
work 3 = SumD MalformedScriptWitnesses <! From
work 4 = SumD MalformedReferenceScripts <! From
work n = Invalid n
deriving via
InspectHeapNamed "BabbageUtxowPred" (BabbageUtxowPredFailure era)
instance
NoThunks (BabbageUtxowPredFailure era)
-- ==================================================
-- Reuseable tests first used in the Babbage Era
-- Int the Babbage Era with reference scripts, the needed
-- scripts only has to be a subset of the txscripts.
{- { s | (_,s) ∈ scriptsNeeded utxo tx} - dom(refScripts tx utxo) = dom(txscripts txw) -}
{- sNeeded := scriptsNeeded utxo tx -}
{- sReceived := Map.keysSet (tx ^. witsTxL . scriptTxWitsL) -}
babbageMissingScripts ::
forall era.
PParams era ->
Set (ScriptHash (EraCrypto era)) ->
Set (ScriptHash (EraCrypto era)) ->
Set (ScriptHash (EraCrypto era)) ->
Test (ShelleyUtxowPredFailure era)
babbageMissingScripts _ sNeeded sRefs sReceived =
sequenceA_
[ failureUnless (Set.null extra) $ Shelley.ExtraneousScriptWitnessesUTXOW extra
, failureUnless (Set.null missing) $ Shelley.MissingScriptWitnessesUTXOW missing
]
where
neededNonRefs = sNeeded `Set.difference` sRefs
missing = neededNonRefs `Set.difference` sReceived
extra = sReceived `Set.difference` neededNonRefs
{- ∀ s ∈ (txscripts txw utxo ∩ Scriptnative), validateScript s tx -}
validateFailedBabbageScripts ::
forall era.
( EraTx era
, ExtendedUTxO era
, Script era ~ AlonzoScript era
) =>
Tx era ->
UTxO era ->
Set (ScriptHash (EraCrypto era)) ->
Test (ShelleyUtxowPredFailure era)
validateFailedBabbageScripts tx utxo neededHashes =
let phase1Map = getPhase1 (txscripts utxo tx)
failedScripts =
Map.filterWithKey
( \hs (script, phased) ->
let needed = hs `Set.member` neededHashes
hashDisagrees = hashScript @era script /= hs
-- TODO this is probably not needed. Only the script is transmitted on the wire, we compute the hash
scriptDoesNotValidate = not (validateScript @era phased tx)
answer = needed && (hashDisagrees || scriptDoesNotValidate)
in answer
)
phase1Map
in failureUnless
(Map.null failedScripts)
(Shelley.ScriptWitnessNotValidatingUTXOW $ Map.keysSet failedScripts)
{- ∀x ∈ range(txdats txw) ∪ range(txwitscripts txw) ∪ ⋃ ( , ,d,s)∈txouts tx{s, d},
x ∈ Script ∪ Datum ⇒ isWellFormed x
-}
validateScriptsWellFormed ::
forall era.
( EraTx era
, BabbageEraTxBody era
, Script era ~ AlonzoScript era
) =>
PParams era ->
Tx era ->
Test (BabbageUtxowPredFailure era)
validateScriptsWellFormed pp tx =
sequenceA_
[ failureUnless (Map.null invalidScriptWits) $ MalformedScriptWitnesses (Map.keysSet invalidScriptWits)
, failureUnless (null invalidRefScripts) $ MalformedReferenceScripts invalidRefScriptHashes
]
where
scriptWits = tx ^. witsTxL . scriptTxWitsL
invalidScriptWits = Map.filter (not . validScript (pp ^. ppProtocolVersionL)) scriptWits
txBody = tx ^. bodyTxL
normalOuts = toList $ txBody ^. outputsTxBodyL
returnOut = txBody ^. collateralReturnTxBodyL
outs = case returnOut of
SNothing -> normalOuts
SJust rOut -> rOut : normalOuts
rScripts = mapMaybe (strictMaybeToMaybe . view referenceScriptTxOutL) outs
invalidRefScripts = filter (not . validScript (pp ^. ppProtocolVersionL)) rScripts
invalidRefScriptHashes = Set.fromList $ map (hashScript @era) invalidRefScripts
-- ==============================================================
-- Here we define the transtion function, using reusable tests.
-- The tests are very generic and reusable, but the transition
-- function is very specific to the Babbage Era.
-- | A very specialized transitionRule function for the Babbage Era.
babbageUtxowTransition ::
forall era.
( AlonzoEraTx era
, ExtendedUTxO era
, EraUTxO era
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
, Script era ~ AlonzoScript era
, STS (BabbageUTXOW era)
, BabbageEraTxBody era
, Signable (DSIGN (EraCrypto era)) (Hash (HASH (EraCrypto era)) EraIndependentTxBody)
, -- Allow UTXOW to call UTXO
Embed (EraRule "UTXO" era) (BabbageUTXOW era)
, Environment (EraRule "UTXO" era) ~ UtxoEnv era
, Signal (EraRule "UTXO" era) ~ Tx era
, State (EraRule "UTXO" era) ~ UTxOState era
) =>
TransitionRule (BabbageUTXOW era)
babbageUtxowTransition = do
(TRC (UtxoEnv slot pp stakepools genDelegs, u, tx)) <- judgmentContext
{- (utxo,_,_,_ ) := utxoSt -}
{- txb := txbody tx -}
{- txw := txwits tx -}
{- witsKeyHashes := { hashKey vk | vk ∈ dom(txwitsVKey txw) } -}
let utxo = utxosUtxo u
txBody = tx ^. bodyTxL
witsKeyHashes = witsFromTxWitnesses @era tx
hashScriptMap = txscripts utxo tx
inputs = (txBody ^. referenceInputsTxBodyL) `Set.union` (txBody ^. inputsTxBodyL)
-- check scripts
{- neededHashes := {h | ( , h) ∈ scriptsNeeded utxo txb} -}
{- neededHashes − dom(refScripts tx utxo) = dom(txwitscripts txw) -}
let scriptsNeeded = getScriptsNeeded utxo txBody
scriptHashesNeeded = getScriptsHashesNeeded scriptsNeeded
{- ∀s ∈ (txscripts txw utxo neededHashes ) ∩ Scriptph1 , validateScript s tx -}
-- CHANGED In BABBAGE txscripts depends on UTxO
runTest $ validateFailedBabbageScripts tx utxo scriptHashesNeeded
{- neededHashes − dom(refScripts tx utxo) = dom(txwitscripts txw) -}
let sReceived = Map.keysSet $ tx ^. witsTxL . scriptTxWitsL
sRefs = Map.keysSet $ refScripts inputs utxo
runTest $ babbageMissingScripts pp scriptHashesNeeded sRefs sReceived
{- inputHashes ⊆ dom(txdats txw) ⊆ allowed -}
runTest $ missingRequiredDatums hashScriptMap utxo tx
{- dom (txrdmrs tx) = { rdptr txb sp | (sp, h) ∈ scriptsNeeded utxo tx,
h ↦ s ∈ txscripts txw, s ∈ Scriptph2} -}
runTest $ hasExactSetOfRedeemers utxo tx scriptsNeeded
-- check VKey witnesses
-- let txbodyHash = hashAnnotated @(Crypto era) txbody
{- ∀ (vk ↦ σ) ∈ (txwitsVKey txw), V_vk⟦ txbodyHash ⟧_σ -}
runTestOnSignal $ Shelley.validateVerifiedWits tx
{- witsVKeyNeeded utxo tx genDelegs ⊆ witsKeyHashes -}
runTest $ validateNeededWitnesses genDelegs utxo tx witsKeyHashes
-- TODO can we add the required signers to witsVKeyNeeded so we dont need the check below?
{- THIS DOES NOT APPPEAR IN THE SPEC as a separate check, but
witsVKeyNeeded must include the reqSignerHashes in the union -}
{- reqSignerHashes txbody ⊆ witsKeyHashes -}
runTestOnSignal $ requiredSignersAreWitnessed txBody witsKeyHashes
-- check genesis keys signatures for instantaneous rewards certificates
{- genSig := { hashKey gkey | gkey ∈ dom(genDelegs)} ∩ witsKeyHashes -}
{- { c ∈ txcerts txb ∩ TxCert_mir} ≠ ∅ ⇒ |genSig| ≥ Quorum -}
coreNodeQuorum <- liftSTS $ asks quorum
runTest $
Shelley.validateMIRInsufficientGenesisSigs genDelegs coreNodeQuorum witsKeyHashes tx
-- check metadata hash
{- adh := txADhash txb; ad := auxiliaryData tx -}
{- ((adh = ◇) ∧ (ad= ◇)) ∨ (adh = hashAD ad) -}
runTestOnSignal $
Shelley.validateMetadata pp tx
{- ∀x ∈ range(txdats txw) ∪ range(txwitscripts txw) ∪ (⋃ ( , ,d,s) ∈ txouts tx {s, d}),
x ∈ Script ∪ Datum ⇒ isWellFormed x
-}
runTest $ validateScriptsWellFormed pp tx
-- Note that Datum validation is done during deserialization,
-- as given by the decoders in the Plutus libraray
{- languages tx utxo ⊆ dom(costmdls tx) -}
-- This check is checked when building the TxInfo using collectTwoPhaseScriptInputs, if it fails
-- It raises 'NoCostModel' a construcotr of the predicate failure 'CollectError'. This check
-- which appears in the spec, seems broken since costmdls is a projection of PPrams, not Tx
{- scriptIntegrityHash txb = hashScriptIntegrity pp (languages txw) (txrdmrs txw) -}
runTest $ ppViewHashesMatch tx pp utxo scriptHashesNeeded
trans @(EraRule "UTXO" era) $
TRC (UtxoEnv slot pp stakepools genDelegs, u, tx)
-- ================================
instance
forall era.
( ExtendedUTxO era
, AlonzoEraTx era
, EraUTxO era
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
, BabbageEraTxBody era
, Signable (DSIGN (EraCrypto era)) (Hash (HASH (EraCrypto era)) EraIndependentTxBody)
, Script era ~ AlonzoScript era
, -- Allow UTXOW to call UTXO
Embed (EraRule "UTXO" era) (BabbageUTXOW era)
, Environment (EraRule "UTXO" era) ~ UtxoEnv era
, State (EraRule "UTXO" era) ~ UTxOState era
, Signal (EraRule "UTXO" era) ~ Tx era
, Eq (PredicateFailure (EraRule "UTXOS" era))
, Show (PredicateFailure (EraRule "UTXOS" era))
) =>
STS (BabbageUTXOW era)
where
type State (BabbageUTXOW era) = UTxOState era
type Signal (BabbageUTXOW era) = Tx era
type Environment (BabbageUTXOW era) = UtxoEnv era
type BaseM (BabbageUTXOW era) = ShelleyBase
type PredicateFailure (BabbageUTXOW era) = BabbageUtxowPredFailure era
type Event (BabbageUTXOW era) = AlonzoUtxowEvent era
transitionRules = [babbageUtxowTransition]
initialRules = []
instance
( Era era
, STS (BabbageUTXO era)
, PredicateFailure (EraRule "UTXO" era) ~ BabbageUtxoPredFailure era
, Event (EraRule "UTXO" era) ~ AlonzoUtxoEvent era
, BaseM (BabbageUTXOW era) ~ ShelleyBase
, PredicateFailure (BabbageUTXOW era) ~ BabbageUtxowPredFailure era
, Event (BabbageUTXOW era) ~ AlonzoUtxowEvent era
) =>
Embed (BabbageUTXO era) (BabbageUTXOW era)
where
wrapFailed = UtxoFailure
wrapEvent = WrappedShelleyEraEvent . UtxoEvent
instance Inject (BabbageUtxowPredFailure era) (BabbageUtxowPredFailure era) where
inject = id