Skip to content

Commit 650fd4b

Browse files
committed
Define capped realistic ada using size and max supply
Also ensures the minimum lovelace in an output using default pparams.
1 parent 8d13c23 commit 650fd4b

File tree

1 file changed

+13
-11
lines changed
  • hydra-tx/testlib/Test/Hydra/Tx

1 file changed

+13
-11
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Hydra.Prelude hiding (toList)
99
import Cardano.Api.UTxO qualified as UTxO
1010
import Cardano.Crypto.DSIGN qualified as CC
1111
import Cardano.Crypto.Hash (hashToBytes)
12+
import Cardano.Ledger.Api (ensureMinCoinTxOut)
1213
import Cardano.Ledger.BaseTypes qualified as Ledger
1314
import Cardano.Ledger.Credential qualified as Ledger
1415
import Cardano.Ledger.Mary.Value (MaryValue (..))
@@ -27,6 +28,7 @@ import Hydra.Tx.Crypto (Hash (..))
2728
import Hydra.Tx.Party (Party (..))
2829
import PlutusTx.Builtins (fromBuiltin)
2930
import Test.Cardano.Ledger.Conway.Arbitrary ()
31+
import Test.Hydra.Tx.Fixture (pparams)
3032
import Test.Hydra.Tx.Fixture qualified as Fixtures
3133
import Test.QuickCheck (listOf, oneof, scale, shrinkList, shrinkMapBy, sized, suchThat, vector, vectorOf)
3234

@@ -46,7 +48,8 @@ instance Arbitrary (TxOut CtxUTxO) where
4648
genTxOut :: Gen (TxOut ctx)
4749
genTxOut =
4850
(gen `suchThat` notByronAddress)
49-
<&> ensureSomeAda . realisticAda . noRefScripts . noStakeRefPtr
51+
>>= realisticAda
52+
<&> ensureSomeAda . noRefScripts . noStakeRefPtr
5053
where
5154
gen =
5255
oneof
@@ -61,18 +64,17 @@ genTxOut =
6164
ByronAddressInEra{} -> False
6265
_ -> True
6366

64-
realisticAda =
65-
-- 45 billion is max supply
66-
let oneBillionAda = Coin 1_000_000_000_000_000
67-
in modifyTxOutValue $ \v ->
67+
realisticAda o = sized $ \n -> do
68+
let maxSupply = 45_000_000_000_000_000
69+
realistic = Coin $ maxSupply `div` fromIntegral (max n 1)
70+
makeRealistic v =
6871
let MaryValue c ma = toLedgerValue v
69-
in fromLedgerValue (MaryValue (min c oneBillionAda) ma)
72+
in fromLedgerValue (MaryValue (min c realistic) ma)
73+
pure $
74+
modifyTxOutValue makeRealistic o
7075

71-
ensureSomeAda = modifyTxOutValue $ \v ->
72-
let minLovelace = Coin 2_000_000
73-
in if selectLovelace v > minLovelace
74-
then v
75-
else v <> lovelaceToValue minLovelace
76+
ensureSomeAda =
77+
fromLedgerTxOut . ensureMinCoinTxOut pparams . toLedgerTxOut
7678

7779
noStakeRefPtr out@(TxOut addr val dat refScript) = case addr of
7880
ShelleyAddressInEra (ShelleyAddress _ cre sr) ->

0 commit comments

Comments
 (0)