From fddb735bff7d7785d78f459a2a040b5ff52240db Mon Sep 17 00:00:00 2001 From: Alexander Esgen Date: Mon, 1 Jul 2024 13:27:18 +0200 Subject: [PATCH 1/3] `txMeasure`: add `TickedLedgerState` argument --- ...240701_131358_alexander.esgen_conway_txmeasure.md | 3 +++ .../Ouroboros/Consensus/Byron/Ledger/Mempool.hs | 2 +- .../Ouroboros/Consensus/Shelley/Ledger/Mempool.hs | 12 ++++++------ ...240701_133021_alexander.esgen_conway_txmeasure.md | 3 +++ .../Ouroboros/Consensus/Block/Forging.hs | 2 +- .../Ouroboros/Consensus/Mempool/Capacity.hs | 5 ++++- 6 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 ouroboros-consensus-cardano/changelog.d/20240701_131358_alexander.esgen_conway_txmeasure.md create mode 100644 ouroboros-consensus/changelog.d/20240701_133021_alexander.esgen_conway_txmeasure.md diff --git a/ouroboros-consensus-cardano/changelog.d/20240701_131358_alexander.esgen_conway_txmeasure.md b/ouroboros-consensus-cardano/changelog.d/20240701_131358_alexander.esgen_conway_txmeasure.md new file mode 100644 index 0000000000..783b821bc4 --- /dev/null +++ b/ouroboros-consensus-cardano/changelog.d/20240701_131358_alexander.esgen_conway_txmeasure.md @@ -0,0 +1,3 @@ +### Breaking + +- Added `TickedLedgerState` argument to `txMeasure`. diff --git a/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs b/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs index 96e7760873..4c3c0fdc75 100644 --- a/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs +++ b/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs @@ -81,7 +81,7 @@ import Ouroboros.Consensus.Util.Condense instance TxLimits ByronBlock where type TxMeasure ByronBlock = ByteSize - txMeasure = ByteSize . txInBlockSize . txForgetValidated + txMeasure _st = ByteSize . txInBlockSize . txForgetValidated txsBlockCapacity = ByteSize . txsMaxBytes {------------------------------------------------------------------------------- diff --git a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs index d11ea54a29..0aca378e37 100644 --- a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs +++ b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs @@ -295,17 +295,17 @@ theLedgerLens f x = instance ShelleyCompatible p (ShelleyEra c) => Mempool.TxLimits (ShelleyBlock p (ShelleyEra c)) where type TxMeasure (ShelleyBlock p (ShelleyEra c)) = Mempool.ByteSize - txMeasure = Mempool.ByteSize . txInBlockSize . txForgetValidated + txMeasure _st = Mempool.ByteSize . txInBlockSize . txForgetValidated txsBlockCapacity = Mempool.ByteSize . txsMaxBytes instance ShelleyCompatible p (AllegraEra c) => Mempool.TxLimits (ShelleyBlock p (AllegraEra c)) where type TxMeasure (ShelleyBlock p (AllegraEra c)) = Mempool.ByteSize - txMeasure = Mempool.ByteSize . txInBlockSize . txForgetValidated + txMeasure _st = Mempool.ByteSize . txInBlockSize . txForgetValidated txsBlockCapacity = Mempool.ByteSize . txsMaxBytes instance ShelleyCompatible p (MaryEra c) => Mempool.TxLimits (ShelleyBlock p (MaryEra c)) where type TxMeasure (ShelleyBlock p (MaryEra c)) = Mempool.ByteSize - txMeasure = Mempool.ByteSize . txInBlockSize . txForgetValidated + txMeasure _st = Mempool.ByteSize . txInBlockSize . txForgetValidated txsBlockCapacity = Mempool.ByteSize . txsMaxBytes instance ( ShelleyCompatible p (AlonzoEra c) @@ -313,7 +313,7 @@ instance ( ShelleyCompatible p (AlonzoEra c) type TxMeasure (ShelleyBlock p (AlonzoEra c)) = AlonzoMeasure - txMeasure (ShelleyValidatedTx _txid vtx) = + txMeasure _st (ShelleyValidatedTx _txid vtx) = AlonzoMeasure { byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(AlonzoEra c) @p (SL.extractTx vtx)) , exUnits = fromExUnits $ totExUnits (SL.extractTx vtx) @@ -342,7 +342,7 @@ instance ( ShelleyCompatible p (BabbageEra c) type TxMeasure (ShelleyBlock p (BabbageEra c)) = AlonzoMeasure - txMeasure (ShelleyValidatedTx _txid vtx) = + txMeasure _st (ShelleyValidatedTx _txid vtx) = AlonzoMeasure { byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(BabbageEra c) @p (SL.extractTx vtx)) , exUnits = fromExUnits $ totExUnits (SL.extractTx vtx) @@ -361,7 +361,7 @@ instance ( ShelleyCompatible p (ConwayEra c) type TxMeasure (ShelleyBlock p (ConwayEra c)) = AlonzoMeasure - txMeasure (ShelleyValidatedTx _txid vtx) = + txMeasure _st (ShelleyValidatedTx _txid vtx) = AlonzoMeasure { byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(ConwayEra c) @p (SL.extractTx vtx)) , exUnits = fromExUnits $ totExUnits (SL.extractTx vtx) diff --git a/ouroboros-consensus/changelog.d/20240701_133021_alexander.esgen_conway_txmeasure.md b/ouroboros-consensus/changelog.d/20240701_133021_alexander.esgen_conway_txmeasure.md new file mode 100644 index 0000000000..783b821bc4 --- /dev/null +++ b/ouroboros-consensus/changelog.d/20240701_133021_alexander.esgen_conway_txmeasure.md @@ -0,0 +1,3 @@ +### Breaking + +- Added `TickedLedgerState` argument to `txMeasure`. diff --git a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Block/Forging.hs b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Block/Forging.hs index 21273ca1eb..d6400ee676 100644 --- a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Block/Forging.hs +++ b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Block/Forging.hs @@ -165,7 +165,7 @@ takeLargestPrefixThatFits :: -> [Validated (GenTx blk)] -> [Validated (GenTx blk)] takeLargestPrefixThatFits overrides ledger txs = - Measure.take MempoolCapacity.txMeasure capacity txs + Measure.take (MempoolCapacity.txMeasure ledger) capacity txs where capacity = MempoolCapacity.applyOverrides diff --git a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Capacity.hs b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Capacity.hs index cbc1a75f95..6bf11d433c 100644 --- a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Capacity.hs +++ b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Capacity.hs @@ -120,7 +120,10 @@ class BoundedMeasure (TxMeasure blk) => TxLimits blk where type TxMeasure blk -- | What is the measure an individual tx? - txMeasure :: Validated (GenTx blk) -> TxMeasure blk + txMeasure :: + TickedLedgerState blk + -> Validated (GenTx blk) + -> TxMeasure blk -- | What is the allowed capacity for txs in an individual block? txsBlockCapacity :: Ticked (LedgerState blk) -> TxMeasure blk From b07cf3fe2b44831bb33523222f2daa5542cbe07a Mon Sep 17 00:00:00 2001 From: Alexander Esgen Date: Mon, 1 Jul 2024 13:27:23 +0200 Subject: [PATCH 2/3] Factor out `AlonzoMeasure` functionality --- .../Consensus/Shelley/Ledger/Mempool.hs | 67 +++++++++---------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs index 0aca378e37..5a112f4902 100644 --- a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs +++ b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs @@ -39,6 +39,7 @@ import Cardano.Ledger.Alonzo.Core (Tx, TxSeq, bodyTxL, eraProtVerLow, import Cardano.Ledger.Alonzo.Scripts (ExUnits, ExUnits', unWrapExUnits) import Cardano.Ledger.Alonzo.Tx (totExUnits) +import qualified Cardano.Ledger.Api as L import Cardano.Ledger.Binary (Annotator (..), DecCBOR (..), EncCBOR (..), FromCBOR (..), FullByteString (..), ToCBOR (..), toPlainDecoder) @@ -313,19 +314,9 @@ instance ( ShelleyCompatible p (AlonzoEra c) type TxMeasure (ShelleyBlock p (AlonzoEra c)) = AlonzoMeasure - txMeasure _st (ShelleyValidatedTx _txid vtx) = - AlonzoMeasure { - byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(AlonzoEra c) @p (SL.extractTx vtx)) - , exUnits = fromExUnits $ totExUnits (SL.extractTx vtx) - } + txMeasure _st = txMeasureAlonzo - txsBlockCapacity ledgerState = - AlonzoMeasure { - byteSize = Mempool.ByteSize $ txsMaxBytes ledgerState - , exUnits = fromExUnits $ pparams ^. ppMaxBlockExUnitsL - } - where - pparams = getPParams $ tickedShelleyLedgerState ledgerState + txsBlockCapacity = txsBlockCapacityAlonzo data AlonzoMeasure = AlonzoMeasure { byteSize :: !Mempool.ByteSize @@ -337,43 +328,47 @@ data AlonzoMeasure = AlonzoMeasure { fromExUnits :: ExUnits -> ExUnits' (WithTop Natural) fromExUnits = fmap NotTop . unWrapExUnits +txMeasureAlonzo :: + forall proto era. + (ShelleyCompatible proto era, L.AlonzoEraTxWits era) + => Validated (GenTx (ShelleyBlock proto era)) -> AlonzoMeasure +txMeasureAlonzo (ShelleyValidatedTx _txid vtx) = + AlonzoMeasure { + byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @era @proto tx) + , exUnits = fromExUnits $ totExUnits tx + } + where + tx = SL.extractTx vtx + +txsBlockCapacityAlonzo :: + forall proto era. + (ShelleyCompatible proto era, L.AlonzoEraPParams era) + => TickedLedgerState (ShelleyBlock proto era) -> AlonzoMeasure +txsBlockCapacityAlonzo ledgerState = + AlonzoMeasure { + byteSize = Mempool.ByteSize $ txsMaxBytes ledgerState + , exUnits = fromExUnits $ pparams ^. ppMaxBlockExUnitsL + } + where + pparams = getPParams $ tickedShelleyLedgerState ledgerState + instance ( ShelleyCompatible p (BabbageEra c) ) => Mempool.TxLimits (ShelleyBlock p (BabbageEra c)) where type TxMeasure (ShelleyBlock p (BabbageEra c)) = AlonzoMeasure - txMeasure _st (ShelleyValidatedTx _txid vtx) = - AlonzoMeasure { - byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(BabbageEra c) @p (SL.extractTx vtx)) - , exUnits = fromExUnits $ totExUnits (SL.extractTx vtx) - } + txMeasure _st = txMeasureAlonzo - txsBlockCapacity ledgerState = - AlonzoMeasure { - byteSize = Mempool.ByteSize $ txsMaxBytes ledgerState - , exUnits = fromExUnits $ pparams ^. ppMaxBlockExUnitsL - } - where - pparams = getPParams $ tickedShelleyLedgerState ledgerState + txsBlockCapacity = txsBlockCapacityAlonzo instance ( ShelleyCompatible p (ConwayEra c) ) => Mempool.TxLimits (ShelleyBlock p (ConwayEra c)) where type TxMeasure (ShelleyBlock p (ConwayEra c)) = AlonzoMeasure - txMeasure _st (ShelleyValidatedTx _txid vtx) = - AlonzoMeasure { - byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(ConwayEra c) @p (SL.extractTx vtx)) - , exUnits = fromExUnits $ totExUnits (SL.extractTx vtx) - } + txMeasure _st = txMeasureAlonzo - txsBlockCapacity ledgerState = - AlonzoMeasure { - byteSize = Mempool.ByteSize $ txsMaxBytes ledgerState - , exUnits = fromExUnits $ pparams ^. ppMaxBlockExUnitsL - } - where - pparams = getPParams $ tickedShelleyLedgerState ledgerState + txsBlockCapacity = txsBlockCapacityAlonzo {------------------------------------------------------------------------------- WithTop From 3be53f23668bedbaf1a52f0113e499b7793b2cd8 Mon Sep 17 00:00:00 2001 From: Alexander Esgen Date: Mon, 1 Jul 2024 13:27:29 +0200 Subject: [PATCH 3/3] Conway: add ref scripts size to `TxMeasure` --- ...132131_alexander.esgen_conway_txmeasure.md | 4 +++ .../Consensus/Shelley/Ledger/Mempool.hs | 29 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 ouroboros-consensus-cardano/changelog.d/20240701_132131_alexander.esgen_conway_txmeasure.md diff --git a/ouroboros-consensus-cardano/changelog.d/20240701_132131_alexander.esgen_conway_txmeasure.md b/ouroboros-consensus-cardano/changelog.d/20240701_132131_alexander.esgen_conway_txmeasure.md new file mode 100644 index 0000000000..32e9b38b01 --- /dev/null +++ b/ouroboros-consensus-cardano/changelog.d/20240701_132131_alexander.esgen_conway_txmeasure.md @@ -0,0 +1,4 @@ +### Breaking + +- Introduced `ConwayMeasure`, a Conway-specific `TxMeasure` adding the total + reference scripts size as a new dimension on top of `AlonzoMeasure`. diff --git a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs index 5a112f4902..b76e149157 100644 --- a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs +++ b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs @@ -30,6 +30,7 @@ module Ouroboros.Consensus.Shelley.Ledger.Mempool ( , perTxOverhead -- * Exported for tests , AlonzoMeasure (..) + , ConwayMeasure (..) , fromExUnits ) where @@ -361,14 +362,36 @@ instance ( ShelleyCompatible p (BabbageEra c) txsBlockCapacity = txsBlockCapacityAlonzo +data ConwayMeasure = ConwayMeasure { + alonzoMeasure :: !AlonzoMeasure + , refScriptsSize :: !Mempool.ByteSize + } deriving stock (Eq, Generic, Show) + deriving (BoundedMeasure, Measure) + via (InstantiatedAt Generic ConwayMeasure) + instance ( ShelleyCompatible p (ConwayEra c) ) => Mempool.TxLimits (ShelleyBlock p (ConwayEra c)) where - type TxMeasure (ShelleyBlock p (ConwayEra c)) = AlonzoMeasure + type TxMeasure (ShelleyBlock p (ConwayEra c)) = ConwayMeasure - txMeasure _st = txMeasureAlonzo + txMeasure st genTx@(ShelleyValidatedTx _txid vtx) = + ConwayMeasure { + alonzoMeasure = txMeasureAlonzo genTx + , refScriptsSize = Mempool.ByteSize $ fromIntegral $ + SL.txNonDistinctRefScriptsSize utxo (SL.extractTx vtx) + } + where + utxo = SL.getUTxO . tickedShelleyLedgerState $ st - txsBlockCapacity = txsBlockCapacityAlonzo + + txsBlockCapacity st = + ConwayMeasure { + alonzoMeasure = txsBlockCapacityAlonzo st + , refScriptsSize = + -- TODO use maxRefScriptSizePerBlock from + -- https://github.com/IntersectMBO/cardano-ledger/pull/4450 + Mempool.ByteSize $ 2560 * 1024 -- 2.5 MB + } {------------------------------------------------------------------------------- WithTop