diff --git a/shelley-ma/impl/src/Cardano/Ledger/Mary/Value.hs b/shelley-ma/impl/src/Cardano/Ledger/Mary/Value.hs index a1ef024f6eb..57482561a6d 100644 --- a/shelley-ma/impl/src/Cardano/Ledger/Mary/Value.hs +++ b/shelley-ma/impl/src/Cardano/Ledger/Mary/Value.hs @@ -182,11 +182,13 @@ decodeValue = do tt <- peekTokenType case tt of TypeUInt -> inject . Coin <$> decodeInteger + TypeUInt64 -> inject . Coin <$> decodeInteger TypeNInt -> inject . Coin <$> decodeInteger + TypeNInt64 -> inject . Coin <$> decodeInteger TypeListLen -> decodeValuePair decodeInteger TypeListLen64 -> decodeValuePair decodeInteger TypeListLenIndef -> decodeValuePair decodeInteger - _ -> fail $ "Value: expected array or int" + _ -> fail $ "Value: expected array or int, got " ++ show tt decodeValuePair :: ( Typeable (Core.Script era), @@ -229,10 +231,11 @@ decodeNonNegativeValue = do tt <- peekTokenType case tt of TypeUInt -> inject . Coin <$> decodeNonNegativeInteger + TypeUInt64 -> inject . Coin <$> decodeNonNegativeInteger TypeListLen -> decodeValuePair decodeNonNegativeInteger TypeListLen64 -> decodeValuePair decodeNonNegativeInteger TypeListLenIndef -> decodeValuePair decodeNonNegativeInteger - _ -> fail $ "Value: expected array or int" + _ -> fail $ "Value: expected array or int, got " ++ show tt instance (Era era, Typeable (Core.Script era)) => @@ -409,7 +412,10 @@ prune assets = -- | Rather than using prune to remove 0 assets, when can avoid adding them in the -- first place by using valueFromList to construct a Value. valueFromList :: Integer -> [(PolicyID era, AssetName, Integer)] -> Value era -valueFromList ada triples = foldr (\(p, n, i) ans -> insert (+) p n i ans) (Value ada Map.empty) triples +valueFromList ada = + foldr + (\(p, n, i) ans -> insert (+) p n i ans) + (Value ada Map.empty) -- | Display a Value as a String, one token per line showValue :: Value era -> String diff --git a/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/Serialisation/Generators.hs b/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/Serialisation/Generators.hs index c337441100f..c6b61bb82a5 100644 --- a/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/Serialisation/Generators.hs +++ b/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/Serialisation/Generators.hs @@ -15,11 +15,13 @@ {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} +{-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -Wno-orphans #-} module Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators ( sizedTimelock, maxTimelockDepth, + genMintValues, ) where @@ -30,7 +32,11 @@ import Cardano.Ledger.Allegra (AllegraEra) import Cardano.Ledger.Era (Era (..)) import Cardano.Ledger.Mary (MaryEra) import qualified Cardano.Ledger.Mary.Value as ConcreteValue -import qualified Cardano.Ledger.Mary.Value as Mary (AssetName (..), PolicyID (..), Value (..)) +import qualified Cardano.Ledger.Mary.Value as Mary + ( AssetName (..), + PolicyID (..), + Value (..), + ) import Cardano.Ledger.ShelleyMA (ShelleyMAEra) import qualified Cardano.Ledger.ShelleyMA.Metadata as MA import qualified Cardano.Ledger.ShelleyMA.Rules.Utxo as MA.STS @@ -38,8 +44,11 @@ import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..), ValidityInterval (..)) import qualified Cardano.Ledger.ShelleyMA.Timelocks as MA (Timelock (..)) import qualified Cardano.Ledger.ShelleyMA.TxBody as MA (TxBody (..)) import Data.Coerce (coerce) -import Data.Sequence.Strict (StrictSeq,fromList) +import Data.Int (Int64) +import qualified Data.Map.Strict as Map +import Data.Sequence.Strict (StrictSeq, fromList) import Data.Typeable (Typeable) +import Data.Word (Word64) import Generic.Random (genericArbitraryU) import Shelley.Spec.Ledger.API hiding (SignedDSIGN, TxBody (..)) import Test.QuickCheck @@ -48,10 +57,10 @@ import Test.QuickCheck choose, genericShrink, listOf, - vectorOf, oneof, resize, shrink, + vectorOf, ) import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock) import Test.Shelley.Spec.Ledger.Generator.MetaData (genMetaData') @@ -59,7 +68,6 @@ import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators () import Test.Shelley.Spec.Ledger.Serialisation.Generators () import Test.Tasty.QuickCheck (Gen) - {------------------------------------------------------------------------------- ShelleyMAEra Generators Generators used for roundtrip tests, generated values are not @@ -83,8 +91,18 @@ sizedTimelock 0 = (MA.RequireSignature . KeyHash . mkDummyHash) <$> arbitrary sizedTimelock n = oneof [ (MA.RequireSignature . KeyHash . mkDummyHash) <$> arbitrary, - MA.RequireAllOf <$> (fromList <$> resize maxTimelockListLens (listOf (sizedTimelock (n -1)))), - MA.RequireAnyOf <$> (fromList <$> resize maxTimelockListLens (listOf (sizedTimelock (n -1)))), + MA.RequireAllOf + <$> ( fromList + <$> resize + maxTimelockListLens + (listOf (sizedTimelock (n -1))) + ), + MA.RequireAnyOf + <$> ( fromList + <$> resize + maxTimelockListLens + (listOf (sizedTimelock (n -1))) + ), do subs <- resize maxTimelockListLens (listOf (sizedTimelock (n -1))) let i = length subs @@ -112,15 +130,16 @@ instance arbitrary = genMetaData' >>= \case MetaData m -> - do ss <- genScriptSeq ; pure (MA.Metadata m ss) + do ss <- genScriptSeq; pure (MA.Metadata m ss) -genScriptSeq :: (Arbitrary (Timelock (ShelleyMAEra ma c))) => Gen(StrictSeq (Timelock (ShelleyMAEra ma c))) +genScriptSeq :: + (Arbitrary (Timelock (ShelleyMAEra ma c))) => + Gen (StrictSeq (Timelock (ShelleyMAEra ma c))) genScriptSeq = do - n <- choose (0,3) + n <- choose (0, 3) l <- vectorOf n arbitrary pure (fromList l) - {------------------------------------------------------------------------------- MaryEra Generators -------------------------------------------------------------------------------} @@ -145,12 +164,43 @@ instance Mock c => Arbitrary (Mary.PolicyID (MaryEra c)) where arbitrary = Mary.PolicyID <$> arbitrary instance Mock c => Arbitrary (Mary.Value (MaryEra c)) where - arbitrary = Mary.Value <$> (abs <$> arbitrary) <*> (ConcreteValue.prune . pointwiseAbs <$> arbitrary) - where - pointwiseAbs = fmap (fmap abs) - -genMintValues :: Mock c => Gen (Mary.Value (MaryEra c)) -genMintValues = Mary.Value 0 . ConcreteValue.prune <$> arbitrary + arbitrary = valueFromListBounded @Word64 <$> arbitrary <*> arbitrary + + shrink (Mary.Value ada assets) = + concat + [ -- Shrink the ADA value + flip Mary.Value assets <$> shrink ada, + -- Shrink the non-ADA assets by reducing the list length + Mary.Value + ada + <$> shrink assets + ] + +-- | When generating values for the mint field, we do two things: +-- +-- - Fix the ADA value to 0 +-- - Allow both positive and negative quantities +genMintValues :: forall c. Mock c => Gen (Mary.Value (MaryEra c)) +genMintValues = valueFromListBounded @Int64 0 <$> arbitrary + +-- | Variant on @valueFromList@ that makes sure that generated values stay +-- bounded within the range of a given integral type. +valueFromListBounded :: + forall i era. + (Bounded i, Integral i) => + i -> + [(Mary.PolicyID era, Mary.AssetName, i)] -> + Mary.Value era +valueFromListBounded (fromIntegral -> ada) = + foldr + (\(p, n, fromIntegral -> i) ans -> ConcreteValue.insert comb p n i ans) + (Mary.Value ada Map.empty) + where + comb :: Integer -> Integer -> Integer + comb a b = + max + (fromIntegral $ minBound @i) + (min (fromIntegral $ maxBound @i) (a + b)) instance Arbitrary Mary.AssetName where arbitrary = Mary.AssetName <$> arbitrary diff --git a/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/Serialisation/Roundtrip.hs b/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/Serialisation/Roundtrip.hs index bd0073dc757..d91ccefe2bb 100644 --- a/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/Serialisation/Roundtrip.hs +++ b/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/Serialisation/Roundtrip.hs @@ -4,6 +4,7 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} @@ -15,28 +16,34 @@ {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} -{-# LANGUAGE GADTs #-} {-# OPTIONS_GHC -Wno-orphans #-} -module Test.Cardano.Ledger.ShelleyMA.Serialisation.Roundtrip - where +module Test.Cardano.Ledger.ShelleyMA.Serialisation.Roundtrip where -import Data.String(fromString) -import Data.Sequence.Strict (StrictSeq,fromList) -import qualified Data.ByteString.Lazy as Lazy(null) -import Cardano.Binary( Annotator (..), FromCBOR, ToCBOR ) +import Cardano.Binary (Annotator (..), FromCBOR, ToCBOR) +import qualified Cardano.Ledger.Mary.Value as Mary + ( AssetName (..), + PolicyID (..), + Value (..), + valueFromList, + ) import qualified Cardano.Ledger.ShelleyMA.Metadata as MA -import qualified Cardano.Ledger.Mary.Value as Mary(Value(..),AssetName(..),PolicyID(..),valueFromList) -import Shelley.Spec.Ledger.Scripts(ScriptHash(..)) -import Shelley.Spec.Ledger.MetaData(MetaData(..)) -import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators() -- import Arbitrary instances -import Test.Shelley.Spec.Ledger.Generator.MetaData() -- import Arbitrary instances +import qualified Data.ByteString.Lazy as Lazy (null) +import Data.Sequence.Strict (StrictSeq, fromList) +import Data.String (fromString) +import Shelley.Spec.Ledger.MetaData (MetaData (..)) +import Shelley.Spec.Ledger.Scripts (ScriptHash (..)) +import Test.Cardano.Ledger.EraBuffet +import Test.Cardano.Ledger.ShelleyMA.Serialisation.Coders + ( roundTrip, + roundTripAnn, + ) +import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators () +import Test.Shelley.Spec.Ledger.Generator.MetaData () import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators (genHash) import Test.Shelley.Spec.Ledger.Serialisation.Generators () -import Test.Cardano.Ledger.ShelleyMA.Serialisation.Coders(roundTrip, roundTripAnn) -import Test.Cardano.Ledger.EraBuffet -import Test.Tasty.QuickCheck (Gen,arbitrary, choose, vectorOf, testProperty) -import Test.Tasty(TestTree, testGroup) +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.QuickCheck (Gen, arbitrary, choose, testProperty, vectorOf) -- ====================================================================== -- Witnesses to each Era @@ -45,34 +52,31 @@ data EraIndex index where Mary :: EraIndex (MaryEra StandardCrypto) Shelley :: EraIndex (ShelleyEra StandardCrypto) Allegra :: EraIndex (AllegraEra StandardCrypto) - -- Add new Era's here, like this: - -- Alonzo :: EraIndex (AlonzoEra StandardCrypto) instance Show (EraIndex e) where show Mary = "Mary Era" show Shelley = "Shelley Era" show Allegra = "Allegra Era" - -- Show Alonzo = "Alonzo Era" -- ============================================================ -- EraIndex parameterized generators for each type family -genTxBody :: EraIndex e -> Gen(TxBody e) +genTxBody :: EraIndex e -> Gen (TxBody e) genTxBody Shelley = arbitrary genTxBody Mary = arbitrary genTxBody Allegra = arbitrary -genScript :: EraIndex e -> Gen(Script e) +genScript :: EraIndex e -> Gen (Script e) genScript Shelley = arbitrary genScript Mary = arbitrary genScript Allegra = arbitrary -genValue :: EraIndex e -> Gen(Value e) +genValue :: EraIndex e -> Gen (Value e) genValue Shelley = arbitrary genValue Mary = genMaryValue Mary genValue Allegra = arbitrary -genMeta :: EraIndex e -> Gen(Metadata e) +genMeta :: EraIndex e -> Gen (Metadata e) genMeta Mary = do m <- arbitrary s <- genScriptSeq Mary @@ -81,62 +85,87 @@ genMeta Allegra = do m <- arbitrary s <- genScriptSeq Allegra pure (MA.Metadata m s) -genMeta Shelley = do - m <- arbitrary - pure (MetaData m) +genMeta Shelley = MetaData <$> arbitrary -- ========================================================== -- Parameterized helper functions for generating Mary style Values genAssetName :: Gen Mary.AssetName -genAssetName = (Mary.AssetName . fromString) <$> arbitrary +genAssetName = Mary.AssetName . fromString <$> arbitrary genPolicyID :: EraIndex e -> Gen (Mary.PolicyID e) genPolicyID index = Mary.PolicyID <$> genScriptHash index -genScriptHash :: EraIndex e -> Gen(ScriptHash e) +genScriptHash :: EraIndex e -> Gen (ScriptHash e) genScriptHash Shelley = ScriptHash <$> genHash genScriptHash Mary = ScriptHash <$> genHash genScriptHash Allegra = ScriptHash <$> genHash -genMaryValue :: EraIndex era -> Gen(Mary.Value era) +genMaryValue :: EraIndex era -> Gen (Mary.Value era) genMaryValue index = do - ada <- arbitrary - size <- choose (0,10) - triples <- vectorOf size (do { p <- genPolicyID index; n <- genAssetName; i <- choose (-3,50); pure(p,n,i)}) - pure $ Mary.valueFromList ada triples + ada <- arbitrary + size <- choose (0, 10) + triples <- + vectorOf + size + ( do + p <- genPolicyID index + n <- genAssetName + i <- choose (-3, 50) + pure (p, n, i) + ) + pure $ Mary.valueFromList ada triples -- ========================================================== -- Parameterized helper function for generating MA style Metadata -genScriptSeq :: EraIndex e -> Gen(StrictSeq (Script e)) +genScriptSeq :: EraIndex e -> Gen (StrictSeq (Script e)) genScriptSeq index = do - n <- choose (0,6) + n <- choose (0, 6) l <- vectorOf n (genScript index) pure (fromList l) -- ========================================================== -- EraIndex parameterized property tests -propertyAnn :: forall e t. (Eq t, Show t, ToCBOR t, FromCBOR(Annotator t)) => - String -> EraIndex e -> (EraIndex e -> Gen t) -> TestTree -propertyAnn name i gen = testProperty ("roundtripAnn "++name) $ do +propertyAnn :: + forall e t. + (Eq t, Show t, ToCBOR t, FromCBOR (Annotator t)) => + String -> + EraIndex e -> + (EraIndex e -> Gen t) -> + TestTree +propertyAnn name i gen = testProperty ("roundtripAnn " ++ name) $ do x <- gen i case roundTripAnn x of - Right(left,_) | not(Lazy.null left) -> error("unconsumed trailing bytes: "++show left) - Right(_,y) -> if (x==y) then pure True else error("Unequal\n "++show x++"\n "++show y) - Left s -> error (show s) - -property :: forall e t. (Eq t, Show t, ToCBOR t, FromCBOR t) => - String -> EraIndex e -> (EraIndex e -> Gen t) -> TestTree -property name i gen = testProperty ("roundtrip "++name) $ do + Right (left, _) + | not (Lazy.null left) -> + error ("unconsumed trailing bytes: " ++ show left) + Right (_, y) -> + if x == y + then pure True + else error ("Unequal\n " ++ show x ++ "\n " ++ show y) + Left s -> error (show (s, x)) + +property :: + forall e t. + (Eq t, Show t, ToCBOR t, FromCBOR t) => + String -> + EraIndex e -> + (EraIndex e -> Gen t) -> + TestTree +property name i gen = testProperty ("roundtrip " ++ name) $ do x <- gen i case roundTrip x of - Right(left,_) | not(Lazy.null left) -> error("unconsumed trailing bytes: "++show left) - Right(_,y) -> if (x==y) then pure True else error("Unequal\n "++show x++"\n "++show y) + Right (left, _) + | not (Lazy.null left) -> + error ("unconsumed trailing bytes: " ++ show left) + Right (_, y) -> + if x == y + then pure True + else error ("Unequal\n " ++ show x ++ "\n " ++ show y) Left s -> error (show s) - allprops :: ( ToCBOR (TxBody e), ToCBOR (Metadata e), @@ -154,15 +183,20 @@ allprops :: FromCBOR (Annotator (TxBody e)), FromCBOR (Annotator (Metadata e)), FromCBOR (Annotator (Script e)) - ) => EraIndex e -> TestTree -allprops index = testGroup (show index) - [ propertyAnn "TxBody" index genTxBody - , propertyAnn "Metadata" index genMeta - , property "Value" index genValue - , propertyAnn "Script" index genScript - ] + ) => + EraIndex e -> + TestTree +allprops index = + testGroup + (show index) + [ propertyAnn "TxBody" index genTxBody, + propertyAnn "Metadata" index genMeta, + property "Value" index genValue, + propertyAnn "Script" index genScript + ] allEraRoundtripTests :: TestTree allEraRoundtripTests = - testGroup "All Era Roundtrip Tests" - [ allprops Shelley, allprops Allegra, allprops Mary ] \ No newline at end of file + testGroup + "All Era Roundtrip Tests" + [allprops Shelley, allprops Allegra, allprops Mary] diff --git a/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/TxBody.hs b/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/TxBody.hs index f5a6246040a..0576fa59f08 100644 --- a/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/TxBody.hs +++ b/shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/TxBody.hs @@ -5,8 +5,9 @@ {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} +-- Arbitrary instances +{-# OPTIONS_GHC -fno-warn-orphans #-} -{-# OPTIONS_GHC -fno-warn-orphans #-} -- Arbitrary instances -- ========================= module Test.Cardano.Ledger.ShelleyMA.TxBody @@ -14,61 +15,65 @@ module Test.Cardano.Ledger.ShelleyMA.TxBody TestEra, genShelleyBody, genMaryBody, - genMaryTxBody, - oldStyleRoundTrip, - ) where + ) +where + +import Cardano.Binary (ToCBOR (..)) +import qualified Cardano.Ledger.Core as Core +-- Arbitrary instances +-- Arbitrary instances -import Cardano.Binary(ToCBOR(..)) +import Cardano.Ledger.Mary (MaryEra) import Cardano.Ledger.Mary.Value ( AssetName (..), PolicyID (..), Value (..), ) -import qualified Cardano.Ledger.Core as Core -import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval (..), Timelock (..)) +import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..), ValidityInterval (..)) +import Cardano.Ledger.ShelleyMA.TxBody + ( FamsFrom, + TxBodyRaw (..), + bodyFields, + initial, + txSparse, + ) import qualified Cardano.Ledger.ShelleyMA.TxBody as Mary import Cardano.Ledger.Val (Val (..)) import Cardano.Slotting.Slot (SlotNo (..)) -import Shelley.Spec.Ledger.Tx (hashScript) import Data.ByteString.Short (ShortByteString) import qualified Data.ByteString.Short as Short +import Data.Coders + ( Decode (..), + Density (..), + Wrapped (..), + decode, + encode, + ) import qualified Data.Map.Strict as Map import Data.MemoBytes (MemoBytes (Memo), roundTripMemo) -import Data.Sequence.Strict (StrictSeq, fromList) +import Data.Sequence.Strict (fromList) +import qualified Data.Sequence.Strict as StrictSeq import Data.Set (empty) import Data.String (fromString) import GHC.Records import Shelley.Spec.Ledger.BaseTypes (StrictMaybe (SJust, SNothing)) import Shelley.Spec.Ledger.Coin (Coin (..)) +import Shelley.Spec.Ledger.Tx (hashScript) import Shelley.Spec.Ledger.TxBody (Wdrl (..)) import qualified Shelley.Spec.Ledger.TxBody as Shelley -import Test.Tasty -import Test.Tasty.HUnit -import Test.Cardano.Ledger.EraBuffet(TestCrypto) -import Cardano.Ledger.ShelleyMA.TxBody - ( TxBodyRaw(..), - FamsFrom, - txSparse, - bodyFields, - initial, - ) -import Data.Coders - ( Wrapped(..), - Density(..), - encode, - Decode(..), - decode, - ) -import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators() -- Arbitrary instances -import Test.Shelley.Spec.Ledger.Serialisation.Generators() -- Arbitrary instances -import Test.Tasty.QuickCheck +import Test.Cardano.Ledger.EraBuffet (TestCrypto) import Test.Cardano.Ledger.ShelleyMA.Serialisation.Coders - ( roundTrip', + ( RoundTripResult, embedTrip', + roundTrip', roundTripAnn, - RoundTripResult ) -import Cardano.Ledger.Mary (MaryEra) +import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators (genMintValues) +import Test.Shelley.Spec.Ledger.Serialisation.Generators () +import Test.Tasty +import Test.Tasty.HUnit +import Test.Tasty.QuickCheck + -- ============================================================================================ -- make an example -- ============================================================================================ @@ -82,15 +87,12 @@ type TestEra = MaryEra TestCrypto -- ==================================================================================================== -- Make a TxBody to test with -eseq :: StrictSeq a -eseq = fromList [] - txM :: Mary.TxBody TestEra txM = Mary.TxBody empty - eseq - eseq + StrictSeq.empty + StrictSeq.empty (Wdrl Map.empty) (Coin 6) (ValidityInterval (SJust (SlotNo 3)) (SJust (SlotNo 42))) @@ -101,8 +103,8 @@ txM = testmint :: Value TestEra testmint = Value 0 (Map.singleton policyId (Map.singleton aname 2)) where - policyId = PolicyID . hashScript . RequireAnyOf $ fromList [] - aname = AssetName $ fromString "asset name" + policyId = PolicyID . hashScript . RequireAnyOf $ fromList [] + aname = AssetName $ fromString "asset name" bytes :: Mary.TxBody era -> ShortByteString bytes (Mary.TxBodyConstr (Memo _ b)) = b @@ -112,11 +114,23 @@ fieldTests = testGroup "getField tests" [ testCase "inputs" (assertEqual "inputs" (getField @"inputs" txM) empty), - testCase "outputs" (assertEqual "outputs" (getField @"outputs" txM) eseq), - testCase "certs" (assertEqual "certs" (getField @"certs" txM) eseq), + testCase + "outputs" + ( assertEqual + "outputs" + (getField @"outputs" txM) + StrictSeq.empty + ), + testCase "certs" (assertEqual "certs" (getField @"certs" txM) StrictSeq.empty), testCase "wdrls" (assertEqual "wdrls" (getField @"wdrls" txM) (Wdrl Map.empty)), testCase "txfree" (assertEqual "txfree" (getField @"txfee" txM) (Coin 6)), - testCase "vldt" (assertEqual "vldt" (getField @"vldt" txM) (ValidityInterval (SJust (SlotNo 3)) (SJust (SlotNo 42)))), + testCase + "vldt" + ( assertEqual + "vldt" + (getField @"vldt" txM) + (ValidityInterval (SJust (SlotNo 3)) (SJust (SlotNo 42))) + ), testCase "update" (assertEqual "update" (getField @"update" txM) SNothing), testCase "mdHash" (assertEqual "mdHash" (getField @"mdHash" txM) SNothing), testCase "mint" (assertEqual "mint" (getField @"mint" txM) testmint) @@ -134,44 +148,64 @@ roundtrip (Mary.TxBodyConstr memo) = checkSparse :: TxBodyRaw TestEra -> Bool checkSparse tx = case oldStyleRoundTrip tx of - Right("",_) -> True - Right(left,_) -> error ("left over input: "++show left) - Left s -> error (show s) + Right ("", _) -> True + Right (left, _) -> error ("left over input: " ++ show left) + Left s -> error (show s) embedTest :: Gen Bool embedTest = do shelleybody <- genShelleyBody case embedTrip' toCBOR (decode (getTxSparse @TestEra)) shelleybody of - Right("",_) -> pure True - Right(left,_) -> error ("left over input: "++show left) - Left s -> error (show s) + Right ("", _) -> pure True + Right (left, _) -> error ("left over input: " ++ show left) + Left s -> error (show s) -getTxSparse :: (Val (Core.Value era),FamsFrom era) => Decode ('Closed 'Dense) (TxBodyRaw era) -getTxSparse = SparseKeyed "TxBodyRaw" initial bodyFields [(0,"inputs"),(1,"outputs"),(2,"txfee")] +getTxSparse :: + (Val (Core.Value era), FamsFrom era) => + Decode ('Closed 'Dense) (TxBodyRaw era) +getTxSparse = + SparseKeyed + "TxBodyRaw" + initial + bodyFields + [(0, "inputs"), (1, "outputs"), (2, "txfee")] -oldStyleRoundTrip:: TxBodyRaw TestEra -> RoundTripResult (TxBodyRaw TestEra) -oldStyleRoundTrip x = roundTrip' (encode . txSparse) (decode getTxSparse) x +oldStyleRoundTrip :: TxBodyRaw TestEra -> RoundTripResult (TxBodyRaw TestEra) +oldStyleRoundTrip = roundTrip' (encode . txSparse) (decode getTxSparse) genShelleyBody :: Gen (Shelley.TxBody TestEra) -genShelleyBody = Shelley.TxBody <$> arbitrary <*> pure eseq <*> arbitrary <*> arbitrary - <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary +genShelleyBody = + Shelley.TxBody + <$> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary genMaryBody :: Gen (TxBodyRaw TestEra) -genMaryBody = TxBodyRaw <$> arbitrary <*> pure eseq <*> arbitrary <*> arbitrary - <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary +genMaryBody = + TxBodyRaw + <$> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> genMintValues instance Arbitrary (TxBodyRaw TestEra) where arbitrary = genMaryBody -genMaryTxBody :: Gen (Mary.TxBody TestEra) -genMaryTxBody = Mary.TxBody <$> arbitrary <*> pure eseq <*> arbitrary <*> arbitrary - <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary - checkSparseAnn :: Mary.TxBody TestEra -> Bool checkSparseAnn tx = case roundTripAnn tx of - Right("",_) -> True - Right(left,_) -> error ("left over input: "++show left) - Left s -> error (show s) + Right ("", _) -> True + Right (left, _) -> error ("left over input: " ++ show left) + Left s -> error (show s) -- ====================================================== @@ -179,10 +213,10 @@ txBodyTest :: TestTree txBodyTest = testGroup "TxBody" - [ fieldTests - , testCase "length" (assertEqual "length" 36 (Short.length (bytes txM))) - , testCase "roundtrip txM" (roundtrip txM) - , testProperty "roundtrip sparse TxBodyRaw" checkSparse - , testProperty "embed Shelley sparse TxBodyRaw" embedTest - , testProperty "routrip sparse TxBody" checkSparseAnn + [ fieldTests, + testCase "length" (assertEqual "length" 36 (Short.length (bytes txM))), + testCase "roundtrip txM" (roundtrip txM), + testProperty "roundtrip sparse TxBodyRaw" checkSparse, + testProperty "embed Shelley sparse TxBodyRaw" embedTest, + testProperty "roundtrip sparse TxBody" checkSparseAnn ] diff --git a/shelley/chain-and-ledger/shelley-spec-ledger-test/src/Test/Shelley/Spec/Ledger/Serialisation/EraIndepGenerators.hs b/shelley/chain-and-ledger/shelley-spec-ledger-test/src/Test/Shelley/Spec/Ledger/Serialisation/EraIndepGenerators.hs index 9cfc82ee863..4948316f9d4 100644 --- a/shelley/chain-and-ledger/shelley-spec-ledger-test/src/Test/Shelley/Spec/Ledger/Serialisation/EraIndepGenerators.hs +++ b/shelley/chain-and-ledger/shelley-spec-ledger-test/src/Test/Shelley/Spec/Ledger/Serialisation/EraIndepGenerators.hs @@ -304,6 +304,7 @@ instance Arbitrary STS.VotingPeriod where instance Arbitrary Coin where -- Cannot be negative even though it is an 'Integer' arbitrary = Coin <$> choose (0, 1000) + shrink (Coin i) = Coin <$> shrink i instance Arbitrary DeltaCoin where arbitrary = DeltaCoin <$> choose (-1000, 1000)