Skip to content

Commit 7cb77b5

Browse files
authored
Merge pull request #5140 from IntersectMBO/td/refactor-max-refscript-size-check
Refactor max refscript size check
2 parents 1cfa24b + c76d023 commit 7cb77b5

File tree

22 files changed

+385
-216
lines changed

22 files changed

+385
-216
lines changed

eras/alonzo/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.14.0.0
44

5+
* Add `Generic` instance for `AlonzoBbodyEvent`
56
* Fix `AlonzoPlutusPurpose` CBOR(Group) instances. #5135
67
* Reset shuffled serialization indexes for `AlonzoPlutusPurpose AsItem`.
78
* Deprecated `toAlonzoTransitionConfigPairs`

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Bbody.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ data AlonzoBbodyPredFailure era
8686

8787
newtype AlonzoBbodyEvent era
8888
= ShelleyInAlonzoEvent (ShelleyBbodyEvent era)
89+
deriving (Generic)
8990

9091
type instance EraRuleFailure "BBODY" AlonzoEra = AlonzoBbodyPredFailure AlonzoEra
9192

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Imp/UtxowSpec/Invalid.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ spec = describe "Invalid transactions" $ do
145145
_ -> error "Expected non-empty outputs"
146146
)
147147

148-
txInAt (0 :: Int)
148+
txInAt 0
149149
<$> withPostFixup
150150
(fixupResetAddrWits <$> resetTxOutDataHash)
151151
(submitTx tx)

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Imp/UtxowSpec/Valid.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ spec = describe "Valid transactions" $ do
6262
datumHash = hashData @era $ Data (P.I 123)
6363
txOut = mkBasicTxOut addr (inject amount) & dataHashTxOutL .~ SJust datumHash
6464
tx1 = mkBasicTx mkBasicTxBody & bodyTxL . outputsTxBodyL .~ [txOut]
65-
txIn <- txInAt (0 :: Int) <$> submitTx tx1
65+
txIn <- txInAt 0 <$> submitTx tx1
6666
let
6767
tx2 = mkBasicTx mkBasicTxBody & bodyTxL . inputsTxBodyL .~ [txIn]
6868
submitTx_ tx2

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/TreeDiff.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,7 @@ instance
179179
instance
180180
ToExpr (PredicateFailure (EraRule "LEDGERS" era)) =>
181181
ToExpr (AlonzoBbodyPredFailure era)
182+
183+
instance
184+
ToExpr (Event (EraRule "LEDGERS" era)) =>
185+
ToExpr (AlonzoBbodyEvent era)

eras/babbage/impl/testlib/Test/Cardano/Ledger/Babbage/Imp/UtxoSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ spec = describe "UTXO" $ do
5959
submitTx $
6060
mkBasicTx mkBasicTxBody
6161
& bodyTxL . outputsTxBodyL .~ SSeq.singleton txOut
62-
let txIn = txInAt (0 :: Integer) tx
62+
let txIn = txInAt 0 tx
6363
majorVer <- pvMajor <$> getsPParams ppProtocolVersionL
6464
when (majorVer < natVersion @9 || majorVer > natVersion @10) $
6565
submitTx_ @era $

