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

Perform verification of SelectionCollateralError values. #2988

Merged
merged 1 commit into from
Oct 22, 2021

Conversation

jonathanknowles
Copy link
Contributor

@jonathanknowles jonathanknowles commented Oct 22, 2021

Issue Number

ADP-1037

Summary

This PR adds verification of SelectionCollateralError values.

We verify that:

  • The reported largestCombinationAvailable value does not include any UTxO entries that are unsuitable for use as collateral.
  • The number of entries within largestCombinationAvailable is not greater than maximumCollateralInputCount.
  • The total value of entries within largestCombinationAvailable is not greater than the computed minimumSelectionAmount (as this would mean that the error was returned erroneously: we could have generated a selection).

Example failures

Collateral suitability

If we introduce a mutation that deliberately removes the collateral suitability check:

diff --git a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
index 994103a68..a137cfc05 100644
--- a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
+++ b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
@@ -316,9 +316,8 @@ toCollateralConstraintsParams balanceResult (constraints, params) =
         }
     collateralParams = Collateral.SelectionParams
         { coinsAvailable =
-            Map.mapMaybeWithKey
-                (curry (view #utxoSuitableForCollateral constraints))
-                (unUTxO (view #utxoAvailableForCollateral params))
+            view (#tokens . #coin) <$> unUTxO
+                (view #utxoAvailableForCollateral params)
         , minimumSelectionAmount =
             computeMinimumCollateral ComputeMinimumCollateralParams
                 { minimumCollateralPercentage =

Then we see a verification failure similar to:

collateralErrorVerificationFailure

Maximum number of collateral inputs

If we introduce a mutation that deliberately passes an incorrect maximum number of collateral inputs to the collateral selection algorithm:

diff --git a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
index 6fc4d4f5d..1b18e0360 100644
--- a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
+++ b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
@@ -305,7 +305,7 @@ toCollateralConstraintsParams balanceResult (constraints, params) =
   where
     collateralConstraints = Collateral.SelectionConstraints
         { maximumSelectionSize =
-            view #maximumCollateralInputCount constraints
+            view #maximumCollateralInputCount constraints + 1
         , searchSpaceLimit =
             -- We use the default search space limit here, as this value is
             -- used in the test suite for 'Collateral.performSelection'. We

Then we see a verification failure similar to:

collateralErrorVerificationFailure2

@jonathanknowles jonathanknowles self-assigned this Oct 22, 2021

-- The subset of the largest reported combination that is suitable for
-- use as collateral. This set should be exactly the same size as the
-- reporte largest combination.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-- reporte largest combination.
-- reported largest combination.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, thanks!

@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1037/test-coin-selection-9 branch from 14571e4 to f2165d7 Compare October 22, 2021 05:38
@jonathanknowles
Copy link
Contributor Author

bors r+

iohk-bors bot added a commit that referenced this pull request Oct 22, 2021
2988: Perform verification of `SelectionCollateralError` values. r=jonathanknowles a=jonathanknowles

## Issue Number

ADP-1037

## Summary

This PR adds verification of `SelectionCollateralError` values.

We verify that:
- The reported `largestCombinationAvailable` value does not include any UTxO entries that are unsuitable for use as collateral.
- The number of entries within `largestCombinationAvailable` is not greater than `maximumCollateralInputCount`.
- The total value of entries within `largestCombinationAvailable` is not greater than the computed `minimumSelectionAmount` (as this would mean that the error was returned erroneously: we **_could_** have generated a selection).

## Example failures

### Collateral suitability

If we introduce a mutation that deliberately removes the collateral suitability check:
```patch
diff --git a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
index 994103a68..a137cfc05 100644
--- a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
+++ b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
@@ -316,9 +316,8 @@ toCollateralConstraintsParams balanceResult (constraints, params) =
         }
     collateralParams = Collateral.SelectionParams
         { coinsAvailable =
-            Map.mapMaybeWithKey
-                (curry (view #utxoSuitableForCollateral constraints))
-                (unUTxO (view #utxoAvailableForCollateral params))
+            view (#tokens . #coin) <$> unUTxO
+                (view #utxoAvailableForCollateral params)
         , minimumSelectionAmount =
             computeMinimumCollateral ComputeMinimumCollateralParams
                 { minimumCollateralPercentage =
```
Then we see a verification failure similar to:

![collateralErrorVerificationFailure](https://user-images.githubusercontent.com/206319/138395759-2cc8d652-6a8e-4f96-a852-3c82734efb2b.png)

### Maximum number of collateral inputs

If we introduce a mutation that deliberately passes an incorrect maximum number of collateral inputs to the collateral selection algorithm:
```patch
diff --git a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
index 6fc4d4f5d..1b18e0360 100644
--- a/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
+++ b/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
@@ -305,7 +305,7 @@ toCollateralConstraintsParams balanceResult (constraints, params) =
   where
     collateralConstraints = Collateral.SelectionConstraints
         { maximumSelectionSize =
-            view #maximumCollateralInputCount constraints
+            view #maximumCollateralInputCount constraints + 1
         , searchSpaceLimit =
             -- We use the default search space limit here, as this value is
             -- used in the test suite for 'Collateral.performSelection'. We
```

Then we see a verification failure similar to:

![collateralErrorVerificationFailure2](https://user-images.githubusercontent.com/206319/138395037-ee779897-bafe-411e-9fdc-b30de41aba89.png)


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

iohk-bors bot commented Oct 22, 2021

Build failed:

Another cached failure.

The cause seems to be from a completely unrelated PR that had several jobs that failed because of resource limitatons on hydra, and these job failures have propagated to the merge attempt for this PR.

I've restarted all the failed jobs manually, and cleared the errors.

@jonathanknowles
Copy link
Contributor Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Oct 22, 2021

Build succeeded:

@iohk-bors iohk-bors bot merged commit 0c1daac into master Oct 22, 2021
@iohk-bors iohk-bors bot deleted the jonathanknowles/adp-1037/test-coin-selection-9 branch October 22, 2021 07:46
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