Skip to content

Commit

Permalink
Merge pull request #4890 from IntersectMBO/lehins/invert-mempool
Browse files Browse the repository at this point in the history
Invert mempool
  • Loading branch information
lehins authored Feb 14, 2025
2 parents 185a428 + c46c716 commit a6f276a
Show file tree
Hide file tree
Showing 40 changed files with 193 additions and 326 deletions.
6 changes: 4 additions & 2 deletions eras/allegra/impl/src/Cardano/Ledger/Allegra.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

Expand All @@ -19,7 +20,7 @@ import Cardano.Ledger.Allegra.Translation ()
import Cardano.Ledger.Allegra.Tx ()
import Cardano.Ledger.Allegra.TxSeq ()
import Cardano.Ledger.Allegra.UTxO ()
import Cardano.Ledger.Shelley.API (ApplyBlock, ApplyTx)
import Cardano.Ledger.Shelley.API

type Allegra = AllegraEra

Expand All @@ -29,6 +30,7 @@ type Allegra = AllegraEra
-- Mempool instances
--------------------------------------------------------------------------------

instance ApplyTx AllegraEra
instance ApplyTx AllegraEra where
applyTxValidation = ruleApplyTxValidation @"LEDGER"

instance ApplyBlock AllegraEra
1 change: 1 addition & 0 deletions eras/alonzo/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.13.0.0

* Remove `reapplyAlonzoTx` as no longer needed.
* Add `TxInfoResult` data family, `mkTxInfoResult` and `lookupTxInfoResult` to `EraPlutusContext`
* Add `lookupTxInfoResultImpossible` helper
* Add `TxInfoResult era` parameter to `toPlutusWithContext` and `mkPlutusWithContext`
Expand Down
1 change: 0 additions & 1 deletion eras/alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ library
deepseq,
mempack,
microlens,
mtl,
nothunks,
plutus-ledger-api >=1.37,
set-algebra >=1.0,
Expand Down
25 changes: 1 addition & 24 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module Cardano.Ledger.Alonzo (
AlonzoTxBody,
AlonzoScript,
AlonzoTxAuxData,
reapplyAlonzoTx,
)
where

Expand All @@ -30,39 +29,17 @@ import Cardano.Ledger.Alonzo.TxAuxData (AlonzoTxAuxData)
import Cardano.Ledger.Alonzo.TxBody (AlonzoTxBody, AlonzoTxOut)
import Cardano.Ledger.Alonzo.TxWits ()
import Cardano.Ledger.Alonzo.UTxO ()
import Cardano.Ledger.Core
import Cardano.Ledger.Mary.Value (MaryValue)
import Cardano.Ledger.Plutus.Data ()
import Cardano.Ledger.Rules.ValidationMode (applySTSNonStatic)
import Cardano.Ledger.Shelley.API
import Control.Arrow (left)
import Control.Monad.Except (MonadError, liftEither)
import Control.Monad.Reader (runReader)
import Control.State.Transition.Extended (TRC (TRC))

type Alonzo = AlonzoEra

{-# DEPRECATED Alonzo "In favor of `AlonzoEra`" #-}

-- =====================================================

reapplyAlonzoTx ::
forall era m.
(ApplyTx era, MonadError (ApplyTxError era) m) =>
Globals ->
MempoolEnv era ->
MempoolState era ->
Validated (Tx era) ->
m (MempoolState era)
reapplyAlonzoTx globals env state vtx =
let res =
flip runReader globals
. applySTSNonStatic
@(EraRule "LEDGER" era)
$ TRC (env, state, extractTx vtx)
in liftEither . left ApplyTxError $ res

instance ApplyTx AlonzoEra where
reapplyTx = reapplyAlonzoTx
applyTxValidation = ruleApplyTxValidation @"LEDGER"

instance ApplyBlock AlonzoEra
2 changes: 1 addition & 1 deletion eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Ledger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ ledgerTransition ::
) =>
TransitionRule (someLEDGER era)
ledgerTransition = do
TRC (LedgerEnv slot mbCurEpochNo txIx pp account _, LedgerState utxoSt certState, tx) <-
TRC (LedgerEnv slot mbCurEpochNo txIx pp account, LedgerState utxoSt certState, tx) <-
judgmentContext
let txBody = tx ^. bodyTxL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ instance
LedgerEnv (SlotNo 0) Nothing minBound
<$> genEraPParams @era geConstants
<*> genAccountState geConstants
<*> pure False

sigGen genenv env state = genTx genenv env state

Expand Down
4 changes: 2 additions & 2 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

Expand All @@ -14,7 +15,6 @@ module Cardano.Ledger.Babbage (
)
where

import Cardano.Ledger.Alonzo (reapplyAlonzoTx)
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..))
import Cardano.Ledger.Alonzo.TxAuxData (AlonzoTxAuxData (..))
import Cardano.Ledger.Babbage.Era (BabbageEra)
Expand All @@ -33,6 +33,6 @@ type Babbage = BabbageEra
-- =====================================================

instance ApplyTx BabbageEra where
reapplyTx = reapplyAlonzoTx
applyTxValidation = ruleApplyTxValidation @"LEDGER"

