Skip to content

Commit 32cbd80

Browse files
committed
Add property tests for tx size computation in each era
1 parent d647871 commit 32cbd80

File tree

8 files changed

+29
-4
lines changed

8 files changed

+29
-4
lines changed

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/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/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/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
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
12
{-# LANGUAGE TypeApplications #-}
23

34
module Test.Cardano.Ledger.Shelley.BinarySpec (spec) where
45

56
import Cardano.Ledger.Shelley (ShelleyEra)
67
import Test.Cardano.Ledger.Common
7-
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec)
8+
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec, txSizeSpec)
89
import qualified Test.Cardano.Ledger.Shelley.Binary.GoldenSpec as Golden
910
import qualified Test.Cardano.Ledger.Shelley.Binary.RoundTripSpec as RoundTrip
1011

@@ -13,3 +14,4 @@ spec = do
1314
Golden.spec
1415
RoundTrip.spec
1516
Binary.decoderEquivalenceCoreEraTypesSpec @ShelleyEra
17+
Binary.txSizeSpec @ShelleyEra

libs/cardano-ledger-core/cardano-ledger-core.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ library testlib
206206
hedgehog-quickcheck,
207207
heredoc,
208208
hspec,
209+
microlens,
209210
mtl,
210211
nothunks,
211212
plutus-ledger-api:{plutus-ledger-api, plutus-ledger-api-testlib},

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Core/Binary.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ module Test.Cardano.Ledger.Core.Binary (
1111
decoderEquivalenceCoreEraTypesSpec,
1212
specUpgrade,
1313
decoderEquivalenceEraSpec,
14+
txSizeSpec,
1415
) where
1516

1617
import Cardano.Ledger.Binary (Annotator, DecCBOR, ToCBOR, decNoShareCBOR, encodeMemPack)
1718
import Cardano.Ledger.Core
1819
import Cardano.Ledger.MemoBytes (EqRaw (eqRaw))
1920
import Cardano.Ledger.Plutus (Data)
2021
import Data.Default (Default (def))
22+
import Lens.Micro
2123
import qualified Prettyprinter as Pretty
2224
import Test.Cardano.Ledger.Binary (decoderEquivalenceSpec)
2325
import Test.Cardano.Ledger.Binary.RoundTrip
@@ -325,3 +327,16 @@ decoderEquivalenceCoreEraTypesSpec =
325327
decoderEquivalenceEraSpec @era @(TxWits era)
326328
decoderEquivalenceEraSpec @era @(TxBody era)
327329
decoderEquivalenceEraSpec @era @(Tx era)
330+
331+
txSizeSpec ::
332+
forall era.
333+
( EraTx era
334+
, Arbitrary (Tx era)
335+
, SafeToHash (TxWits era)
336+
) =>
337+
Spec
338+
txSizeSpec =
339+
describe "Transaction size" $ do
340+
prop "should match the size of the cbor encoding" $ \(tx :: Tx era) -> do
341+
let txSize = sizeTxForFeeCalculation tx
342+
txSize `shouldBe` tx ^. sizeTxF

0 commit comments

Comments
 (0)