Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Jul 29, 2021
1 parent ae7a44c commit ac1e615
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/core/src/Cardano/Wallet/Primitive/CoinSelection/Collateral.hs
Expand Up @@ -108,20 +108,28 @@ selectCollateralSmallest params =
Nothing ->
Left (SelectCollateralError mempty)
where
coinsToConsider :: [(inputId, Coin)]
coinsToConsider = coinsAvailable
& Map.toList
& L.sortOn snd
& takeUntil ((>= minimumCollateralAmount) . snd)

numberOfCoinsToConsider :: Int
numberOfCoinsToConsider = length coinsToConsider

smallestValidCombination :: Maybe (Map inputId Coin)
smallestValidCombination =
listToMaybe $ L.sortOn F.fold validCombinations
where
validCombinations :: [Map inputId Coin]
validCombinations = mapMaybe smallestValidCombinationOfSize
validCombinations =
mapMaybe smallestValidCombinationOfSize
[1 .. maximumCollateralEntryCount]

smallestValidCombinationOfSize :: Int -> Maybe (Map inputId Coin)
smallestValidCombinationOfSize size = guardSearchSpaceSize $
-- TODO: Pre-filter the list, removing anything above the smallest
-- single coin that satisfies the minimum.
coinsAvailable
& Map.toList
smallestValidCombinationOfSize size =
guardSearchSpaceSize
$ coinsToConsider
& (`subsequencesOfSize` size)
& fmap (\ics -> (ics, F.foldMap snd ics))
& L.sortOn snd
Expand All @@ -138,7 +146,7 @@ selectCollateralSmallest params =
_ -> id
where
requiredSearchSpaceSize =
Map.size coinsAvailable `numberOfSubsequencesOfSize` size
numberOfCoinsToConsider `numberOfSubsequencesOfSize` size

SelectCollateralParams
{ coinsAvailable
Expand Down Expand Up @@ -218,3 +226,10 @@ subsequencesOfSize xs k
(next ++ [[]])
where
next = subsequencesBySize xs

--------------------------------------------------------------------------------
-- Utility functions
--------------------------------------------------------------------------------

takeUntil :: (a -> Bool) -> [a] -> [a]
takeUntil p = foldr (\x ys -> x : if p x then [] else ys) []

0 comments on commit ac1e615

Please sign in to comment.