Skip to content

Commit 655b552

Browse files
authored
Merge pull request #4959 from IntersectMBO/td/improve-sizeTxF
Improve performance of sizeTxF
2 parents 0c8706f + 32cbd80 commit 655b552

File tree

15 files changed

+69
-6
lines changed

15 files changed

+69
-6
lines changed

eras/allegra/impl/src/Cardano/Ledger/Allegra/Rules/Utxo.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ utxoTransition ::
176176
, InjectRuleFailure "UTXO" AllegraUtxoPredFailure era
177177
, InjectRuleFailure "UTXO" Shelley.ShelleyUtxoPredFailure era
178178
, EraRule "UTXO" era ~ AllegraUTXO era
179+
, SafeToHash (TxWits era)
179180
) =>
180181
TransitionRule (EraRule "UTXO" era)
181182
utxoTransition = do
@@ -310,6 +311,7 @@ instance
310311
, GovState era ~ ShelleyGovState era
311312
, InjectRuleFailure "UTXO" AllegraUtxoPredFailure era
312313
, InjectRuleFailure "UTXO" Shelley.ShelleyUtxoPredFailure era
314+
, SafeToHash (TxWits era)
313315
) =>
314316
STS (AllegraUTXO era)
315317
where
@@ -322,6 +324,7 @@ instance
322324

323325
initialRules = []
324326
transitionRules = [utxoTransition]
327+
assertions = [Shelley.validSizeComputationCheck]
325328

326329
instance
327330
( Era era

eras/allegra/impl/test/Test/Cardano/Ledger/Allegra/BinarySpec.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Test.Cardano.Ledger.Allegra.Arbitrary ()
1111
import Test.Cardano.Ledger.Allegra.TreeDiff ()
1212
import Test.Cardano.Ledger.Common
1313
import Test.Cardano.Ledger.Core.Binary (specUpgrade)
14-
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec)
14+
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec, txSizeSpec)
1515
import Test.Cardano.Ledger.Core.Binary.RoundTrip (RuleListEra (..))
1616
import Test.Cardano.Ledger.Shelley.Binary.RoundTrip (roundTripShelleyCommonSpec)
1717

@@ -22,6 +22,7 @@ spec = do
2222
roundTripShelleyCommonSpec @AllegraEra
2323
describe "DecCBOR instances equivalence" $ do
2424
Binary.decoderEquivalenceCoreEraTypesSpec @AllegraEra
25+
Binary.txSizeSpec @AllegraEra
2526

2627
instance RuleListEra AllegraEra where
2728
type

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ utxoTransition ::
492492
, State (EraRule "UTXOS" era) ~ UTxOState era
493493
, Signal (EraRule "UTXOS" era) ~ Tx era
494494
, EraCertState era
495+
, SafeToHash (TxWits era)
495496
) =>
496497
TransitionRule (EraRule "UTXO" era)
497498
utxoTransition = do
@@ -580,6 +581,7 @@ instance
580581
, InjectRuleFailure "UTXO" AllegraUtxoPredFailure era
581582
, ProtVerAtMost era 8
582583
, EraCertState era
584+
, SafeToHash (TxWits era)
583585
) =>
584586
STS (AlonzoUTXO era)
585587
where
@@ -592,6 +594,7 @@ instance
592594

593595
initialRules = []
594596
transitionRules = [utxoTransition]
597+
assertions = [Shelley.validSizeComputationCheck]
595598

596599
instance
597600
( Era era

eras/alonzo/impl/test/Test/Cardano/Ledger/Alonzo/BinarySpec.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Test.Cardano.Ledger.Core.Binary (BinaryUpgradeOpts (..), specUpgrade)
1313
import Test.Cardano.Ledger.Core.Binary as Binary (
1414
decoderEquivalenceCoreEraTypesSpec,
1515
decoderEquivalenceEraSpec,
16+
txSizeSpec,
1617
)
1718
import Test.Cardano.Ledger.Core.Binary.RoundTrip (roundTripEraSpec)
1819

@@ -31,3 +32,4 @@ spec = do
3132
Binary.decoderEquivalenceCoreEraTypesSpec @AlonzoEra
3233
decoderEquivalenceEraSpec @AlonzoEra @(TxDats AlonzoEra)
3334
decoderEquivalenceEraSpec @AlonzoEra @(Redeemers AlonzoEra)
35+
Binary.txSizeSpec @AlonzoEra

eras/babbage/impl/src/Cardano/Ledger/Babbage/Rules/Utxo.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ instance
464464
, State (EraRule "UTXOS" era) ~ UTxOState era
465465
, Signal (EraRule "UTXOS" era) ~ Tx era
466466
, EraCertState era
467+
, SafeToHash (TxWits era)
467468
) =>
468469
STS (BabbageUTXO era)
469470
where
@@ -476,6 +477,7 @@ instance
476477

477478
initialRules = []
478479
transitionRules = [utxoTransition @era]
480+
assertions = [Shelley.validSizeComputationCheck]
479481

