1111
1212import Cardano.Api qualified as Api
1313import Cardano.Api.Experimental qualified as Exp
14+ import Cardano.Api.Experimental.Era (convert )
1415import Cardano.Api.Genesis qualified as Genesis
1516import Cardano.Api.Ledger qualified as Ledger
1617import Cardano.Api.Plutus qualified as Script
@@ -23,15 +24,21 @@ import Cardano.Slotting.Slot qualified as Slotting
2324import Cardano.Slotting.Time qualified as Slotting
2425
2526import Control.Monad.Identity (Identity )
27+ import Data.Bifunctor (first )
2628import Data.Maybe (fromMaybe )
2729import Data.Ratio ((%) )
30+ import Data.Text.Encoding qualified as Text
2831import Data.Time qualified as Time
2932import Data.Time.Clock.POSIX qualified as Time
3033import Lens.Micro ((&) )
3134
32- import Hedgehog (Property )
35+ import Test.Gen.Cardano.Api.Typed (genTx )
36+
37+ import Hedgehog (Gen , Property )
3338import Hedgehog qualified as H
3439import Hedgehog.Extras qualified as H
40+ import Hedgehog.Gen qualified as Gen
41+ import Hedgehog.Internal.Property qualified as H
3542import Test.Tasty (TestTree , testGroup )
3643import Test.Tasty.Hedgehog (testProperty )
3744
@@ -52,6 +59,12 @@ tests =
5259 , testProperty
5360 " Check two methods of balancing transaction are equivalent"
5461 prop_balance_transaction_two_ways
62+ , testProperty
63+ " Roundtrip SerialiseAsRawBytes UnsignedTx"
64+ prop_roundtrip_serialise_as_raw_bytes_unsigned_tx
65+ , testProperty
66+ " Roundtrip SerialiseAsRawBytes SignedTx"
67+ prop_roundtrip_serialise_as_raw_bytes_signed_tx
5568 ]
5669
5770prop_created_transaction_with_both_apis_are_the_same :: Property
@@ -280,3 +293,38 @@ exampleSigningKey =
280293 H. evalEither $
281294 Api. deserialiseFromBech32
282295 " addr_sk1648253w4tf6fv5fk28dc7crsjsaw7d9ymhztd4favg3cwkhz7x8sl5u3ms"
296+
297+ expEraGen :: Gen (Exp. Some Exp. Era )
298+ expEraGen =
299+ let eras :: [Exp. Some Exp. Era ] = [minBound .. maxBound ]
300+ in Gen. element eras
301+
302+ expTxForEraGen :: Exp. Era era -> Gen (Ledger. Tx (Exp. LedgerEra era ))
303+ expTxForEraGen era = do
304+ Exp. obtainCommonConstraints era $ do
305+ ShelleyTx _ tx <- genTx (convert era)
306+ return tx
307+
308+ prop_roundtrip_serialise_as_raw_bytes_unsigned_tx :: Property
309+ prop_roundtrip_serialise_as_raw_bytes_unsigned_tx = H. withTests (H. TestLimit 20 ) $ H. property $ do
310+ Exp. Some era <- H. forAll expEraGen
311+ Exp. obtainCommonConstraints era $ do
312+ tx <- H. forAll $ expTxForEraGen era
313+ let signedTx = Exp. UnsignedTx tx
314+ signedTx H. === signedTx
315+ H. tripping
316+ signedTx
317+ (Text. decodeUtf8 . Api. serialiseToRawBytesHex)
318+ (first show . Api. deserialiseFromRawBytesHex . Text. encodeUtf8)
319+
320+ prop_roundtrip_serialise_as_raw_bytes_signed_tx :: Property
321+ prop_roundtrip_serialise_as_raw_bytes_signed_tx = H. withTests (H. TestLimit 20 ) $ H. property $ do
322+ Exp. Some era <- H. forAll expEraGen
323+ Exp. obtainCommonConstraints era $ do
324+ tx <- H. forAll $ expTxForEraGen era
325+ let signedTx = Exp. SignedTx tx
326+ signedTx H. === signedTx
327+ H. tripping
328+ signedTx
329+ (Text. decodeUtf8 . Api. serialiseToRawBytesHex)
330+ (first show . Api. deserialiseFromRawBytesHex . Text. encodeUtf8)
0 commit comments