Skip to content

Commit

Permalink
Fail Conway Bbody rule for too big total reference script size
Browse files Browse the repository at this point in the history
  • Loading branch information
teodanciu committed Jun 27, 2024
1 parent 3fd77f4 commit e86598b
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 (..),
Expand All @@ -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 (..),
Expand All @@ -85,6 +89,10 @@ import Cardano.Ledger.Binary.Coders (
(!>),
(<!),
)
import Cardano.Ledger.Conway.UTxO (txNonDistinctRefScriptsSize)
import Data.Foldable (Foldable (foldMap'))
import Data.Monoid (Sum (getSum))
import qualified Data.Monoid as Monoid (Sum (..))

data ConwayBbodyPredFailure era
= WrongBlockBodySizeBBODY
Expand Down Expand Up @@ -248,7 +256,10 @@ instance
, EraSegWits era
, AlonzoEraPParams era
, InjectRuleFailure "BBODY" AlonzoBbodyPredFailure era
, InjectRuleFailure "BBODY" ConwayBbodyPredFailure era
, EraRule "BBODY" era ~ ConwayBBODY era
, EraTx era
, BabbageEraTxBody era
) =>
STS (ConwayBBODY era)
where
Expand All @@ -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
Expand Down

0 comments on commit e86598b

Please sign in to comment.