diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types/Coin/Gen.hs b/lib/core/src/Cardano/Wallet/Primitive/Types/Coin/Gen.hs index 4b6098745e8..95721f67b5e 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types/Coin/Gen.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types/Coin/Gen.hs @@ -1,14 +1,10 @@ -{-# LANGUAGE NumericUnderscores #-} - module Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinAny - , genCoinSmall - , genCoinSmallPositive - , genCoinLargePositive - , shrinkCoinAny - , shrinkCoinSmall - , shrinkCoinSmallPositive - , shrinkCoinLargePositive + ( genCoin + , genCoinPositive + , genCoinFullRange + , shrinkCoin + , shrinkCoinPositive + , shrinkCoinFullRange ) where import Prelude @@ -16,44 +12,49 @@ import Prelude import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) import Test.QuickCheck - ( Gen, choose, shrink ) - --------------------------------------------------------------------------------- --- Coins chosen from the full range available --------------------------------------------------------------------------------- - -genCoinAny :: Gen Coin -genCoinAny = Coin <$> choose (unCoin minBound, unCoin maxBound) - -shrinkCoinAny :: Coin -> [Coin] -shrinkCoinAny (Coin c) = Coin <$> shrink c + ( Gen, choose, frequency, shrink, sized ) -------------------------------------------------------------------------------- --- Coins chosen to be small and possibly zero +-- Coins chosen according to the size parameter. -------------------------------------------------------------------------------- -genCoinSmall :: Gen Coin -genCoinSmall = Coin <$> choose (0, 10) +genCoin :: Gen Coin +genCoin = sized $ \n -> Coin . fromIntegral <$> choose (0, n) -shrinkCoinSmall :: Coin -> [Coin] -shrinkCoinSmall (Coin c) = Coin <$> shrink c +shrinkCoin :: Coin -> [Coin] +shrinkCoin (Coin c) = Coin <$> shrink c -------------------------------------------------------------------------------- --- Coins chosen to be small and strictly positive +-- Coins chosen according to the size parameter, but strictly positive. -------------------------------------------------------------------------------- -genCoinSmallPositive :: Gen Coin -genCoinSmallPositive = Coin <$> choose (1, 10) +genCoinPositive :: Gen Coin +genCoinPositive = sized $ \n -> Coin . fromIntegral <$> choose (1, max 1 n) -shrinkCoinSmallPositive :: Coin -> [Coin] -shrinkCoinSmallPositive (Coin c) = Coin <$> filter (> 0) (shrink c) +shrinkCoinPositive :: Coin -> [Coin] +shrinkCoinPositive (Coin c) = Coin <$> filter (> 0) (shrink c) -------------------------------------------------------------------------------- --- Coins chosen from a large range and strictly positive +-- Coins chosen from the full range available. -------------------------------------------------------------------------------- -genCoinLargePositive :: Gen Coin -genCoinLargePositive = Coin <$> choose (1, 1_000_000_000_000) +-- | Generates coins across the full range available. +-- +-- This generator has a slight bias towards the limits of the range, but +-- otherwise generates values uniformly across the whole range. +-- +-- This can be useful when testing roundtrip conversions between different +-- types. +-- +genCoinFullRange :: Gen Coin +genCoinFullRange = frequency + [ (1, pure (Coin 0)) + , (1, pure (maxBound :: Coin)) + , (8, Coin <$> choose (1, unCoin (maxBound :: Coin) - 1)) + ] -shrinkCoinLargePositive :: Coin -> [Coin] -shrinkCoinLargePositive (Coin c) = Coin <$> filter (> 0) (shrink c) +shrinkCoinFullRange :: Coin -> [Coin] +shrinkCoinFullRange = + -- Given that we may have a large value, we limit the number of results + -- returned in order to avoid processing long lists of shrunken values. + take 8 . shrinkCoin diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types/TokenBundle/Gen.hs b/lib/core/src/Cardano/Wallet/Primitive/Types/TokenBundle/Gen.hs index c53af75b297..423f4bfdb3a 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types/TokenBundle/Gen.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types/TokenBundle/Gen.hs @@ -9,13 +9,12 @@ module Cardano.Wallet.Primitive.Types.TokenBundle.Gen import Prelude -import Cardano.Wallet.Primitive.Types.Coin - ( Coin (..) ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinSmall - , genCoinSmallPositive - , shrinkCoinSmall - , shrinkCoinSmallPositive + ( genCoin + , genCoinFullRange + , genCoinPositive + , shrinkCoin + , shrinkCoinPositive ) import Cardano.Wallet.Primitive.Types.TokenBundle ( TokenBundle (..) ) @@ -43,17 +42,12 @@ import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle genFixedSizeTokenBundle :: Int -> Gen TokenBundle genFixedSizeTokenBundle fixedAssetCount = TokenBundle.fromFlatList - <$> genCoin + <$> genCoinFullRange <*> replicateM fixedAssetCount genAssetQuantity where genAssetQuantity = (,) <$> genAssetIdLargeRange <*> genTokenQuantity - genCoin = Coin <$> oneof - [ pure $ unCoin minBound - , pure $ unCoin maxBound - , choose (unCoin minBound + 1, unCoin maxBound - 1) - ] genTokenQuantity = integerToTokenQuantity <$> oneof [ pure $ tokenQuantityToInteger txOutMinTokenQuantity , pure $ tokenQuantityToInteger txOutMaxTokenQuantity @@ -85,22 +79,22 @@ genVariableSizedTokenBundle maxAssetCount = genTokenBundleSmallRange :: Gen TokenBundle genTokenBundleSmallRange = TokenBundle - <$> genCoinSmall + <$> genCoin <*> genTokenMapSmallRange shrinkTokenBundleSmallRange :: TokenBundle -> [TokenBundle] shrinkTokenBundleSmallRange (TokenBundle c m) = uncurry TokenBundle <$> shrinkInterleaved - (c, shrinkCoinSmall) + (c, shrinkCoin) (m, shrinkTokenMapSmallRange) genTokenBundleSmallRangePositive :: Gen TokenBundle genTokenBundleSmallRangePositive = TokenBundle - <$> genCoinSmallPositive + <$> genCoinPositive <*> genTokenMapSmallRange shrinkTokenBundleSmallRangePositive :: TokenBundle -> [TokenBundle] shrinkTokenBundleSmallRangePositive (TokenBundle c m) = uncurry TokenBundle <$> shrinkInterleaved - (c, shrinkCoinSmallPositive) + (c, shrinkCoinPositive) (m, shrinkTokenMapSmallRange) diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs b/lib/core/src/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs index 22f483aaf04..ad7c408e21c 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs @@ -3,12 +3,11 @@ {-# LANGUAGE TypeApplications #-} module Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetIdSized + ( genAssetId , genAssetIdLargeRange - , genAssetIdSmallRange , genTokenMapSized , genTokenMapSmallRange - , shrinkAssetIdSmallRange + , shrinkAssetId , shrinkTokenMapSmallRange , AssetIdF (..) ) where @@ -18,19 +17,17 @@ import Prelude import Cardano.Wallet.Primitive.Types.TokenMap ( AssetId (..), TokenMap ) import Cardano.Wallet.Primitive.Types.TokenPolicy.Gen - ( genTokenNameLargeRange - , genTokenNameSized - , genTokenNameSmallRange + ( genTokenName + , genTokenNameLargeRange + , genTokenPolicyId , genTokenPolicyIdLargeRange - , genTokenPolicyIdSized - , genTokenPolicyIdSmallRange - , shrinkTokenNameSmallRange - , shrinkTokenPolicyIdSmallRange - , tokenNamesMediumRange - , tokenPolicies + , shrinkTokenName + , shrinkTokenPolicyId + , testTokenNames + , testTokenPolicyIds ) import Cardano.Wallet.Primitive.Types.TokenQuantity.Gen - ( genTokenQuantitySized, genTokenQuantitySmall, shrinkTokenQuantitySmall ) + ( genTokenQuantity, shrinkTokenQuantity ) import Control.Monad ( replicateM ) import Data.List @@ -60,8 +57,8 @@ import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap -- Asset identifiers chosen from a range that depends on the size parameter -------------------------------------------------------------------------------- -genAssetIdSized :: Gen AssetId -genAssetIdSized = sized $ \size -> do +genAssetId :: Gen AssetId +genAssetId = sized $ \size -> do -- Ideally, we want to choose asset identifiers from a range that scales -- /linearly/ with the size parameter. -- @@ -75,22 +72,13 @@ genAssetIdSized = sized $ \size -> do -- let sizeSquareRoot = max 1 $ ceiling $ sqrt $ fromIntegral @Int @Double size AssetId - <$> resize sizeSquareRoot genTokenPolicyIdSized - <*> resize sizeSquareRoot genTokenNameSized + <$> resize sizeSquareRoot genTokenPolicyId + <*> resize sizeSquareRoot genTokenName --------------------------------------------------------------------------------- --- Asset identifiers chosen from a small range (to allow collisions) --------------------------------------------------------------------------------- - -genAssetIdSmallRange :: Gen AssetId -genAssetIdSmallRange = AssetId - <$> genTokenPolicyIdSmallRange - <*> genTokenNameSmallRange - -shrinkAssetIdSmallRange :: AssetId -> [AssetId] -shrinkAssetIdSmallRange (AssetId p t) = uncurry AssetId <$> shrinkInterleaved - (p, shrinkTokenPolicyIdSmallRange) - (t, shrinkTokenNameSmallRange) +shrinkAssetId :: AssetId -> [AssetId] +shrinkAssetId (AssetId p t) = uncurry AssetId <$> shrinkInterleaved + (p, shrinkTokenPolicyId) + (t, shrinkTokenName) -------------------------------------------------------------------------------- -- Asset identifiers chosen from a large range (to minimize collisions) @@ -112,8 +100,8 @@ genTokenMapSized = sized $ \size -> do TokenMap.fromFlatList <$> replicateM assetCount genAssetQuantity where genAssetQuantity = (,) - <$> genAssetIdSized - <*> genTokenQuantitySized + <$> genAssetId + <*> genTokenQuantity -------------------------------------------------------------------------------- -- Token maps with assets and quantities chosen from small ranges @@ -129,8 +117,8 @@ genTokenMapSmallRange = do TokenMap.fromFlatList <$> replicateM assetCount genAssetQuantity where genAssetQuantity = (,) - <$> genAssetIdSmallRange - <*> genTokenQuantitySmall + <$> genAssetId + <*> genTokenQuantity shrinkTokenMapSmallRange :: TokenMap -> [TokenMap] shrinkTokenMapSmallRange @@ -139,8 +127,8 @@ shrinkTokenMapSmallRange . TokenMap.toFlatList where shrinkAssetQuantity (a, q) = shrinkInterleaved - (a, shrinkAssetIdSmallRange) - (q, shrinkTokenQuantitySmall) + (a, shrinkAssetId) + (q, shrinkTokenQuantity) -------------------------------------------------------------------------------- -- Filtering functions @@ -154,6 +142,6 @@ instance Function AssetIdF where instance CoArbitrary AssetIdF where coarbitrary (AssetIdF AssetId{tokenName, tokenPolicyId}) genB = do - let n = fromMaybe 0 (elemIndex tokenName tokenNamesMediumRange) - let m = fromMaybe 0 (elemIndex tokenPolicyId tokenPolicies) + let n = fromMaybe 0 (elemIndex tokenName testTokenNames) + let m = fromMaybe 0 (elemIndex tokenPolicyId testTokenPolicyIds) variant (n+m) genB diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types/TokenPolicy/Gen.hs b/lib/core/src/Cardano/Wallet/Primitive/Types/TokenPolicy/Gen.hs index fd68d218925..7160a3fd018 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types/TokenPolicy/Gen.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types/TokenPolicy/Gen.hs @@ -1,17 +1,21 @@ module Cardano.Wallet.Primitive.Types.TokenPolicy.Gen - ( genTokenNameSized + ( + -- * Generators and shrinkers + genTokenName , genTokenNameLargeRange - , genTokenNameMediumRange - , genTokenNameSmallRange - , genTokenPolicyIdSized + , genTokenPolicyId , genTokenPolicyIdLargeRange - , genTokenPolicyIdSmallRange + , shrinkTokenName + , shrinkTokenPolicyId + + -- * Test values + , testTokenNames + , testTokenPolicyIds + + -- * Creation of test values + , mkTokenName , mkTokenPolicyId - , shrinkTokenNameSmallRange - , shrinkTokenPolicyIdSmallRange - , tokenNamesMediumRange - , tokenNamesSmallRange - , tokenPolicies + ) where import Prelude @@ -35,37 +39,15 @@ import qualified Data.Text as T -- Token names chosen from a range that depends on the size parameter -------------------------------------------------------------------------------- -genTokenNameSized :: Gen TokenName -genTokenNameSized = sized $ \size -> - elements $ UnsafeTokenName . B8.snoc "Token" <$> take size ['A' ..] - --------------------------------------------------------------------------------- --- Token names chosen from a small range (to allow collisions) --------------------------------------------------------------------------------- +genTokenName :: Gen TokenName +genTokenName = sized $ \n -> elements $ take (max 1 n) testTokenNames -genTokenNameSmallRange :: Gen TokenName -genTokenNameSmallRange = elements tokenNamesSmallRange - -shrinkTokenNameSmallRange :: TokenName -> [TokenName] -shrinkTokenNameSmallRange x - | x == simplest = [] +shrinkTokenName :: TokenName -> [TokenName] +shrinkTokenName i + | i == simplest = [] | otherwise = [simplest] where - simplest = head tokenNamesSmallRange - -tokenNamesSmallRange :: [TokenName] -tokenNamesSmallRange = UnsafeTokenName . B8.snoc "Token" <$> ['A' .. 'D'] - --------------------------------------------------------------------------------- --- Token names chosen from a medium-sized range (to minimize the risk of --- collisions) --------------------------------------------------------------------------------- - -genTokenNameMediumRange :: Gen TokenName -genTokenNameMediumRange = elements tokenNamesMediumRange - -tokenNamesMediumRange :: [TokenName] -tokenNamesMediumRange = UnsafeTokenName . B8.snoc "Token" <$> ['A' .. 'Z'] + simplest = head testTokenNames -------------------------------------------------------------------------------- -- Token names chosen from a large range (to minimize the risk of collisions) @@ -79,26 +61,15 @@ genTokenNameLargeRange = UnsafeTokenName . BS.pack <$> vector 32 -- parameter -------------------------------------------------------------------------------- -genTokenPolicyIdSized :: Gen TokenPolicyId -genTokenPolicyIdSized = sized $ \size -> - elements $ mkTokenPolicyId <$> take size mkTokenPolicyIdValidChars - --------------------------------------------------------------------------------- --- Token policy identifiers chosen from a small range (to allow collisions) --------------------------------------------------------------------------------- +genTokenPolicyId :: Gen TokenPolicyId +genTokenPolicyId = sized $ \n -> elements $ take (max 1 n) testTokenPolicyIds -genTokenPolicyIdSmallRange :: Gen TokenPolicyId -genTokenPolicyIdSmallRange = elements tokenPolicies - -shrinkTokenPolicyIdSmallRange :: TokenPolicyId -> [TokenPolicyId] -shrinkTokenPolicyIdSmallRange x - | x == simplest = [] +shrinkTokenPolicyId :: TokenPolicyId -> [TokenPolicyId] +shrinkTokenPolicyId i + | i == simplest = [] | otherwise = [simplest] where - simplest = head tokenPolicies - -tokenPolicies :: [TokenPolicyId] -tokenPolicies = mkTokenPolicyId <$> ['A' .. 'D'] + simplest = head testTokenPolicyIds -------------------------------------------------------------------------------- -- Token policy identifiers chosen from a large range (to minimize the risk of @@ -112,6 +83,15 @@ genTokenPolicyIdLargeRange = UnsafeTokenPolicyId . Hash . BS.pack <$> vector 28 -- Internal utilities -------------------------------------------------------------------------------- +testTokenNames :: [TokenName] +testTokenNames = mkTokenName <$> ['A' .. 'Z'] + +testTokenPolicyIds :: [TokenPolicyId] +testTokenPolicyIds = mkTokenPolicyId <$> mkTokenPolicyIdValidChars + +mkTokenName :: Char -> TokenName +mkTokenName = UnsafeTokenName . B8.snoc "Token" + -- The set of characters that can be passed to the 'mkTokenPolicyId' function. -- mkTokenPolicyIdValidChars :: [Char] diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types/TokenQuantity/Gen.hs b/lib/core/src/Cardano/Wallet/Primitive/Types/TokenQuantity/Gen.hs index 06d695a138a..f98604cc167 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types/TokenQuantity/Gen.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types/TokenQuantity/Gen.hs @@ -1,16 +1,12 @@ +{-# LANGUAGE TypeApplications #-} + module Cardano.Wallet.Primitive.Types.TokenQuantity.Gen - ( genTokenQuantitySized - , genTokenQuantitySmall - , genTokenQuantitySmallPositive - , genTokenQuantityLarge - , genTokenQuantityMassive - , genTokenQuantityMixed - , shrinkTokenQuantitySmall - , shrinkTokenQuantitySmallPositive - , shrinkTokenQuantityMixed - , tokenQuantitySmall - , tokenQuantityLarge - , tokenQuantityMassive + ( genTokenQuantity + , genTokenQuantityPositive + , genTokenQuantityFullRange + , shrinkTokenQuantity + , shrinkTokenQuantityPositive + , shrinkTokenQuantityFullRange ) where import Prelude @@ -19,99 +15,67 @@ import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) import Data.Word ( Word64 ) -import Numeric.Natural - ( Natural ) import Test.QuickCheck - ( Gen, choose, oneof, shrink, sized ) - --------------------------------------------------------------------------------- --- Token quantities chosen from a range that depends on the size parameter --------------------------------------------------------------------------------- - -genTokenQuantitySized :: Gen TokenQuantity -genTokenQuantitySized = sized $ \n -> - quantityFromInt <$> choose (0, n) - --------------------------------------------------------------------------------- --- Small token quantities --------------------------------------------------------------------------------- - -genTokenQuantitySmall :: Gen TokenQuantity -genTokenQuantitySmall = quantityFromInteger <$> oneof - [ pure 0 - , choose (1, quantityToInteger tokenQuantitySmall) - ] - -shrinkTokenQuantitySmall :: TokenQuantity -> [TokenQuantity] -shrinkTokenQuantitySmall = shrinkTokenQuantity - --------------------------------------------------------------------------------- --- Small strictly-positive token quantities --------------------------------------------------------------------------------- - -genTokenQuantitySmallPositive :: Gen TokenQuantity -genTokenQuantitySmallPositive = quantityFromInteger <$> - choose (1, quantityToInteger tokenQuantitySmall) - -shrinkTokenQuantitySmallPositive :: TokenQuantity -> [TokenQuantity] -shrinkTokenQuantitySmallPositive q = quantityFromInteger <$> - filter (> 0) (shrink $ quantityToInteger q) + ( Gen, choose, frequency, shrink, sized ) -------------------------------------------------------------------------------- --- Large token quantities +-- Token quantities chosen according to the size parameter. -------------------------------------------------------------------------------- -genTokenQuantityLarge :: Gen TokenQuantity -genTokenQuantityLarge = quantityFromInteger <$> choose - ( quantityToInteger tokenQuantitySmall + 1 - , quantityToInteger tokenQuantityLarge - ) - --------------------------------------------------------------------------------- --- Massive token quantities --------------------------------------------------------------------------------- +genTokenQuantity :: Gen TokenQuantity +genTokenQuantity = sized $ \n -> quantityFromInt <$> choose (0, n) -genTokenQuantityMassive :: Gen TokenQuantity -genTokenQuantityMassive = quantityFromInteger <$> choose - ( quantityToInteger tokenQuantityLarge + 1 - , quantityToInteger tokenQuantityMassive - ) +shrinkTokenQuantity :: TokenQuantity -> [TokenQuantity] +shrinkTokenQuantity = fmap quantityFromInteger . shrink . quantityToInteger -------------------------------------------------------------------------------- --- Mixed token quantities (both small and large) +-- Token quantities chosen according to the size parameter, but strictly +-- positive. -------------------------------------------------------------------------------- -genTokenQuantityMixed :: Gen TokenQuantity -genTokenQuantityMixed = oneof - [ genTokenQuantitySmall - , genTokenQuantityLarge - , genTokenQuantityMassive - ] - -shrinkTokenQuantityMixed :: TokenQuantity -> [TokenQuantity] -shrinkTokenQuantityMixed = shrinkTokenQuantity +genTokenQuantityPositive :: Gen TokenQuantity +genTokenQuantityPositive = sized $ \n -> quantityFromInt <$> choose (1, max 1 n) --------------------------------------------------------------------------------- --- Utilities --------------------------------------------------------------------------------- - -shrinkTokenQuantity :: TokenQuantity -> [TokenQuantity] -shrinkTokenQuantity - -- Since token quantities can be very large, we limit the number of results - -- that the shrinker can return: - = take 8 - . fmap quantityFromInteger +shrinkTokenQuantityPositive :: TokenQuantity -> [TokenQuantity] +shrinkTokenQuantityPositive + = fmap quantityFromInteger + . filter (> 0) . shrink . quantityToInteger -tokenQuantitySmall :: TokenQuantity -tokenQuantitySmall = TokenQuantity 10 - -tokenQuantityLarge :: TokenQuantity -tokenQuantityLarge = TokenQuantity $ fromIntegral (maxBound :: Word64) - -tokenQuantityMassive :: TokenQuantity -tokenQuantityMassive = TokenQuantity $ (10 :: Natural) ^ (1000 :: Natural) +-------------------------------------------------------------------------------- +-- Token quantities chosen from the full range available. +-------------------------------------------------------------------------------- + +-- | Generates token quantities across the full range of what may be encoded +-- within a single on-chain token bundle. +-- +-- This generator has a slight bias towards the limits of the range, but +-- otherwise generates values uniformly across the whole range. +-- +-- This can be useful when testing roundtrip conversions between different +-- types. +-- +genTokenQuantityFullRange :: Gen TokenQuantity +genTokenQuantityFullRange = frequency + [ ( 1, pure minTokenQuantity ) + , ( 1, pure maxTokenQuantity ) + , ( 8 + , quantityFromInteger <$> + choose (1, quantityToInteger maxTokenQuantity - 1) + ) + ] + where + minTokenQuantity :: TokenQuantity + minTokenQuantity = TokenQuantity 0 + maxTokenQuantity :: TokenQuantity + maxTokenQuantity = TokenQuantity $ fromIntegral $ maxBound @Word64 + +shrinkTokenQuantityFullRange :: TokenQuantity -> [TokenQuantity] +shrinkTokenQuantityFullRange = + -- Given that we may have a large value, we limit the number of results + -- returned in order to avoid processing long lists of shrunken values. + take 8 . shrinkTokenQuantity -------------------------------------------------------------------------------- -- Internal functions diff --git a/lib/core/test/unit/Cardano/Pool/DB/Arbitrary.hs b/lib/core/test/unit/Cardano/Pool/DB/Arbitrary.hs index 0f6a540d746..407d463e6b4 100644 --- a/lib/core/test/unit/Cardano/Pool/DB/Arbitrary.hs +++ b/lib/core/test/unit/Cardano/Pool/DB/Arbitrary.hs @@ -47,7 +47,7 @@ import Cardano.Wallet.Primitive.Types import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinLargePositive, shrinkCoinLargePositive ) + ( genCoinFullRange, shrinkCoinFullRange ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) import Control.Arrow @@ -141,8 +141,8 @@ instance Arbitrary (Quantity "lovelace" Word64) where arbitrary = Quantity <$> arbitrary instance Arbitrary Coin where - shrink = shrinkCoinLargePositive - arbitrary = genCoinLargePositive + shrink = shrinkCoinFullRange + arbitrary = genCoinFullRange arbitraryEpochLength :: Word32 arbitraryEpochLength = 100 diff --git a/lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs b/lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs index 7f0fb0a159b..78a3aad9bc7 100644 --- a/lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs @@ -236,7 +236,7 @@ import Cardano.Wallet.Primitive.Types.Address import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinLargePositive, genCoinSmallPositive ) + ( genCoinFullRange, genCoinPositive ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) import Cardano.Wallet.Primitive.Types.RewardAccount @@ -248,7 +248,7 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen import Cardano.Wallet.Primitive.Types.TokenMap ( TokenMap ) import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetIdSmallRange, genTokenMapSmallRange, shrinkTokenMapSmallRange ) + ( genAssetId, genTokenMapSmallRange, shrinkTokenMapSmallRange ) import Cardano.Wallet.Primitive.Types.TokenPolicy ( AssetDecimals (..) , AssetLogo (..) @@ -260,7 +260,7 @@ import Cardano.Wallet.Primitive.Types.TokenPolicy , mkTokenFingerprint ) import Cardano.Wallet.Primitive.Types.TokenPolicy.Gen - ( genTokenNameSmallRange ) + ( genTokenName ) import Cardano.Wallet.Primitive.Types.Tx ( Direction (..) , SerialisedTx (..) @@ -1915,7 +1915,7 @@ instance Arbitrary TokenMetadataError where ] instance Arbitrary ApiAsset where - arbitrary = toApiAsset <$> arbitrary <*> genAssetIdSmallRange + arbitrary = toApiAsset <$> arbitrary <*> genAssetId instance Arbitrary a => Arbitrary (AddressAmount a) where arbitrary = applyArbitrary3 AddressAmount @@ -1953,7 +1953,7 @@ instance Arbitrary (ApiConstructTransaction t) where instance Arbitrary (ApiMintBurnData t) where arbitrary = ApiMintBurnData <$> arbitrary - <*> (ApiT <$> genTokenNameSmallRange) + <*> (ApiT <$> genTokenName) <*> arbitrary instance Arbitrary ApiStakeKeyIndex where @@ -2124,7 +2124,7 @@ instance Arbitrary RewardAccount where instance Arbitrary Coin where -- No Shrinking - arbitrary = genCoinLargePositive + arbitrary = genCoinFullRange instance Arbitrary UTxO where shrink (UTxO utxo) = UTxO <$> shrink utxo @@ -2163,8 +2163,8 @@ instance Arbitrary ApiWalletUtxoSnapshot where where genEntry :: Gen ApiWalletUtxoSnapshotEntry genEntry = do - adaValue1 <- genCoinSmallPositive - adaValue2 <- genCoinSmallPositive + adaValue1 <- genCoinPositive + adaValue2 <- genCoinPositive -- The actual ada quantity of an output's token bundle must be -- greater than or equal to the minimum permissible ada quantity: let ada = Api.coinToQuantity $ max adaValue1 adaValue2 @@ -2242,7 +2242,7 @@ instance Arbitrary ApiPostAccountKeyDataWithPurpose where instance Arbitrary TokenFingerprint where arbitrary = do - AssetId policy aName <- genAssetIdSmallRange + AssetId policy aName <- genAssetId pure $ mkTokenFingerprint policy aName shrink _ = [] diff --git a/lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs b/lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs index 8aef13fa3e6..436fc19e2e7 100644 --- a/lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs +++ b/lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs @@ -111,7 +111,7 @@ import Cardano.Wallet.Primitive.Types.Address import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinLargePositive ) + ( genCoinFullRange ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) import Cardano.Wallet.Primitive.Types.RewardAccount @@ -415,7 +415,7 @@ instance Arbitrary TxMetadata where shrink = shrinkTxMetadata instance Arbitrary Coin where - arbitrary = genCoinLargePositive + arbitrary = genCoinFullRange instance Arbitrary UTxO where shrink (UTxO u) = diff --git a/lib/core/test/unit/Cardano/Wallet/DB/Sqlite/TypesSpec.hs b/lib/core/test/unit/Cardano/Wallet/DB/Sqlite/TypesSpec.hs index 8fbf51c29cc..dbb7a3d7790 100644 --- a/lib/core/test/unit/Cardano/Wallet/DB/Sqlite/TypesSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/DB/Sqlite/TypesSpec.hs @@ -20,18 +20,15 @@ import Cardano.Wallet.Primitive.Types import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) import Cardano.Wallet.Primitive.Types.TokenQuantity.Gen - ( genTokenQuantityMixed - , shrinkTokenQuantityMixed - , tokenQuantityLarge - , tokenQuantityMassive - , tokenQuantitySmall - ) + ( genTokenQuantityFullRange, shrinkTokenQuantityFullRange ) import Data.Proxy ( Proxy (..) ) import Data.Time.Clock.POSIX ( POSIXTime ) import Data.Typeable ( Typeable, typeRep ) +import Data.Word + ( Word64 ) import Data.Word.Odd ( Word31 ) import Database.Persist.Class @@ -50,8 +47,6 @@ import Test.QuickCheck , (===) ) -import qualified Cardano.Wallet.Primitive.Types.TokenQuantity as TokenQuantity - spec :: Spec spec = do describe "Values can be persisted and unpersisted successfully" $ do @@ -78,19 +73,18 @@ persistRoundtrip proxy = it prop_checkTokenQuantityCoverage :: TokenQuantity -> Property prop_checkTokenQuantityCoverage q = checkCoverage - $ cover 2 (TokenQuantity.isZero q) - "token quantity is zero" - $ cover 2 isSmall - "token quantity is small" - $ cover 2 isLarge - "token quantity is large" - $ cover 2 isMassive - "token quantity is massive" + $ cover 2 (q == minTokenQuantity) + "token quantity is smallest allowable" + $ cover 2 (q == maxTokenQuantity) + "token quantity is greatest allowable" + $ cover 2 (q > minTokenQuantity && q < maxTokenQuantity) + "token quantity is between smallest and greatest" True where - isSmall = TokenQuantity 0 < q && q <= tokenQuantitySmall - isLarge = tokenQuantitySmall < q && q <= tokenQuantityLarge - isMassive = tokenQuantityLarge < q && q <= tokenQuantityMassive + minTokenQuantity :: TokenQuantity + minTokenQuantity = TokenQuantity 0 + maxTokenQuantity :: TokenQuantity + maxTokenQuantity = TokenQuantity $ fromIntegral $ maxBound @Word64 {------------------------------------------------------------------------------- Arbitrary Instances @@ -118,5 +112,5 @@ instance Arbitrary Word31 where shrink = shrinkIntegral instance Arbitrary TokenQuantity where - arbitrary = genTokenQuantityMixed - shrink = shrinkTokenQuantityMixed + arbitrary = genTokenQuantityFullRange + shrink = shrinkTokenQuantityFullRange diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/MA/RoundRobinSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/MA/RoundRobinSpec.hs index ca6f0d07bff..72309842e17 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/MA/RoundRobinSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/MA/RoundRobinSpec.hs @@ -71,11 +71,7 @@ import Cardano.Wallet.Primitive.Types.Address import Cardano.Wallet.Primitive.Types.Coin ( Coin (..), addCoin ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinLargePositive - , genCoinSmall - , genCoinSmallPositive - , shrinkCoinSmallPositive - ) + ( genCoin, genCoinPositive, shrinkCoinPositive ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) import Cardano.Wallet.Primitive.Types.TokenBundle @@ -85,20 +81,20 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen import Cardano.Wallet.Primitive.Types.TokenMap ( AssetId (..), TokenMap ) import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetIdLargeRange - , genAssetIdSmallRange + ( genAssetId + , genAssetIdLargeRange , genTokenMapSmallRange - , shrinkAssetIdSmallRange + , shrinkAssetId , shrinkTokenMapSmallRange ) import Cardano.Wallet.Primitive.Types.TokenPolicy ( TokenName (..), TokenPolicyId (..) ) import Cardano.Wallet.Primitive.Types.TokenPolicy.Gen - ( genTokenNameMediumRange ) + ( genTokenName ) import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) import Cardano.Wallet.Primitive.Types.TokenQuantity.Gen - ( genTokenQuantitySmallPositive, shrinkTokenQuantitySmallPositive ) + ( genTokenQuantityPositive, shrinkTokenQuantityPositive ) import Cardano.Wallet.Primitive.Types.Tx ( TokenBundleSizeAssessment (..) , TokenBundleSizeAssessor (..) @@ -505,11 +501,11 @@ prop_AssetCount_TokenMap_placesEmptyMapsFirst maps = $ cover 20 (emptyMapCount >= 8 && nonEmptyMapCount >= 8) "empty map count >= 8 && non-empty map count >= 8" -- Check head and last element of list: - $ cover 40 (isEmptyMap $ NE.head maps) + $ cover 20 (isEmptyMap $ NE.head maps) "head element is empty map" $ cover 40 (not $ isEmptyMap $ NE.head maps) "head element is non-empty map" - $ cover 40 (isEmptyMap $ NE.last maps) + $ cover 20 (isEmptyMap $ NE.last maps) "last element is empty map" $ cover 40 (not $ isEmptyMap $ NE.last maps) "last element is non-empty map" @@ -603,7 +599,7 @@ genSelectionCriteria genUTxOIndex = do (1, UTxOIndex.size utxoAvailable `div` 8) ) ] - extraCoinSource <- oneof [ pure Nothing, Just <$> genCoinSmall ] + extraCoinSource <- oneof [ pure Nothing, Just <$> genCoin ] (assetsToMint, assetsToBurn) <- genAssetsToMintAndBurn utxoAvailable outputsToCover pure $ SelectionCriteria @@ -742,7 +738,7 @@ prop_performSelection_small minCoinValueFor costFor (Blind (Small criteria)) = prop_performSelection minCoinValueFor costFor (Blind criteria) $ \result -> cover 10 (selectionUnlimited && selectionSufficient result) "selection unlimited and sufficient" - . cover 5 (selectionLimited && selectionSufficient result) + . cover 4 (selectionLimited && selectionSufficient result) "selection limited but sufficient" . cover 10 (selectionLimited && selectionInsufficient result) "selection limited and insufficient" @@ -1865,8 +1861,8 @@ genMakeChangeData = flip suchThat isValidMakeChangeData $ do MakeChangeCriteria <$> arbitrary <*> pure NoBundleSizeLimit - <*> genCoinSmall - <*> oneof [pure Nothing, Just <$> genCoinSmallPositive] + <*> genCoin + <*> oneof [pure Nothing, Just <$> genCoinPositive] <*> pure inputBundles <*> pure outputBundles <*> pure assetsToMint @@ -3481,8 +3477,8 @@ instance Arbitrary a => Arbitrary (AssetCount a) where shrink = fmap AssetCount . shrink . unAssetCount instance Arbitrary AssetId where - arbitrary = genAssetIdSmallRange - shrink = shrinkAssetIdSmallRange + arbitrary = genAssetId + shrink = shrinkAssetId instance Arbitrary Natural where arbitrary = arbitrarySizedNatural @@ -3492,7 +3488,7 @@ instance Arbitrary MakeChangeData where arbitrary = genMakeChangeData instance Arbitrary (MockRoundRobinState TokenName Word8) where - arbitrary = genMockRoundRobinState genTokenNameMediumRange arbitrary + arbitrary = genMockRoundRobinState genTokenName arbitrary shrink = shrinkMockRoundRobinState shrink instance Arbitrary TokenBundle where @@ -3501,7 +3497,7 @@ instance Arbitrary TokenBundle where instance Arbitrary (Large TokenBundle) where arbitrary = fmap Large $ TokenBundle - <$> genCoinLargePositive + <$> genCoinPositive <*> genTokenMapLarge -- No shrinking @@ -3516,15 +3512,15 @@ genTokenMapLarge = do where genAssetQuantity = (,) <$> genAssetIdLargeRange - <*> genTokenQuantitySmallPositive + <*> genTokenQuantityPositive instance Arbitrary TokenMap where arbitrary = genTokenMapSmallRange shrink = shrinkTokenMapSmallRange instance Arbitrary TokenQuantity where - arbitrary = genTokenQuantitySmallPositive - shrink = shrinkTokenQuantitySmallPositive + arbitrary = genTokenQuantityPositive + shrink = shrinkTokenQuantityPositive instance Arbitrary TxOut where arbitrary = genTxOutSmallRange @@ -3555,8 +3551,8 @@ instance Arbitrary (Small UTxOIndex) where shrink = fmap Small . shrinkUTxOIndexSmall . getSmall instance Arbitrary Coin where - arbitrary = genCoinSmallPositive - shrink = shrinkCoinSmallPositive + arbitrary = genCoinPositive + shrink = shrinkCoinPositive instance Arbitrary MinCoinValueFor where arbitrary = arbitraryBoundedEnum diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenBundleSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenBundleSpec.hs index 096489334bb..bf93ce8543c 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenBundleSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenBundleSpec.hs @@ -20,7 +20,7 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) import Cardano.Wallet.Primitive.Types.TokenQuantity.Gen - ( genTokenQuantitySmallPositive, shrinkTokenQuantitySmallPositive ) + ( genTokenQuantityPositive, shrinkTokenQuantityPositive ) import Data.Ratio ( (%) ) import Test.Hspec @@ -182,5 +182,5 @@ instance Arbitrary TokenBundle where shrink = shrinkTokenBundleSmallRange instance Arbitrary TokenQuantity where - arbitrary = genTokenQuantitySmallPositive - shrink = shrinkTokenQuantitySmallPositive + arbitrary = genTokenQuantityPositive + shrink = shrinkTokenQuantityPositive diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenMapSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenMapSpec.hs index 641b4b9ab07..8ed0f6fef86 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenMapSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenMapSpec.hs @@ -21,28 +21,24 @@ import Cardano.Wallet.Primitive.Types.TokenMap ( AssetId (..), Flat (..), Nested (..), TokenMap, difference ) import Cardano.Wallet.Primitive.Types.TokenMap.Gen ( AssetIdF (..) + , genAssetId , genAssetIdLargeRange - , genAssetIdSmallRange , genTokenMapSized , genTokenMapSmallRange - , shrinkAssetIdSmallRange + , shrinkAssetId , shrinkTokenMapSmallRange ) import Cardano.Wallet.Primitive.Types.TokenPolicy ( TokenName, TokenPolicyId, mkTokenName ) import Cardano.Wallet.Primitive.Types.TokenPolicy.Gen - ( genTokenNameSmallRange - , genTokenPolicyIdSmallRange - , shrinkTokenNameSmallRange - , shrinkTokenPolicyIdSmallRange - ) + ( genTokenName, genTokenPolicyId, shrinkTokenName, shrinkTokenPolicyId ) import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) import Cardano.Wallet.Primitive.Types.TokenQuantity.Gen - ( genTokenQuantitySmall - , genTokenQuantitySmallPositive - , shrinkTokenQuantitySmall - , shrinkTokenQuantitySmallPositive + ( genTokenQuantity + , genTokenQuantityPositive + , shrinkTokenQuantity + , shrinkTokenQuantityPositive ) import Control.Monad ( replicateM ) @@ -777,9 +773,9 @@ prop_equipartitionQuantitiesWithUpperBound_coverage :: TokenMap -> Positive TokenQuantity -> Property prop_equipartitionQuantitiesWithUpperBound_coverage m (Positive maxQuantity) = checkCoverage $ - cover 8 (maxQuantity == TokenQuantity 1) + cover 4 (maxQuantity == TokenQuantity 1) "Maximum allowable quantity == 1" $ - cover 8 (maxQuantity == TokenQuantity 2) + cover 4 (maxQuantity == TokenQuantity 2) "Maximum allowable quantity == 2" $ cover 8 (maxQuantity >= TokenQuantity 3) "Maximum allowable quantity >= 3" $ @@ -1012,8 +1008,8 @@ instance Arbitrary a => Arbitrary (NonEmpty a) where shrink = mapMaybe NE.nonEmpty . shrink . NE.toList instance Arbitrary AssetId where - arbitrary = genAssetIdSmallRange - shrink = shrinkAssetIdSmallRange + arbitrary = genAssetId + shrink = shrinkAssetId instance Arbitrary TokenMap where arbitrary = genTokenMapSmallRange @@ -1030,16 +1026,16 @@ instance Arbitrary (Large TokenMap) where where genAssetQuantity = (,) <$> genAssetIdLargeRange - <*> genTokenQuantitySmallPositive + <*> genTokenQuantityPositive -- No shrinking instance Arbitrary TokenName where - arbitrary = genTokenNameSmallRange - shrink = shrinkTokenNameSmallRange + arbitrary = genTokenName + shrink = shrinkTokenName instance Arbitrary TokenPolicyId where - arbitrary = genTokenPolicyIdSmallRange - shrink = shrinkTokenPolicyIdSmallRange + arbitrary = genTokenPolicyId + shrink = shrinkTokenPolicyId instance Arbitrary TokenQuantity where -- We generate small token quantities in order to increase the chance of @@ -1050,9 +1046,9 @@ instance Arbitrary TokenQuantity where -- The generation of zero-valued tokens is useful, as it allows us to -- verify that the token map invariant (that a map contains no -- zero-valued tokens) is maintained. - arbitrary = genTokenQuantitySmall - shrink = shrinkTokenQuantitySmall + arbitrary = genTokenQuantity + shrink = shrinkTokenQuantity instance Arbitrary (Positive TokenQuantity) where - arbitrary = Positive <$> genTokenQuantitySmallPositive - shrink = fmap Positive . shrinkTokenQuantitySmallPositive . getPositive + arbitrary = Positive <$> genTokenQuantityPositive + shrink = fmap Positive . shrinkTokenQuantityPositive . getPositive diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenQuantitySpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenQuantitySpec.hs index 07388dda545..bc68f2b6fc2 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenQuantitySpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/TokenQuantitySpec.hs @@ -15,7 +15,7 @@ import Prelude import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) import Cardano.Wallet.Primitive.Types.TokenQuantity.Gen - ( genTokenQuantityMixed, shrinkTokenQuantityMixed ) + ( genTokenQuantityFullRange, shrinkTokenQuantityFullRange ) import Data.Aeson ( FromJSON (..), ToJSON (..) ) import Data.Proxy @@ -194,5 +194,5 @@ instance Arbitrary TokenQuantity where -- We test with token quantities of a variety of magnitudes to ensure that -- roundtrip serialization works even with large values, both positive and -- negative. - arbitrary = genTokenQuantityMixed - shrink = shrinkTokenQuantityMixed + arbitrary = genTokenQuantityFullRange + shrink = shrinkTokenQuantityFullRange diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/UTxOIndexSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/UTxOIndexSpec.hs index 7ef4f03657c..91758ee1ca0 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/Types/UTxOIndexSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/Types/UTxOIndexSpec.hs @@ -14,7 +14,7 @@ import Prelude import Cardano.Wallet.Primitive.Types.TokenMap ( AssetId ) import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetIdSmallRange, shrinkAssetIdSmallRange ) + ( genAssetId, shrinkAssetId ) import Cardano.Wallet.Primitive.Types.Tx ( TxIn, TxOut ) import Cardano.Wallet.Primitive.Types.Tx.Gen @@ -433,11 +433,11 @@ prop_selectRandom_one_withAdaOnly u = checkCoverage $ monadicIO $ do prop_selectRandom_one_withAsset :: UTxOIndex -> AssetId -> Property prop_selectRandom_one_withAsset u a = checkCoverage $ monadicIO $ do result <- run $ UTxOIndex.selectRandom u (WithAsset a) - monitor $ cover 70 (a `Set.member` UTxOIndex.assets u) + monitor $ cover 50 (a `Set.member` UTxOIndex.assets u) "index has the specified asset" - monitor $ cover 70 (Set.size (UTxOIndex.assets u) > 1) + monitor $ cover 50 (Set.size (UTxOIndex.assets u) > 1) "index has more than one asset" - monitor $ cover 70 (isJust result) + monitor $ cover 50 (isJust result) "selected an entry" case result of Nothing -> @@ -459,11 +459,11 @@ prop_selectRandom_one_withAsset u a = checkCoverage $ monadicIO $ do prop_selectRandom_one_withAssetOnly :: UTxOIndex -> AssetId -> Property prop_selectRandom_one_withAssetOnly u a = checkCoverage $ monadicIO $ do result <- run $ UTxOIndex.selectRandom u (WithAssetOnly a) - monitor $ cover 70 (a `Set.member` UTxOIndex.assets u) + monitor $ cover 50 (a `Set.member` UTxOIndex.assets u) "index has the specified asset" - monitor $ cover 70 (Set.size (UTxOIndex.assets u) > 1) + monitor $ cover 50 (Set.size (UTxOIndex.assets u) > 1) "index has more than one asset" - monitor $ cover 20 (isJust result) + monitor $ cover 10 (isJust result) "selected an entry" case result of Nothing -> @@ -509,11 +509,11 @@ prop_selectRandom_all_withAdaOnly u = checkCoverage $ monadicIO $ do prop_selectRandom_all_withAsset :: UTxOIndex -> AssetId -> Property prop_selectRandom_all_withAsset u a = checkCoverage $ monadicIO $ do (selectedEntries, u') <- run $ selectAll (WithAsset a) u - monitor $ cover 70 (a `Set.member` UTxOIndex.assets u) + monitor $ cover 50 (a `Set.member` UTxOIndex.assets u) "index has the specified asset" - monitor $ cover 70 (Set.size (UTxOIndex.assets u) > 1) + monitor $ cover 50 (Set.size (UTxOIndex.assets u) > 1) "index has more than one asset" - monitor $ cover 70 (not (null selectedEntries)) + monitor $ cover 50 (not (null selectedEntries)) "selected at least one entry" assert $ L.all (\(_, o) -> not (txOutHasAsset o a)) (UTxOIndex.toList u') assert $ L.all (\(_, o) -> txOutHasAsset o a) selectedEntries @@ -526,11 +526,11 @@ prop_selectRandom_all_withAsset u a = checkCoverage $ monadicIO $ do prop_selectRandom_all_withAssetOnly :: UTxOIndex -> AssetId -> Property prop_selectRandom_all_withAssetOnly u a = checkCoverage $ monadicIO $ do (selectedEntries, u') <- run $ selectAll (WithAssetOnly a) u - monitor $ cover 70 (a `Set.member` UTxOIndex.assets u) + monitor $ cover 50 (a `Set.member` UTxOIndex.assets u) "index has the specified asset" - monitor $ cover 70 (Set.size (UTxOIndex.assets u) > 1) + monitor $ cover 50 (Set.size (UTxOIndex.assets u) > 1) "index has more than one asset" - monitor $ cover 20 (not (null selectedEntries)) + monitor $ cover 10 (not (null selectedEntries)) "selected at least one entry" assert $ all (\(_, o) -> not (txOutHasAssetOnly o a)) (UTxOIndex.toList u') assert $ all (\(_, o) -> txOutHasAssetOnly o a) selectedEntries @@ -542,8 +542,8 @@ prop_selectRandom_all_withAssetOnly u a = checkCoverage $ monadicIO $ do -- prop_selectRandomWithPriority :: UTxOIndex -> Property prop_selectRandomWithPriority u = - forAll (genAssetIdSmallRange) $ \a1 -> - forAll (genAssetIdSmallRange `suchThat` (/= a1)) $ \a2 -> + forAll (genAssetId) $ \a1 -> + forAll (genAssetId `suchThat` (/= a1)) $ \a2 -> checkCoverage $ monadicIO $ do haveMatchForAsset1 <- isJust <$> run (UTxOIndex.selectRandom u $ WithAssetOnly a1) @@ -685,8 +685,8 @@ txOutIsAdaOnly = TokenBundle.isCoin . view #tokens -------------------------------------------------------------------------------- instance Arbitrary AssetId where - arbitrary = genAssetIdSmallRange - shrink = shrinkAssetIdSmallRange + arbitrary = genAssetId + shrink = shrinkAssetId instance Arbitrary UTxOIndex where arbitrary = genUTxOIndexSmall @@ -708,8 +708,8 @@ genSelectionFilterSmallRange :: Gen SelectionFilter genSelectionFilterSmallRange = oneof [ pure Any , pure WithAdaOnly - , WithAsset <$> genAssetIdSmallRange - , WithAssetOnly <$> genAssetIdSmallRange + , WithAsset <$> genAssetId + , WithAssetOnly <$> genAssetId ] shrinkSelectionFilterSmallRange :: SelectionFilter -> [SelectionFilter] @@ -717,10 +717,10 @@ shrinkSelectionFilterSmallRange = \case Any -> [] WithAdaOnly -> [Any] WithAsset a -> - case WithAsset <$> shrinkAssetIdSmallRange a of + case WithAsset <$> shrinkAssetId a of [] -> [WithAdaOnly] xs -> xs WithAssetOnly a -> - case WithAssetOnly <$> shrinkAssetIdSmallRange a of + case WithAssetOnly <$> shrinkAssetId a of [] -> [WithAsset a] xs -> xs diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/TypesSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/TypesSpec.hs index dc5c78287e6..da27ac504f1 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/TypesSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/TypesSpec.hs @@ -91,7 +91,7 @@ import Cardano.Wallet.Primitive.Types.Address import Cardano.Wallet.Primitive.Types.Coin ( Coin (..), isValidCoin ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinSmall ) + ( genCoin ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) import Cardano.Wallet.Primitive.Types.HashSpec @@ -891,29 +891,28 @@ prop_2_1_1 :: (Set TxIn, UTxO) -> Property prop_2_1_1 (ins, u) = cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop) where - cond = not $ Set.null $ dom u `Set.intersection` ins + cond = not $ dom u `Set.disjoint` ins prop = (u `restrictedBy` ins) `isSubsetOf` u prop_2_1_2 :: (Set TxIn, UTxO) -> Property prop_2_1_2 (ins, u) = cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop) where - cond = not $ Set.null $ dom u `Set.intersection` ins + cond = not $ dom u `Set.disjoint` ins prop = (u `excluding` ins) `isSubsetOf` u prop_2_1_3 :: (Set TxOut, UTxO) -> Property prop_2_1_3 (outs, u) = cover 50 cond "u ⋂ outs ≠ ∅" (property prop) where - cond = not $ Set.null $ - Set.fromList (Map.elems (getUTxO u)) `Set.intersection` outs + cond = not $ Set.fromList (Map.elems (getUTxO u)) `Set.disjoint` outs prop = (u `restrictedTo` outs) `isSubsetOf` u prop_2_1_4 :: (Set TxIn, UTxO, UTxO) -> Property prop_2_1_4 (ins, u, v) = cover 50 cond "(dom u ⋃ dom v) ⋂ ins ≠ ∅" (property prop) where - cond = not $ Set.null $ Set.union (dom u) (dom v) `Set.intersection` ins + cond = not $ Set.union (dom u) (dom v) `Set.disjoint` ins prop = ((u <> v) `restrictedBy` ins) === @@ -923,7 +922,7 @@ prop_2_1_5 :: (Set TxIn, UTxO, UTxO) -> Property prop_2_1_5 (ins, u, v) = cover 50 cond "(dom u ⋃ dom v) ⋂ ins ≠ ∅" (property prop) where - cond = not $ Set.null $ Set.union (dom u) (dom v) `Set.intersection` ins + cond = not $ Set.union (dom u) (dom v) `Set.disjoint` ins prop = ((u <> v) `excluding` ins) === @@ -933,7 +932,7 @@ prop_2_1_6 :: (Set TxIn, UTxO) -> Property prop_2_1_6 (ins, u) = cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop) where - cond = not $ Set.null $ dom u `Set.intersection` ins + cond = not $ dom u `Set.disjoint` ins prop = (u `restrictedBy` (dom u `Set.intersection` ins)) === @@ -943,7 +942,7 @@ prop_2_1_7 :: (Set TxIn, UTxO) -> Property prop_2_1_7 (ins, u) = cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop) where - cond = not $ Set.null $ dom u `Set.intersection` ins + cond = not $ dom u `Set.disjoint` ins prop = (u `excluding` (dom u `Set.intersection` ins)) === @@ -953,7 +952,7 @@ prop_2_1_8 :: (Set TxIn, UTxO, UTxO) -> Property prop_2_1_8 (ins, u, v) = cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop) where - cond = not $ Set.null $ dom u `Set.intersection` ins + cond = not $ dom u `Set.disjoint` ins prop = ((u <> v) `excluding` (dom u <> ins)) === @@ -963,7 +962,7 @@ prop_2_1_9 :: (Set TxIn, UTxO) -> Property prop_2_1_9 (ins, u) = cover 50 cond "dom u ⋂ ins ≠ ∅" (property prop) where - cond = not $ Set.null $ dom u `Set.intersection` ins + cond = not $ dom u `Set.disjoint` ins prop = (u `excluding` ins) === u `restrictedBy` (dom u \\ ins) @@ -1110,7 +1109,7 @@ instance Arbitrary AddressState where instance Arbitrary Coin where -- No Shrinking - arbitrary = genCoinSmall + arbitrary = genCoin instance (Arbitrary a, Ord a) => Arbitrary (Range a) where arbitrary = @@ -1150,7 +1149,9 @@ instance Arbitrary TxOut where -- No Shrinking arbitrary = TxOut <$> arbitrary - <*> fmap TokenBundle.fromCoin genCoinSmall + -- Here we deliberately restrict the range of coins in order to increase + -- the probability of collisions between identical transaction outputs: + <*> fmap TokenBundle.fromCoin (scale (`mod` 8) genCoin) instance Arbitrary TxIn where -- No Shrinking diff --git a/lib/core/test/unit/Cardano/WalletSpec.hs b/lib/core/test/unit/Cardano/WalletSpec.hs index c014d008822..aea44bd61bb 100644 --- a/lib/core/test/unit/Cardano/WalletSpec.hs +++ b/lib/core/test/unit/Cardano/WalletSpec.hs @@ -110,7 +110,7 @@ import Cardano.Wallet.Primitive.Types.Address.Gen import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinLargePositive ) + ( genCoinPositive ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) import Cardano.Wallet.Primitive.Types.RewardAccount @@ -1417,7 +1417,7 @@ instance Arbitrary (Hash "Tx") where instance Arbitrary Coin where shrink _ = [] - arbitrary = genCoinLargePositive + arbitrary = genCoinPositive instance Arbitrary Tx where shrink (Tx tid fees ins outs wdrls md) = mconcat @@ -1458,7 +1458,7 @@ instance Arbitrary TxIn where instance Arbitrary TxOut where arbitrary = TxOut (Address "address") . TokenBundle.fromCoin - <$> genCoinLargePositive + <$> genCoinPositive instance Arbitrary TxMeta where shrink _ = [] diff --git a/lib/shelley/test/unit/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs b/lib/shelley/test/unit/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs index e84cfc29247..182b537e94d 100644 --- a/lib/shelley/test/unit/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs +++ b/lib/shelley/test/unit/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs @@ -13,7 +13,7 @@ import Prelude import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinAny, shrinkCoinAny ) + ( genCoinFullRange, shrinkCoinFullRange ) import Cardano.Wallet.Primitive.Types.TokenBundle ( Flat (..), TokenBundle ) import Cardano.Wallet.Primitive.Types.TokenBundle.Gen @@ -28,7 +28,7 @@ import Cardano.Wallet.Primitive.Types.TokenPolicy.Gen import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) import Cardano.Wallet.Primitive.Types.TokenQuantity.Gen - ( genTokenQuantityMixed, shrinkTokenQuantityMixed ) + ( genTokenQuantityFullRange, shrinkTokenQuantityFullRange ) import Cardano.Wallet.Primitive.Types.Tx ( txOutMaxTokenQuantity, txOutMinTokenQuantity ) import Cardano.Wallet.Shelley.Compatibility.Ledger @@ -260,8 +260,10 @@ newtype FixedSize256 a = FixedSize256 { unFixedSize256 :: a } -------------------------------------------------------------------------------- instance Arbitrary Coin where - arbitrary = genCoinAny - shrink = shrinkCoinAny + -- This instance is used to test roundtrip conversions, so it's important + -- that we generate coins across the full range available. + arbitrary = genCoinFullRange + shrink = shrinkCoinFullRange instance Arbitrary (ProtocolMinimum Coin) where arbitrary @@ -301,5 +303,5 @@ instance Arbitrary TokenPolicyId where -- No shrinking instance Arbitrary TokenQuantity where - arbitrary = genTokenQuantityMixed - shrink = shrinkTokenQuantityMixed + arbitrary = genTokenQuantityFullRange + shrink = shrinkTokenQuantityFullRange diff --git a/lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs b/lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs index b6462654804..ffee0c4983f 100644 --- a/lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs +++ b/lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs @@ -61,7 +61,7 @@ import Cardano.Wallet.Primitive.Types.Address import Cardano.Wallet.Primitive.Types.Coin ( Coin (..), coinToInteger ) import Cardano.Wallet.Primitive.Types.Coin.Gen - ( genCoinLargePositive, shrinkCoinLargePositive ) + ( genCoinPositive, shrinkCoinPositive ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) import Cardano.Wallet.Primitive.Types.RewardAccount @@ -76,7 +76,7 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen import Cardano.Wallet.Primitive.Types.TokenPolicy ( TokenName (UnsafeTokenName), TokenPolicyId, unTokenName ) import Cardano.Wallet.Primitive.Types.TokenPolicy.Gen - ( genTokenPolicyIdSmallRange, shrinkTokenPolicyIdSmallRange ) + ( genTokenPolicyId, shrinkTokenPolicyId ) import Cardano.Wallet.Primitive.Types.Tx ( TxConstraints (..) , TxIn (..) @@ -197,12 +197,21 @@ spec = do prop_decodeSignedShelleyTxRoundtrip Cardano.ShelleyBasedEraAllegra prop "roundtrip for Byron witnesses" prop_decodeSignedByronTxRoundtrip + -- Note: + -- + -- In the tests below, the expected numbers of inputs are highly sensitive + -- to the size distribution of token bundles within generated transaction + -- outputs. + -- + -- If these tests fail unexpectedly, it's a good idea to check whether or + -- not the distribution of generated token bundles has changed. + -- estimateMaxInputsTests @ShelleyKey - [(1,114),(5,106),(10,101),(20,85),(50,32)] + [(1,114),(5,108),(10,103),(20,89),(50,37)] estimateMaxInputsTests @ByronKey - [(1,73),(5,67),(10,63),(20,52),(50,14)] + [(1,73),(5,69),(10,64),(20,54),(50,17)] estimateMaxInputsTests @IcarusKey - [(1,73),(5,67),(10,63),(20,52),(50,14)] + [(1,73),(5,69),(10,64),(20,54),(50,17)] describe "fee calculations" $ do let pp :: ProtocolParameters @@ -759,11 +768,11 @@ instance Arbitrary (Hash "Tx") where -- transactions. -- instance Arbitrary Coin where - arbitrary = genCoinLargePositive - shrink = shrinkCoinLargePositive + arbitrary = genCoinPositive + shrink = shrinkCoinPositive instance Arbitrary TxOut where - arbitrary = TxOut addr <$> genTokenBundleSmallRange + arbitrary = TxOut addr <$> scale (`mod` 4) genTokenBundleSmallRange where addr = Address $ BS.pack (1:replicate 64 0) @@ -1085,8 +1094,8 @@ instance Arbitrary AssetId where <*> (UnsafeTokenName . BS.pack <$> vector 128) instance Arbitrary TokenPolicyId where - arbitrary = genTokenPolicyIdSmallRange - shrink = shrinkTokenPolicyIdSmallRange + arbitrary = genTokenPolicyId + shrink = shrinkTokenPolicyId instance Arbitrary (Script KeyHash) where arbitrary = do