Skip to content

Commit 17841f1

Browse files
committed
txWireSize: added tests
1 parent 5061a15 commit 17841f1

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ import Test.Tasty
2626
import Test.Tasty.QuickCheck (Property, testProperty, (===))
2727
import Test.Util.Orphans.Arbitrary ()
2828
import Test.Util.Serialisation.Roundtrip
29+
import Test.Util.Serialisation.TxWireSize
2930

3031
tests :: TestTree
3132
tests = testGroup "Cardano"
3233
[ testGroup "Examples roundtrip" $ examplesRoundtrip Cardano.Examples.codecConfig Cardano.Examples.examples
34+
, testProperty "GenTx.txWireSize.txSubmission" $ prop_txWireSize_txSubmission testCodecCfg
35+
, testProperty "GenTx.txWireSize.tight" $ prop_txWireSize (const Nothing) testCodecCfg
3336
, roundtrip_all_skipping result testCodecCfg dictNestedHdr
3437
, testProperty "BinaryBlockInfo sanity check" prop_CardanoBinaryBlockInfo
3538
]

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
module Test.Consensus.Shelley.Serialisation (tests) where
77

8+
import qualified Cardano.Ledger.MemoBytes.Internal as SL
9+
import qualified Cardano.Ledger.Shelley.Tx.Internal as SL
810
import qualified Codec.CBOR.Write as CBOR
911
import qualified Data.ByteString.Lazy as Lazy
1012
import Data.Constraint
@@ -26,10 +28,16 @@ import Test.Tasty.QuickCheck
2628
import Test.Util.Corruption
2729
import Test.Util.Orphans.Arbitrary ()
2830
import Test.Util.Serialisation.Roundtrip
31+
import Test.Util.Serialisation.TxWireSize
32+
33+
getTxBytes :: GenTx Block -> Maybe String
34+
getTxBytes (ShelleyTx _ (SL.TxConstr (SL.Memo _ bytes))) = Just $ show bytes
2935

3036
tests :: TestTree
3137
tests = testGroup "Shelley"
3238
[ roundtrip_all testCodecCfg dictNestedHdr
39+
, testProperty "GenTx.txWireSize.txSubmission" (prop_txWireSize_txSubmission testCodecCfg)
40+
, testProperty "GenTx.txWireSize.tight" (prop_txWireSize getTxBytes testCodecCfg)
3341

3442
-- Test for real crypto too
3543
, 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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 qualified Data.ByteString.Lazy as BSL
10+
import Ouroboros.Consensus.Block.Abstract (CodecConfig)
11+
import Ouroboros.Consensus.Ledger.SupportsMempool (GenTx,
12+
TxLimits (..))
13+
import Ouroboros.Consensus.Node.NetworkProtocolVersion
14+
(BlockNodeToNodeVersion)
15+
import Ouroboros.Consensus.Node.Serialisation
16+
import Ouroboros.Network.SizeInBytes
17+
import Ouroboros.Network.TxSubmission.Inbound.V2.State
18+
(config_MAX_TX_SIZE_DISCREPANCY)
19+
import Test.Tasty.QuickCheck
20+
import Test.Util.Serialisation.Roundtrip (WithVersion (..))
21+
22+
23+
-- | Verify that `txWriteSize` agrees with the encoded `GenTx` size up to
24+
-- `config_MX_TX_SIZE_DISCREPANCY` allowed by `tx-submission` mini-protocol.
25+
--
26+
-- NOTE: `tx`s which do not satisfy this property will terminate connections.
27+
--
28+
prop_txWireSize_txSubmission ::
29+
( SerialiseNodeToNode blk (GenTx blk)
30+
, TxLimits blk
31+
)
32+
=> CodecConfig blk
33+
-> WithVersion (BlockNodeToNodeVersion blk) (GenTx blk)
34+
-> Property
35+
prop_txWireSize_txSubmission ccfg (WithVersion version tx) =
36+
counterexample ("encoded size " ++ show encSize ++ ", computed size " ++ show cmpSize)
37+
$ counterexample ("diff size " ++ show diffSize)
38+
$ label (show diffSize)
39+
$ fromIntegral (abs diffSize) <= config_MAX_TX_SIZE_DISCREPANCY
40+
where
41+
encSize, cmpSize :: SizeInBytes
42+
43+
encSize = fromIntegral (BSL.length $ toLazyByteString (encodeNodeToNode ccfg version tx))
44+
cmpSize = txWireSize tx
45+
46+
diffSize :: Int
47+
diffSize = fromIntegral encSize - fromIntegral cmpSize
48+
49+
50+
-- | Verify that `txWriteSize` is very close to the real tx size.
51+
--
52+
-- The `txWireSize` doesn't take into account if HFC is enabled or not. If it
53+
-- is enabled, the `wireTxSize` for `GenTx (HardForkBlock xs)` will agree with
54+
-- the encoded size, but if it is disabled it will overestimate the value by HFC
55+
-- envelope (at most 3 bytes, 2 for forcible future)
56+
--
57+
prop_txWireSize ::
58+
( SerialiseNodeToNode blk (GenTx blk)
59+
, TxLimits blk
60+
)
61+
=> (GenTx blk -> Maybe String)
62+
-- ^ show tx bytes
63+
-> CodecConfig blk
64+
-> WithVersion (BlockNodeToNodeVersion blk) (GenTx blk)
65+
-> Property
66+
prop_txWireSize getTxBytes ccfg (WithVersion version tx) =
67+
counterexample ("encoded size " ++ show encSize ++ ", computed size " ++ show cmpSize)
68+
$ counterexample ("encoded tx:\n" ++ show encoded)
69+
$ label (show diffSize)
70+
$ case getTxBytes tx of
71+
Just txBytes -> counterexample ("tx bytes:\n" ++ txBytes)
72+
Nothing -> property
73+
$ encSize <= cmpSize
74+
.&&.
75+
encSize + 3 >= cmpSize
76+
77+
78+
where
79+
encoded :: BSL.ByteString
80+
encoded = toLazyByteString (encodeNodeToNode ccfg version tx)
81+
82+
encSize, cmpSize :: SizeInBytes
83+
encSize = fromIntegral (BSL.length encoded)
84+
cmpSize = txWireSize tx
85+
86+
diffSize :: Int
87+
diffSize = fromIntegral encSize - fromIntegral cmpSize

0 commit comments

Comments
 (0)