@@ -28,39 +28,27 @@ module Test.Util.Serialisation.Roundtrip (
28
28
, roundtrip_SerialiseNodeToNode
29
29
, roundtrip_all
30
30
, roundtrip_envelopes
31
- -- * Roundtrip tests for 'Example's
32
- , examplesRoundtrip
33
31
) where
34
32
35
33
import Codec.CBOR.Decoding (Decoder )
36
34
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 )
39
36
import Codec.CBOR.Write (toLazyByteString )
40
- import Codec.Serialise (decode , encode )
41
- import Control.Arrow (left )
42
- import Control.Monad (unless )
43
37
import qualified Data.ByteString.Base16.Lazy as Base16
44
38
import qualified Data.ByteString.Lazy as Lazy
45
39
import qualified Data.ByteString.Lazy.Char8 as Char8
46
40
import qualified Data.ByteString.Short as Short
47
41
import Data.Function (on )
48
- import Data.Maybe (fromMaybe )
49
- import qualified Data.Text.Lazy as T
50
42
import Data.Typeable
51
43
import GHC.Generics (Generic )
52
44
import Ouroboros.Consensus.Block
53
45
import Ouroboros.Consensus.HeaderValidation (AnnTip )
54
46
import Ouroboros.Consensus.Ledger.Abstract (LedgerState )
55
- import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState ,
56
- decodeExtLedgerState , encodeExtLedgerState )
57
47
import Ouroboros.Consensus.Ledger.Query (BlockQuery , Query (.. ),
58
48
QueryVersion )
59
49
import qualified Ouroboros.Consensus.Ledger.Query as Query
60
50
import Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr , GenTx ,
61
51
GenTxId )
62
- import Ouroboros.Consensus.Ledger.SupportsProtocol
63
- (LedgerSupportsProtocol )
64
52
import Ouroboros.Consensus.Node.NetworkProtocolVersion
65
53
import Ouroboros.Consensus.Node.Run (SerialiseNodeToClientConstraints ,
66
54
SerialiseNodeToNodeConstraints (.. ))
@@ -73,12 +61,9 @@ import Ouroboros.Network.Block (Serialised (..), fromSerialised,
73
61
mkSerialised )
74
62
import Quiet (Quiet (.. ))
75
63
import Test.Tasty
76
- import Test.Tasty.ExpectedFailure (expectFailBecause )
77
64
import Test.Tasty.QuickCheck
78
65
import Test.Util.Orphans.Arbitrary ()
79
- import Test.Util.Serialisation.Examples (Examples (.. ), Labelled )
80
66
import Test.Util.Serialisation.SomeResult (SomeResult (.. ))
81
- import Text.Pretty.Simple (pShow )
82
67
83
68
{- -----------------------------------------------------------------------------
84
69
Basic test helpers
@@ -93,75 +78,31 @@ roundtrip enc dec = roundtrip' enc (const <$> dec)
93
78
94
79
-- | Roundtrip property for values annotated with their serialized form
95
80
--
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
- --
113
81
-- NOTE: Suppose @a@ consists of a pair of the unannotated value @a'@ and some
114
82
-- 'Lazy.ByteString'. The roundtrip property will fail if that
115
83
-- 'Lazy.ByteString' encoding is not equal to @enc a'@. One way in which this
116
84
-- might happen is if the annotation is not canonical CBOR, but @enc@ does
117
85
-- produce canonical CBOR.
118
- roundtrip' :: forall a .
119
- (Eq a , Show a )
86
+ roundtrip' :: (Eq a , Show a )
120
87
=> (a -> Encoding ) -- ^ @enc@
121
88
-> (forall s . Decoder s (Lazy. ByteString -> a ))
122
89
-> a
123
90
-> 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
137
100
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)
157
102
158
103
toBase16 :: Lazy. ByteString -> String
159
104
toBase16 = Char8. unpack . Base16. encode
160
105
161
- checkRoundtripResult :: Either String () -> Property
162
- checkRoundtripResult (Left str) = counterexample str False
163
- checkRoundtripResult (Right () ) = property ()
164
-
165
106
{- -----------------------------------------------------------------------------
166
107
Test skeleton
167
108
------------------------------------------------------------------------------}
@@ -651,57 +592,3 @@ decodeThroughSerialised
651
592
decodeThroughSerialised dec decSerialised = do
652
593
serialised <- decSerialised
653
594
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