Skip to content

Commit

Permalink
Introduce function reduceSelectionLimitBy.
Browse files Browse the repository at this point in the history
This function reduces a selection limit by a given amount.

We use this function to replace the incorrect subtraction in
`adjustComputeSelectionLimit`.

Background:

The `subtract` function (from `Prelude`) is equivalent to `flip (-)`,
which means that `subtract a b` is equivalent to `b - a`, rather than
the other way round.

Thanks to @KtorZ for discovering this.
  • Loading branch information
jonathanknowles committed Oct 11, 2021
1 parent 63df46a commit 92d1fed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
Expand Up @@ -246,8 +246,8 @@ toBalanceConstraintsParams (constraints, params) =
-- there is still space available.
--
adjustSelectionLimit :: SelectionLimit -> SelectionLimit
adjustSelectionLimit = fmap
(subtract (view #maximumCollateralInputCount constraints))
adjustSelectionLimit = flip Balance.reduceSelectionLimitBy
(view #maximumCollateralInputCount constraints)

balanceParams = Balance.SelectionParams
{ assetsToBurn =
Expand Down
16 changes: 16 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/CoinSelection/Balance.hs
Expand Up @@ -47,6 +47,7 @@ module Cardano.Wallet.Primitive.CoinSelection.Balance
, SelectionLimitOf (..)
, selectionLimitExceeded
, SelectionLimitReachedError (..)
, reduceSelectionLimitBy

-- * Querying selections
, SelectionDelta (..)
Expand Down Expand Up @@ -442,6 +443,21 @@ selectionLimitExceeded s = \case
NoLimit -> False
MaximumInputLimit n -> UTxOSelection.selectedSize s > n

-- | Reduces a selection limit by a given reduction amount.
--
-- If the given reduction amount is positive, then this function will reduce
-- the selection limit by that amount.
--
-- If the given reduction amount is zero or negative, then this function will
-- return the original limit unchanged.
--
reduceSelectionLimitBy :: SelectionLimit -> Int -> SelectionLimit
reduceSelectionLimitBy limit reduction
| reduction <= 0 =
limit
| otherwise =
subtract reduction <$> limit

type SelectionResult = SelectionResultOf [TxOut]

-- | The result of performing a successful selection.
Expand Down

0 comments on commit 92d1fed

Please sign in to comment.