eras/babbage/impl/testlib/Test/Cardano/Ledger/Babbage/ImpTest.hs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE CPP #-}
33
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE OverloadedLists #-}
45
{-# LANGUAGE RankNTypes #-}
56
{-# LANGUAGE ScopedTypeVariables #-}
67
{-# LANGUAGE TypeFamilies #-}
@@ -13,6 +14,9 @@ module Test.Cardano.Ledger.Babbage.ImpTest (
1314
module Test.Cardano.Ledger.Alonzo.ImpTest,
1415
produceRefScript,
1516
produceRefScripts,
17+
produceRefScriptsTx,
18+
mkTxWithRefInputs,
19+
submitTxWithRefInputs,
1620
) where
1721

1822
import Cardano.Ledger.Alonzo.Tx (IsValid (..))
@@ -35,6 +39,7 @@ import qualified Data.List.NonEmpty as NE
3539
import qualified Data.Map.Strict as Map
3640
import Data.Maybe (isNothing)
3741
import qualified Data.Sequence.Strict as SSeq
42+
import qualified Data.Set as Set
3843
import GHC.Stack (HasCallStack)
3944
import Lens.Micro
4045
import Test.Cardano.Ledger.Alonzo.ImpTest
@@ -120,12 +125,37 @@ produceRefScripts ::
120125
NonEmpty (Script era) ->
121126
ImpTestM era (NonEmpty TxIn)
122127
produceRefScripts scripts = do
128+
txId <- txIdTx <$> produceRefScriptsTx scripts
129+
pure $ NE.zipWith (\_ -> mkTxInPartial txId) scripts (0 :| [1 ..])
130+
131+
produceRefScriptsTx ::
132+
(ShelleyEraImp era, BabbageEraTxOut era) =>
133+
NonEmpty (Script era) ->
134+
ImpTestM era (Tx era)
135+
produceRefScriptsTx scripts = do
123136
pp <- getsNES $ nesEsL . curPParamsEpochStateL
124137
txOuts <- forM scripts $ \script -> do
125138
addr <- freshKeyAddr_
126139
let txOutZero =
127140
mkBasicTxOut addr mempty & referenceScriptTxOutL .~ SJust script
128141
pure $ setMinCoinTxOut pp txOutZero
129142
let txBody = mkBasicTxBody & outputsTxBodyL .~ SSeq.fromList (NE.toList txOuts)
130-
txId <- txIdTx <$> submitTx (mkBasicTx txBody)
131-
pure $ NE.zipWith (\_ -> mkTxInPartial txId) scripts (0 :| [1 ..])
143+
submitTx (mkBasicTx txBody)
144+
145+
mkTxWithRefInputs ::
146+
(ShelleyEraImp era, BabbageEraTxBody era) =>
147+
TxIn ->
148+
NonEmpty TxIn ->
149+
Tx era
150+
mkTxWithRefInputs txIn refIns =
151+
mkBasicTx $
152+
mkBasicTxBody
153+
& referenceInputsTxBodyL .~ Set.fromList (NE.toList refIns)
154+
& inputsTxBodyL .~ [txIn]
155+
156+
submitTxWithRefInputs ::
157+
(ShelleyEraImp era, BabbageEraTxBody era) =>
158+
TxIn ->
159+
NonEmpty TxIn ->
160+
ImpTestM era (Tx era)
161+
submitTxWithRefInputs txIn refIns = submitTx $ mkTxWithRefInputs txIn refIns

eras/conway/impl/CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
## 1.20.0.0
44

5-
- Move some hard-fork triggers and export them from `Cardano.Ledger.Conway` module.
5+
* Add `AlonzoEraTx` constraint to `STS` instance for `ConwayBBODY`
6+
* Add `totalRefScriptSizeInBlock`
7+
* Move some hard-fork triggers and export them from `Cardano.Ledger.Conway` module.
68
- `bootstrapPhase` to `hardforkConwayBootstrapPhase`.
79
- `disallowUnelectedCommitteeFromVoting` to `hardforkConwayDisallowUnelectedCommitteeFromVoting`.
8-
- Add `UnelectedCommitteeVoters` to `ConwayGovPredFailure` #5091
9-
- Change the type of `authorizedELectedCommitteeCredentials` to
10+
* Add `UnelectedCommitteeVoters` to `ConwayGovPredFailure` #5091
11+
* Change the type of `authorizedELectedCommitteeCredentials` to
1012
`StrictMaybe (Committee era) -> CommitteeState era -> Set.Set (Credential 'HotCommitteeRole)` #5091
1113
* Deprecated `toConwayTransitionConfigPairs`
1214
* Fixed `FromJSON` instance for `TransitionConfig ConwayEra`

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE BangPatterns #-}
12
{-# LANGUAGE DataKinds #-}
23
{-# LANGUAGE DeriveAnyClass #-}
34
{-# LANGUAGE DeriveGeneric #-}
@@ -21,6 +22,7 @@ module Cardano.Ledger.Conway.Rules.Bbody (
2122
maxRefScriptSizePerBlock,
2223
alonzoToConwayBbodyPredFailure,
2324
shelleyToConwayBbodyPredFailure,
25+
totalRefScriptSizeInBlock,
2426
) where
2527

2628
import Cardano.Ledger.Allegra.Rules (AllegraUtxoPredFailure)
@@ -35,13 +37,21 @@ import Cardano.Ledger.Alonzo.Rules (
3537
)
3638
import qualified Cardano.Ledger.Alonzo.Rules as Alonzo (AlonzoBbodyPredFailure (..))
3739
import Cardano.Ledger.Alonzo.Scripts (ExUnits (..))
38-
import Cardano.Ledger.Alonzo.Tx (AlonzoTx)
40+
import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx, AlonzoTx, IsValid (..), isValidTxL)
3941
import Cardano.Ledger.Alonzo.TxSeq (AlonzoTxSeq, txSeqTxns)
4042
import Cardano.Ledger.Alonzo.TxWits (AlonzoEraTxWits (..))
4143
import Cardano.Ledger.BHeaderView (BHeaderView (..))
44+
import Cardano.Ledger.Babbage.Collateral (collOuts)
4245
import Cardano.Ledger.Babbage.Core (BabbageEraTxBody)
4346
import Cardano.Ledger.Babbage.Rules (BabbageUtxoPredFailure, BabbageUtxowPredFailure)
44-
import Cardano.Ledger.BaseTypes (Mismatch (..), Relation (..), ShelleyBase)
47+
import Cardano.Ledger.BaseTypes (
48+
Mismatch (..),
49+
ProtVer,
50+
Relation (..),
51+
ShelleyBase,
52+
natVersion,
53+
pvMajor,
54+
)
4555
import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
4656
import Cardano.Ledger.Binary.Coders (Decode (..), Encode (..), decode, encode, (!>), (<!))
4757
import Cardano.Ledger.Block (Block (..))
@@ -71,6 +81,7 @@ import Cardano.Ledger.Shelley.Rules (
7181
ShelleyUtxowPredFailure,
7282
)
7383
import qualified Cardano.Ledger.Shelley.Rules as Shelley (ShelleyBbodyPredFailure (..))
84+
import Cardano.Ledger.Shelley.UTxO (UTxO (..), txouts, unUTxO)
7485
import Control.State.Transition (
7586
Embed (..),
7687
STS (..),
@@ -80,10 +91,14 @@ import Control.State.Transition (
8091
(?!),
8192
)
8293
import Data.Foldable (Foldable (foldMap'))
94+
import qualified Data.Foldable as F (foldl')
95+
import qualified Data.Map.Strict as Map
8396
import Data.Monoid (Sum (getSum))
8497
import qualified Data.Monoid as Monoid (Sum (..))
8598
import Data.Sequence (Seq)
99+
import Data.Sequence.Strict (StrictSeq (..))
86100
import GHC.Generics (Generic)
101+
import Lens.Micro ((^.))
87102
import NoThunks.Class (NoThunks (..))
88103

89104
-- | In the next era this will become a proper protocol parameter.
@@ -242,7 +257,7 @@ instance
242257
, InjectRuleFailure "BBODY" AlonzoBbodyPredFailure era
243258
, InjectRuleFailure "BBODY" ConwayBbodyPredFailure era
244259
, EraRule "BBODY" era ~ ConwayBBODY era
245-
, EraTx era
260+
, AlonzoEraTx era
246261
, BabbageEraTxBody era
247262
) =>
248263
STS (ConwayBBODY era)
@@ -266,27 +281,26 @@ conwayBbodyTransition ::
266281
forall era.
267282
( Signal (EraRule "BBODY" era) ~ Block BHeaderView era
268283
, State (EraRule "BBODY" era) ~ ShelleyBbodyState era
284+
, Environment (EraRule "BBODY" era) ~ BbodyEnv era
269285
, State (EraRule "LEDGERS" era) ~ LedgerState era
270286
, TxSeq era ~ AlonzoTxSeq era
271-
, Tx era ~ AlonzoTx era
272287
, InjectRuleFailure "BBODY" AlonzoBbodyPredFailure era
273288
, InjectRuleFailure "BBODY" ConwayBbodyPredFailure era
274-
, EraTx era
289+
, AlonzoEraTx era
275290
, BabbageEraTxBody era
276291
) =>
277292
TransitionRule (EraRule "BBODY" era)
278293
conwayBbodyTransition = do
279294
judgmentContext
280295
>>= \( TRC
281-
( _
296+
( BbodyEnv pp _
282297
, state@(BbodyState ls _)
283298
, Block _ txsSeq
284299
)
285300
) -> do
286301
let utxo = utxosUtxo (lsUTxOState ls)
287302
txs = txSeqTxns txsSeq
288-
totalRefScriptSize =
289-
getSum $ foldMap' (Monoid.Sum . txNonDistinctRefScriptsSize utxo) txs
303+
totalRefScriptSize = totalRefScriptSizeInBlock (pp ^. ppProtocolVersionL) txs utxo
290304
totalRefScriptSize
291305
<= maxRefScriptSizePerBlock
292306
?! injectFailure
@@ -308,3 +322,18 @@ instance
308322
where
309323
wrapFailed = LedgersFailure
310324
wrapEvent = ShelleyInAlonzoEvent . LedgersEvent
325+
326+
totalRefScriptSizeInBlock ::
327+
(AlonzoEraTx era, BabbageEraTxBody era) => ProtVer -> StrictSeq (Tx era) -> UTxO era -> Int
328+
totalRefScriptSizeInBlock protVer txs (UTxO utxo)
329+
| pvMajor protVer <= natVersion @10 =
330+
getSum $ foldMap' (Monoid.Sum . txNonDistinctRefScriptsSize (UTxO utxo)) txs
331+
| otherwise =
332+
snd $ F.foldl' accum (utxo, 0) txs
333+
where
334+
accum (!accUtxo, !accSum) tx =
335+
let updatedUtxo = accUtxo `Map.union` unUTxO toAdd
336+
toAdd
337+
| IsValid True <- tx ^. isValidTxL = txouts $ tx ^. bodyTxL
338+
| otherwise = collOuts $ tx ^. bodyTxL
339+
in (updatedUtxo, accSum + txNonDistinctRefScriptsSize (UTxO accUtxo) tx)

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ spec ::
9090
, ToExpr (Event (EraRule "ENACT" era))
9191
, Eq (Event (EraRule "ENACT" era))
9292
, Typeable (Event (EraRule "ENACT" era))
93+
, ToExpr (Event (EraRule "BBODY" era))
9394
) =>
9495
Spec
9596
spec = do
@@ -125,6 +126,7 @@ conwaySpec ::
125126
, ToExpr (Event (EraRule "ENACT" era))
126127
, Eq (Event (EraRule "ENACT" era))
127128
, Typeable (Event (EraRule "ENACT" era))
129+
, ToExpr (Event (EraRule "BBODY" era))
128130
) =>
129131
SpecWith (ImpInit (LedgerSpec era))
130132
conwaySpec = do

0 commit comments

Comments
 (0)