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 more property tests for distributeSurplus. #3243

Merged
merged 6 commits into from
Apr 21, 2022

Conversation

jonathanknowles
Copy link
Member

@jonathanknowles jonathanknowles commented Apr 21, 2022

Issue

ADP-1514

(Also fixes #3242)

Summary

This PR adds a pair of property tests for distributeSurplus:

  • prop_distributeSurplus_onSuccess_conservesSurplus
  • prop_distributeSurplus_onSuccess_coversCostIncrease

This PR also adds descriptions of these properties to the comment for TransactionLayer.distributeSurplus.

Finally, this PR also improves test coverage of the surplus value, by defining and using a TxBalanceSurplus Coin generator that:

  • includes zero Coin values (previously, this value was excluded).
  • includes difference ranges of coins.

When testing `distributeSurplus`, we definitely want to test the
behaviour when the given surplus is zero.

The existing `Arbitrary` instance for `Coin` always generates non-zero
coins.

This commit introduces a helper type `TxBalanceSurplus`, with an
`Arbitrary` instance that allows zero-valued coins.
Specifically, check that we cover the case where the surplus is zero.
Fixes issue #3242.

This bug caused the following property to sporadically fail:

    "distributeSurplusDelta, when no change output is present, will burn
    surplus as excess fees"

Example failure seeds:

    --seed 862218635
    --seed 1926926873
    --seed 404630283
    --seed 1602363838
    --seed 1472406660
    --seed 31096920
    --seed 166643919

Failures were always of the form:

```hs
       expected: Right (TxFeeAndChange {fee = Coin 1, change = []})
        but got: Left (ErrMoreSurplusNeeded (Coin 0))
```

This indicates that it was possible for `burnSurplusAsFees` to fail with
an indicated shortfall of zero.

To fix this, we update `burnSurplusAsFees` to only fail if the computed
shortfall is non-zero.

We also adjust the following property to check that failure cases are
covered, and to verify that the shortfall is correct:

`prop_distributeSurplusDelta_coversCostIncreaseAndConservesSurplus`
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/improve-distributeSurplus-tests branch from e0ba91a to 578e50a Compare April 21, 2022 04:54
--
-- - Any increase in cost is covered:
-- If the total cost has increased by 𝛿c, then the fee value
-- will have increased by at least 𝛿c.
Copy link
Member

Choose a reason for hiding this comment

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

👍

instance Arbitrary (TxBalanceSurplus Coin) where
-- We want to test cases where the surplus is zero. So it's important that
-- we do not restrict ourselves to positive coins here.
arbitrary = TxBalanceSurplus <$> frequency
Copy link
Member

Choose a reason for hiding this comment

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

Ah! 👍

Copy link
Member

@Anviking Anviking Apr 21, 2022

Choose a reason for hiding this comment

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

To be honest, I wonder if not the Arbitrary Coin instance probably should include Coin 0.

I tried this, and all the tests in the module seemed to pass. But still out of caution refraining from changing this now. It would be easier to change after we reorganize the module structure.

@Anviking
Copy link
Member

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 21, 2022

Build succeeded:

@iohk-bors iohk-bors bot merged commit 34c5adc into master Apr 21, 2022
@iohk-bors iohk-bors bot deleted the jonathanknowles/improve-distributeSurplus-tests branch April 21, 2022 10:16
WilliamKingNoel-Bot pushed a commit that referenced this pull request Apr 21, 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.

Test failure: distributeSurplusDelta, when no change output is present, will burn surplus as excess fees
2 participants