Copyright | © 2021 IOHK |
---|---|
License | Apache-2.0 |
Safe Haskell | None |
Language | Haskell2010 |
Provides functions for selecting coins for use as collateral from a UTxO set.
See the documentation for
performSelection
for more details.
Synopsis
- performSelection :: forall u. Ord u => PerformSelection u
- type PerformSelection u = SelectionConstraints -> SelectionParams u -> Either ( SelectionCollateralError u) ( SelectionResult u)
- data SelectionConstraints = SelectionConstraints { }
-
data
SelectionParams
u =
SelectionParams
{
- coinsAvailable :: Map u Coin
- minimumSelectionAmount :: Coin
-
newtype
SelectionResult
u =
SelectionResult
{
- coinsSelected :: Map u Coin
- selectionResultEmpty :: SelectionResult u
- data SelectionCollateralError u = SelectionCollateralError { }
- data SearchSpaceLimit
- searchSpaceLimitDefault :: SearchSpaceLimit
- selectCollateralSmallest :: forall u. Ord u => PerformSelection u
- selectCollateralLargest :: forall u. Ord u => PerformSelection u
- data SearchSpaceRequirement
- guardSearchSpaceSize :: SearchSpaceRequirement -> SearchSpaceLimit -> Maybe a -> Maybe a
- submaps :: forall a b. ( Ord a, Ord b) => Map a b -> Set ( Map a b)
- subsequencesOfSize :: [a] -> Int -> [[a]]
- numberOfSubsequencesOfSize :: Int -> Int -> Maybe Int
- firstRight :: NonEmpty (a -> Either e r) -> a -> Either e r
- takeUntil :: (a -> Bool ) -> [a] -> [a]
Public API
performSelection :: forall u. Ord u => PerformSelection u Source #
Selects coins for collateral.
This function tries two strategies in the following order, picking the first strategy that succeeds:
- Attempt to select an amount of collateral that is as small as possible.
- Attempt to select collateral from the largest coins available.
The first strategy, given unlimited computation time, will always produce an
optimal result: the smallest possible amount of collateral. However, if the
required search space is large, and if the
$sel:searchSpaceLimit:SelectionConstraints
parameter is
set to a value that's smaller than the required search space size, then this
strategy will fail without computing a result.
The second strategy sacrifices optimality and always produces a result if one is available, by looking only at the very largest coins available. This result can be computed very quickly, without using much search space.
The combination of these two strategies means that we can satisfy the following properties:
If the attempt to select collateral succeeds:
>>>
sum coinsSelected ≥ minimumSelectionAmount
>>>
size coinsSelected ≤ maximumSelectionSize
>>>
coinsSelected ⊆ coinsAvailable
If the attempt to select collateral fails:
>>>
sum largestCombinationAvailable < minimumSelectionAmount
>>>
size largestCombinationAvailable ≤ maximumSelectionSize
>>>
largestCombinationAvailable ⊆ coinsAvailable
type PerformSelection u = SelectionConstraints -> SelectionParams u -> Either ( SelectionCollateralError u) ( SelectionResult u) Source #
The type of all functions that perform selections.
data SelectionConstraints Source #
Specifies all constraints required for collateral selection.
Selection constraints:
- are dependent on the current set of protocol parameters.
- are not specific to a given selection.
- place limits on the selection algorithm, enabling it to produce selections that are acceptable to the ledger.
SelectionConstraints | |
|
Instances
data SelectionParams u Source #
Specifies all parameters that are specific to a given selection.
SelectionParams | |
|
Instances
newtype SelectionResult u Source #
Represents a successful selection of collateral.
SelectionResult | |
|
Instances
Eq u => Eq ( SelectionResult u) Source # | |
Defined in Cardano.Wallet.CoinSelection.Internal.Collateral (==) :: SelectionResult u -> SelectionResult u -> Bool Source # (/=) :: SelectionResult u -> SelectionResult u -> Bool Source # |
|
Show u => Show ( SelectionResult u) Source # | |
|
|
Generic ( SelectionResult u) Source # | |
Defined in Cardano.Wallet.CoinSelection.Internal.Collateral from :: SelectionResult u -> Rep ( SelectionResult u) x Source # to :: Rep ( SelectionResult u) x -> SelectionResult u Source # |
|
type Rep ( SelectionResult u) Source # | |
Defined in Cardano.Wallet.CoinSelection.Internal.Collateral
type
Rep
(
SelectionResult
u) =
D1
('
MetaData
"SelectionResult" "Cardano.Wallet.CoinSelection.Internal.Collateral" "cardano-wallet-core-2022.7.1-AGKhlyz9liLKN3QqZD1gj" '
True
) (
C1
('
MetaCons
"SelectionResult" '
PrefixI
'
True
) (
S1
('
MetaSel
('
Just
"coinsSelected") '
NoSourceUnpackedness
'
NoSourceStrictness
'
DecidedLazy
) (
Rec0
(
Map
u
Coin
))))
|
selectionResultEmpty :: SelectionResult u Source #
A completely empty result, with no inputs selected.
data SelectionCollateralError u Source #
Represents an unsuccessful attempt to select collateral.
SelectionCollateralError | |
|
Instances
data SearchSpaceLimit Source #
Specifies an upper bound on the search space size.
SearchSpaceLimit Int |
Specifies an upper bound on the number of coin combinations that can be considered in any single step. |
UnsafeNoSearchSpaceLimit |
Specifies that there is no search space limit. This should only be used for testing purposes. |
Instances
Eq SearchSpaceLimit Source # | |
Defined in Cardano.Wallet.CoinSelection.Internal.Collateral (==) :: SearchSpaceLimit -> SearchSpaceLimit -> Bool Source # (/=) :: SearchSpaceLimit -> SearchSpaceLimit -> Bool Source # |
|
Show SearchSpaceLimit Source # | |
|
searchSpaceLimitDefault :: SearchSpaceLimit Source #
The default search space limit.
This constant is used by the test suite, so we can be reasonably confident that performing selections with this limit will not use inordinate amounts of time and space.
Internal API
Selecting collateral by giving priority to smallest values first
selectCollateralSmallest :: forall u. Ord u => PerformSelection u Source #
Attempts to select an amount of collateral that is as small as possible.
This function, given unlimited computation time, will always produce an
optimal result: the smallest possible amount of collateral. However, if the
required search space is large, and if the
$sel:searchSpaceLimit:SelectionConstraints
parameter is
set to a value that's smaller than the required search space size, then this
function will return without computing a result.
Selecting collateral by giving priority to largest values first
selectCollateralLargest :: forall u. Ord u => PerformSelection u Source #
Selects collateral from the largest coins available.
This function sacrifices optimality and always produces a result if one is available, by looking only at the very largest coins available.
This result can be computed very quickly, without using much search space.
Guarding search space size
data SearchSpaceRequirement Source #
SearchSpaceRequirement Int |
Indicates a known search space requirement. |
SearchSpaceRequirementUnknown |
Indicates that the search space requirement is unknown. |
:: SearchSpaceRequirement |
The search space requirement |
-> SearchSpaceLimit |
The search space limit |
-> Maybe a |
A computation that potentially yields a value |
-> Maybe a |
The guarded computation |
Generating submaps
submaps :: forall a b. ( Ord a, Ord b) => Map a b -> Set ( Map a b) Source #
Generates all submaps of a given map.
This function is analogous to
powerSet
.
For a map
m
of size
n
, this function will generate all possible submaps,
including the empty map and the original map
m
.
Generating subsequences
:: [a] |
The sequence from which to generate subsequences. |
-> Int |
The size
|
-> [[a]] |
All subsequences of size
|
Generates all subsequences of size
k
from a particular sequence.
Warning: this function can use an excessive amount of time and space.
To check how many results would be returned without actually generating
them, use the
numberOfSubsequencesOfSize
function.
Properties:
>>>
all (== k) (length <$> xs `subsequencesOfSize` k)
>>>
length (xs `subsequencesOfSize` k) ==
>>>
length xs `numberOfSubsequencesOfSize` k
numberOfSubsequencesOfSize Source #
Computes the number of subsequences generated by
subsequencesOfSize
.
This function can be used to determine whether calling
subsequencesOfSize
would use an excessive amount of time and space, and if so, avoid calling
it.
Returns
Nothing
if the result is larger than 'maxBound :: Int'.
Control flow
firstRight :: NonEmpty (a -> Either e r) -> a -> Either e r Source #
Applies a sequence of functions to an argument until one succeeds.
This function iterates through the specified sequence from left to right,
applying each function to the given argument, and returning the very first
Right
result encountered, without evaluating the subsequent functions.
If none of the given functions produces a
Right
result, then this function
returns the
Left
result produced by the last function in the sequence.