Skip to content

Commit 9b99d5c

Browse files
authored
Conway: add ref scripts size to TxMeasure (#1172)
Making sure that we never forge blocks that violate the new max ref scripts size limit per block that is enforced at the ledger level starting in Conway: IntersectMBO/cardano-ledger#4450 Also see #1168 for restoring a similar limit for the size of the total mempool in Conway.
2 parents ed92af2 + 3be53f2 commit 9b99d5c

File tree

7 files changed

+73
-42
lines changed

7 files changed

+73
-42
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Breaking
2+
3+
- Added `TickedLedgerState` argument to `txMeasure`.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Breaking
2+
3+
- Introduced `ConwayMeasure`, a Conway-specific `TxMeasure` adding the total
4+
reference scripts size as a new dimension on top of `AlonzoMeasure`.

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ import Ouroboros.Consensus.Util.Condense
8181

8282
instance TxLimits ByronBlock where
8383
type TxMeasure ByronBlock = ByteSize
84-
txMeasure = ByteSize . txInBlockSize . txForgetValidated
84+
txMeasure _st = ByteSize . txInBlockSize . txForgetValidated
8585
txsBlockCapacity = ByteSize . txsMaxBytes
8686

8787
{-------------------------------------------------------------------------------

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Ouroboros.Consensus.Shelley.Ledger.Mempool (
3030
, perTxOverhead
3131
-- * Exported for tests
3232
, AlonzoMeasure (..)
33+
, ConwayMeasure (..)
3334
, fromExUnits
3435
) where
3536

@@ -39,6 +40,7 @@ import Cardano.Ledger.Alonzo.Core (Tx, TxSeq, bodyTxL, eraProtVerLow,
3940
import Cardano.Ledger.Alonzo.Scripts (ExUnits, ExUnits',
4041
unWrapExUnits)
4142
import Cardano.Ledger.Alonzo.Tx (totExUnits)
43+
import qualified Cardano.Ledger.Api as L
4244
import Cardano.Ledger.Binary (Annotator (..), DecCBOR (..),
4345
EncCBOR (..), FromCBOR (..), FullByteString (..),
4446
ToCBOR (..), toPlainDecoder)
@@ -295,37 +297,27 @@ theLedgerLens f x =
295297

296298
instance ShelleyCompatible p (ShelleyEra c) => Mempool.TxLimits (ShelleyBlock p (ShelleyEra c)) where
297299
type TxMeasure (ShelleyBlock p (ShelleyEra c)) = Mempool.ByteSize
298-
txMeasure = Mempool.ByteSize . txInBlockSize . txForgetValidated
300+
txMeasure _st = Mempool.ByteSize . txInBlockSize . txForgetValidated
299301
txsBlockCapacity = Mempool.ByteSize . txsMaxBytes
300302

301303
instance ShelleyCompatible p (AllegraEra c) => Mempool.TxLimits (ShelleyBlock p (AllegraEra c)) where
302304
type TxMeasure (ShelleyBlock p (AllegraEra c)) = Mempool.ByteSize
303-
txMeasure = Mempool.ByteSize . txInBlockSize . txForgetValidated
305+
txMeasure _st = Mempool.ByteSize . txInBlockSize . txForgetValidated
304306
txsBlockCapacity = Mempool.ByteSize . txsMaxBytes
305307

306308
instance ShelleyCompatible p (MaryEra c) => Mempool.TxLimits (ShelleyBlock p (MaryEra c)) where
307309
type TxMeasure (ShelleyBlock p (MaryEra c)) = Mempool.ByteSize
308-
txMeasure = Mempool.ByteSize . txInBlockSize . txForgetValidated
310+
txMeasure _st = Mempool.ByteSize . txInBlockSize . txForgetValidated
309311
txsBlockCapacity = Mempool.ByteSize . txsMaxBytes
310312

311313
instance ( ShelleyCompatible p (AlonzoEra c)
312314
) => Mempool.TxLimits (ShelleyBlock p (AlonzoEra c)) where
313315

314316
type TxMeasure (ShelleyBlock p (AlonzoEra c)) = AlonzoMeasure
315317

316-
txMeasure (ShelleyValidatedTx _txid vtx) =
317-
AlonzoMeasure {
318-
byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(AlonzoEra c) @p (SL.extractTx vtx))
319-
, exUnits = fromExUnits $ totExUnits (SL.extractTx vtx)
320-
}
318+
txMeasure _st = txMeasureAlonzo
321319

322-
txsBlockCapacity ledgerState =
323-
AlonzoMeasure {
324-
byteSize = Mempool.ByteSize $ txsMaxBytes ledgerState
325-
, exUnits = fromExUnits $ pparams ^. ppMaxBlockExUnitsL
326-
}
327-
where
328-
pparams = getPParams $ tickedShelleyLedgerState ledgerState
320+
txsBlockCapacity = txsBlockCapacityAlonzo
329321

330322
data AlonzoMeasure = AlonzoMeasure {
331323
byteSize :: !Mempool.ByteSize
@@ -337,43 +329,69 @@ data AlonzoMeasure = AlonzoMeasure {
337329
fromExUnits :: ExUnits -> ExUnits' (WithTop Natural)
338330
fromExUnits = fmap NotTop . unWrapExUnits
339331

332+
txMeasureAlonzo ::
333+
forall proto era.
334+
(ShelleyCompatible proto era, L.AlonzoEraTxWits era)
335+
=> Validated (GenTx (ShelleyBlock proto era)) -> AlonzoMeasure
336+
txMeasureAlonzo (ShelleyValidatedTx _txid vtx) =
337+
AlonzoMeasure {
338+
byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @era @proto tx)
339+
, exUnits = fromExUnits $ totExUnits tx
340+
}
341+
where
342+
tx = SL.extractTx vtx
343+
344+
txsBlockCapacityAlonzo ::
345+
forall proto era.
346+
(ShelleyCompatible proto era, L.AlonzoEraPParams era)
347+
=> TickedLedgerState (ShelleyBlock proto era) -> AlonzoMeasure
348+
txsBlockCapacityAlonzo ledgerState =
349+
AlonzoMeasure {
350+
byteSize = Mempool.ByteSize $ txsMaxBytes ledgerState
351+
, exUnits = fromExUnits $ pparams ^. ppMaxBlockExUnitsL
352+
}
353+
where
354+
pparams = getPParams $ tickedShelleyLedgerState ledgerState
355+
340356
instance ( ShelleyCompatible p (BabbageEra c)
341357
) => Mempool.TxLimits (ShelleyBlock p (BabbageEra c)) where
342358

343359
type TxMeasure (ShelleyBlock p (BabbageEra c)) = AlonzoMeasure
344360

345-
txMeasure (ShelleyValidatedTx _txid vtx) =
346-
AlonzoMeasure {
347-
byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(BabbageEra c) @p (SL.extractTx vtx))
348-
, exUnits = fromExUnits $ totExUnits (SL.extractTx vtx)
349-
}
361+
txMeasure _st = txMeasureAlonzo
350362

351-
txsBlockCapacity ledgerState =
352-
AlonzoMeasure {
353-
byteSize = Mempool.ByteSize $ txsMaxBytes ledgerState
354-
, exUnits = fromExUnits $ pparams ^. ppMaxBlockExUnitsL
355-
}
356-
where
357-
pparams = getPParams $ tickedShelleyLedgerState ledgerState
363+
txsBlockCapacity = txsBlockCapacityAlonzo
364+
365+
data ConwayMeasure = ConwayMeasure {
366+
alonzoMeasure :: !AlonzoMeasure
367+
, refScriptsSize :: !Mempool.ByteSize
368+
} deriving stock (Eq, Generic, Show)
369+
deriving (BoundedMeasure, Measure)
370+
via (InstantiatedAt Generic ConwayMeasure)
358371

359372
instance ( ShelleyCompatible p (ConwayEra c)
360373
) => Mempool.TxLimits (ShelleyBlock p (ConwayEra c)) where
361374

362-
type TxMeasure (ShelleyBlock p (ConwayEra c)) = AlonzoMeasure
375+
type TxMeasure (ShelleyBlock p (ConwayEra c)) = ConwayMeasure
363376

364-
txMeasure (ShelleyValidatedTx _txid vtx) =
365-
AlonzoMeasure {
366-
byteSize = Mempool.ByteSize $ txInBlockSize (mkShelleyTx @(ConwayEra c) @p (SL.extractTx vtx))
367-
, exUnits = fromExUnits $ totExUnits (SL.extractTx vtx)
368-
}
369-
370-
txsBlockCapacity ledgerState =
371-
AlonzoMeasure {
372-
byteSize = Mempool.ByteSize $ txsMaxBytes ledgerState
373-
, exUnits = fromExUnits $ pparams ^. ppMaxBlockExUnitsL
377+
txMeasure st genTx@(ShelleyValidatedTx _txid vtx) =
378+
ConwayMeasure {
379+
alonzoMeasure = txMeasureAlonzo genTx
380+
, refScriptsSize = Mempool.ByteSize $ fromIntegral $
381+
SL.txNonDistinctRefScriptsSize utxo (SL.extractTx vtx)
374382
}
375383
where
376-
pparams = getPParams $ tickedShelleyLedgerState ledgerState
384+
utxo = SL.getUTxO . tickedShelleyLedgerState $ st
385+
386+
387+
txsBlockCapacity st =
388+
ConwayMeasure {
389+
alonzoMeasure = txsBlockCapacityAlonzo st
390+
, refScriptsSize =
391+
-- TODO use maxRefScriptSizePerBlock from
392+
-- https://github.com/IntersectMBO/cardano-ledger/pull/4450
393+
Mempool.ByteSize $ 2560 * 1024 -- 2.5 MB
394+
}
377395

378396
{-------------------------------------------------------------------------------
379397
WithTop
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Breaking
2+
3+
- Added `TickedLedgerState` argument to `txMeasure`.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Block/Forging.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ takeLargestPrefixThatFits ::
165165
-> [Validated (GenTx blk)]
166166
-> [Validated (GenTx blk)]
167167
takeLargestPrefixThatFits overrides ledger txs =
168-
Measure.take MempoolCapacity.txMeasure capacity txs
168+
Measure.take (MempoolCapacity.txMeasure ledger) capacity txs
169169
where
170170
capacity =
171171
MempoolCapacity.applyOverrides

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Capacity.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ class BoundedMeasure (TxMeasure blk) => TxLimits blk where
120120
type TxMeasure blk
121121

122122
-- | What is the measure an individual tx?
123-
txMeasure :: Validated (GenTx blk) -> TxMeasure blk
123+
txMeasure ::
124+
TickedLedgerState blk
125+
-> Validated (GenTx blk)
126+
-> TxMeasure blk
124127

125128
-- | What is the allowed capacity for txs in an individual block?
126129
txsBlockCapacity :: Ticked (LedgerState blk) -> TxMeasure blk

0 commit comments

Comments
 (0)