Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UTxOIndex agnostic to the distinction between ada and non-ada assets. #3217

Merged
merged 37 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0fc1156
Add `Asset` type, which can represent any asset, including ada.
jonathanknowles Apr 1, 2022
d842a7d
Add fields `index{All,Singletons,Pairs}` to type `UTxOIndex`.
jonathanknowles Apr 1, 2022
44012c0
Add type `UTxOIndex.BundleCategoryNew`.
jonathanknowles Apr 1, 2022
c7144d8
Add function `UTxOIndex.assetsNew`.
jonathanknowles Apr 1, 2022
2f651a7
Add function `UTxOIndex.selectRandomNew`.
jonathanknowles Apr 1, 2022
a809b36
Add function `UTxOIndex.selectRandomWithPriorityNew`.
jonathanknowles Apr 1, 2022
91f62e0
Use new-style indexing in `UTxOIndex.delete`.
jonathanknowles Apr 1, 2022
bad83b6
Use new-style indexing in `UTxOIndex.insertUnsafe`.
jonathanknowles Apr 1, 2022
d756932
Use new-style indexing in `UTxOIndex.indexIsComplete`.
jonathanknowles Apr 1, 2022
f6da5ad
Use new-style indexing in `UTxOIndex.indexIsMinimal`.
jonathanknowles Apr 1, 2022
fbb99c5
Use new-style indexing in `UTxOIndex.assetsConsistent`.
jonathanknowles Apr 1, 2022
29b57fa
Add `prop_SelectionFilterNew_coverage`.
jonathanknowles Apr 1, 2022
6d45346
Use new-style indexing in module `Balance`.
jonathanknowles Apr 1, 2022
4aa222e
Use new-style indexing in `prop_selectRandom_invariant`.
jonathanknowles Apr 1, 2022
c0721fb
Use new-style indexing in `prop_selectRandom_empty`.
jonathanknowles Apr 1, 2022
36c6bd0
Use new-style indexing in `prop_selectRandom_singleton`.
jonathanknowles Apr 1, 2022
fe646a1
Use new-style indexing in `prop_selectRandom_one_any`.
jonathanknowles Apr 1, 2022
bd705cc
Use new-style indexing in `prop_selectRandom_one_withAdaOnly`.
jonathanknowles Apr 1, 2022
2522479
Use new-style indexing in `prop_selectRandom_one_withAsset`.
jonathanknowles Apr 1, 2022
aca9c42
Use new-style indexing in `prop_selectRandom_one_withAssetOnly`.
jonathanknowles Apr 1, 2022
87df2b8
Use new-style indexing in `prop_selectRandom_all_any`.
jonathanknowles Apr 1, 2022
acb64b2
Use new-style indexing in `prop_selectRandom_all_withAdaOnly`.
jonathanknowles Apr 1, 2022
c4c5efa
Use new-style indexing in `prop_selectRandom_all_withAsset`.
jonathanknowles Apr 1, 2022
7057772
Use new-style indexing in `prop_selectRandom_all_withAssetOnly`.
jonathanknowles Apr 1, 2022
6f38cbe
Use new-style indexing in `prop_selectRandomWithPriority`.
jonathanknowles Apr 1, 2022
c920fef
Use new-style indexing in `prop_Small_UTxOIndex_coverage`.
jonathanknowles Apr 2, 2022
0d11552
Use new-style indexing in `prop_Large_UTxOIndex_coverage`.
jonathanknowles Apr 2, 2022
408947b
Use new-style indexing in `prop_assetSelectionLens_givesPriorityToSin…
jonathanknowles Apr 1, 2022
d2eaa96
Use new-style indexing in `prop_coinSelectionLens_givesPriorityToCoins`.
jonathanknowles Apr 1, 2022
8042817
Use new-style indexing in `prop_insert_assets`.
jonathanknowles Apr 2, 2022
54181c7
Remove all old-style indexing functions and types.
jonathanknowles Apr 1, 2022
dadd033
Remove `New` suffix from all new-style indexing functions and types.
jonathanknowles Apr 2, 2022
a1a34e7
Add function `selectQuantityOf` to eliminate repetition.
jonathanknowles Apr 2, 2022
9caca74
Add TODO to `prop_assetSelectionLens_givesPriorityToSingletonAssets`.
jonathanknowles Apr 5, 2022
274a755
Add a further check for index set consistency.
jonathanknowles Apr 5, 2022
bc06ef9
Add comments to constructors of `SelectionFilter` type.
jonathanknowles Apr 5, 2022
c6bf8fa
Make a slight adjustment to the golden test data for `ping-pong-2`.
jonathanknowles Apr 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions lib/core/src/Cardano/Wallet/CoinSelection/Internal/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
Expand Down Expand Up @@ -141,7 +142,7 @@ import Cardano.Wallet.Primitive.Types.TokenQuantity
import Cardano.Wallet.Primitive.Types.Tx
( TokenBundleSizeAssessment (..), TokenBundleSizeAssessor (..) )
import Cardano.Wallet.Primitive.Types.UTxOIndex
( SelectionFilter (..), UTxOIndex (..) )
( Asset (..), SelectionFilter (..), UTxOIndex (..) )
import Cardano.Wallet.Primitive.Types.UTxOSelection
( IsUTxOSelection, UTxOSelection, UTxOSelectionNonEmpty )
import Control.Monad.Extra
Expand Down Expand Up @@ -757,7 +758,7 @@ deriving instance SelectionContext ctx =>

instance SelectionContext ctx => Buildable (InsufficientMinCoinValueError ctx)
where
build (InsufficientMinCoinValueError (a, b) c) = unlinesF
build (InsufficientMinCoinValueError (a, b) c) = unlinesF @[]
[ nameF "Expected min coin value" (build c)
, nameF "Address" (build a)
, nameF "Token bundle" (build (Flat b))
Expand Down Expand Up @@ -1102,7 +1103,7 @@ performSelectionNonEmpty constraints params
, assetsToBurn
}

selectOneEntry = selectCoinQuantity selectionLimit
selectOneEntry = selectQuantityOf AssetLovelace selectionLimit

requiredCost = computeMinimumCost SelectionSkeleton
{ skeletonInputCount = UTxOSelection.selectedSize s
Expand Down Expand Up @@ -1149,7 +1150,9 @@ runSelectionNonEmpty
=> RunSelectionParams u
-> m (Maybe (UTxOSelectionNonEmpty u))
runSelectionNonEmpty = (=<<)
<$> runSelectionNonEmptyWith . selectCoinQuantity . view #selectionLimit
<$> runSelectionNonEmptyWith
. selectQuantityOf AssetLovelace
. view #selectionLimit
<*> runSelection

runSelectionNonEmptyWith
Expand Down Expand Up @@ -1203,7 +1206,7 @@ assetSelectionLens limit strategy (asset, minimumAssetQuantity) = SelectionLens
{ currentQuantity = selectedAssetQuantity asset
, updatedQuantity = selectedAssetQuantity asset
, minimumQuantity = unTokenQuantity minimumAssetQuantity
, selectQuantity = selectAssetQuantity asset limit
, selectQuantity = selectQuantityOf (Asset asset) limit
, selectionStrategy = strategy
}

Expand All @@ -1218,32 +1221,22 @@ coinSelectionLens limit strategy minimumCoinQuantity = SelectionLens
{ currentQuantity = selectedCoinQuantity
, updatedQuantity = selectedCoinQuantity
, minimumQuantity = intCast $ unCoin minimumCoinQuantity
, selectQuantity = selectCoinQuantity limit
, selectQuantity = selectQuantityOf AssetLovelace limit
, selectionStrategy = strategy
}

-- | Specializes 'selectMatchingQuantity' to a particular asset.
--
selectAssetQuantity
selectQuantityOf
:: (MonadRandom m, Ord u)
=> IsUTxOSelection utxoSelection u
=> AssetId
=> Asset
-> SelectionLimit
-> utxoSelection u
-> m (Maybe (UTxOSelectionNonEmpty u))
selectAssetQuantity asset =
selectMatchingQuantity (WithAssetOnly asset :| [WithAsset asset])

-- | Specializes 'selectMatchingQuantity' to ada.
--
selectCoinQuantity
:: (MonadRandom m, Ord u)
=> IsUTxOSelection utxoSelection u
=> SelectionLimit
-> utxoSelection u
-> m (Maybe (UTxOSelectionNonEmpty u))
selectCoinQuantity =
selectMatchingQuantity (WithAdaOnly :| [Any])
selectQuantityOf a = selectMatchingQuantity
[ SelectSingleton a
, SelectPairWith a
, SelectAnyWith a
]

-- | Selects a UTxO entry that matches one of the specified filters.
--
Expand All @@ -1264,7 +1257,7 @@ selectCoinQuantity =
selectMatchingQuantity
:: forall m utxoSelection u. (MonadRandom m, Ord u)
=> IsUTxOSelection utxoSelection u
=> NonEmpty SelectionFilter
=> NonEmpty (SelectionFilter Asset)
-- ^ A list of selection filters to be traversed from left-to-right,
-- in descending order of priority.
-> SelectionLimit
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Wallet/Primitive/Types/UTxOIndex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module Cardano.Wallet.Primitive.Types.UTxOIndex
, disjoint

-- * Selection
, Asset (..)
, SelectionFilter (..)
, selectRandom
, selectRandomWithPriority
Expand Down
Loading