Skip to content

Commit d913163

Browse files
authored
Merge pull request #5491 from IntersectMBO/td/nested-tx-rules
Nested transactions rules
2 parents 44aada7 + f5048ce commit d913163

File tree

21 files changed

+2070
-188
lines changed

21 files changed

+2070
-188
lines changed

eras/conway/impl/CHANGELOG.md

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

33
## 1.21.0.0
44

5+
* Add `conwayLedgerTransitionTRC`
56
* Deprecate
67
- `constitutionScriptL` in favor of new `constitutionGuardrailsScriptHashL`
78
- `InvalidPolicyHash` in favor of new `InvalidGuardrailsScriptHash`

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ledger.hs

Lines changed: 154 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Cardano.Ledger.Conway.Rules.Ledger (
2020
ConwayLedgerEvent (..),
2121
shelleyToConwayLedgerPredFailure,
2222
conwayLedgerTransition,
23+
conwayLedgerTransitionTRC,
2324
) where
2425

2526
import Cardano.Ledger.Address (RewardAccount (..))
@@ -79,7 +80,6 @@ import Cardano.Ledger.Conway.Rules.Certs (
7980
)
8081
import Cardano.Ledger.Conway.Rules.Deleg (ConwayDelegPredFailure)
8182
import Cardano.Ledger.Conway.Rules.Gov (
82-
ConwayGovEvent (..),
8383
ConwayGovPredFailure,
8484
GovEnv (..),
8585
GovSignal (..),
@@ -327,7 +327,6 @@ instance
327327
, Signal (EraRule "CERTS" era) ~ Seq (TxCert era)
328328
, Signal (EraRule "GOV" era) ~ GovSignal era
329329
, ConwayEraCertState era
330-
, EraCertState era
331330
, EraRule "LEDGER" era ~ ConwayLEDGER era
332331
, InjectRuleFailure "LEDGER" ShelleyLedgerPredFailure era
333332
, InjectRuleFailure "LEDGER" ConwayLedgerPredFailure era
@@ -350,7 +349,7 @@ instance
350349

351350
-- =======================================
352351

353-
conwayLedgerTransition ::
352+
conwayLedgerTransitionTRC ::
354353
forall (someLEDGER :: Type -> Type) era.
355354
( AlonzoEraTx era
356355
, ConwayEraTxBody era
@@ -378,133 +377,163 @@ conwayLedgerTransition ::
378377
, InjectRuleFailure "LEDGER" ShelleyLedgerPredFailure era
379378
, InjectRuleFailure "LEDGER" ConwayLedgerPredFailure era
380379
) =>
380+
TRC (someLEDGER era) ->
381381
TransitionRule (someLEDGER era)
382-
conwayLedgerTransition = do
383-
TRC
384-
( LedgerEnv slot mbCurEpochNo _txIx pp chainAccountState
385-
, LedgerState utxoState certState
386-
, tx
387-
) <-
388-
judgmentContext
389-
390-
curEpochNo <- maybe (liftSTS $ epochFromSlot slot) pure mbCurEpochNo
391-
392-
(utxoState', certStateAfterCERTS) <-
393-
if tx ^. isValidTxL == IsValid True
394-
then do
395-
let txBody = tx ^. bodyTxL
396-
actualTreasuryValue = chainAccountState ^. casTreasuryL
397-
case txBody ^. currentTreasuryValueTxBodyL of
398-
SNothing -> pure ()
399-
SJust submittedTreasuryValue ->
400-
submittedTreasuryValue
401-
== actualTreasuryValue
402-
?! (injectFailure . ConwayTreasuryValueMismatch)
403-
( Mismatch
404-
{ mismatchSupplied = submittedTreasuryValue
405-
, mismatchExpected = actualTreasuryValue
406-
}
407-
)
408-
409-
let
410-
totalRefScriptSize = txNonDistinctRefScriptsSize (utxoState ^. utxoL) tx
411-
maxRefScriptSizePerTx = fromIntegral @Word32 @Int $ pp ^. ppMaxRefScriptSizePerTxG
412-
totalRefScriptSize
413-
<= maxRefScriptSizePerTx
414-
?! injectFailure
415-
( ConwayTxRefScriptsSizeTooBig
416-
Mismatch
417-
{ mismatchSupplied = totalRefScriptSize
418-
, mismatchExpected = maxRefScriptSizePerTx
419-
}
420-
)
421-
422-
let govState = utxoState ^. utxosGovStateL
423-
committee = govState ^. committeeGovStateL
424-
proposals = govState ^. proposalsGovStateL
425-
committeeProposals = proposalsWithPurpose grCommitteeL proposals
426-
427-
-- Starting with version 10, we don't allow withdrawals into RewardAcounts that are
428-
-- KeyHashes and not delegated to Dreps.
429-
--
430-
-- We also need to make sure we are using the certState before certificates are applied,
431-
-- because otherwise it would not be possible to unregister a reward account and withdraw
432-
-- all funds from it in the same transaction.
433-
unless (hardforkConwayBootstrapPhase (pp ^. ppProtocolVersionL)) $ do
434-
let accounts = certState ^. certDStateL . accountsL
435-
wdrls = unWithdrawals $ tx ^. bodyTxL . withdrawalsTxBodyL
436-
wdrlsKeyHashes =
437-
[kh | (ra, _) <- Map.toList wdrls, Just kh <- [credKeyHash $ raCredential ra]]
438-
isNotDRepDelegated keyHash = isNothing $ do
439-
accountState <- lookupAccountState (KeyHashObj keyHash) accounts
440-
accountState ^. dRepDelegationAccountStateL
441-
nonExistentDelegations =
442-
filter isNotDRepDelegated wdrlsKeyHashes
443-
failOnNonEmpty nonExistentDelegations (injectFailure . ConwayWdrlNotDelegatedToDRep)
444-
445-
certState' <-
446-
if hardforkConwayMoveWithdrawalsAndDRepChecksToLedgerRule $ pp ^. ppProtocolVersionL
447-
then do
448-
let withdrawals = tx ^. bodyTxL . withdrawalsTxBodyL
449-
testIncompleteAndMissingWithdrawals (certState ^. certDStateL . accountsL) withdrawals
450-
pure $
451-
certState
452-
& updateDormantDRepExpiries tx curEpochNo
453-
& updateVotingDRepExpiries tx curEpochNo (pp ^. ppDRepActivityL)
454-
& certDStateL . accountsL %~ drainAccounts withdrawals
455-
else pure certState
456-
457-
certStateAfterCERTS <-
458-
trans @(EraRule "CERTS" era) $
459-
TRC
460-
( CertsEnv tx pp curEpochNo committee committeeProposals
461-
, certState'
462-
, StrictSeq.fromStrict $ txBody ^. certsTxBodyL
463-
)
464-
465-
-- Votes and proposals from signal tx
466-
let govSignal =
467-
GovSignal
468-
{ gsVotingProcedures = txBody ^. votingProceduresTxBodyL
469-
, gsProposalProcedures = txBody ^. proposalProceduresTxBodyL
470-
, gsCertificates = txBody ^. certsTxBodyL
471-
}
472-
proposalsState <-
473-
trans @(EraRule "GOV" era) $
474-
TRC
475-
( GovEnv
476-
(txIdTxBody txBody)
477-
curEpochNo
478-
pp
479-
(govState ^. constitutionGovStateL . constitutionGuardrailsScriptHashL)
480-
certStateAfterCERTS
481-
(govState ^. committeeGovStateL)
482-
, proposals
483-
, govSignal
484-
)
485-
let utxoState' =
486-
utxoState
487-
& utxosGovStateL . proposalsGovStateL .~ proposalsState
488-
pure (utxoState', certStateAfterCERTS)
489-
else pure (utxoState, certState)
490-
491-
utxoState'' <-
492-
trans @(EraRule "UTXOW" era) $
493-
TRC
494-
-- Pass to UTXOW the unmodified CertState in its Environment,
495-
-- so it can process refunds of deposits for deregistering
496-
-- stake credentials and DReps. The modified CertState
497-
-- (certStateAfterCERTS) has these already removed from its
498-
-- UMap.
499-
( UtxoEnv @era slot pp certState
500-
, utxoState'
382+
conwayLedgerTransitionTRC
383+
( TRC
384+
( LedgerEnv slot mbCurEpochNo _txIx pp chainAccountState
385+
, LedgerState utxoState certState
501386
, tx
502387
)
503-
pure $ LedgerState utxoState'' certStateAfterCERTS
388+
) = do
389+
curEpochNo <- maybe (liftSTS $ epochFromSlot slot) pure mbCurEpochNo
390+
391+
(utxoState', certStateAfterCERTS) <-
392+
if tx ^. isValidTxL == IsValid True
393+
then do
394+
let txBody = tx ^. bodyTxL
395+
actualTreasuryValue = chainAccountState ^. casTreasuryL
396+
case txBody ^. currentTreasuryValueTxBodyL of
397+
SNothing -> pure ()
398+
SJust submittedTreasuryValue ->
399+
submittedTreasuryValue
400+
== actualTreasuryValue
401+
?! (injectFailure . ConwayTreasuryValueMismatch)
402+
( Mismatch
403+
{ mismatchSupplied = submittedTreasuryValue
404+
, mismatchExpected = actualTreasuryValue
405+
}
406+
)
407+
408+
let
409+
totalRefScriptSize = txNonDistinctRefScriptsSize (utxoState ^. utxoL) tx
410+
maxRefScriptSizePerTx = fromIntegral @Word32 @Int $ pp ^. ppMaxRefScriptSizePerTxG
411+
totalRefScriptSize
412+
<= maxRefScriptSizePerTx
413+
?! injectFailure
414+
( ConwayTxRefScriptsSizeTooBig
415+
Mismatch
416+
{ mismatchSupplied = totalRefScriptSize
417+
, mismatchExpected = maxRefScriptSizePerTx
418+
}
419+
)
420+
421+
let govState = utxoState ^. utxosGovStateL
422+
committee = govState ^. committeeGovStateL
423+
proposals = govState ^. proposalsGovStateL
424+
committeeProposals = proposalsWithPurpose grCommitteeL proposals
425+
426+
-- Starting with version 10, we don't allow withdrawals into RewardAcounts that are
427+
-- KeyHashes and not delegated to Dreps.
428+
--
429+
-- We also need to make sure we are using the certState before certificates are applied,
430+
-- because otherwise it would not be possible to unregister a reward account and withdraw
431+
-- all funds from it in the same transaction.
432+
unless (hardforkConwayBootstrapPhase (pp ^. ppProtocolVersionL)) $ do
433+
let accounts = certState ^. certDStateL . accountsL
434+
wdrls = unWithdrawals $ tx ^. bodyTxL . withdrawalsTxBodyL
435+
wdrlsKeyHashes =
436+
[kh | (ra, _) <- Map.toList wdrls, Just kh <- [credKeyHash $ raCredential ra]]
437+
isNotDRepDelegated keyHash = isNothing $ do
438+
accountState <- lookupAccountState (KeyHashObj keyHash) accounts
439+
accountState ^. dRepDelegationAccountStateL
440+
nonExistentDelegations =
441+
filter isNotDRepDelegated wdrlsKeyHashes
442+
failOnNonEmpty nonExistentDelegations (injectFailure . ConwayWdrlNotDelegatedToDRep)
443+
444+
certState' <-
445+
if hardforkConwayMoveWithdrawalsAndDRepChecksToLedgerRule $ pp ^. ppProtocolVersionL
446+
then do
447+
let withdrawals = tx ^. bodyTxL . withdrawalsTxBodyL
448+
testIncompleteAndMissingWithdrawals (certState ^. certDStateL . accountsL) withdrawals
449+
pure $
450+
certState
451+
& updateDormantDRepExpiries tx curEpochNo
452+
& updateVotingDRepExpiries tx curEpochNo (pp ^. ppDRepActivityL)
453+
& certDStateL . accountsL %~ drainAccounts withdrawals
454+
else pure certState
455+
456+
certStateAfterCERTS <-
457+
trans @(EraRule "CERTS" era) $
458+
TRC
459+
( CertsEnv tx pp curEpochNo committee committeeProposals
460+
, certState'
461+
, StrictSeq.fromStrict $ txBody ^. certsTxBodyL
462+
)
463+
464+
-- Votes and proposals from signal tx
465+
let govSignal =
466+
GovSignal
467+
{ gsVotingProcedures = txBody ^. votingProceduresTxBodyL
468+
, gsProposalProcedures = txBody ^. proposalProceduresTxBodyL
469+
, gsCertificates = txBody ^. certsTxBodyL
470+
}
471+
proposalsState <-
472+
trans @(EraRule "GOV" era) $
473+
TRC
474+
( GovEnv
475+
(txIdTxBody txBody)
476+
curEpochNo
477+
pp
478+
(govState ^. constitutionGovStateL . constitutionGuardrailsScriptHashL)
479+
certStateAfterCERTS
480+
(govState ^. committeeGovStateL)
481+
, proposals
482+
, govSignal
483+
)
484+
let utxoState' =
485+
utxoState
486+
& utxosGovStateL . proposalsGovStateL .~ proposalsState
487+
pure (utxoState', certStateAfterCERTS)
488+
else pure (utxoState, certState)
489+
490+
utxoState'' <-
491+
trans @(EraRule "UTXOW" era) $
492+
TRC
493+
-- Pass to UTXOW the unmodified CertState in its Environment,
494+
-- so it can process refunds of deposits for deregistering
495+
-- stake credentials and DReps. The modified CertState
496+
-- (certStateAfterCERTS) has these already removed from its
497+
-- UMap.
498+
( UtxoEnv @era slot pp certState
499+
, utxoState'
500+
, tx
501+
)
502+
pure $ LedgerState utxoState'' certStateAfterCERTS
503+
504+
conwayLedgerTransition ::
505+
forall (someLEDGER :: Type -> Type) era.
506+
( AlonzoEraTx era
507+
, ConwayEraTxBody era
508+
, ConwayEraGov era
509+
, GovState era ~ ConwayGovState era
510+
, Signal (someLEDGER era) ~ Tx TopTx era
511+
, State (someLEDGER era) ~ LedgerState era
512+
, Environment (someLEDGER era) ~ LedgerEnv era
513+
, Embed (EraRule "UTXOW" era) (someLEDGER era)
514+
, Embed (EraRule "GOV" era) (someLEDGER era)
515+
, Embed (EraRule "CERTS" era) (someLEDGER era)
516+
, State (EraRule "UTXOW" era) ~ UTxOState era
517+
, State (EraRule "CERTS" era) ~ CertState era
518+
, State (EraRule "GOV" era) ~ Proposals era
519+
, Environment (EraRule "UTXOW" era) ~ UtxoEnv era
520+
, Environment (EraRule "GOV" era) ~ GovEnv era
521+
, Environment (EraRule "CERTS" era) ~ CertsEnv era
522+
, Signal (EraRule "UTXOW" era) ~ Tx TopTx era
523+
, Signal (EraRule "CERTS" era) ~ Seq (TxCert era)
524+
, Signal (EraRule "GOV" era) ~ GovSignal era
525+
, BaseM (someLEDGER era) ~ ShelleyBase
526+
, STS (someLEDGER era)
527+
, ConwayEraCertState era
528+
, EraRule "LEDGER" era ~ someLEDGER era
529+
, InjectRuleFailure "LEDGER" ShelleyLedgerPredFailure era
530+
, InjectRuleFailure "LEDGER" ConwayLedgerPredFailure era
531+
) =>
532+
TransitionRule (someLEDGER era)
533+
conwayLedgerTransition = judgmentContext >>= conwayLedgerTransitionTRC
504534

505535
instance
506-
( BaseM (ConwayUTXOW era) ~ ShelleyBase
507-
, AlonzoEraTx era
536+
( AlonzoEraTx era
508537
, EraUTxO era
509538
, BabbageEraTxBody era
510539
, Embed (EraRule "UTXO" era) (ConwayUTXOW era)
@@ -534,12 +563,9 @@ instance
534563
, State (EraRule "CERT" era) ~ CertState era
535564
, Environment (EraRule "CERT" era) ~ CertEnv era
536565
, Signal (EraRule "CERT" era) ~ TxCert era
537-
, PredicateFailure (EraRule "CERTS" era) ~ ConwayCertsPredFailure era
538566
, PredicateFailure (EraRule "CERT" era) ~ ConwayCertPredFailure era
539567
, EraRuleFailure "CERT" era ~ ConwayCertPredFailure era
540-
, Event (EraRule "CERTS" era) ~ ConwayCertsEvent era
541568
, EraRule "CERTS" era ~ ConwayCERTS era
542-
, EraCertState era
543569
, ConwayEraCertState era
544570
) =>
545571
Embed (ConwayCERTS era) (ConwayLEDGER era)
@@ -558,16 +584,11 @@ instance
558584
, GovState era ~ ConwayGovState era
559585
, Environment (EraRule "UTXOW" era) ~ UtxoEnv era
560586
, Environment (EraRule "CERTS" era) ~ CertsEnv era
561-
, Environment (EraRule "GOV" era) ~ GovEnv era
562587
, Signal (EraRule "UTXOW" era) ~ Tx TopTx era
563588
, Signal (EraRule "CERTS" era) ~ Seq (TxCert era)
564-
, Signal (EraRule "GOV" era) ~ GovSignal era
565589
, State (EraRule "UTXOW" era) ~ UTxOState era
566590
, State (EraRule "CERTS" era) ~ CertState era
567-
, State (EraRule "GOV" era) ~ Proposals era
568591
, EraRule "GOV" era ~ ConwayGOV era
569-
, Event (EraRule "LEDGER" era) ~ ConwayLedgerEvent era
570-
, EraGov era
571592
, ConwayEraCertState era
572593
, EraRule "LEDGER" era ~ ConwayLEDGER era
573594
, InjectRuleFailure "LEDGER" ShelleyLedgerPredFailure era
@@ -583,12 +604,8 @@ instance
583604
( ConwayEraTxCert era
584605
, ConwayEraPParams era
585606
, ConwayEraGov era
586-
, BaseM (ConwayLEDGER era) ~ ShelleyBase
587-
, PredicateFailure (EraRule "GOV" era) ~ ConwayGovPredFailure era
588-
, Event (EraRule "GOV" era) ~ ConwayGovEvent era
589607
, EraRule "GOV" era ~ ConwayGOV era
590608
, InjectRuleFailure "GOV" ConwayGovPredFailure era
591-
, EraCertState era
592609
, ConwayEraCertState era
593610
) =>
594611
Embed (ConwayGOV era) (ConwayLEDGER era)
@@ -603,7 +620,6 @@ instance
603620
, PredicateFailure (EraRule "CERT" era) ~ ConwayCertPredFailure era
604621
, Event (EraRule "CERTS" era) ~ ConwayCertsEvent era
605622
, Event (EraRule "CERT" era) ~ ConwayCertEvent era
606-
, EraCertState era
607623
, ConwayEraCertState era
608624
) =>
609625
Embed (ConwayDELEG era) (ConwayLEDGER era)

0 commit comments

Comments
 (0)