instance ApplyBlock BabbageEra
3 changes: 3 additions & 0 deletions eras/conway/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.19.0.0

* Remove `ConwayMempoolPredFailure` and `ConwayMempoolEvent`
* Switch to `MEMPOOL` rule to be the entry point for `ApplyTx` instead of `LEDGER` and invert their
ivocation.
* Added `ToCBOR` and `FromCBOR` instances for `DefaultVote`.
* Made the fields of predicate failures and environments lazy
* Add `MemPack` instance for `PlutusScript ConwayEra`
Expand Down
1 change: 1 addition & 0 deletions eras/conway/impl/cardano-ledger-conway.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ library testlib
plutus-ledger-api,
prettyprinter,
small-steps >=1.1,
text,

executable huddle-cddl
main-is: Main.hs
Expand Down
4 changes: 2 additions & 2 deletions eras/conway/impl/src/Cardano/Ledger/Conway.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

Expand All @@ -10,7 +11,6 @@ module Cardano.Ledger.Conway (
)
where

import Cardano.Ledger.Alonzo (reapplyAlonzoTx)
import Cardano.Ledger.Babbage.TxBody ()
import Cardano.Ledger.Conway.Era (ConwayEra)
import Cardano.Ledger.Conway.Governance (RunConwayRatify (..))
Expand All @@ -30,7 +30,7 @@ type Conway = ConwayEra
-- =====================================================

instance ApplyTx ConwayEra where
reapplyTx = reapplyAlonzoTx
applyTxValidation = ruleApplyTxValidation @"MEMPOOL"

instance ApplyBlock ConwayEra

