Skip to content

Commit b6466c8

Browse files
vrom911v0d1ch
authored andcommitted
Fix after review
1 parent a02c8d1 commit b6466c8

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

hydra-tx/test/Hydra/Tx/DepositSpec.hs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Data.Set qualified as Set
88
import Hydra.Cardano.Api (Coin (..), UTxO, selectLovelace, txOutValue)
99
import Hydra.Tx.Deposit (capUTxO)
1010
import Test.Hydra.Tx.Gen (genUTxOSized)
11-
import Test.QuickCheck (Property, counterexample, (===), (==>))
11+
import Test.QuickCheck (Property, chooseInteger, counterexample, (===), (==>))
1212

1313
spec :: Spec
1414
spec =
@@ -28,13 +28,13 @@ spec =
2828

2929
it "selects UTxO entries up to target amount" $ do
3030
let utxo = genUTxOSized 5 `generateWith` 42
31-
let totalValue = UTxO.totalValue utxo
32-
let target = toInteger (selectLovelace totalValue) `div` 2
31+
let totalValue = UTxO.totalLovelace utxo
32+
let target = chooseInteger (1, toInteger totalValue) `generateWith` 42
3333
let (selected, leftovers) = capUTxO utxo (Coin target)
3434

35-
toInteger (selectLovelace (UTxO.totalValue selected)) `shouldSatisfy` \v -> v <= target
35+
toInteger (UTxO.totalLovelace selected) `shouldSatisfy` \v -> v <= target
3636

37-
UTxO.totalValue selected <> UTxO.totalValue leftovers `shouldBe` totalValue
37+
UTxO.totalValue selected <> UTxO.totalValue leftovers `shouldBe` UTxO.totalValue utxo
3838

3939
let originalSize = length (UTxO.toList utxo)
4040
selectedSize = length (UTxO.toList selected)
@@ -55,10 +55,11 @@ spec =
5555
let largeOutput = genUTxOSized 3 `generateWith` 43
5656
let mixedUTxO = smallOutput <> largeOutput
5757

58-
let target = Coin $ toInteger (selectLovelace (UTxO.totalValue smallOutput)) + 1000
59-
let (selected, _) = capUTxO mixedUTxO target
58+
let totalLovelace = UTxO.totalLovelace mixedUTxO
59+
let target = chooseInteger (1, toInteger totalLovelace) `generateWith` 44
60+
let (selected, _) = capUTxO mixedUTxO (Coin target)
6061

61-
length (UTxO.toList selected) `shouldSatisfy` (> 1)
62+
length (UTxO.toList selected) `shouldSatisfy` (> 0)
6263

6364
describe "property tests" $ do
6465
prop "preserves total value" propPreservesTotalValue
@@ -73,13 +74,10 @@ spec =
7374
propPreservesTotalValue :: UTxO -> Coin -> Property
7475
propPreservesTotalValue utxo target =
7576
let (selected, leftovers) = capUTxO utxo target
76-
inputTotal = UTxO.totalValue utxo
77-
selectedTotal = UTxO.totalValue selected
78-
leftoverTotal = UTxO.totalValue leftovers
79-
inputLovelace = selectLovelace inputTotal
80-
selectedLovelace = selectLovelace selectedTotal
81-
leftoverLovelace = selectLovelace leftoverTotal
82-
in selectedLovelace + leftoverLovelace === inputLovelace
77+
inputTotal = UTxO.totalLovelace utxo
78+
selectedTotal = UTxO.totalLovelace selected
79+
leftoverTotal = UTxO.totalLovelace leftovers
80+
in selectedTotal + leftoverTotal === inputTotal
8381
& counterexample ("Input total: " <> show inputTotal)
8482
& counterexample ("Selected total: " <> show selectedTotal)
8583
& counterexample ("Leftover total: " <> show leftoverTotal)
@@ -88,8 +86,8 @@ propPreservesTotalValue utxo target =
8886
propSelectedValueNeverExceedsTarget :: UTxO -> Coin -> Property
8987
propSelectedValueNeverExceedsTarget utxo target =
9088
let (selected, _) = capUTxO utxo target
91-
selectedTotal = UTxO.totalValue selected
92-
in selectLovelace selectedTotal <= target
89+
selectedTotal = UTxO.totalLovelace selected
90+
in selectedTotal <= target
9391
& counterexample ("Selected total: " <> show selectedTotal)
9492
& counterexample ("Target: " <> show target)
9593

@@ -113,11 +111,11 @@ propGreedySelection utxo target =
113111
propExactTargetWhenPossible :: UTxO -> Coin -> Property
114112
propExactTargetWhenPossible utxo target =
115113
let (selected, _) = capUTxO utxo target
116-
selectedTotal = UTxO.totalValue selected
117-
inputTotal = UTxO.totalValue utxo
118-
in (selectLovelace inputTotal >= target) ==>
119-
( selectLovelace selectedTotal == target
120-
|| selectLovelace selectedTotal == target - 1
114+
selectedTotal = UTxO.totalLovelace selected
115+
inputTotal = UTxO.totalLovelace utxo
116+
in (inputTotal >= target) ==>
117+
( selectedTotal == target
118+
|| selectedTotal == target - 1
121119
)
122120
& counterexample ("Selected total: " <> show selectedTotal)
123121
& counterexample ("Target: " <> show target)
@@ -138,11 +136,11 @@ propMonotonicTarget utxo target1 target2 =
138136
(target1 <= target2) ==>
139137
let (selected1, _) = capUTxO utxo target1
140138
(selected2, _) = capUTxO utxo target2
141-
total1 = UTxO.totalValue selected1
142-
total2 = UTxO.totalValue selected2
143-
in selectLovelace total1 <= selectLovelace total2
144-
& counterexample ("Target1: " <> show target1 <> ", Selected1: " <> show total1)
145-
& counterexample ("Target2: " <> show target2 <> ", Selected2: " <> show total2)
139+
selectedTotal1 = UTxO.totalLovelace selected1
140+
selectedTotal2 = UTxO.totalLovelace selected2
141+
in selectedTotal1 <= selectedTotal2
142+
& counterexample ("Target1: " <> show target1 <> ", Selected1: " <> show selectedTotal1)
143+
& counterexample ("Target2: " <> show target2 <> ", Selected2: " <> show selectedTotal2)
146144

147145
-- | Property: No UTxO loss - all input UTxOs appear in either selected or leftovers
148146
propNoUTxOLoss :: UTxO -> Coin -> Property

0 commit comments

Comments
 (0)