Skip to content

Commit 14f66be

Browse files
committed
Change genUTxO and genTxOut
Re-using the generators more and also including reference scripts. Unsure whether this will break some of the Commit script tests (where bespoke non-visibility of reference scripts could be a problem), but we should instead modify the generators there.
1 parent d1e24e3 commit 14f66be

File tree

3 files changed

+8
-22
lines changed

3 files changed

+8
-22
lines changed

hydra-tx/hydra-tx.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ library testlib
130130
, cardano-ledger-conway:testlib
131131
, cardano-ledger-core
132132
, cardano-ledger-mary
133-
, cardano-ledger-shelley
134133
, cardano-strict-containers
135134
, cborg
136135
, containers

hydra-tx/test/Hydra/Tx/Contract/Deposit.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Hydra.Tx.Deposit (depositTx)
1515
import Test.Hydra.Tx.Fixture (depositDeadline, testNetworkId, testPolicyId)
1616
import Test.Hydra.Tx.Gen (genUTxO)
1717
import Test.Hydra.Tx.Mutation (Mutation (ChangeOutput), SomeMutation (..))
18-
import Test.QuickCheck (chooseInteger, elements, oneof)
18+
import Test.QuickCheck (chooseInteger, elements, oneof, resize)
1919

2020
healthyDepositTx :: (Tx, UTxO)
2121
healthyDepositTx =
@@ -29,7 +29,7 @@ healthyDepositTx =
2929
depositDeadline
3030

3131
healthyDepositUTxO :: UTxO
32-
healthyDepositUTxO = genUTxO `generateWith` 42
32+
healthyDepositUTxO = resize 3 genUTxO `generateWith` 42
3333

3434
data DepositMutation
3535
= -- | Change the output value to a subset of the deposited value. This

hydra-tx/testlib/Test/Hydra/Tx/Gen.hs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Cardano.Crypto.DSIGN qualified as CC
1111
import Cardano.Crypto.Hash (hashToBytes)
1212
import Cardano.Ledger.BaseTypes qualified as Ledger
1313
import Cardano.Ledger.Credential qualified as Ledger
14-
import Cardano.Ledger.Shelley.UTxO qualified as Ledger
1514
import Codec.CBOR.Magic (uintegerFromBytes)
1615
import Data.ByteString qualified as BS
1716
import Data.Map.Strict qualified as Map
@@ -28,24 +27,23 @@ import Hydra.Tx.Party (Party (..))
2827
import PlutusTx.Builtins (fromBuiltin)
2928
import Test.Cardano.Ledger.Conway.Arbitrary ()
3029
import Test.Hydra.Tx.Fixture qualified as Fixtures
31-
import Test.QuickCheck (listOf, oneof, scale, shrinkList, shrinkMapBy, suchThat, vector, vectorOf)
30+
import Test.QuickCheck (listOf, oneof, scale, shrinkList, shrinkMapBy, sized, suchThat, vector, vectorOf)
3231

3332
-- * TxOut
3433

3534
instance Arbitrary (TxOut CtxUTxO) where
3635
arbitrary = genTxOut
3736
shrink txOut = fromLedgerTxOut <$> shrink (toLedgerTxOut txOut)
3837

39-
-- | Generate a 'Babbage' era 'TxOut', which may contain arbitrary assets
38+
-- | Generate a 'Conway' era 'TxOut', which may contain arbitrary assets
4039
-- addressed to public keys and scripts, as well as datums.
4140
--
4241
-- NOTE: This generator does
4342
-- * not produce byron addresses as most of the cardano ecosystem dropped support for that (including plutus),
44-
-- * not produce reference scripts as they are not fully "visible" from plutus,
4543
-- * replace stake pointers with null references as nobody uses that.
4644
genTxOut :: Gen (TxOut ctx)
4745
genTxOut =
48-
(noRefScripts . noStakeRefPtr <$> gen)
46+
(noStakeRefPtr <$> gen)
4947
`suchThat` notByronAddress
5048
where
5149
gen =
@@ -71,9 +69,6 @@ genTxOut =
7169
TxOut (ShelleyAddressInEra (ShelleyAddress Ledger.Testnet cre sr)) val dat refScript
7270
_ -> out
7371

74-
noRefScripts out =
75-
out{txOutReferenceScript = ReferenceScriptNone}
76-
7772
-- | Generate a 'TxOut' with a byron address. This is usually not supported by
7873
-- Hydra or Plutus.
7974
genTxOutByron :: Gen (TxOut ctx)
@@ -114,19 +109,11 @@ shrinkUTxO = shrinkMapBy (UTxO . fromList) UTxO.pairs (shrinkList shrinkOne)
114109
| value' <- shrinkValue value
115110
]
116111

117-
-- | Generate a complete arbitrary UTxO, which may contain arbitrary assets in
118-
-- 'TxOut's addressed to public keys *and* scripts. NOTE: This is not reducing
119-
-- size when generating assets in 'TxOut's, so will end up regularly with 300+
120-
-- assets with generator size 30. NOTE: The Arbitrary TxIn instance from the
121-
-- ledger is producing colliding values, so we replace them.
112+
-- | Generate a complete arbitrary UTxO. See also 'genTxOut'.
122113
genUTxO :: Gen UTxO
123-
genUTxO = do
124-
utxoMap <- Map.toList . Ledger.unUTxO <$> arbitrary
125-
fmap UTxO.fromPairs . forM utxoMap $ \(_, o) -> do
126-
i <- arbitrary
127-
pure (i, fromLedgerTxOut o)
114+
genUTxO = sized genUTxOSized
128115

129-
-- | Generate a 'Babbage' era 'UTxO' with given number of outputs. See also
116+
-- | Generate a 'Conway' era 'UTxO' with given number of outputs. See also
130117
-- 'genTxOut'.
131118
genUTxOSized :: Int -> Gen UTxO
132119
genUTxOSized numUTxO =

0 commit comments

Comments
 (0)