Skip to content

Commit f743b8f

Browse files
author
Damian Nadales
authored
Test that consensus emits valid CBOR (#323)
- Check that the encoders used in the serialization roundtrip are valid. - Add a unit test to check that the encoders used to encode the Cardano examples are valid. Needs: well-typed/cborg#324. Closes IntersectMBO/ouroboros-network#3099.
2 parents f318ffe + f8426b2 commit f743b8f

File tree

16 files changed

+347
-200
lines changed

16 files changed

+347
-200
lines changed

cabal.project

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ 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/src/unstable-byron-testlib/Test/Consensus/Byron/Examples.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ import qualified Test.Cardano.Chain.Genesis.Dummy as CC
4848
import qualified Test.Cardano.Chain.Update.Example as CC
4949
import qualified Test.Cardano.Chain.UTxO.Example as CC
5050
import Test.ThreadNet.Infra.Byron.ProtocolInfo (mkLeaderCredentials)
51-
import qualified Test.Util.Serialisation.Golden as Golden
52-
import Test.Util.Serialisation.Golden (Labelled, labelled, unlabelled)
53-
import Test.Util.Serialisation.Roundtrip (SomeResult (..))
51+
import qualified Test.Util.Serialisation.Examples as Examples
52+
import Test.Util.Serialisation.Examples (Examples (Examples),
53+
Labelled, labelled, unlabelled)
54+
import Test.Util.Serialisation.SomeResult (SomeResult (..))
5455

5556
{-------------------------------------------------------------------------------
5657
Setup
@@ -89,8 +90,8 @@ leaderCredentials =
8990
Examples
9091
-------------------------------------------------------------------------------}
9192

92-
examples :: Golden.Examples ByronBlock
93-
examples = Golden.Examples {
93+
examples :: Examples ByronBlock
94+
examples = Examples {
9495
exampleBlock = regularAndEBB exampleBlock exampleEBB
9596
, exampleSerialisedBlock = regularAndEBB exampleSerialisedBlock exampleSerialisedEBB
9697
, exampleHeader = regularAndEBB exampleHeader exampleEBBHeader

ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/Consensus/Byron/Generators.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ import Test.QuickCheck hiding (Result)
5656
import Test.QuickCheck.Hedgehog (hedgehog)
5757
import Test.Util.Orphans.Arbitrary ()
5858
import Test.Util.Serialisation.Roundtrip (Coherent (..),
59-
SomeResult (..), WithVersion (..))
59+
WithVersion (..))
60+
import Test.Util.Serialisation.SomeResult (SomeResult (..))
6061

6162
{-------------------------------------------------------------------------------
6263
Generators

ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/Consensus/Cardano/Examples.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ import Ouroboros.Consensus.TypeFamilyWrappers
4949
import Ouroboros.Network.Block (Serialised (..))
5050
import qualified Test.Consensus.Byron.Examples as Byron
5151
import qualified Test.Consensus.Shelley.Examples as Shelley
52-
import qualified Test.Util.Serialisation.Golden as Golden
53-
import Test.Util.Serialisation.Golden (Examples, Labelled, labelled)
54-
import Test.Util.Serialisation.Roundtrip (SomeResult (..))
52+
import Test.Util.Serialisation.Examples (Examples (..), Labelled,
53+
labelled, prefixExamples)
54+
import Test.Util.Serialisation.SomeResult (SomeResult (..))
5555

5656
type Crypto = StandardCrypto
5757

@@ -91,7 +91,7 @@ combineEras = mconcat . hcollapse . hap eraInjections
9191
-> Examples blk
9292
-> Examples (CardanoBlock Crypto)
9393
injExamples eraName idx =
94-
Golden.prefixExamples eraName
94+
prefixExamples eraName
9595
. inject exampleStartBounds idx
9696

9797
{-------------------------------------------------------------------------------
@@ -108,7 +108,7 @@ instance Inject SomeResult where
108108
SomeResult (QueryIfCurrent (injectQuery idx q)) (Right r)
109109

110110
instance Inject Examples where
111-
inject startBounds (idx :: Index xs x) Golden.Examples {..} = Golden.Examples {
111+
inject startBounds (idx :: Index xs x) Examples {..} = Examples {
112112
exampleBlock = inj (Proxy @I) exampleBlock
113113
, exampleSerialisedBlock = inj (Proxy @Serialised) exampleSerialisedBlock
114114
, exampleHeader = inj (Proxy @Header) exampleHeader
@@ -240,7 +240,7 @@ summary =
240240
(State.TransitionKnown shelleyTransitionEpoch)
241241
(hardForkLedgerStatePerEra (ledgerStateByron byronLedger))
242242
where
243-
(_, byronLedger) = head $ Golden.exampleLedgerState Byron.examples
243+
(_, byronLedger) = head $ exampleLedgerState Byron.examples
244244

245245
eraInfoByron :: SingleEraInfo ByronBlock
246246
eraInfoByron = singleEraInfo (Proxy @ByronBlock)
@@ -277,16 +277,16 @@ ledgerStateByron stByron =
277277
-- | Multi-era examples, e.g., applying a transaction to the wrong era.
278278
multiEraExamples :: Examples (CardanoBlock Crypto)
279279
multiEraExamples = mempty {
280-
Golden.exampleApplyTxErr = labelled [
280+
exampleApplyTxErr = labelled [
281281
("WrongEraByron", exampleApplyTxErrWrongEraByron)
282282
, ("WrongEraShelley", exampleApplyTxErrWrongEraShelley)
283283
]
284-
, Golden.exampleQuery = labelled [
284+
, exampleQuery = labelled [
285285
("AnytimeByron", exampleQueryAnytimeByron)
286286
, ("AnytimeShelley", exampleQueryAnytimeShelley)
287287
, ("HardFork", exampleQueryHardFork)
288288
]
289-
, Golden.exampleResult = labelled [
289+
, exampleResult = labelled [
290290
("EraMismatchByron", exampleResultEraMismatchByron)
291291
, ("EraMismatchShelley", exampleResultEraMismatchShelley)
292292
, ("AnytimeByron", exampleResultAnytimeByron)

ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/Consensus/Shelley/Examples.hs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ import Test.Cardano.Ledger.Shelley.Examples.Consensus
5959
(ShelleyLedgerExamples (..), ShelleyResultExamples (..),
6060
ledgerExamplesShelley, testShelleyGenesis)
6161
import Test.Util.Orphans.Arbitrary ()
62-
import qualified Test.Util.Serialisation.Golden as Golden
63-
import Test.Util.Serialisation.Golden (labelled, unlabelled)
64-
import Test.Util.Serialisation.Roundtrip (SomeResult (..))
62+
import Test.Util.Serialisation.Examples (Examples (..), labelled,
63+
unlabelled)
64+
import Test.Util.Serialisation.SomeResult (SomeResult (..))
6565

6666

6767
{-------------------------------------------------------------------------------
@@ -74,11 +74,11 @@ codecConfig = ShelleyCodecConfig
7474
fromShelleyLedgerExamples
7575
:: ShelleyCompatible (TPraos (EraCrypto era)) era
7676
=> ShelleyLedgerExamples era
77-
-> Golden.Examples (ShelleyBlock (TPraos (EraCrypto era)) era)
77+
-> Examples (ShelleyBlock (TPraos (EraCrypto era)) era)
7878
fromShelleyLedgerExamples ShelleyLedgerExamples {
7979
sleResultExamples = ShelleyResultExamples{..}
8080
, ..} =
81-
Golden.Examples {
81+
Examples {
8282
exampleBlock = unlabelled blk
8383
, exampleSerialisedBlock = unlabelled serialisedBlock
8484
, exampleHeader = unlabelled $ getHeader blk
@@ -148,11 +148,11 @@ fromShelleyLedgerExamplesPraos ::
148148
TranslateProto (TPraos (EraCrypto era)) (Praos (EraCrypto era))
149149
)
150150
=> ShelleyLedgerExamples era
151-
-> Golden.Examples (ShelleyBlock (Praos (EraCrypto era)) era)
151+
-> Examples (ShelleyBlock (Praos (EraCrypto era)) era)
152152
fromShelleyLedgerExamplesPraos ShelleyLedgerExamples {
153153
sleResultExamples = ShelleyResultExamples{..}
154154
, ..} =
155-
Golden.Examples {
155+
Examples {
156156
exampleBlock = unlabelled blk
157157
, exampleSerialisedBlock = unlabelled serialisedBlock
158158
, exampleHeader = unlabelled $ getHeader blk
@@ -237,20 +237,20 @@ fromShelleyLedgerExamplesPraos ShelleyLedgerExamples {
237237

238238

239239

240-
examplesShelley :: Golden.Examples StandardShelleyBlock
240+
examplesShelley :: Examples StandardShelleyBlock
241241
examplesShelley = fromShelleyLedgerExamples ledgerExamplesShelley
242242

243-
examplesAllegra :: Golden.Examples StandardAllegraBlock
243+
examplesAllegra :: Examples StandardAllegraBlock
244244
examplesAllegra = fromShelleyLedgerExamples ledgerExamplesAllegra
245245

246-
examplesMary :: Golden.Examples StandardMaryBlock
246+
examplesMary :: Examples StandardMaryBlock
247247
examplesMary = fromShelleyLedgerExamples ledgerExamplesMary
248248

249-
examplesAlonzo :: Golden.Examples StandardAlonzoBlock
249+
examplesAlonzo :: Examples StandardAlonzoBlock
250250
examplesAlonzo = fromShelleyLedgerExamples ledgerExamplesAlonzo
251251

252-
examplesBabbage :: Golden.Examples StandardBabbageBlock
252+
examplesBabbage :: Examples StandardBabbageBlock
253253
examplesBabbage = fromShelleyLedgerExamplesPraos ledgerExamplesBabbage
254254

255-
examplesConway :: Golden.Examples StandardConwayBlock
255+
examplesConway :: Examples StandardConwayBlock
256256
examplesConway = fromShelleyLedgerExamplesPraos ledgerExamplesConway

ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/Consensus/Shelley/Generators.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ import Test.Consensus.Shelley.MockCrypto (CanMock)
5050
import Test.QuickCheck hiding (Result)
5151
import Test.Util.Orphans.Arbitrary ()
5252
import Test.Util.Serialisation.Roundtrip (Coherent (..),
53-
SomeResult (..), WithVersion (..))
53+
WithVersion (..))
54+
import Test.Util.Serialisation.SomeResult (SomeResult (..))
5455

5556
{-------------------------------------------------------------------------------
5657
Generators

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

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import Test.Tasty
4848
import Test.Tasty.QuickCheck
4949
import Test.Util.Orphans.Arbitrary ()
5050
import Test.Util.Serialisation.Roundtrip
51+
import Test.Util.Serialisation.SomeResult (SomeResult (..))
5152

5253
tests :: TestTree
5354
tests = adjustOption reduceTests $

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE EmptyCase #-}
2+
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE GADTs #-}
34
{-# LANGUAGE LambdaCase #-}
45
{-# LANGUAGE NamedFieldPuns #-}
@@ -19,16 +20,18 @@ import Ouroboros.Consensus.Shelley.Node ()
1920
import Ouroboros.Consensus.Storage.Serialisation
2021
import Ouroboros.Consensus.Util (Dict (..))
2122
import Ouroboros.Network.Block (Serialised (..))
23+
import qualified Test.Consensus.Cardano.Examples as Cardano.Examples
2224
import Test.Consensus.Cardano.Generators (epochSlots)
2325
import Test.Consensus.Cardano.MockCrypto (MockCryptoCompatByron)
2426
import Test.Tasty
25-
import Test.Tasty.QuickCheck
27+
import Test.Tasty.QuickCheck (Property, testProperty, (===))
2628
import Test.Util.Orphans.Arbitrary ()
2729
import Test.Util.Serialisation.Roundtrip
2830

2931
tests :: TestTree
3032
tests = testGroup "Cardano"
31-
[ roundtrip_all testCodecCfg dictNestedHdr
33+
[ testGroup "Examples roundtrip" $ examplesRoundtrip Cardano.Examples.codecConfig Cardano.Examples.examples
34+
, roundtrip_all testCodecCfg dictNestedHdr
3235
, testProperty "BinaryBlockInfo sanity check" prop_CardanoBinaryBlockInfo
3336
]
3437

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

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

0 commit comments

Comments
 (0)