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

Add coverage for toBalanceConstraintsParams. #2962

Merged

Conversation

jonathanknowles
Copy link
Contributor

@jonathanknowles jonathanknowles commented Oct 11, 2021

Issue Number

ADP-1037

Summary

This PR:

  • replaces an erroneous call to subtract with a dedicated function reduceSelectionLimitBy.
  • provides property tests to verify that reduceSelectionLimitBy has the correct behaviour for all selection limits.
  • provides property tests to verify that toBalanceConstraintsParams performs the correct adjustment to the computeMinimumCost function.
  • provides property tests to verify that toBalanceConstraintsParams performs the correct adjustment to the computeSelectionLimit function.

Background

The subtract function (from Prelude) is equivalent to flip (-), which means that subtract a b is equivalent to b - a, rather than the other way round.

Thanks to @KtorZ for discovering this.

@jonathanknowles jonathanknowles self-assigned this Oct 11, 2021
@jonathanknowles jonathanknowles changed the title Fix order of arguments to subtract in adjustComputeSelectionLimit. Fix subtraction in adjustComputeSelectionLimit. Oct 11, 2021
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1037/fix-subtract-argument-order branch 5 times, most recently from 433d8f0 to 5acc15b Compare October 11, 2021 05:18
@jonathanknowles jonathanknowles changed the title Fix subtraction in adjustComputeSelectionLimit. Add coverage for toBalanceConstraintsParams. Oct 11, 2021
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1037/fix-subtract-argument-order branch 4 times, most recently from f9aacc4 to 14d5bac Compare October 11, 2021 06:05
Copy link
Member

@Anviking Anviking left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the walkthrough call. Left one comment / thought.

Comment on lines +3723 to +3746
prop_reduceSelectionLimitBy_lessThanOrEqual
:: SelectionLimit -> Int -> Property
prop_reduceSelectionLimitBy_lessThanOrEqual limit reduction =
prop_reduceSelectionLimitBy_coverage_limit limit .&&.
prop_reduceSelectionLimitBy_coverage_reduction reduction .&&.
limit `reduceSelectionLimitBy` reduction <= limit

prop_reduceSelectionLimitBy_reductionNegative
:: SelectionLimit -> Negative Int -> Property
prop_reduceSelectionLimitBy_reductionNegative limit (Negative reduction) =
prop_reduceSelectionLimitBy_coverage_limit limit .&&.
limit `reduceSelectionLimitBy` reduction == limit

prop_reduceSelectionLimitBy_reductionZero
:: SelectionLimit -> Property
prop_reduceSelectionLimitBy_reductionZero limit =
prop_reduceSelectionLimitBy_coverage_limit limit .&&.
limit `reduceSelectionLimitBy` 0 == limit

prop_reduceSelectionLimitBy_reductionPositive
:: SelectionLimit -> Positive Int -> Property
prop_reduceSelectionLimitBy_reductionPositive limit (Positive reduction) =
prop_reduceSelectionLimitBy_coverage_limit limit .&&.
limit == fmap (+ reduction) (limit `reduceSelectionLimitBy` reduction)
Copy link
Member

Choose a reason for hiding this comment

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

👍

& fst & view #computeMinimumCost

costOriginal :: Coin
costOriginal = computeMinimumCostOriginal skeleton
Copy link
Member

Choose a reason for hiding this comment

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

Looking at this again it occurred to me: isn't it strange that the outer constraints contain the unadjusted and wrong minimum cost? If someone were to call the outer computeMinimumCost they would get the wrong results?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If someone were to call the outer computeMinimumCost they would get the wrong results?

The only way a caller could call this function is if they prepared a SelectionSkeleton externally.

If the caller chose to do this, then the onus would be on the caller to provide a correct SelectionSkeleton that accurately represented the transaction for which they wanted to compute a minimum cost. But assuming they did prepare an accurate SelectionSkeleton, then this function would still give them the correct result, since the contract required of computeMinimumCost is that:

computeMinimumCost skeleton >= actualCostOfSerializedTx skeleton

@jonathanknowles
Copy link
Contributor Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Oct 11, 2021

Merge conflict.

This function reduces a selection limit by a given amount.

We use this function to replace the incorrect subtraction in
`adjustComputeSelectionLimit`.

Background:

The `subtract` function (from `Prelude`) is equivalent to `flip (-)`,
which means that `subtract a b` is equivalent to `b - a`, rather than
the other way round.

Thanks to @KtorZ for discovering this.
This property verifies that the `toBalanceConstraintsParams` function
adjusts the `computeSelectionLimit` function in an appropriate way,
namely that:

- the selection limit is only reduced when collateral is required; and that
- the selection limit is reduced by the correct amount.
This property verifies that the `toBalanceConstraintsParams` function
adjusts the `computeMinimumCost` function in an appropriate way,
namely that:

- the cost is only increased when collateral is required; and that
- the cost is increased by the correct amount.
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1037/fix-subtract-argument-order branch from 14d5bac to e3ea261 Compare October 11, 2021 12:08
@jonathanknowles
Copy link
Contributor Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Oct 11, 2021

Build succeeded:

@iohk-bors iohk-bors bot merged commit 2e1760a into master Oct 11, 2021
@iohk-bors iohk-bors bot deleted the jonathanknowles/adp-1037/fix-subtract-argument-order branch October 11, 2021 13:15
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