|
1 |
| -{-# LANGUAGE RankNTypes #-} |
| 1 | +{-# LANGUAGE RankNTypes #-} |
| 2 | +{-# LANGUAGE ViewPatterns #-} |
2 | 3 | {-# OPTIONS_GHC -fno-warn-orphans #-}
|
3 | 4 |
|
4 | 5 | module Cardano.Wallet.Kernel.CoinSelection.FromGeneric (
|
@@ -266,7 +267,23 @@ runCoinSelT :: forall m. Monad m
|
266 | 267 | => NonEmpty (Output (Dom utxo))
|
267 | 268 | -> CoinSelT utxo CoinSelHardErr m [CoinSelResult (Dom utxo)])
|
268 | 269 | -> CoinSelPolicy Core.Utxo m CoinSelFinalResult
|
269 |
| -runCoinSelT opts pickUtxo policy request utxo = do |
| 270 | +runCoinSelT opts pickUtxo policy (NE.sortBy (flip (comparing outVal)) -> request) utxo = do |
| 271 | + -- NOTE: we sort the payees by output value, to maximise our chances of succees. |
| 272 | + -- In particular, let's consider a scenario where: |
| 273 | + -- |
| 274 | + -- 1. We have a payment request with two outputs, one smaller and one |
| 275 | + -- larger (but both relatively large) |
| 276 | + -- 2. Random selection tries to cover the smaller one first, fail because |
| 277 | + -- it exceeds the maximum number of inputs, fall back on largest first, |
| 278 | + -- which will pick the n largest outputs, and then traverse from from |
| 279 | + -- small to large to cover the payment. |
| 280 | + -- 3. It then tries to deal with the larger output, and fail because in |
| 281 | + -- step (2) we picked an output we needed due to only consider the n |
| 282 | + -- largest outputs (rather than sorting the entire UTxO, |
| 283 | + -- which would be much too expensive). |
| 284 | + -- |
| 285 | + -- Therefore, just always considering them in order from large to small |
| 286 | + -- is probably a good idea. |
270 | 287 | mSelection <- unwrapCoinSelT policy' utxo
|
271 | 288 | case mSelection of
|
272 | 289 | Left err -> return (Left err)
|
|
0 commit comments