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

Prevent the migration algorithm from creating money. #77

Merged
merged 8 commits into from
May 12, 2020

Conversation

jonathanknowles
Copy link
Contributor

@jonathanknowles jonathanknowles commented May 12, 2020

Related Issue

#74

Summary

The migration algorithm should satisfy the following property:

total value of all inputs ≥ sum of selection change coins

However, recent test failures have showed that the existing implementation occasionally failed to respect this property, in very rare situations.

In particular, supplying a coin selection where the total value of all inputs (in a given batch) is less than the minimum value of a non-dust coin will reliably produce a set of change coins whose total value is greater than the total value of all inputs. (See #74 for examples of test failures.)

This PR fixes the above issue by adding a guard to the mkCoinSelection function, preventing it from creating an initial change coin if that change coin would be higher than the total value of all inputs.

Demonstration (with one million tests per property):

  selectCoins properties
    No coin selection has outputs
      +++ OK, passed 1000000 tests.  
    Every coin in the selection change > dust threshold
      +++ OK, passed 1000000 tests.  
    Total input UTxO value >= sum of selection change coins
      +++ OK, passed 1000000 tests.
    Every selection input is unique   
      +++ OK, passed 1000000 tests.
    Every selection input is a member of the UTxO
      +++ OK, passed 1000000 tests.
    Every coin selection is well-balanced
      +++ OK, passed 1000000 tests.

Other Changes

This PR also makes the following changes:

  • Corrects a number of inconsistent dust inequality checks in the code. The library currently defines the dust threshold as the maximum size of a dust coin, rather than the minimum size of a non-dust coin. (By defining it in this way, we can arrange that zero-valued coins are always eliminated by the dust elimination mechanism, as it is not possible to specify a dust threshold lower than zero.)
  • Adds a function isDust to provide a uniform way to determine whether or not a given coin is a dust coin, and uses it to replace a number of inequality checks.

The library currently defines the dust threshold as the maximum size of
a dust coin, rather than the minimum size of a non-dust coin.

It is necessary to define it this way to ensure that zero-valued coins
are always eliminated from adjusted selections. (It is not possible to
specify a dust threshold lower than zero, and therefore not possible to
prevent zero-valued coins from being eliminated.)
This function provides a reusable indicator for whether or not a given
coin is a dust coin.
We can now eliminate the auxilliary `noDust` function.
The migration algorithm should satisfy the following property:

>>> Total value of all inputs >= sum of selection change coins

Before this change, it was possible to cause the algorithm to violate
this property by giving it a single input value lower than the dust
threshold.

This change adds a guard to the `mkCoinSelection` function, preventing
it from creating a change coin if that change coin would be higher than
the total value of all inputs.
@jonathanknowles jonathanknowles self-assigned this May 12, 2020
@jonathanknowles jonathanknowles linked an issue May 12, 2020 that may be closed by this pull request
Copy link
Contributor

@paweljakubas paweljakubas left a comment

Choose a reason for hiding this comment

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

look fine to me

@jonathanknowles jonathanknowles merged commit 14ef17a into master May 12, 2020
@jonathanknowles jonathanknowles deleted the jonathanknowles/fix-migration branch May 12, 2020 09:06
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.

Sporadic migration property failure.
2 participants