Skip to content

Commit bc9bfaf

Browse files
committed
Improve TxOut Hedgehog generators and fix failing CBOR test for Conway
1 parent 88525ac commit bc9bfaf

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

cardano-api/ChangeLog.md

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
- **Breaking change** - `calculateMinimumUTxO` no longer fails, it is a total computation.
8383
([PR 5013](https://github.com/input-output-hk/cardano-node/pull/5013))
8484

85+
- New generators in `gen` sublibrary: `genPositiveLovelace`, `genPositiveQuantity` and
86+
`genSignedNonZeroQuantity`. ([PR 5013](https://github.com/input-output-hk/cardano-node/pull/5013))
87+
8588
### Bugs
8689

8790
- Allow reading text envelopes from pipes ([PR 4384](https://github.com/input-output-hk/cardano-node/pull/4384))

cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs

+27-5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module Test.Gen.Cardano.Api.Typed
6363
, genShelleyWitness
6464
, genShelleyWitnessSigningKey
6565
, genSignedQuantity
66+
, genSignedNonZeroQuantity
6667
, genSigningKey
6768
, genSlotNo
6869
, genStakeAddress
@@ -81,6 +82,7 @@ module Test.Gen.Cardano.Api.Typed
8182
, genTxMetadataInEra
8283
, genTxMintValue
8384
, genLovelace
85+
, genPositiveLovelace
8486
, genValue
8587
, genValueDefault
8688
, genVerificationKey
@@ -100,6 +102,7 @@ module Test.Gen.Cardano.Api.Typed
100102
, genTxValidityUpperBound
101103
, genTxWithdrawals
102104
, genUnsignedQuantity
105+
, genPositiveQuantity
103106
, genValueForMinting
104107
, genValueForTxOut
105108
, genWitnesses
@@ -179,6 +182,9 @@ genKESPeriod = KESPeriod <$> Gen.word Range.constantBounded
179182
genLovelace :: Gen Lovelace
180183
genLovelace = Lovelace <$> Gen.integral (Range.linear 0 5000)
181184

185+
genPositiveLovelace :: Gen Lovelace
186+
genPositiveLovelace = Lovelace <$> Gen.integral (Range.linear 1 5000)
187+
182188

183189
----------------------------------------------------------------------------
184190
-- SimpleScript generators
@@ -333,9 +339,19 @@ genQuantity range = fromInteger <$> Gen.integral range
333339
genSignedQuantity :: Gen Quantity
334340
genSignedQuantity = genQuantity (Range.constantFrom 0 (-2) 2)
335341

342+
-- | Generate a positive or negative, but not zero quantity.
343+
genSignedNonZeroQuantity :: Gen Quantity
344+
genSignedNonZeroQuantity =
345+
Gen.choice [ genQuantity (Range.constant (-2) (-1))
346+
, genQuantity (Range.constant 1 2)
347+
]
348+
336349
genUnsignedQuantity :: Gen Quantity
337350
genUnsignedQuantity = genQuantity (Range.constant 0 2)
338351

352+
genPositiveQuantity :: Gen Quantity
353+
genPositiveQuantity = genQuantity (Range.constant 1 2)
354+
339355
genValue :: Gen AssetId -> Gen Quantity -> Gen Value
340356
genValue genAId genQuant =
341357
valueFromList <$>
@@ -344,20 +360,26 @@ genValue genAId genQuant =
344360

345361
-- | Generate a 'Value' with any asset ID and a positive or negative quantity.
346362
genValueDefault :: Gen Value
347-
genValueDefault = genValue genAssetId genSignedQuantity
363+
genValueDefault = genValue genAssetId genSignedNonZeroQuantity
348364

349365
-- | Generate a 'Value' suitable for minting, i.e. non-ADA asset ID and a
350366
-- positive or negative quantity.
351367
genValueForMinting :: Gen Value
352-
genValueForMinting = genValue genAssetIdNoAda genSignedQuantity
368+
genValueForMinting = genValue genAssetIdNoAda genSignedNonZeroQuantity
353369
where
354370
genAssetIdNoAda :: Gen AssetId
355371
genAssetIdNoAda = AssetId <$> genPolicyId <*> genAssetName
356372

357373
-- | Generate a 'Value' suitable for usage in a transaction output, i.e. any
358374
-- asset ID and a positive quantity.
359375
genValueForTxOut :: Gen Value
360-
genValueForTxOut = genValue genAssetId genUnsignedQuantity
376+
genValueForTxOut = do
377+
-- Generate a potentially empty list with multi assets
378+
val <- genValue genAssetId genPositiveQuantity
379+
-- Generate at least one positive ADA, without it Value in TxOut makes no sense
380+
-- and will fail deserialization starting with ConwayEra
381+
ada <- (,) AdaAssetId <$> genPositiveQuantity
382+
pure $ valueFromList (ada : valueToList val)
361383

362384

363385
-- Note that we expect to sometimes generate duplicate policy id keys since we
@@ -464,7 +486,7 @@ genTxIndex = TxIx . fromIntegral <$> Gen.word16 Range.constantBounded
464486
genTxOutValue :: CardanoEra era -> Gen (TxOutValue era)
465487
genTxOutValue era =
466488
case multiAssetSupportedInEra era of
467-
Left adaOnlyInEra -> TxOutAdaOnly adaOnlyInEra <$> genLovelace
489+
Left adaOnlyInEra -> TxOutAdaOnly adaOnlyInEra <$> genPositiveLovelace
468490
Right multiAssetInEra -> TxOutValue multiAssetInEra <$> genValueForTxOut
469491

470492
genTxOutTxContext :: CardanoEra era -> Gen (TxOut CtxTx era)
@@ -660,7 +682,7 @@ genTxTotalCollateral era =
660682
case totalAndReturnCollateralSupportedInEra era of
661683
Nothing -> return TxTotalCollateralNone
662684
Just supp ->
663-
TxTotalCollateral supp <$> genLovelace
685+
TxTotalCollateral supp <$> genPositiveLovelace
664686

665687
genTxFee :: CardanoEra era -> Gen (TxFee era)
666688
genTxFee era =

0 commit comments

Comments
 (0)