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

Conversation

jonathanknowles
Copy link
Member

@jonathanknowles jonathanknowles commented Apr 5, 2022

Issue Number

ADP-1419

Summary

This PR makes the UTxOIndex type agnostic to the distinction between ada and non-ada assets.

Motivation

We want to evolve the coin selection algorithm so that:

  • the concept of "value" (provided by inputs and consumed by outputs) is generalized.
  • all asset types are treated equally, with no special treatment for ada versus non-ada assets.

Currently, there are a few obstacles that prevent such a generalization:

  1. The UTxOIndex type indexes ada quantities differently from other assets.
  2. The computeMinimumCost function is ada-specific.
  3. The computeMinimumAdaQuantity function is ada-specific.

This PR tackles the first of these obstacles.

Details

We make the following change to SelectionFilter:

- data SelectionFilter
-     = WithAdaOnly
-     | WithAssetOnly AssetId
-     | WithAsset AssetId
-     | Any
+ data SelectionFilter asset
+     = SelectSingleton asset
+     | SelectPairWith asset
+     | SelectAnyWith asset
+     | SelectAny

We also (temporarily) introduce a type called Asset, which can represent both ada and non-ada assets:

data Asset
    = AssetLovelace
    | Asset AssetId

This allows us to generalize the way we perform random selection for ada and non-ada assets in the Balance module:

 selectAdaQuantity =
-    selectMatchingQuantity (WithAdaOnly :| [Any])
+    selectMatchingQuantity
+        [ SelectSingleton AssetLovelace
+        , SelectPairWith AssetLovelace
+        , SelectAnyWith AssetLovelace
+        ]

 selectNonAdaAssetQuantity asset =
-    selectMatchingQuantity [WithAssetOnly asset, WithAsset asset]
+    selectMatchingQuantity
+        [ SelectSingleton (Asset asset)
+        , SelectPairWith (Asset asset)
+        , SelectAnyWith (Asset asset)
+        ]

In the above functions, selection filters are processed in the following order of priority:

  1. SelectSingleton
    matches UTxOs that contain just the given asset and no other assets
  2. SelectPairWith
    matches UTxOs that contain the given asset and one other asset.
  3. SelectAnyWith
    matches UTxOs that contain the given asset and any number of other assets.

Because both selection functions now share exactly the same priority order, we can simplify these definitions even further by extracting out the shared priority order and parameterizing over the type of asset.

@jonathanknowles jonathanknowles self-assigned this Apr 5, 2022
@jonathanknowles jonathanknowles changed the title WIP: Make UTxOIndex agnostic to the distinction between ada and non-ada assets. Make UTxOIndex agnostic to the distinction between ada and non-ada assets. Apr 5, 2022
jonathanknowles added a commit that referenced this pull request Apr 5, 2022
@jonathanknowles jonathanknowles marked this pull request as ready for review April 5, 2022 08:48
jonathanknowles added a commit that referenced this pull request Apr 5, 2022
jonathanknowles added a commit that referenced this pull request Apr 5, 2022
jonathanknowles added a commit that referenced this pull request Apr 5, 2022
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/generalize-utxo-index branch from f730c15 to 8c6b725 Compare April 5, 2022 09:56
This is meant to provide a replacement for `BundleCategory`, but with
categories that are agnostic to the type of assets.
This is similar to the original `assets` function, but uses new-style
indexing.
This is similar to `selectRandom`, but uses `SelectionFilterNew` instead
of `SelectionFilter`.

The `SelectionFilterNew` type is similar to `SelectionFilter`, but
provides filter conditions that are agnosic to the type of assets.
This is similar to `prop_SelectionFilter_coverage` but covers new filter
conditions.
Additionally, add some coverage checks to verify that:

- sometimes something is selected.
- sometimes nothing is selected.
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/generalize-utxo-index branch from 21a617a to bc06ef9 Compare April 5, 2022 10:37
@jonathanknowles
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Apr 5, 2022
3217: Make `UTxOIndex` agnostic to the distinction between ada and non-ada assets. r=jonathanknowles a=jonathanknowles

## Issue Number

ADP-1419

## Summary

This PR makes the `UTxOIndex` type **_agnostic_** to the distinction between **_ada_** and **_non-ada_** assets.

## Motivation

