Skip to content

Commit ec4c12a

Browse files
committed
Extract for logic updating UTxOState for both valid and invalid cases
1 parent ba5444b commit ec4c12a

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

eras/babbage/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.13.0.0
44

5+
* Add `updateUTxOStateByTxValidity`
56
* Change `babbageEvalScriptsTxInvalid` to return a `Rule` instead of `TransitionRule`
67
* Change `STS` instance of `BabbageUTXOS`: use `UtxosEnv` as `Environment` and `ShelleyGovState` as `State`
78
* Add `Generic` instance for `ApplyTxError`

eras/babbage/impl/src/Cardano/Ledger/Babbage/Rules/Utxo.hs

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Cardano.Ledger.Babbage.Rules.Utxo (
2727
validateCollateralEqBalance,
2828
validateOutputTooSmallUTxO,
2929
disjointRefInputs,
30+
updateUTxOStateByTxValidity,
3031
) where
3132

3233
import Cardano.Ledger.Allegra.Rules (AllegraUtxoPredFailure, shelleyToAllegraUtxoPredFailure)
@@ -461,36 +462,52 @@ utxoTransition ::
461462
utxoTransition = do
462463
TRC (UtxoEnv slot pp certState, utxos, tx) <- judgmentContext
463464
babbageUtxoValidation
464-
let utxo = utxosUtxo utxos
465465
updatedGovState <-
466466
trans @(EraRule "UTXOS" era) $
467-
TRC (UtxosEnv slot pp certState utxo, utxosGovState utxos, tx)
467+
TRC (UtxosEnv slot pp certState (utxosUtxo utxos), utxosGovState utxos, tx)
468+
updateUTxOStateByTxValidity pp certState updatedGovState tx utxos
468469

470+
updateUTxOStateByTxValidity ::
471+
forall era.
472+
( AlonzoEraTx era
473+
, BabbageEraTxBody era
474+
, EraStake era
475+
, EraCertState era
476+
, Event (EraRule "UTXO" era) ~ AlonzoUtxoEvent era
477+
) =>
478+
PParams era ->
479+
CertState era ->
480+
GovState era ->
481+
Tx TopTx era ->
482+
UTxOState era ->
483+
Rule (EraRule "UTXO" era) 'Transition (UTxOState era)
484+
updateUTxOStateByTxValidity pp certState govState tx utxoState =
469485
let txBody = tx ^. bodyTxL
470-
case tx ^. isValidTxL of
471-
IsValid True ->
472-
updateUTxOState
473-
pp
474-
utxos
475-
txBody
476-
certState
477-
updatedGovState
478-
(tellEvent . TotalDeposits (hashAnnotated txBody))
479-
(\a b -> tellEvent $ TxUTxODiff a b)
480-
IsValid False ->
481-
{- utxoKeep = txBody ^. collateralInputsTxBodyL ⋪ utxo -}
482-
{- utxoDel = txBody ^. collateralInputsTxBodyL ◁ utxo -}
483-
let !(utxoKeep, utxoDel) = extractKeys (unUTxO utxo) (txBody ^. collateralInputsTxBodyL)
484-
UTxO collouts = collOuts txBody
485-
DeltaCoin collateralFees = collAdaBalance txBody utxoDel -- NEW to Babbage
486-
in pure $!
487-
utxos {- (collInputs txb ⋪ utxo) ∪ collouts tx -}
488-
{ utxosUtxo = UTxO (Map.union utxoKeep collouts) -- NEW to Babbage
489-
{- fees + collateralFees -}
490-
, utxosFees = utxosFees utxos <> Coin collateralFees -- NEW to Babbage
491-
, utxosInstantStake =
492-
deleteInstantStake (UTxO utxoDel) (addInstantStake (UTxO collouts) (utxos ^. instantStakeL))
493-
}
486+
utxo = utxosUtxo utxoState
487+
in case tx ^. isValidTxL of
488+
IsValid True ->
489+
updateUTxOState
490+
pp
491+
utxoState
492+
txBody
493+
certState
494+
govState
495+
(tellEvent . TotalDeposits (hashAnnotated txBody))
496+
(\a b -> tellEvent $ TxUTxODiff a b)
497+
IsValid False ->
498+
{- utxoKeep = txBody ^. collateralInputsTxBodyL ⋪ utxo -}
499+
{- utxoDel = txBody ^. collateralInputsTxBodyL ◁ utxo -}
500+
let !(utxoKeep, utxoDel) = extractKeys (unUTxO utxo) (txBody ^. collateralInputsTxBodyL)
501+
UTxO collouts = collOuts txBody
502+
DeltaCoin collateralFees = collAdaBalance txBody utxoDel -- NEW to Babbage
503+
in pure $!
504+
utxoState {- (collInputs txb ⋪ utxo) ∪ collouts tx -}
505+
{ utxosUtxo = UTxO (Map.union utxoKeep collouts) -- NEW to Babbage
506+
{- fees + collateralFees -}
507+
, utxosFees = utxosFees utxoState <> Coin collateralFees -- NEW to Babbage
508+
, utxosInstantStake =
509+
deleteInstantStake (UTxO utxoDel) (addInstantStake (UTxO collouts) (utxoState ^. instantStakeL))
510+
}
494511

495512
--------------------------------------------------------------------------------
496513
-- BabbageUTXO STS

0 commit comments

Comments
 (0)