Skip to content

Commit 1201eb6

Browse files
committed
txWireSize: added tests
1 parent 7749404 commit 1201eb6

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

ouroboros-consensus-cardano/test/byron-test/Test/Consensus/Byron/Serialisation.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ import Test.Tasty.QuickCheck
3131
import Test.Util.Corruption
3232
import Test.Util.Orphans.Arbitrary ()
3333
import Test.Util.Serialisation.Roundtrip
34+
import Test.Util.Serialisation.TxWireSize
3435

3536
tests :: TestTree
3637
tests = testGroup "Byron"
3738
[ roundtrip_all testCodecCfg dictNestedHdr
39+
, testProperty "GenTx.txWireSize.txSubmission" (prop_txWireSize_txSubmission testCodecCfg)
40+
, testProperty "GenTx.txWireSize.tight" (prop_txWireSize (const Nothing) testCodecCfg)
3841

3942
, testProperty "BinaryBlockInfo sanity check" prop_byronBinaryBlockInfo
4043

ouroboros-consensus-cardano/test/cardano-test/Test/Consensus/Cardano/Serialisation.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE LambdaCase #-}
33
{-# LANGUAGE NamedFieldPuns #-}
44
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE TypeOperators #-}
56

67
module Test.Consensus.Cardano.Serialisation (tests) where
78

@@ -26,10 +27,13 @@ import Test.Tasty
2627
import Test.Tasty.QuickCheck (Property, testProperty, (===))
2728
import Test.Util.Orphans.Arbitrary ()
2829
import Test.Util.Serialisation.Roundtrip
30+
import Test.Util.Serialisation.TxWireSize
2931

3032
tests :: TestTree
3133
tests = testGroup "Cardano"
3234
[ testGroup "Examples roundtrip" $ examplesRoundtrip Cardano.Examples.codecConfig Cardano.Examples.examples
35+
, testProperty "GenTx.txWireSize.txSubmission" $ prop_txWireSize_txSubmission testCodecCfg
36+
, testProperty "GenTx.txWireSize.tight" $ prop_txWireSize (const Nothing) testCodecCfg
3337
, roundtrip_all_skipping result testCodecCfg dictNestedHdr
3438
, testProperty "BinaryBlockInfo sanity check" prop_CardanoBinaryBlockInfo
3539
]

ouroboros-consensus-cardano/test/shelley-test/Test/Consensus/Shelley/Serialisation.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,19 @@ import Test.Tasty.QuickCheck
2626
import Test.Util.Corruption
2727
import Test.Util.Orphans.Arbitrary ()
2828
import Test.Util.Serialisation.Roundtrip
29+
import Test.Util.Serialisation.TxWireSize
30+
31+
import qualified Cardano.Ledger.MemoBytes.Internal as SL
32+
import qualified Cardano.Ledger.Shelley.Tx.Internal as SL
33+
34+
getTxBytes :: GenTx Block -> Maybe String
35+
getTxBytes (ShelleyTx _ (SL.TxConstr (SL.Memo _ bytes))) = Just $ show bytes
2936

3037
tests :: TestTree
3138
tests = testGroup "Shelley"
3239
[ roundtrip_all testCodecCfg dictNestedHdr
40+
, testProperty "GenTx.txWireSize.txSubmission" (prop_txWireSize_txSubmission testCodecCfg)
41+
, testProperty "GenTx.txWireSize.tight" (prop_txWireSize getTxBytes testCodecCfg)
3342

3443
-- Test for real crypto too
3544
, testProperty "hashSize real crypto" $ prop_hashSize pReal

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ library unstable-consensus-testlib
412412
Test.Util.Serialisation.Golden
413413
Test.Util.Serialisation.Roundtrip
414414
Test.Util.Serialisation.SomeResult
415+
Test.Util.Serialisation.TxWireSize
415416
Test.Util.Shrink
416417
Test.Util.Slots
417418
Test.Util.Split
@@ -456,6 +457,7 @@ library unstable-consensus-testlib
456457
nothunks,
457458
optparse-applicative,
458459
ouroboros-consensus,
460+
ouroboros-network,
459461
ouroboros-network-api,
460462
ouroboros-network-mock,
461463
pretty-simple,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{-# LANGUAGE FlexibleContexts #-}
2+
3+
module Test.Util.Serialisation.TxWireSize (
4+
prop_txWireSize
5+
, prop_txWireSize_txSubmission
6+
) where
7+
8+
import Codec.CBOR.Write (toLazyByteString)
9+
import Ouroboros.Consensus.Block.Abstract (CodecConfig)
10+
import Ouroboros.Consensus.Ledger.SupportsMempool (GenTx, TxLimits (..))
11+
import Ouroboros.Consensus.Node.NetworkProtocolVersion (BlockNodeToNodeVersion)
12+
import Ouroboros.Consensus.Node.Serialisation
13+
import Ouroboros.Network.SizeInBytes
14+
import Ouroboros.Network.TxSubmission.Inbound.V2.State (config_MAX_TX_SIZE_DISCREPANCY)
15+
import qualified Data.ByteString.Lazy as BSL
16+
import Test.Tasty.QuickCheck
17+
import Test.Util.Serialisation.Roundtrip (WithVersion (..))
18+
19+
20+
-- | Verify that `txWriteSize` agrees with the encoded `GenTx` size up to
21+
-- `config_MX_TX_SIZE_DISCREPANCY` allowed by `tx-submission` mini-protocol.
22+
--
23+
-- NOTE: `tx`s which do not satisfy this property will terminate connections.
24+
--
25+
prop_txWireSize_txSubmission ::
26+
( SerialiseNodeToNode blk (GenTx blk)
27+
, TxLimits blk
28+
)
29+
=> CodecConfig blk
30+
-> WithVersion (BlockNodeToNodeVersion blk) (GenTx blk)
31+
-> Property
32+
prop_txWireSize_txSubmission ccfg (WithVersion version tx) =
33+
counterexample ("encoded size " ++ show encSize ++ ", computed size " ++ show cmpSize)
34+
$ counterexample ("diff size " ++ show diffSize)
35+
$ label (show diffSize)
36+
$ fromIntegral (abs diffSize) <= config_MAX_TX_SIZE_DISCREPANCY
37+
where
38+
encSize, cmpSize :: SizeInBytes
39+
40+
encSize = fromIntegral (BSL.length $ toLazyByteString (encodeNodeToNode ccfg version tx))
41+
cmpSize = txWireSize tx
42+
43+
diffSize :: Int
44+
diffSize = fromIntegral encSize - fromIntegral cmpSize
45+
46+
47+
-- | Verify that `txWriteSize` is very close to the real tx size.
48+
--
49+
-- The `txWireSize` doesn't take into account if HFC is enabled or not. If it
50+
-- is enabled, the `wireTxSize` for `GenTx (HardForkBlock xs)` will agree with
51+
-- the encoded size, but if it is disabled it will overestimate the value by HFC
52+
-- envelope (at most 3 bytes, 2 for forcible future)
53+
--
54+
prop_txWireSize ::
55+
( SerialiseNodeToNode blk (GenTx blk)
56+
, TxLimits blk
57+
)
58+
=> (GenTx blk -> Maybe String)
59+
-- ^ show tx bytes
60+
-> CodecConfig blk
61+
-> WithVersion (BlockNodeToNodeVersion blk) (GenTx blk)
62+
-> Property
63+
prop_txWireSize getTxBytes ccfg (WithVersion version tx) =
64+
counterexample ("encoded size " ++ show encSize ++ ", computed size " ++ show cmpSize)
65+
$ counterexample ("encoded tx:\n" ++ show encoded)
66+
$ label (show diffSize)
67+
$ case getTxBytes tx of
68+
Just txBytes -> counterexample ("tx bytes:\n" ++ txBytes)
69+
Nothing -> property
70+
$ encSize <= cmpSize
71+
.&&.
72+
encSize + 3 >= cmpSize
73+
74+
75+
where
76+
encoded :: BSL.ByteString
77+
encoded = toLazyByteString (encodeNodeToNode ccfg version tx)
78+
79+
encSize, cmpSize :: SizeInBytes
80+
encSize = fromIntegral (BSL.length encoded)
81+
cmpSize = txWireSize tx
82+
83+
diffSize :: Int
84+
diffSize = fromIntegral encSize - fromIntegral cmpSize

0 commit comments

Comments
 (0)