480482
instance
481483
( Era era

eras/babbage/impl/test/Test/Cardano/Ledger/Babbage/BinarySpec.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Test.Cardano.Ledger.Core.Binary (specUpgrade)
1616
import Test.Cardano.Ledger.Core.Binary as Binary (
1717
decoderEquivalenceCoreEraTypesSpec,
1818
decoderEquivalenceEraSpec,
19+
txSizeSpec,
1920
)
2021
import Test.Cardano.Ledger.Core.Binary.RoundTrip (RuleListEra (..))
2122

@@ -28,6 +29,7 @@ spec = do
2829
Binary.decoderEquivalenceCoreEraTypesSpec @BabbageEra
2930
decoderEquivalenceEraSpec @BabbageEra @(TxDats BabbageEra)
3031
decoderEquivalenceEraSpec @BabbageEra @(Redeemers BabbageEra)
32+
Binary.txSizeSpec @BabbageEra
3133

3234
instance RuleListEra BabbageEra where
3335
type

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import Cardano.Ledger.Conway.Rules.Utxos (
6767
import Cardano.Ledger.Plutus (ExUnits)
6868
import qualified Cardano.Ledger.Shelley.LedgerState as Shelley (UTxOState)
6969
import Cardano.Ledger.Shelley.Rules (ShelleyUtxoPredFailure)
70-
import qualified Cardano.Ledger.Shelley.Rules as Shelley (UtxoEnv)
70+
import qualified Cardano.Ledger.Shelley.Rules as Shelley (UtxoEnv, validSizeComputationCheck)
7171
import Cardano.Ledger.State (EraCertState (..), EraUTxO, UTxO (..))
7272
import Cardano.Ledger.TxIn (TxIn)
7373
import Control.DeepSeq (NFData)
@@ -241,6 +241,7 @@ instance
241241
, Signal (EraRule "UTXOS" era) ~ Tx era
242242
, PredicateFailure (EraRule "UTXO" era) ~ ConwayUtxoPredFailure era
243243
, EraCertState era
244+
, SafeToHash (TxWits era)
244245
) =>
245246
STS (ConwayUTXO era)
246247
where
@@ -255,6 +256,8 @@ instance
255256

256257
transitionRules = [Babbage.utxoTransition @era]
257258

259+
assertions = [Shelley.validSizeComputationCheck]
260+
258261
instance
259262
( Era era
260263
, STS (ConwayUTXOS era)

eras/conway/impl/test/Test/Cardano/Ledger/Conway/BinarySpec.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Test.Cardano.Ledger.Conway.Arbitrary ()
2222
import Test.Cardano.Ledger.Conway.Binary.RoundTrip (roundTripConwayCommonSpec)
2323
import Test.Cardano.Ledger.Conway.TreeDiff ()
2424
import Test.Cardano.Ledger.Core.Binary (specUpgrade)
25-
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec)
25+
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec, txSizeSpec)
2626
import Test.Cardano.Ledger.Core.Binary.RoundTrip (roundTripEraSpec)
2727

2828
spec :: Spec
@@ -43,6 +43,7 @@ spec = do
4343
Binary.decoderEquivalenceCoreEraTypesSpec @ConwayEra
4444
decoderEquivalenceLenientSpec @(TxDats ConwayEra)
4545
decoderEquivalenceLenientSpec @(Redeemers ConwayEra)
46+
Binary.txSizeSpec @ConwayEra
4647
where
4748
-- The expectation used in this spec allows for the deserialization to fail, in which case
4849
-- it only checks that it fails for both decoders.

eras/mary/impl/test/Test/Cardano/Ledger/Mary/BinarySpec.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Cardano.Ledger.Mary
99
import Data.Default (def)
1010
import Test.Cardano.Ledger.Common
1111
import Test.Cardano.Ledger.Core.Binary (specUpgrade)
12-
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec)
12+
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec, txSizeSpec)
1313
import Test.Cardano.Ledger.Core.Binary.RoundTrip (RuleListEra (..))
1414
import Test.Cardano.Ledger.Mary.Arbitrary ()
1515
import Test.Cardano.Ledger.Mary.TreeDiff ()
@@ -22,6 +22,7 @@ spec = do
2222
roundTripShelleyCommonSpec @MaryEra
2323
describe "DecCBOR instances equivalence" $ do
2424
Binary.decoderEquivalenceCoreEraTypesSpec @MaryEra
25+
Binary.txSizeSpec @MaryEra
2526

2627
instance RuleListEra MaryEra where
2728
type

eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Utxo.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Cardano.Ledger.Shelley.Rules.Utxo (
2323
ShelleyUtxoPredFailure (..),
2424
UtxoEvent (..),
2525
PredicateFailure,
26+
validSizeComputationCheck,
2627
updateUTxOState,
2728

2829
-- * Validations
@@ -282,6 +283,7 @@ instance
282283
, EraRule "UTXO" era ~ ShelleyUTXO era
283284
, InjectRuleFailure "UTXO" ShelleyUtxoPredFailure era
284285
, EraCertState era
286+
, SafeToHash (TxWits era)
285287
) =>
286288
STS (ShelleyUTXO era)
287289
where
@@ -333,6 +335,7 @@ instance
333335
( \(TRC (_, us, tx)) us' ->
334336
utxoBalance us <> withdrawals (tx ^. bodyTxL) == utxoBalance us'
335337
)
338+
, validSizeComputationCheck @era
336339
]
337340

338341
utxoInductive ::
@@ -628,3 +631,16 @@ instance
628631
where
629632
wrapFailed = UpdateFailure
630633
wrapEvent = UpdateEvent
634+
635+
validSizeComputationCheck ::
636+
( EraTx era
637+
, SafeToHash (TxWits era)
638+
, Signal (rule era) ~ Tx era
639+
) =>
640+
Assertion (rule era)
641+
validSizeComputationCheck =
642+
PreCondition
643+
"Tx size should be the length of the serialization bytestring"
644+
( \(TRC (_, _, tx)) ->
645+
tx ^. sizeTxF == sizeTxForFeeCalculation tx
646+
)

0 commit comments

Comments
 (0)