Skip to content

Commit

Permalink
Add property prop_toBalanceConstraintsParams_computeMinimumCost.
Browse files Browse the repository at this point in the history
This property verifies that the `toBalanceConstraintsParams` function
adjusts the `computeMinimumCost` function in an appropriate way,
namely that:

- the cost is only increased when collateral is required; and that
- the cost is increased by the correct amount.
  • Loading branch information
jonathanknowles committed Oct 11, 2021
1 parent 438748b commit fbf3c8e
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelectionSpec.hs
Expand Up @@ -32,7 +32,9 @@ import Cardano.Wallet.Primitive.CoinSelection
, toBalanceConstraintsParams
)
import Cardano.Wallet.Primitive.CoinSelection.Balance
( SelectionLimit )
( SelectionLimit, SelectionSkeleton )
import Cardano.Wallet.Primitive.CoinSelection.Balance.Gen
( genSelectionSkeleton, shrinkSelectionSkeleton )
import Cardano.Wallet.Primitive.CoinSelection.BalanceSpec
( MockAssessTokenBundleSize
, MockComputeMinimumAdaQuantity
Expand Down Expand Up @@ -82,7 +84,7 @@ import Data.Function
import Data.Functor
( (<&>) )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
( over, view, (^.) )
import Data.Maybe
( isJust )
import GHC.Generics
Expand Down Expand Up @@ -142,6 +144,8 @@ spec = describe "Cardano.Wallet.Primitive.CoinSelectionSpec" $ do

parallel $ describe "Constructing balance constraints and parameters" $ do

it "prop_toBalanceConstraintsParams_computeMinimumCost" $
property prop_toBalanceConstraintsParams_computeMinimumCost
it "prop_toBalanceConstraintsParams_computeSelectionLimit" $
property prop_toBalanceConstraintsParams_computeSelectionLimit

Expand Down Expand Up @@ -270,6 +274,58 @@ prop_performSelection_onSuccess_hasSuitableCollateral cs _ps selection =
-- Construction of balance constraints and parameters
--------------------------------------------------------------------------------

prop_toBalanceConstraintsParams_computeMinimumCost
:: MockSelectionConstraints
-> SelectionParams
-> SelectionSkeleton
-> Property
prop_toBalanceConstraintsParams_computeMinimumCost
mockConstraints params skeleton =
checkCoverage $
cover 10 (selectionCollateralRequired params)
"collateral required: yes" $
cover 10 (not (selectionCollateralRequired params))
"collateral required: no" $
cover 10 (costOriginal < costAdjusted)
"cost (original) < cost (adjusted)" $
report costOriginal
"cost (original)" $
report costAdjusted
"cost (adjusted)" $
if selectionCollateralRequired params
then
conjoin
[ costOriginal <= costAdjusted
, costOriginal ==
( computeMinimumCostAdjusted
. over #skeletonInputCount
(subtract maximumCollateralInputCount)
$ skeleton
)
]
else
costOriginal === costAdjusted
where
constraints :: SelectionConstraints
constraints = unMockSelectionConstraints mockConstraints

maximumCollateralInputCount :: Int
maximumCollateralInputCount = constraints ^. #maximumCollateralInputCount

computeMinimumCostOriginal :: SelectionSkeleton -> Coin
computeMinimumCostOriginal = constraints ^. #computeMinimumCost

computeMinimumCostAdjusted :: SelectionSkeleton -> Coin
computeMinimumCostAdjusted =
toBalanceConstraintsParams (constraints, params)
& fst & view #computeMinimumCost

costOriginal :: Coin
costOriginal = computeMinimumCostOriginal skeleton

costAdjusted :: Coin
costAdjusted = computeMinimumCostAdjusted skeleton

prop_toBalanceConstraintsParams_computeSelectionLimit
:: MockSelectionConstraints
-> SelectionParams
Expand Down Expand Up @@ -688,3 +744,7 @@ instance Arbitrary MockSelectionConstraints where
instance Arbitrary SelectionParams where
arbitrary = genSelectionParams
shrink = shrinkSelectionParams

instance Arbitrary SelectionSkeleton where
arbitrary = genSelectionSkeleton
shrink = shrinkSelectionSkeleton

0 comments on commit fbf3c8e

Please sign in to comment.