Skip to content

Commit 5bcd62e

Browse files
authored
Merge pull request #944 from IntersectMBO/roundtrip-cbor-encoding
Add roundtrip tests for `SerialiseAsRawBytes` for `SignedTx` and `UnsignedTx`
2 parents 84635ee + 9bf4a06 commit 5bcd62e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

cardano-api/src/Cardano/Api/Experimental/Tx.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ instance
209209
:: Ledger.DecoderError -> SerialiseAsRawBytesError
210210
wrapError = SerialiseAsRawBytesError . displayException
211211

212+
deriving instance Eq (UnsignedTx era)
213+
212214
deriving instance Show (UnsignedTx era)
213215

214216
newtype UnsignedTxError
@@ -338,6 +340,8 @@ makeKeyWitness era (UnsignedTx unsignedTx) wsk =
338340
data SignedTx era
339341
= L.EraTx (LedgerEra era) => SignedTx (Ledger.Tx (LedgerEra era))
340342

343+
deriving instance Eq (SignedTx era)
344+
341345
deriving instance Show (SignedTx era)
342346

343347
instance HasTypeProxy era => HasTypeProxy (SignedTx era) where

cardano-api/test/cardano-api-test/Test/Cardano/Api/Experimental.hs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ where
1111

1212
import Cardano.Api qualified as Api
1313
import Cardano.Api.Experimental qualified as Exp
14+
import Cardano.Api.Experimental.Era (convert)
1415
import Cardano.Api.Genesis qualified as Genesis
1516
import Cardano.Api.Ledger qualified as Ledger
1617
import Cardano.Api.Plutus qualified as Script
@@ -23,15 +24,21 @@ import Cardano.Slotting.Slot qualified as Slotting
2324
import Cardano.Slotting.Time qualified as Slotting
2425

2526
import Control.Monad.Identity (Identity)
27+
import Data.Bifunctor (first)
2628
import Data.Maybe (fromMaybe)
2729
import Data.Ratio ((%))
30+
import Data.Text.Encoding qualified as Text
2831
import Data.Time qualified as Time
2932
import Data.Time.Clock.POSIX qualified as Time
3033
import Lens.Micro ((&))
3134

32-
import Hedgehog (Property)
35+
import Test.Gen.Cardano.Api.Typed (genTx)
36+
37+
import Hedgehog (Gen, Property)
3338
import Hedgehog qualified as H
3439
import Hedgehog.Extras qualified as H
40+
import Hedgehog.Gen qualified as Gen
41+
import Hedgehog.Internal.Property qualified as H
3542
import Test.Tasty (TestTree, testGroup)
3643
import 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

5770
prop_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

Comments
 (0)