Expand Down
44 changes: 3 additions & 41 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ledger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import Cardano.Ledger.Conway.Era (
ConwayEra,
ConwayGOV,
ConwayLEDGER,
ConwayMEMPOOL,
ConwayUTXOW,
)
import Cardano.Ledger.Conway.Governance (
Expand All @@ -81,7 +80,6 @@ import Cardano.Ledger.Conway.Rules.Gov (
GovSignal (..),
)
import Cardano.Ledger.Conway.Rules.GovCert (ConwayGovCertPredFailure)
import Cardano.Ledger.Conway.Rules.Mempool (ConwayMempoolEvent (..), ConwayMempoolPredFailure (..))
import Cardano.Ledger.Conway.Rules.Utxo (ConwayUtxoPredFailure)
import Cardano.Ledger.Conway.Rules.Utxos (ConwayUtxosPredFailure)
import Cardano.Ledger.Conway.Rules.Utxow (ConwayUtxowPredFailure)
Expand Down Expand Up @@ -114,7 +112,7 @@ import Cardano.Ledger.State (EraUTxO (..))
import Cardano.Ledger.UMap (UView (..))
import qualified Cardano.Ledger.UMap as UMap
import Control.DeepSeq (NFData)
import Control.Monad (unless, void, when)
import Control.Monad (unless)
import Control.State.Transition.Extended (
Embed (..),
STS (..),
Expand Down Expand Up @@ -210,9 +208,6 @@ instance InjectRuleFailure "LEDGER" ConwayGovCertPredFailure ConwayEra where
instance InjectRuleFailure "LEDGER" ConwayGovPredFailure ConwayEra where
injectFailure = ConwayGovFailure

instance InjectRuleFailure "LEDGER" ConwayMempoolPredFailure ConwayEra where
injectFailure (ConwayMempoolPredFailure t) = ConwayMempoolFailure t

deriving instance
( Era era
, Eq (PredicateFailure (EraRule "UTXOW" era))
Expand Down Expand Up @@ -286,22 +281,19 @@ data ConwayLedgerEvent era
= UtxowEvent (Event (EraRule "UTXOW" era))
| CertsEvent (Event (EraRule "CERTS" era))
| GovEvent (Event (EraRule "GOV" era))
| MempoolEvent (Event (EraRule "MEMPOOL" era))
deriving (Generic)

deriving instance
( Eq (Event (EraRule "CERTS" era))
, Eq (Event (EraRule "UTXOW" era))
, Eq (Event (EraRule "GOV" era))
, Eq (Event (EraRule "MEMPOOL" era))
) =>
Eq (ConwayLedgerEvent era)

instance
( NFData (Event (EraRule "CERTS" era))
, NFData (Event (EraRule "UTXOW" era))
, NFData (Event (EraRule "GOV" era))
, NFData (Event (EraRule "MEMPOOL" era))
) =>
NFData (ConwayLedgerEvent era)

Expand All @@ -313,19 +305,15 @@ instance
, Embed (EraRule "UTXOW" era) (ConwayLEDGER era)
, Embed (EraRule "GOV" era) (ConwayLEDGER era)
, Embed (EraRule "CERTS" era) (ConwayLEDGER era)
, Embed (EraRule "MEMPOOL" era) (ConwayLEDGER era)
, State (EraRule "UTXOW" era) ~ UTxOState era
, State (EraRule "CERTS" era) ~ CertState era
, State (EraRule "GOV" era) ~ Proposals era
, State (EraRule "MEMPOOL" era) ~ LedgerState era
, Environment (EraRule "UTXOW" era) ~ UtxoEnv era
, Environment (EraRule "CERTS" era) ~ CertsEnv era
, Environment (EraRule "GOV" era) ~ GovEnv era
, Environment (EraRule "MEMPOOL" era) ~ LedgerEnv era
, Signal (EraRule "UTXOW" era) ~ Tx era
, Signal (EraRule "CERTS" era) ~ Seq (TxCert era)
, Signal (EraRule "GOV" era) ~ GovSignal era
, Signal (EraRule "MEMPOOL" era) ~ Tx era
) =>
STS (ConwayLEDGER era)
where
Expand Down Expand Up @@ -358,36 +346,27 @@ ledgerTransition ::
, Embed (EraRule "UTXOW" era) (someLEDGER era)
, Embed (EraRule "GOV" era) (someLEDGER era)
, Embed (EraRule "CERTS" era) (someLEDGER era)
, Embed (EraRule "MEMPOOL" era) (someLEDGER era)
, State (EraRule "UTXOW" era) ~ UTxOState era
, State (EraRule "CERTS" era) ~ CertState era
, State (EraRule "GOV" era) ~ Proposals era
, State (EraRule "MEMPOOL" era) ~ LedgerState era
, Environment (EraRule "UTXOW" era) ~ UtxoEnv era
, Environment (EraRule "GOV" era) ~ GovEnv era
, Environment (EraRule "CERTS" era) ~ CertsEnv era
, Environment (EraRule "MEMPOOL" era) ~ LedgerEnv era
, Signal (EraRule "UTXOW" era) ~ Tx era
, Signal (EraRule "CERTS" era) ~ Seq (TxCert era)
, Signal (EraRule "GOV" era) ~ GovSignal era
, Signal (EraRule "MEMPOOL" era) ~ Tx era
, BaseM (someLEDGER era) ~ ShelleyBase
, STS (someLEDGER era)
) =>
TransitionRule (someLEDGER era)
ledgerTransition = do
TRC
( le@(LedgerEnv slot mbCurEpochNo _txIx pp account mempool)
, ls@(LedgerState utxoState certState)
( LedgerEnv slot mbCurEpochNo _txIx pp account
, LedgerState utxoState certState
, tx
) <-
judgmentContext

when mempool $
void $
trans @(EraRule "MEMPOOL" era) $
TRC (le, ls, tx)

curEpochNo <- maybe (liftSTS $ epochFromSlot slot) pure mbCurEpochNo

(utxoState', certStateAfterCERTS) <-
Expand Down Expand Up @@ -531,7 +510,6 @@ instance
( Embed (EraRule "UTXOW" era) (ConwayLEDGER era)
, Embed (EraRule "CERTS" era) (ConwayLEDGER era)
, Embed (EraRule "GOV" era) (ConwayLEDGER era)
, Embed (EraRule "MEMPOOL" era) (ConwayLEDGER era)
, ConwayEraGov era
, AlonzoEraTx era
, ConwayEraTxBody era
Expand All @@ -540,15 +518,12 @@ instance
, Environment (EraRule "UTXOW" era) ~ UtxoEnv era
, Environment (EraRule "CERTS" era) ~ CertsEnv era
, Environment (EraRule "GOV" era) ~ GovEnv era
, Environment (EraRule "MEMPOOL" era) ~ LedgerEnv era
, Signal (EraRule "UTXOW" era) ~ Tx era
, Signal (EraRule "CERTS" era) ~ Seq (TxCert era)
, Signal (EraRule "GOV" era) ~ GovSignal era
, Signal (EraRule "MEMPOOL" era) ~ Tx era
, State (EraRule "UTXOW" era) ~ UTxOState era
, State (EraRule "CERTS" era) ~ CertState era
, State (EraRule "GOV" era) ~ Proposals era
, State (EraRule "MEMPOOL" era) ~ LedgerState era
, EraRule "GOV" era ~ ConwayGOV era
, PredicateFailure (EraRule "LEDGER" era) ~ ConwayLedgerPredFailure era
, Event (EraRule "LEDGER" era) ~ ConwayLedgerEvent era
Expand Down Expand Up @@ -585,16 +560,3 @@ instance
where
wrapFailed = ConwayCertsFailure . CertFailure . DelegFailure
wrapEvent = CertsEvent . CertEvent . DelegEvent

instance
( EraTx era
, ConwayEraGov era
, ConwayEraTxBody era
, EraRule "MEMPOOL" era ~ ConwayMEMPOOL era
, PredicateFailure (EraRule "MEMPOOL" era) ~ ConwayMempoolPredFailure era
, Event (EraRule "MEMPOOL" era) ~ ConwayMempoolEvent era
) =>
Embed (ConwayMEMPOOL era) (ConwayLEDGER era)
where
wrapFailed (ConwayMempoolPredFailure t) = ConwayMempoolFailure t
wrapEvent = MempoolEvent
Loading

0 comments on commit a6f276a

Please sign in to comment.