Skip to content

Commit 2a30527

Browse files
authored
Revert "Test that Consensus emits valid CBOR" (#362)
This reverts commit f8426b2 from #323. These tests turned out to be flaky, see eg [here](https://github.com/input-output-hk/ouroboros-consensus/actions/runs/6252676869/job/16976447179?pr=354#step:15:1589).
2 parents f743b8f + d0cfb91 commit 2a30527

File tree

9 files changed

+25
-142
lines changed

9 files changed

+25
-142
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
os: [ubuntu-latest]
2626
env:
2727
# Modify this value to "invalidate" the Cabal cache.
28-
CABAL_CACHE_VERSION: "2023-09-04"
28+
CABAL_CACHE_VERSION: "2023-09-25"
2929

3030
steps:
3131
- uses: actions/checkout@v4

cabal.project

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,3 @@ tests: true
3131
benchmarks: true
3232

3333
import: ./asserts.cabal
34-
35-
-- This can be removed once this fix is released https://github.com/well-typed/cborg/pull/325
36-
source-repository-package
37-
type: git
38-
location: https://github.com/well-typed/cborg.git
39-
tag: c8013b3474d876f4da56c869d57e3f3ac7f42dc6
40-
--sha256: 1rahq47qm977fawkq3d3718bz7fvd7hvy0s9qnbhlzafkqhqnqzj
41-
subdir: cborg

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ import Test.Util.Serialisation.Roundtrip
3838
tests :: TestTree
3939
tests = testGroup "Byron"
4040
[ roundtrip_all testCodecCfg dictNestedHdr
41+
4142
, testProperty "BinaryBlockInfo sanity check" prop_byronBinaryBlockInfo
43+
4244
, testGroup "Integrity"
4345
[ testProperty "detect corruption in RegularBlock" prop_detectCorruption_RegularBlock
4446
]

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE EmptyCase #-}
2-
{-# LANGUAGE FlexibleContexts #-}
32
{-# LANGUAGE GADTs #-}
43
{-# LANGUAGE LambdaCase #-}
54
{-# LANGUAGE NamedFieldPuns #-}
@@ -20,18 +19,16 @@ import Ouroboros.Consensus.Shelley.Node ()
2019
import Ouroboros.Consensus.Storage.Serialisation
2120
import Ouroboros.Consensus.Util (Dict (..))
2221
import Ouroboros.Network.Block (Serialised (..))
23-
import qualified Test.Consensus.Cardano.Examples as Cardano.Examples
2422
import Test.Consensus.Cardano.Generators (epochSlots)
2523
import Test.Consensus.Cardano.MockCrypto (MockCryptoCompatByron)
2624
import Test.Tasty
27-
import Test.Tasty.QuickCheck (Property, testProperty, (===))
25+
import Test.Tasty.QuickCheck
2826
import Test.Util.Orphans.Arbitrary ()
2927
import Test.Util.Serialisation.Roundtrip
3028

3129
tests :: TestTree
3230
tests = testGroup "Cardano"
33-
[ testGroup "Examples roundtrip" $ examplesRoundtrip Cardano.Examples.codecConfig Cardano.Examples.examples
34-
, roundtrip_all testCodecCfg dictNestedHdr
31+
[ roundtrip_all testCodecCfg dictNestedHdr
3532
, testProperty "BinaryBlockInfo sanity check" prop_CardanoBinaryBlockInfo
3633
]
3734

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ import Test.Util.Serialisation.Roundtrip
3030
tests :: TestTree
3131
tests = testGroup "Shelley"
3232
[ roundtrip_all testCodecCfg dictNestedHdr
33+
3334
-- Test for real crypto too
3435
, testProperty "hashSize real crypto" $ prop_hashSize pReal
3536
, testProperty "ConvertRawHash real crypto" $ roundtrip_ConvertRawHash pReal
37+
3638
, testProperty "BinaryBlockInfo sanity check" prop_shelleyBinaryBlockInfo
39+
3740
, testGroup "Integrity"
3841
[ testProperty "generate non-corrupt blocks" prop_blockIntegrity
3942
, testProperty "generate non-corrupt headers" prop_headerIntegrity

ouroboros-consensus-diffusion/test/mock-test/Test/ThreadNet/BFT.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Test.ThreadNet.Util.SimpleBlock
2828
import Test.Util.HardFork.Future (singleEraFuture)
2929
import Test.Util.Orphans.Arbitrary ()
3030
import Test.Util.Serialisation.Roundtrip
31+
import Test.Util.Serialisation.SomeResult ()
3132

3233
data TestSetup = TestSetup
3334
{ setupK :: SecurityParam

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ library unstable-consensus-testlib
351351
, ouroboros-consensus
352352
, ouroboros-network-api
353353
, ouroboros-network-mock
354-
, pretty-simple
355354
, QuickCheck
356355
, quickcheck-state-machine
357356
, quiet
@@ -361,11 +360,9 @@ library unstable-consensus-testlib
361360
, sop-extras
362361
, strict-sop-core
363362
, tasty
364-
, tasty-expected-failure
365363
, tasty-golden
366364
, tasty-quickcheck
367365
, template-haskell
368-
, text
369366
, time
370367
, tree-diff
371368
, utf8-string

ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/Serialisation/Golden.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{-# LANGUAGE DefaultSignatures #-}
2+
{-# LANGUAGE DeriveAnyClass #-}
3+
{-# LANGUAGE DeriveGeneric #-}
24
{-# LANGUAGE FlexibleContexts #-}
35
{-# LANGUAGE FlexibleInstances #-}
6+
{-# LANGUAGE LambdaCase #-}
47
{-# LANGUAGE OverloadedStrings #-}
58
{-# LANGUAGE RankNTypes #-}
69
{-# LANGUAGE RecordWildCards #-}
710
{-# LANGUAGE ScopedTypeVariables #-}
11+
{-# LANGUAGE StandaloneDeriving #-}
812
{-# LANGUAGE TypeApplications #-}
913

1014
{-# OPTIONS_GHC -Wno-orphans #-}

ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/Serialisation/Roundtrip.hs

Lines changed: 12 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,27 @@ module Test.Util.Serialisation.Roundtrip (
2828
, roundtrip_SerialiseNodeToNode
2929
, roundtrip_all
3030
, roundtrip_envelopes
31-
-- * Roundtrip tests for 'Example's
32-
, examplesRoundtrip
3331
) where
3432

3533
import Codec.CBOR.Decoding (Decoder)
3634
import Codec.CBOR.Encoding (Encoding)
37-
import Codec.CBOR.FlatTerm (toFlatTerm, validFlatTerm)
38-
import Codec.CBOR.Read (DeserialiseFailure, deserialiseFromBytes)
35+
import Codec.CBOR.Read (deserialiseFromBytes)
3936
import Codec.CBOR.Write (toLazyByteString)
40-
import Codec.Serialise (decode, encode)
41-
import Control.Arrow (left)
42-
import Control.Monad (unless)
4337
import qualified Data.ByteString.Base16.Lazy as Base16
4438
import qualified Data.ByteString.Lazy as Lazy
4539
import qualified Data.ByteString.Lazy.Char8 as Char8
4640
import qualified Data.ByteString.Short as Short
4741
import Data.Function (on)
48-
import Data.Maybe (fromMaybe)
49-
import qualified Data.Text.Lazy as T
5042
import Data.Typeable
5143
import GHC.Generics (Generic)
5244
import Ouroboros.Consensus.Block
5345
import Ouroboros.Consensus.HeaderValidation (AnnTip)
5446
import Ouroboros.Consensus.Ledger.Abstract (LedgerState)
55-
import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState,
56-
decodeExtLedgerState, encodeExtLedgerState)
5747
import Ouroboros.Consensus.Ledger.Query (BlockQuery, Query (..),
5848
QueryVersion)
5949
import qualified Ouroboros.Consensus.Ledger.Query as Query
6050
import Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr, GenTx,
6151
GenTxId)
62-
import Ouroboros.Consensus.Ledger.SupportsProtocol
63-
(LedgerSupportsProtocol)
6452
import Ouroboros.Consensus.Node.NetworkProtocolVersion
6553
import Ouroboros.Consensus.Node.Run (SerialiseNodeToClientConstraints,
6654
SerialiseNodeToNodeConstraints (..))
@@ -73,12 +61,9 @@ import Ouroboros.Network.Block (Serialised (..), fromSerialised,
7361
mkSerialised)
7462
import Quiet (Quiet (..))
7563
import Test.Tasty
76-
import Test.Tasty.ExpectedFailure (expectFailBecause)
7764
import Test.Tasty.QuickCheck
7865
import Test.Util.Orphans.Arbitrary ()
79-
import Test.Util.Serialisation.Examples (Examples (..), Labelled)
8066
import Test.Util.Serialisation.SomeResult (SomeResult (..))
81-
import Text.Pretty.Simple (pShow)
8267

8368
{------------------------------------------------------------------------------
8469
Basic test helpers
@@ -93,75 +78,31 @@ roundtrip enc dec = roundtrip' enc (const <$> dec)
9378

9479
-- | Roundtrip property for values annotated with their serialized form
9580
--
96-
-- In addition, we check that the encoded CBOR is valid using 'validFlatTerm'.
97-
--
98-
-- We check the roundtrip property both by decoding from a 'FlatTerm' directly, and from a bytestring.
99-
--
100-
-- Decoding from a 'FlatTerm' has the advantage that it allows to
101-
-- catch bugs more
102-
-- [easily](https://hackage.haskell.org/package/cborg-0.2.9.0/docs/Codec-CBOR-FlatTerm.html):
103-
--
104-
-- The FlatTerm form is very simple and internally mirrors the
105-
-- original Encoding type very carefully. The intention here
106-
-- is that once you have Encoding and Decoding values for your
107-
-- types, you can round-trip values through FlatTerm to catch
108-
-- bugs more easily and with a smaller amount of code to look
109-
-- through.
110-
--
111-
-- We also check 'ByteString' decoding for extra assurance.
112-
--
11381
-- NOTE: Suppose @a@ consists of a pair of the unannotated value @a'@ and some
11482
-- 'Lazy.ByteString'. The roundtrip property will fail if that
11583
-- 'Lazy.ByteString' encoding is not equal to @enc a'@. One way in which this
11684
-- might happen is if the annotation is not canonical CBOR, but @enc@ does
11785
-- produce canonical CBOR.
118-
roundtrip' :: forall a.
119-
(Eq a, Show a)
86+
roundtrip' :: (Eq a, Show a)
12087
=> (a -> Encoding) -- ^ @enc@
12188
-> (forall s. Decoder s (Lazy.ByteString -> a))
12289
-> a
12390
-> Property
124-
roundtrip' enc dec a = checkRoundtripResult $ do
125-
let enc_a = enc a
126-
bs = toLazyByteString enc_a
127-
flatTerm_a = toFlatTerm enc_a
128-
129-
validFlatTerm flatTerm_a ?! "Encoded flat term is not valid: " <> show enc_a
130-
-- TODO: the decode test via FlatTerm will currently fail because https://github.com/input-output-hk/cardano-ledger/issues/3741
131-
--
132-
-- a' <- fromFlatTerm dec flatTerm_a
133-
-- a == a' bs ?! pShowNeq a (a' bs)
134-
(bsRem, a'' ) <- deserialiseFromBytes dec bs `onError` showByteString bs
135-
Lazy.null bsRem ?! "Left-over bytes: " <> toBase16 bsRem
136-
a == a'' bs ?! pShowNeq a (a'' bs)
91+
roundtrip' enc dec a = case deserialiseFromBytes dec bs of
92+
Right (bs', a')
93+
| Lazy.null bs'
94+
-> a === a' bs
95+
| otherwise
96+
-> counterexample ("left-over bytes: " <> toBase16 bs') False
97+
Left e
98+
-> counterexample (show e) $
99+
counterexample (toBase16 bs) False
137100
where
138-
(?!) :: Bool -> String -> Either String ()
139-
cond ?! msg = unless cond $ Left msg
140-
infix 1 ?!
141-
142-
pShowNeq x y = T.unpack (pShow x) <> "\n \t/= \n" <> T.unpack (pShow y)
143-
144-
onError ::
145-
Either DeserialiseFailure (Char8.ByteString, Char8.ByteString -> a)
146-
-> (DeserialiseFailure -> String)
147-
-> Either String (Char8.ByteString, Char8.ByteString -> a)
148-
onError result showDeserialiseFailure =
149-
left showDeserialiseFailure result
150-
151-
showByteString ::
152-
Char8.ByteString
153-
-> DeserialiseFailure
154-
-> String
155-
showByteString bs deserialiseFailure =
156-
show deserialiseFailure <> "\n" <> "When deserialising " <> toBase16 bs
101+
bs = toLazyByteString (enc a)
157102

158103
toBase16 :: Lazy.ByteString -> String
159104
toBase16 = Char8.unpack . Base16.encode
160105

161-
checkRoundtripResult :: Either String () -> Property
162-
checkRoundtripResult (Left str) = counterexample str False
163-
checkRoundtripResult (Right ()) = property ()
164-
165106
{------------------------------------------------------------------------------
166107
Test skeleton
167108
------------------------------------------------------------------------------}
@@ -651,57 +592,3 @@ decodeThroughSerialised
651592
decodeThroughSerialised dec decSerialised = do
652593
serialised <- decSerialised
653594
fromSerialised dec serialised
654-
655-
{------------------------------------------------------------------------------
656-
Roundtrip tests for examples
657-
------------------------------------------------------------------------------}
658-
659-
examplesRoundtrip ::
660-
forall blk . (SerialiseDiskConstraints blk, Eq blk, Show blk, LedgerSupportsProtocol blk)
661-
=> CodecConfig blk
662-
-> Examples blk
663-
-> [TestTree]
664-
examplesRoundtrip codecConfig examples =
665-
[ testRoundtripFor "Block" (encodeDisk codecConfig) (decodeDisk codecConfig) exampleBlock
666-
, testRoundtripFor "Header hash" encode (const <$> decode) exampleHeaderHash
667-
, testRoundtripFor "Ledger state" (encodeDisk codecConfig) (const <$> decodeDisk codecConfig) exampleLedgerState
668-
, testRoundtripFor "Annotated tip" (encodeDisk codecConfig) (const <$> decodeDisk codecConfig) exampleAnnTip
669-
, testRoundtripFor "Chain dependent state" (encodeDisk codecConfig) (const <$> decodeDisk codecConfig) exampleChainDepState
670-
, testRoundtripFor "Extended ledger state" encodeExt (const <$> decodeExt) exampleExtLedgerState
671-
]
672-
where
673-
testRoundtripFor ::
674-
forall a . (Eq a, Show a)
675-
=> String
676-
-> (a -> Encoding)
677-
-> (forall s . Decoder s (Char8.ByteString -> a))
678-
-> (Examples blk -> Labelled a)
679-
-> TestTree
680-
testRoundtripFor testLabel enc dec field =
681-
testGroup testLabel
682-
[ mkTest exampleName example
683-
| (exampleName, example) <- field examples
684-
]
685-
where
686-
mkTest exampleName example =
687-
let
688-
runTest = testProperty (fromMaybe "" exampleName) $ once $ roundtrip' enc dec example
689-
_3740 = "https://github.com/input-output-hk/cardano-ledger/issues/3740"
690-
in
691-
case (testLabel, exampleName) of
692-
("Ledger state" , Just "Conway") -> expectFailBecause _3740 $ runTest
693-
("Extended ledger state", Just "Conway") -> expectFailBecause _3740 $ runTest
694-
_ -> runTest
695-
696-
encodeExt =
697-
encodeExtLedgerState
698-
(encodeDisk codecConfig)
699-
(encodeDisk codecConfig)
700-
(encodeDisk codecConfig)
701-
702-
decodeExt :: forall s. Decoder s (ExtLedgerState blk)
703-
decodeExt =
704-
decodeExtLedgerState
705-
(decodeDisk codecConfig)
706-
(decodeDisk codecConfig)
707-
(decodeDisk codecConfig)

0 commit comments

Comments
 (0)