From e86598b00a321f1c0aa98a7ef17f0a43dfc9375b Mon Sep 17 00:00:00 2001 From: teodanciu Date: Thu, 27 Jun 2024 22:10:08 +0100 Subject: [PATCH] Fail Conway Bbody rule for too big total reference script size --- .../src/Cardano/Ledger/Conway/Rules/Bbody.hs | 49 +++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs b/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs index bcb5acd681b..715b2dcce95 100644 --- a/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs +++ b/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs @@ -35,7 +35,7 @@ import Cardano.Ledger.Babbage.Rules (BabbageUtxoPredFailure, BabbageUtxowPredFai import Cardano.Ledger.Alonzo.PParams (AlonzoEraPParams) import qualified Cardano.Ledger.Alonzo.Rules as Alonzo (AlonzoBbodyPredFailure (..)) import Cardano.Ledger.Alonzo.Tx (AlonzoTx) -import Cardano.Ledger.Alonzo.TxSeq (AlonzoTxSeq) +import Cardano.Ledger.Alonzo.TxSeq (AlonzoTxSeq, txSeqTxns) import Cardano.Ledger.Alonzo.TxWits (AlonzoEraTxWits (..)) import Cardano.Ledger.BHeaderView (BHeaderView (..)) import Cardano.Ledger.BaseTypes (ShelleyBase) @@ -54,7 +54,7 @@ import Cardano.Ledger.Conway.Rules.Utxow (ConwayUtxowPredFailure) import Cardano.Ledger.Core import qualified Cardano.Ledger.Era as Era import Cardano.Ledger.Keys (DSignable, Hash) -import Cardano.Ledger.Shelley.LedgerState (LedgerState) +import Cardano.Ledger.Shelley.LedgerState (LedgerState (..), utxosUtxo) import Cardano.Ledger.Shelley.Rules ( BbodyEnv (..), ShelleyBbodyEvent (..), @@ -70,12 +70,16 @@ import qualified Cardano.Ledger.Shelley.Rules as Shelley (ShelleyBbodyPredFailur import Control.State.Transition ( Embed (..), STS (..), + TRC (..), TransitionRule, + judgmentContext, + (?!), ) import Data.Sequence (Seq) import GHC.Generics (Generic) import NoThunks.Class (NoThunks (..)) +import Cardano.Ledger.Babbage.Core (BabbageEraTxBody) import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..)) import Cardano.Ledger.Binary.Coders ( Decode (..), @@ -85,6 +89,10 @@ import Cardano.Ledger.Binary.Coders ( (!>), ( STS (ConwayBBODY era) where @@ -268,28 +279,38 @@ instance type Event (ConwayBBODY era) = AlonzoBbodyEvent era initialRules = [] - transitionRules = [conwayBbodyTransition @era] + transitionRules = [conwayBbodyTransition @era >> alonzoBbodyTransition @era] conwayBbodyTransition :: forall era. - ( STS (EraRule "BBODY" era) - , Signal (EraRule "BBODY" era) ~ Block (BHeaderView (EraCrypto era)) era + ( Signal (EraRule "BBODY" era) ~ Block (BHeaderView (EraCrypto era)) era , State (EraRule "BBODY" era) ~ ShelleyBbodyState era - , Environment (EraRule "BBODY" era) ~ BbodyEnv era - , Embed (EraRule "LEDGERS" era) (EraRule "BBODY" era) - , BaseM (EraRule "BBODY" era) ~ ShelleyBase - , Environment (EraRule "LEDGERS" era) ~ ShelleyLedgersEnv era , State (EraRule "LEDGERS" era) ~ LedgerState era - , Signal (EraRule "LEDGERS" era) ~ Seq (Tx era) - , EraSegWits era - , AlonzoEraTxWits era , Era.TxSeq era ~ AlonzoTxSeq era , Tx era ~ AlonzoTx era - , AlonzoEraPParams era , InjectRuleFailure "BBODY" AlonzoBbodyPredFailure era + , InjectRuleFailure "BBODY" ConwayBbodyPredFailure era + , EraTx era + , BabbageEraTxBody era ) => TransitionRule (EraRule "BBODY" era) -conwayBbodyTransition = alonzoBbodyTransition @era +conwayBbodyTransition = do + judgmentContext + >>= \( TRC + ( _ + , state@(BbodyState (LedgerState utxoState _) _) + , UnserialisedBlock _ txsSeq + ) + ) -> do + let maxTotalRefScriptSize = 2560 * 1024 :: Int -- 2.5 MB = 2.5 * 1024 * 1024 bytes + utxo = utxosUtxo utxoState + txs = txSeqTxns txsSeq + totalRefScriptSize = getSum $ foldMap' (Monoid.Sum . txNonDistinctRefScriptsSize utxo) txs + totalRefScriptSize + <= maxTotalRefScriptSize + ?! injectFailure @"BBODY" + (RefScriptsSizeTooBig totalRefScriptSize maxTotalRefScriptSize) + pure state instance ( Era era