Skip to content

Commit

Permalink
Prevent the migration algorithm from creating money.
Browse files Browse the repository at this point in the history
The migration algorithm should satisfy the following property:

>>> Total value of all inputs >= sum of selection change coins

Before this change, it was possible to cause the algorithm to violate
this property by giving it a single input value lower than the dust
threshold.

This change adds a guard to the `mkCoinSelection` function, preventing
it from creating a change coin if that change coin would be higher than
the total value of all inputs.
  • Loading branch information
jonathanknowles committed May 12, 2020
1 parent 50b2400 commit 1ecdf26
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/library/Cardano/CoinSelection/Algorithm/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Cardano.CoinSelection
, CoinSelectionLimit (..)
, coinMapFromList
, coinMapToList
, coinMapValue
, sumChange
, sumInputs
)
Expand Down Expand Up @@ -118,14 +119,16 @@ selectCoins options (BatchSize batchSize) utxo =
where
inputs = coinMapFromList inputEntries
outputs = mempty
change =
if null nonDustInputCoins
then [C.succ threshold]
else nonDustInputCoins
threshold = unDustThreshold dustThreshold
change
| null nonDustInputCoins && totalInputValue >= smallestNonDustCoin =
[smallestNonDustCoin]
| otherwise =
nonDustInputCoins
nonDustInputCoins = filter
(not . isDust dustThreshold)
(entryValue <$> inputEntries)
smallestNonDustCoin = C.succ $ unDustThreshold dustThreshold
totalInputValue = coinMapValue inputs

-- | Attempt to balance the coin selection by reducing or increasing the
-- change values based on the computed fees.
Expand Down

0 comments on commit 1ecdf26

Please sign in to comment.