Skip to content

Commit

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

- the selection limit is only reduced when collateral is required; and that
- the selection limit is reduced by the correct amount.
  • Loading branch information
jonathanknowles committed Oct 11, 2021
1 parent ff11b8e commit fecb0ae
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
Expand Up @@ -64,6 +64,7 @@ module Cardano.Wallet.Primitive.CoinSelection
-- * Internal types and functions
, ComputeMinimumCollateralParams (..)
, computeMinimumCollateral
, toBalanceConstraintsParams

) where

Expand Down
71 changes: 70 additions & 1 deletion lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelectionSpec.hs
Expand Up @@ -29,7 +29,10 @@ import Cardano.Wallet.Primitive.CoinSelection
, selectionHasValidSurplus
, selectionMinimumCollateral
, selectionMinimumCost
, toBalanceConstraintsParams
)
import Cardano.Wallet.Primitive.CoinSelection.Balance
( SelectionLimit )
import Cardano.Wallet.Primitive.CoinSelection.BalanceSpec
( MockAssessTokenBundleSize
, MockComputeMinimumAdaQuantity
Expand Down Expand Up @@ -74,8 +77,12 @@ import Control.Monad.Trans.Except
( runExceptT )
import Data.Either
( isLeft, isRight )
import Data.Function
( (&) )
import Data.Functor
( (<&>) )
import Data.Generics.Internal.VL.Lens
( view )
( view, (^.) )
import Data.Maybe
( isJust )
import GHC.Generics
Expand Down Expand Up @@ -133,6 +140,11 @@ spec = describe "Cardano.Wallet.Primitive.CoinSelectionSpec" $ do
prop_performSelection_onSuccess
prop_performSelection_onSuccess_hasSuitableCollateral

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

it "prop_toBalanceConstraintsParams_computeSelectionLimit" $
property prop_toBalanceConstraintsParams_computeSelectionLimit

parallel $ describe "Preparing outputs" $ do

it "prop_prepareOutputsWith_twice" $
Expand Down Expand Up @@ -254,6 +266,63 @@ prop_performSelection_onSuccess_hasSuitableCollateral cs _ps selection =
suitableForCollateral :: (TxIn, TxOut) -> Bool
suitableForCollateral = isJust . view #utxoSuitableForCollateral cs

--------------------------------------------------------------------------------
-- Construction of balance constraints and parameters
--------------------------------------------------------------------------------

-- Tests that function 'toBalanceConstraintsParams' applies the correct
-- transformation to the 'computeSelectionLimit' function.
--
prop_toBalanceConstraintsParams_computeSelectionLimit
:: MockSelectionConstraints
-> SelectionParams
-> Property
prop_toBalanceConstraintsParams_computeSelectionLimit mockConstraints params =
checkCoverage $
cover 10 (selectionCollateralRequired params)
"collateral required: yes" $
cover 10 (not (selectionCollateralRequired params))
"collateral required: no" $
cover 10 (selectionLimitOriginal > selectionLimitAdjusted)
"selection limit (original) > selection limit (adjusted)" $
report selectionLimitOriginal
"selection limit (original)" $
report selectionLimitAdjusted
"selection limit (adjusted)" $
if selectionCollateralRequired params
then
conjoin
[ selectionLimitOriginal >= selectionLimitAdjusted
-- Here we apply a transformation that is the *inverse* of
-- the transformation within 'toBalanceConstraintsParams':
, selectionLimitOriginal ==
(selectionLimitAdjusted <&> (+ maximumCollateralInputCount))
]
else
selectionLimitOriginal === selectionLimitAdjusted
where
constraints :: SelectionConstraints
constraints = unMockSelectionConstraints mockConstraints

maximumCollateralInputCount :: Int
maximumCollateralInputCount = constraints ^. #maximumCollateralInputCount

computeSelectionLimitOriginal :: [TxOut] -> SelectionLimit
computeSelectionLimitOriginal = constraints ^. #computeSelectionLimit

computeSelectionLimitAdjusted :: [TxOut] -> SelectionLimit
computeSelectionLimitAdjusted =
toBalanceConstraintsParams (constraints, params)
& fst & view #computeSelectionLimit

selectionLimitOriginal :: SelectionLimit
selectionLimitOriginal = computeSelectionLimitOriginal
(params ^. #outputsToCover)

selectionLimitAdjusted :: SelectionLimit
selectionLimitAdjusted = computeSelectionLimitAdjusted
(params ^. #outputsToCover)

--------------------------------------------------------------------------------
-- Preparing outputs
--------------------------------------------------------------------------------
Expand Down

0 comments on commit fecb0ae

Please sign in to comment.