From d70ef7486c132c04a84c6a02d7ad7b939960d452 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Tue, 17 Oct 2023 09:58:48 +0000 Subject: [PATCH] Do not import `Arbitrary` instances into `TransactionSpec` from `BalanceSpec`. This commit breaks the dependency from `TransactionSpec` to `BalanceSpec`. In order to make this possible, we duplicate a small number of `Arbitrary` instances. --- .../Cardano/Wallet/Shelley/TransactionSpec.hs | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs b/lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs index 3bba184c3c1..148381c1b5f 100644 --- a/lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs +++ b/lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs @@ -71,6 +71,8 @@ import Cardano.Wallet.Primitive.Types.Address ( Address (..) ) import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) +import Cardano.Wallet.Primitive.Types.Coin.Gen + ( genCoinPositive, shrinkCoinPositive ) import Cardano.Wallet.Primitive.Types.Credentials ( ClearCredentials, RootCredentials (..) ) import Cardano.Wallet.Primitive.Types.Hash @@ -78,7 +80,7 @@ import Cardano.Wallet.Primitive.Types.Hash import Cardano.Wallet.Primitive.Types.TokenBundle ( AssetId, TokenBundle ) import Cardano.Wallet.Primitive.Types.TokenBundle.Gen - ( genTokenBundleSmallRange ) + ( genTokenBundleSmallRange, shrinkTokenBundleSmallRange ) import Cardano.Wallet.Primitive.Types.TokenPolicy ( TokenName (UnsafeTokenName), TokenPolicyId ) import Cardano.Wallet.Primitive.Types.TokenPolicy.Gen @@ -137,8 +139,6 @@ import Cardano.Write.Tx ) import Cardano.Write.Tx.Balance ( ErrBalanceTx (..), ErrBalanceTxUnableToCreateChangeError (..) ) -import Cardano.Write.Tx.BalanceSpec - () import Cardano.Write.Tx.SizeEstimation ( TxSkeleton (..), estimateTxSize, txConstraints ) import Control.Arrow @@ -200,12 +200,14 @@ import Test.QuickCheck , conjoin , counterexample , cover + , elements , forAll , forAllShow , frequency , label , oneof , property + , scale , suchThat , vector , vectorOf @@ -1504,6 +1506,12 @@ newtype Large a = Large { unLarge :: a } instance Arbitrary (Large TokenBundle) where arbitrary = fmap Large . genTxOutTokenBundle =<< choose (1, 128) +instance Arbitrary AnyRecentEra where + arbitrary = elements + [ AnyRecentEra RecentEraBabbage + , AnyRecentEra RecentEraConway + ] + instance Arbitrary AssetId where arbitrary = TokenBundle.AssetId @@ -1517,6 +1525,25 @@ instance Arbitrary AssetId where -- dominate so we can test the sanity of the estimation algorithm. <*> (UnsafeTokenName . BS.pack <$> vector 128) +instance Arbitrary Coin where + arbitrary = genCoinPositive + shrink = shrinkCoinPositive + +instance Arbitrary (Hash "Tx") where + arbitrary = do + bs <- vectorOf 32 arbitrary + pure $ Hash $ BS.pack bs + +instance Arbitrary Cardano.NetworkId where + arbitrary = oneof + [ pure Cardano.Mainnet + , Cardano.Testnet . Cardano.NetworkMagic <$> arbitrary + ] + +instance Arbitrary TokenBundle where + arbitrary = genTokenBundleSmallRange + shrink = shrinkTokenBundleSmallRange + instance Arbitrary TokenPolicyId where arbitrary = genTokenPolicyId shrink = shrinkTokenPolicyId @@ -1526,6 +1553,22 @@ instance Arbitrary (Script KeyHash) where keyHashes <- vectorOf 10 arbitrary genScript keyHashes +instance Arbitrary TxIn where + arbitrary = do + ix <- scale (`mod` 3) arbitrary + txId <- arbitrary + pure $ TxIn txId ix + +instance Arbitrary TxOut where + arbitrary = + TxOut addr <$> scale (`mod` 4) genTokenBundleSmallRange + where + addr = Address $ BS.pack (1:replicate 56 0) + shrink (TxOut addr bundle) = + [ TxOut addr bundle' + | bundle' <- shrinkTokenBundleSmallRange bundle + ] + instance Arbitrary KeyHash where arbitrary = do cred <- oneof [pure Payment, pure Delegation]