From 63ce68928171358f0f8e470847dcb7d81c488a18 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Mon, 1 Feb 2021 15:51:24 +0100 Subject: [PATCH] allow constructing change output from _any_ input In only a few days, this has proven to be a quite major issue users have been running into. The thing is that we can't really control how the shape of the UTxO evolves. Quite easily, a user can end up with large quantity of Ada bound to a particular asset. Before this commit, when transacting on other assets, these Ada would've been locked and the user will be granted with a 'cannot_cover_fee' error, despite having plenty of Ada in his/her wallet. However, because of the way we construct change outputs, it is rather safe and easy to also select from any inputs and construct change outputs accordingly. As a matter of fact, the change construction is an iterative trial-and-error process. That is, it works in the following steps: - From a given set of input, try to construct valid change output to cover for the transaction need - If impossible, select another input and try again. Selecting an extra input which contains some potentially new assets will result in a completely different change output. However, since we are re-construct the entire change output sets on each iteration, it doesn't matter much. This processus is still imperfect in the sense that there may be some valid subset of the selection which can result in a valid transaction, whereas some other path may not. --- .../Primitive/CoinSelection/MA/RoundRobin.hs | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection/MA/RoundRobin.hs b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection/MA/RoundRobin.hs index 3b290c2d1a6..751e893dbbf 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection/MA/RoundRobin.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection/MA/RoundRobin.hs @@ -394,9 +394,8 @@ performSelection minCoinValueFor costFor criteria state <- runSelection selectionLimit extraCoinSource utxoAvailable balanceRequired let balanceSelected = fullBalance (selected state) extraCoinSource - if balanceRequired `leq` balanceSelected then do - let predictedChange = NE.toList $ predictChange (selected state) - makeChangeRepeatedly predictedChange state + if balanceRequired `leq` balanceSelected then + makeChangeRepeatedly state else pure $ Left $ SelectionInsufficient $ SelectionInsufficientError @@ -494,11 +493,11 @@ performSelection minCoinValueFor costFor criteria -- ada-only inputs are available. -- makeChangeRepeatedly - :: [Set AssetId] - -> SelectionState + :: SelectionState -> m (Either SelectionError (SelectionResult TokenBundle)) - makeChangeRepeatedly changeSkeleton s@SelectionState{selected,leftover} = do + makeChangeRepeatedly s@SelectionState{selected,leftover} = do let inputsSelected = mkInputsSelected selected + let changeSkeleton = NE.toList $ predictChange selected let cost = costFor SelectionSkeleton { inputsSkeleton = selected @@ -523,15 +522,14 @@ performSelection minCoinValueFor costFor criteria , utxoRemaining = leftover } - Left changeErr -> - let - selectionErr = Left $ UnableToConstructChange changeErr - in - selectMatchingQuantity selectionLimit (WithAdaOnly :| []) s - >>= - maybe - (pure selectionErr) - (makeChangeRepeatedly changeSkeleton) + Left changeErr -> do + result <- s & + selectMatchingQuantity selectionLimit (WithAdaOnly :| [ Any ]) + case result of + Nothing -> + pure $ Left $ UnableToConstructChange changeErr + Just s' -> + makeChangeRepeatedly s' invariantSelectAnyInputs = -- This should be impossible, as we have already determined