Skip to content

Commit

Permalink
Rename SelectionInsufficientError to SelectionLimitReachedError.
Browse files Browse the repository at this point in the history
This error relates to the selection limit, and occurs when:

 - the existing selection size has reached (or exceeded) the selection limit.
 - the existing selection is insufficient to cover the required amount.

The existing name did not represent this concept very clearly, so this
commit renames the error to match the specific condition it represents.
  • Loading branch information
jonathanknowles committed Sep 28, 2021
1 parent 1d326a0 commit 95a68a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -3808,7 +3808,7 @@ instance IsServerError ErrSelectAssets where
, "enough funds available in the wallet. I am "
, "missing: ", pretty . Flat $ balanceMissing e
]
Balance.SelectionInsufficient e ->
Balance.SelectionLimitReached e ->
apiError err403 TransactionIsTooBig $ mconcat
[ "I am not able to finalize the transaction "
, "because I need to select additional inputs and "
Expand Down
25 changes: 11 additions & 14 deletions lib/core/src/Cardano/Wallet/Primitive/CoinSelection/Balance.hs
Expand Up @@ -40,14 +40,14 @@ module Cardano.Wallet.Primitive.CoinSelection.Balance
, SelectionResultOf (..)
, SelectionError (..)
, BalanceInsufficientError (..)
, SelectionInsufficientError (..)
, InsufficientMinCoinValueError (..)
, UnableToConstructChangeError (..)

-- * Selection limits
, SelectionLimit
, SelectionLimitOf (..)
, selectionLimitExceeded
, SelectionLimitReachedError (..)

-- * Querying selections
, SelectionDelta (..)
Expand Down Expand Up @@ -612,8 +612,8 @@ selectionMinimumCost c = view #computeMinimumCost c . selectionSkeleton
data SelectionError
= BalanceInsufficient
BalanceInsufficientError
| SelectionInsufficient
SelectionInsufficientError
| SelectionLimitReached
SelectionLimitReachedError
| InsufficientMinCoinValues
(NonEmpty InsufficientMinCoinValueError)
| UnableToConstructChange
Expand All @@ -622,12 +622,9 @@ data SelectionError
deriving (Generic, Eq, Show)

-- | Indicates that the balance of selected UTxO entries was insufficient to
-- cover the balance required.
--
-- This typically occurs when more entries are available, but those entries
-- cannot be selected without breaching the 'selectionLimit'.
-- cover the balance required while remaining within the selection limit.
--
data SelectionInsufficientError = SelectionInsufficientError
data SelectionLimitReachedError = SelectionLimitReachedError
{ utxoBalanceRequired
:: !TokenBundle
-- ^ The UTXO balance required.
Expand Down Expand Up @@ -832,18 +829,18 @@ performSelectionNonEmpty constraints params
}
case maybeSelection of
Nothing | selectionLimit <= MaximumInputLimit 0 ->
selectionInsufficientError []
selectionLimitReachedError []
Nothing ->
pure $ Left EmptyUTxO
Just selection | selectionLimitExceeded selection selectionLimit ->
selectionInsufficientError $ F.toList $
selectionLimitReachedError $ F.toList $
UTxOSelection.selectedList selection
Just selection -> do
let utxoSelected = UTxOSelection.selectedIndex selection
let utxoBalanceSelected = UTxOIndex.balance utxoSelected
if utxoBalanceRequired `leq` utxoBalanceSelected
then makeChangeRepeatedly selection
else selectionInsufficientError (UTxOIndex.toList utxoSelected)
else selectionLimitReachedError (UTxOIndex.toList utxoSelected)
where
SelectionConstraints
{ assessTokenBundleSize
Expand All @@ -860,9 +857,9 @@ performSelectionNonEmpty constraints params
, assetsToBurn
} = params

selectionInsufficientError :: [(TxIn, TxOut)] -> m (Either SelectionError a)
selectionInsufficientError inputsSelected =
pure $ Left $ SelectionInsufficient $ SelectionInsufficientError
selectionLimitReachedError :: [(TxIn, TxOut)] -> m (Either SelectionError a)
selectionLimitReachedError inputsSelected =
pure $ Left $ SelectionLimitReached $ SelectionLimitReachedError
{ inputsSelected
, utxoBalanceRequired
}
Expand Down
Expand Up @@ -32,10 +32,10 @@ import Cardano.Wallet.Primitive.CoinSelection.Balance
, RunSelectionParams (..)
, SelectionConstraints (..)
, SelectionError (..)
, SelectionInsufficientError (..)
, SelectionLens (..)
, SelectionLimit
, SelectionLimitOf (..)
, SelectionLimitReachedError (..)
, SelectionParams
, SelectionParamsOf (..)
, SelectionResult
Expand Down Expand Up @@ -791,7 +791,7 @@ prop_performSelection_small mockConstraints (Blind (Small params)) =

selectionInsufficient :: PerformSelectionResult -> Bool
selectionInsufficient = \case
Left (SelectionInsufficient _) -> True
Left (SelectionLimitReached _) -> True
_ -> False

assetsSpentByUserSpecifiedOutputs :: TokenMap
Expand Down Expand Up @@ -962,8 +962,8 @@ prop_performSelection mockConstraints (Blind params) coverage =
onFailure = \case
BalanceInsufficient e ->
onBalanceInsufficient e
SelectionInsufficient e ->
onSelectionInsufficient e
SelectionLimitReached e ->
onSelectionLimitReached e
InsufficientMinCoinValues es ->
onInsufficientMinCoinValues es
UnableToConstructChange e ->
Expand Down Expand Up @@ -997,23 +997,23 @@ prop_performSelection mockConstraints (Blind params) coverage =
assertWith . (<>) "onBalanceInsufficient: "
BalanceInsufficientError errorBalanceAvailable errorBalanceRequired = e

onSelectionInsufficient e = do
onSelectionLimitReached e = do
monitor $ counterexample $ unlines
[ "required balance:"
, pretty (Flat errorBalanceRequired)
, "selected balance:"
, pretty (Flat errorBalanceSelected)
]
assertOnSelectionInsufficient
assertOnSelectionLimitReached
"selectionLimit <= MaximumInputLimit (length errorInputsSelected)"
(selectionLimit <= MaximumInputLimit (length errorInputsSelected))
assertOnSelectionInsufficient
assertOnSelectionLimitReached
"utxoBalanceRequired == errorBalanceRequired"
(utxoBalanceRequired == errorBalanceRequired)
where
assertOnSelectionInsufficient =
assertWith . (<>) "onSelectionInsufficient: "
SelectionInsufficientError
assertOnSelectionLimitReached =
assertWith . (<>) "onSelectionLimitReached: "
SelectionLimitReachedError
errorBalanceRequired errorInputsSelected = e
errorBalanceSelected =
F.foldMap (view #tokens . snd) errorInputsSelected
Expand Down

0 comments on commit 95a68a7

Please sign in to comment.