We want to evolve the coin selection algorithm so that:
- the concept of "value" (provided by inputs and consumed by outputs) is **_generalized_**.
- all asset types are treated equally, with no special treatment for **_ada_** versus **_non-ada_** assets.

Currently, there are a few obstacles that prevent such a generalization:
1. The `UTxOIndex` type indexes ada quantities differently from other assets.
2. The `computeMinimumCost` function is ada-specific.
3. The `computeMinimumAdaQuantity` function is ada-specific.

This PR tackles the **_first_** of these obstacles.

## Details

We make the following change to `SelectionFilter`:

```patch
- data SelectionFilter
-     = WithAdaOnly
-     | WithAssetOnly AssetId
-     | WithAsset AssetId
-     | Any
+ data SelectionFilter asset
+     = SelectSingleton asset
+     | SelectPairWith asset
+     | SelectAnyWith asset
+     | SelectAny
```

We also (temporarily) introduce a type called `Asset`, which can represent both ada and non-ada assets:
```hs
data Asset
    = AssetLovelace
    | Asset AssetId
```

This allows us to generalize the way we perform random selection for ada and non-ada assets in the `Balance` module:
```patch
 selectAdaQuantity =
-    selectMatchingQuantity (WithAdaOnly :| [Any])
+    selectMatchingQuantity
+        [ SelectSingleton AssetLovelace
+        , SelectPairWith AssetLovelace
+        , SelectAnyWith AssetLovelace
+        ]

 selectNonAdaAssetQuantity asset =
-    selectMatchingQuantity [WithAssetOnly asset, WithAsset asset]
+    selectMatchingQuantity
+        [ SelectSingleton (Asset asset)
+        , SelectPairWith (Asset asset)
+        , SelectAnyWith (Asset asset)
+        ]
```
In the above functions, selection filters are processed in the following order of priority:
1. `SelectSingleton`
    matches UTxOs that contain **_just_** the given asset and **_no other_** assets
2. `SelectPairWith`
    matches UTxOs that contain the given asset and **_one other_** asset.
2. `SelectAnyWith`
    matches UTxOs that contain the given asset and **_any number_** of other assets.

Because both selection functions now share **_exactly the same priority order_**, we can simplify these definitions even further by extracting out the shared priority order and parameterizing over the type of asset.

Co-authored-by: Jonathan Knowles <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 5, 2022

Build failed:

It looks as though this affects (very slightly) one of the golden tests for balanceTransaction. I suspect that this may be because we try slightly harder than before to select UTxOs with singleton bundles.

I've pushed a temporary commit that fixes the test, but I'll continue to investigate.

@jonathanknowles
Copy link
Member Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 5, 2022

Build succeeded:

@iohk-bors iohk-bors bot merged commit 73223bb into master Apr 5, 2022
@iohk-bors iohk-bors bot deleted the jonathanknowles/generalize-utxo-index branch April 5, 2022 12:44
WilliamKingNoel-Bot pushed a commit that referenced this pull request Apr 5, 2022
iohk-bors bot added a commit that referenced this pull request Apr 6, 2022
3220: Generalize `SelectionFilter` tests within `UTxOIndexSpec`. r=jonathanknowles a=jonathanknowles

## Issue Number

ADP-1419

## Summary

This PR follows on from #3217. It:

- Replaces all `prop_selectRandom_one*` properties with a generalized `prop_selectRandom` property.
- Replaces all `prop_selectRandom_all*` properties with a generalized `prop_selectRandom_all` property.

In both cases, we use coverage checks to verify that we maintain the same coverage as before.


Co-authored-by: Jonathan Knowles <[email protected]>
iohk-bors bot added a commit that referenced this pull request Apr 6, 2022
3220: Generalize `SelectionFilter` tests within `UTxOIndexSpec`. r=jonathanknowles a=jonathanknowles

## Issue Number

ADP-1419

## Summary

This PR follows on from #3217. It:

- Replaces all `prop_selectRandom_one*` properties with a generalized `prop_selectRandom` property.
- Replaces all `prop_selectRandom_all*` properties with a generalized `prop_selectRandom_all` property.

In both cases, we use coverage checks to verify that we maintain the same coverage as before.


Co-authored-by: Jonathan Knowles <[email protected]>
paolino pushed a commit that referenced this pull request Apr 26, 2022
paolino pushed a commit that referenced this pull request Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants