/
Utxo.hs
456 lines (419 loc) · 15.1 KB
/
Utxo.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Cardano.Ledger.Allegra.Rules.Utxo (
AllegraUTXO,
AllegraUtxoEvent (..),
AllegraUtxoPredFailure (..),
validateOutsideValidityIntervalUTxO,
)
where
import Cardano.Ledger.Address (Addr, RewardAcnt)
import Cardano.Ledger.Allegra.Era (AllegraUTXO)
import Cardano.Ledger.Allegra.Scripts (
ValidityInterval (ValidityInterval),
inInterval,
)
import Cardano.Ledger.Allegra.TxBody (AllegraEraTxBody (..))
import Cardano.Ledger.BaseTypes (
Network,
ProtVer (pvMajor),
ShelleyBase,
StrictMaybe (..),
networkId,
)
import Cardano.Ledger.Binary (
DecCBOR (..),
EncCBOR (..),
decodeRecordSum,
encodeListLen,
invalidKey,
serialize,
)
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto)
import Cardano.Ledger.Rules.ValidationMode (
Inject (..),
InjectMaybe (..),
Test,
runTest,
)
import Cardano.Ledger.SafeHash (SafeHash, hashAnnotated)
import Cardano.Ledger.Shelley.Governance
import Cardano.Ledger.Shelley.LedgerState (PPUPPredFailure, keyTxRefunds, totalTxDeposits)
import qualified Cardano.Ledger.Shelley.LedgerState as Shelley
import Cardano.Ledger.Shelley.PParams (Update)
import Cardano.Ledger.Shelley.Rules (PpupEnv (..), ShelleyPPUP, ShelleyPpupPredFailure)
import qualified Cardano.Ledger.Shelley.Rules as Shelley
import Cardano.Ledger.Shelley.Tx (ShelleyTx (..), TxIn)
import Cardano.Ledger.Shelley.UTxO (txup)
import Cardano.Ledger.UTxO (EraUTxO (..), UTxO (..), txouts)
import qualified Cardano.Ledger.Val as Val
import Cardano.Slotting.Slot (SlotNo)
import Control.Monad.Trans.Reader (asks)
import Control.State.Transition.Extended
import qualified Data.ByteString.Lazy as BSL (length)
import Data.Foldable (toList)
import Data.Int (Int64)
import qualified Data.Map.Strict as Map
import Data.Set (Set)
import Data.Typeable (Typeable)
import Data.Word (Word8)
import GHC.Generics (Generic)
import Lens.Micro
import NoThunks.Class (NoThunks)
import Validation
-- ==========================================================
data AllegraUtxoPredFailure era
= BadInputsUTxO
!(Set (TxIn (EraCrypto era))) -- The bad transaction inputs
| OutsideValidityIntervalUTxO
!ValidityInterval -- transaction's validity interval
!SlotNo -- current slot
| MaxTxSizeUTxO
!Integer -- the actual transaction size
!Integer -- the max transaction size
| InputSetEmptyUTxO
| FeeTooSmallUTxO
!Coin -- the minimum fee for this transaction
!Coin -- the fee supplied in this transaction
| ValueNotConservedUTxO
!(Value era) -- the Coin consumed by this transaction
!(Value era) -- the Coin produced by this transaction
| WrongNetwork
!Network -- the expected network id
!(Set (Addr (EraCrypto era))) -- the set of addresses with incorrect network IDs
| WrongNetworkWithdrawal
!Network -- the expected network id
!(Set (RewardAcnt (EraCrypto era))) -- the set of reward addresses with incorrect network IDs
| OutputTooSmallUTxO
![TxOut era] -- list of supplied transaction outputs that are too small
| UpdateFailure !(PPUPPredFailure era) -- Subtransition Failures
| OutputBootAddrAttrsTooBig
![TxOut era] -- list of supplied bad transaction outputs
| -- Kept for backwards compatibility: no longer used because the `MultiAsset` type of mint doesn't allow for this possibility
TriesToForgeADA
| OutputTooBigUTxO
![TxOut era] -- list of supplied bad transaction outputs
deriving (Generic)
deriving stock instance
( Show (TxOut era)
, Show (Value era)
, Show (PPUPPredFailure era)
) =>
Show (AllegraUtxoPredFailure era)
deriving stock instance
( Eq (TxOut era)
, Eq (Value era)
, Eq (PPUPPredFailure era)
) =>
Eq (AllegraUtxoPredFailure era)
instance
( NoThunks (TxOut era)
, NoThunks (Value era)
, NoThunks (PPUPPredFailure era)
) =>
NoThunks (AllegraUtxoPredFailure era)
data AllegraUtxoEvent era
= UpdateEvent (Event (EraRule "PPUP" era))
| TotalDeposits (SafeHash (EraCrypto era) EraIndependentTxBody) Coin
-- | The UTxO transition rule for the Allegra era.
utxoTransition ::
forall era.
( EraTx era
, EraUTxO era
, AllegraEraTxBody era
, STS (AllegraUTXO era)
, Tx era ~ ShelleyTx era
, Embed (EraRule "PPUP" era) (AllegraUTXO era)
, Environment (EraRule "PPUP" era) ~ PpupEnv era
, State (EraRule "PPUP" era) ~ ShelleyPPUPState era
, Signal (EraRule "PPUP" era) ~ Maybe (Update era)
, ProtVerAtMost era 8
, GovernanceState era ~ ShelleyPPUPState era
) =>
TransitionRule (AllegraUTXO era)
utxoTransition = do
TRC (Shelley.UtxoEnv slot pp dpstate genDelegs, u, tx) <- judgmentContext
let Shelley.UTxOState utxo _ _ ppup _ = u
let txb = tx ^. bodyTxL
{- ininterval slot (txvld tx) -}
runTest $ validateOutsideValidityIntervalUTxO slot txb
{- txins txb ≠ ∅ -}
-- runValidationTransMaybe fromShelleyFailure $ Shelley.validateInputSetEmptyUTxO txb
runTest $ Shelley.validateInputSetEmptyUTxO txb
{- minfee pp tx ≤ txfee txb -}
runTest $ Shelley.validateFeeTooSmallUTxO pp tx
{- txins txb ⊆ dom utxo -}
runTest $ Shelley.validateBadInputsUTxO utxo $ txb ^. inputsTxBodyL
netId <- liftSTS $ asks networkId
{- ∀(_ → (a, _)) ∈ txouts txb, netId a = NetworkId -}
runTest $ Shelley.validateWrongNetwork netId . toList $ txb ^. outputsTxBodyL
{- ∀(a → ) ∈ txwdrls txb, netId a = NetworkId -}
runTest $ Shelley.validateWrongNetworkWithdrawal netId txb
{- consumed pp utxo txb = produced pp poolParams txb -}
runTest $ Shelley.validateValueNotConservedUTxO pp utxo dpstate txb
-- process Protocol Parameter Update Proposals
ppup' <-
trans @(EraRule "PPUP" era) $ TRC (PPUPEnv slot pp genDelegs, ppup, txup tx)
{- adaPolicy ∉ supp mint tx
above check not needed because mint field of type MultiAsset cannot contain ada -}
let outputs = txouts txb
{- ∀ txout ∈ txouts txb, getValue txout ≥ inject (scaledMinDeposit v (minUTxOValue pp)) -}
runTest $ validateOutputTooSmallUTxO pp outputs
{- ∀ txout ∈ txouts txb, serSize (getValue txout) ≤ MaxValSize -}
-- MaxValSize = 4000
runTest $ validateOutputTooBigUTxO pp outputs
{- ∀ ( _ ↦ (a,_)) ∈ txoutstxb, a ∈ Addrbootstrap → bootstrapAttrsSize a ≤ 64 -}
runTest $ Shelley.validateOutputBootAddrAttrsTooBig (Map.elems (unUTxO outputs))
{- txsize tx ≤ maxTxSize pp -}
runTest $ Shelley.validateMaxTxSizeUTxO pp tx
let refunded = keyTxRefunds pp dpstate txb
let depositChange = totalTxDeposits pp dpstate txb Val.<-> refunded
tellEvent $ TotalDeposits (hashAnnotated txb) depositChange
pure $! Shelley.updateUTxOState pp u txb depositChange ppup'
-- | Ensure the transaction is within the validity window.
--
-- > ininterval slot (txvld tx)
validateOutsideValidityIntervalUTxO ::
AllegraEraTxBody era =>
SlotNo ->
TxBody era ->
Test (AllegraUtxoPredFailure era)
validateOutsideValidityIntervalUTxO slot txb =
failureUnless (inInterval slot (txb ^. vldtTxBodyL)) $
OutsideValidityIntervalUTxO (txb ^. vldtTxBodyL) slot
-- | Ensure that there are no `TxOut`s that have `Value` of size larger than @MaxValSize@
--
-- > ∀ txout ∈ txouts txb, serSize (getValue txout) ≤ MaxValSize
validateOutputTooBigUTxO ::
EraTxOut era =>
PParams era ->
UTxO era ->
Test (AllegraUtxoPredFailure era)
validateOutputTooBigUTxO pp (UTxO outputs) =
failureUnless (null outputsTooBig) $ OutputTooBigUTxO outputsTooBig
where
version = pvMajor (pp ^. ppProtocolVersionL)
maxValSize = 4000 :: Int64
outputsTooBig =
filter
( \out ->
let v = out ^. valueTxOutL
in BSL.length (serialize version v) > maxValSize
)
(Map.elems outputs)
-- | Ensure that there are no `TxOut`s that have value less than the scaled @minUTxOValue@
--
-- > ∀ txout ∈ txouts txb, getValue txout ≥ inject (scaledMinDeposit v (minUTxOValue pp))
validateOutputTooSmallUTxO ::
EraTxOut era =>
PParams era ->
UTxO era ->
Test (AllegraUtxoPredFailure era)
validateOutputTooSmallUTxO pp (UTxO outputs) =
failureUnless (null outputsTooSmall) $ OutputTooSmallUTxO outputsTooSmall
where
outputsTooSmall =
filter
( \txOut ->
let v = txOut ^. valueTxOutL
in Val.pointwise (<) v (Val.inject $ getMinCoinTxOut pp txOut)
)
(Map.elems outputs)
--------------------------------------------------------------------------------
-- UTXO STS
--------------------------------------------------------------------------------
instance
forall era.
( EraTx era
, EraUTxO era
, AllegraEraTxBody era
, Tx era ~ ShelleyTx era
, Embed (EraRule "PPUP" era) (AllegraUTXO era)
, Environment (EraRule "PPUP" era) ~ PpupEnv era
, State (EraRule "PPUP" era) ~ ShelleyPPUPState era
, Signal (EraRule "PPUP" era) ~ Maybe (Update era)
, ProtVerAtMost era 8
, Eq (PPUPPredFailure era)
, Show (PPUPPredFailure era)
, GovernanceState era ~ ShelleyPPUPState era
) =>
STS (AllegraUTXO era)
where
type State (AllegraUTXO era) = Shelley.UTxOState era
type Signal (AllegraUTXO era) = ShelleyTx era
type Environment (AllegraUTXO era) = Shelley.UtxoEnv era
type BaseM (AllegraUTXO era) = ShelleyBase
type PredicateFailure (AllegraUTXO era) = AllegraUtxoPredFailure era
type Event (AllegraUTXO era) = AllegraUtxoEvent era
initialRules = []
transitionRules = [utxoTransition]
instance
( Era era
, STS (ShelleyPPUP era)
, PPUPPredFailure era ~ ShelleyPpupPredFailure era
, Event (EraRule "PPUP" era) ~ Event (ShelleyPPUP era)
) =>
Embed (ShelleyPPUP era) (AllegraUTXO era)
where
wrapFailed = UpdateFailure
wrapEvent = UpdateEvent
--------------------------------------------------------------------------------
-- Serialisation
--------------------------------------------------------------------------------
instance
( Typeable era
, Crypto (EraCrypto era)
, EncCBOR (Value era)
, EncCBOR (TxOut era)
, EncCBOR (PPUPPredFailure era)
) =>
EncCBOR (AllegraUtxoPredFailure era)
where
encCBOR = \case
BadInputsUTxO ins ->
encodeListLen 2 <> encCBOR (0 :: Word8) <> encCBOR ins
(OutsideValidityIntervalUTxO a b) ->
encodeListLen 3
<> encCBOR (1 :: Word8)
<> encCBOR a
<> encCBOR b
(MaxTxSizeUTxO a b) ->
encodeListLen 3
<> encCBOR (2 :: Word8)
<> encCBOR a
<> encCBOR b
InputSetEmptyUTxO -> encodeListLen 1 <> encCBOR (3 :: Word8)
(FeeTooSmallUTxO a b) ->
encodeListLen 3
<> encCBOR (4 :: Word8)
<> encCBOR a
<> encCBOR b
(ValueNotConservedUTxO a b) ->
encodeListLen 3
<> encCBOR (5 :: Word8)
<> encCBOR a
<> encCBOR b
OutputTooSmallUTxO outs ->
encodeListLen 2
<> encCBOR (6 :: Word8)
<> encCBOR outs
(UpdateFailure a) ->
encodeListLen 2
<> encCBOR (7 :: Word8)
<> encCBOR a
(WrongNetwork right wrongs) ->
encodeListLen 3
<> encCBOR (8 :: Word8)
<> encCBOR right
<> encCBOR wrongs
(WrongNetworkWithdrawal right wrongs) ->
encodeListLen 3
<> encCBOR (9 :: Word8)
<> encCBOR right
<> encCBOR wrongs
OutputBootAddrAttrsTooBig outs ->
encodeListLen 2
<> encCBOR (10 :: Word8)
<> encCBOR outs
TriesToForgeADA -> encodeListLen 1 <> encCBOR (11 :: Word8)
OutputTooBigUTxO outs ->
encodeListLen 2
<> encCBOR (12 :: Word8)
<> encCBOR outs
instance
( EraTxOut era
, DecCBOR (PPUPPredFailure era)
) =>
DecCBOR (AllegraUtxoPredFailure era)
where
decCBOR =
decodeRecordSum "PredicateFailureUTXO" $
\case
0 -> do
ins <- decCBOR
pure (2, BadInputsUTxO ins) -- The (2,..) indicates the number of things decoded, INCLUDING the tags, which are decoded by decodeRecordSumNamed
1 -> do
a <- decCBOR
b <- decCBOR
pure (3, OutsideValidityIntervalUTxO a b)
2 -> do
a <- decCBOR
b <- decCBOR
pure (3, MaxTxSizeUTxO a b)
3 -> pure (1, InputSetEmptyUTxO)
4 -> do
a <- decCBOR
b <- decCBOR
pure (3, FeeTooSmallUTxO a b)
5 -> do
a <- decCBOR
b <- decCBOR
pure (3, ValueNotConservedUTxO a b)
6 -> do
outs <- decCBOR
pure (2, OutputTooSmallUTxO outs)
7 -> do
a <- decCBOR
pure (2, UpdateFailure a)
8 -> do
right <- decCBOR
wrongs <- decCBOR
pure (3, WrongNetwork right wrongs)
9 -> do
right <- decCBOR
wrongs <- decCBOR
pure (3, WrongNetworkWithdrawal right wrongs)
10 -> do
outs <- decCBOR
pure (2, OutputBootAddrAttrsTooBig outs)
11 -> pure (1, TriesToForgeADA)
12 -> do
outs <- decCBOR
pure (2, OutputTooBigUTxO outs)
k -> invalidKey k
-- ===============================================
-- Inject instances
fromShelleyFailure :: Shelley.ShelleyUtxoPredFailure era -> Maybe (AllegraUtxoPredFailure era)
fromShelleyFailure = \case
Shelley.BadInputsUTxO ins -> Just $ BadInputsUTxO ins
Shelley.ExpiredUTxO {} -> Nothing -- Rule was replaced with `OutsideValidityIntervalUTxO`
Shelley.MaxTxSizeUTxO a m -> Just $ MaxTxSizeUTxO a m
Shelley.InputSetEmptyUTxO -> Just InputSetEmptyUTxO
Shelley.FeeTooSmallUTxO mf af -> Just $ FeeTooSmallUTxO mf af
Shelley.ValueNotConservedUTxO c p -> Just $ ValueNotConservedUTxO c p
Shelley.WrongNetwork n as -> Just $ WrongNetwork n as
Shelley.WrongNetworkWithdrawal n as -> Just $ WrongNetworkWithdrawal n as
Shelley.OutputTooSmallUTxO {} -> Nothing -- Rule was updated
Shelley.UpdateFailure ppf -> Just $ UpdateFailure ppf
Shelley.OutputBootAddrAttrsTooBig outs -> Just $ OutputBootAddrAttrsTooBig outs
instance InjectMaybe (Shelley.ShelleyUtxoPredFailure era) (AllegraUtxoPredFailure era) where
injectMaybe = fromShelleyFailure
instance Inject (AllegraUtxoPredFailure era) (AllegraUtxoPredFailure era) where
inject = id
instance Inject (Shelley.ShelleyUtxoPredFailure era) (AllegraUtxoPredFailure era) where
inject (Shelley.BadInputsUTxO ins) = BadInputsUTxO ins
inject (Shelley.ExpiredUTxO ttl current) =
OutsideValidityIntervalUTxO (ValidityInterval SNothing (SJust ttl)) current
inject (Shelley.MaxTxSizeUTxO a m) = MaxTxSizeUTxO a m
inject Shelley.InputSetEmptyUTxO = InputSetEmptyUTxO
inject (Shelley.FeeTooSmallUTxO mf af) = FeeTooSmallUTxO mf af
inject (Shelley.ValueNotConservedUTxO vc vp) = ValueNotConservedUTxO vc vp
inject (Shelley.WrongNetwork n as) = WrongNetwork n as
inject (Shelley.WrongNetworkWithdrawal n as) = WrongNetworkWithdrawal n as
inject (Shelley.OutputTooSmallUTxO x) = OutputTooSmallUTxO x
inject (Shelley.UpdateFailure x) = UpdateFailure x
inject (Shelley.OutputBootAddrAttrsTooBig outs) = OutputTooBigUTxO outs