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

CIP2 - Coin Selection Algorithms #2

Merged
merged 6 commits into from
Aug 4, 2020
Merged

CIP2 - Coin Selection Algorithms #2

merged 6 commits into from
Aug 4, 2020

Conversation

jonathanknowles
Copy link
Member

@jonathanknowles jonathanknowles commented May 6, 2020

About this PR

This PR adds a new informational CIP entitled "Coin Selection Algorithms for Cardano".

The purpose of this CIP is to describe, in human-readable terms, the coin selection algorithms currently used by Cardano Wallet and other parts of the Cardano ecosystem.

Reading the Draft

Since the draft is written in Markdown format, see here to read the fully-rendered version.

Section Guide

The draft CIP document is divided into a number of sections:

Background

  • Introduces the fundamental concepts behind coin selection.
  • Provides a discussion of why coin selection is a non-trivial problem.
  • Describes the goals of coin selection algorithms.

Interface

  • Describes the common interface unifying all coin selection algorithms used within Cardano Wallet.
  • Describes the standard parameter types, result types, and error types used by the common interface.
  • Describes the properties that all conforming implementations are expected to satisfy.

Algorithms

  • Gives detailed descriptions of each of the individual coin selection algorithms used in Cardano Wallet.
  • Gives step-by-step descriptions of the computations involved.

Reference Implementations

  • Provides links to reference implementations of each algorithm in various languages (this is currently limited to Haskell, but additional languages could be added in future revisions).

External Resources

  • Provides links to other resources useful for understanding issues relating to coin selection.

This change adds a new informational CIP entitled "Coin Selection
Algorithms for Cardano".

The purpose of this CIP is to describe, in human-readable terms, the
coin selection algorithms currently used by Cardano Wallet and other
parts of the Cardano ecosystem.

The CIP is divided into a number of sections:

The "Background" section:

  * introduces the fundamental concepts behind coin selection;
  * provides a discussion of why coin selection is a non-trivial problem;
  * describes the goals of coin selection algorithms.

The "Interface" section:

  * describes the common interface unifying all coin selection algorithms
    used within Cardano Wallet;
  * describes the standard parameter types, result types, and error types
    used by the common interface;
  * describes the properties that all conforming implementations are expected
    to satisfy.

The "Algorithms" section:

  * gives detailed descriptions of each of the individual coin selection
    algorithms used in Cardano Wallet;
  * gives step-by-step descriptions of the computations involved.

The "Reference Implementations" section:

  * provides links to reference implementations of each algorithm in
    various languages (currently limited to Haskell, but additional
    languages could be added in future revisions).

The "External Resources" section:

  * provides links to other resources useful for understanding issues
    relating to coin selection.
@jonathanknowles jonathanknowles changed the title Add CIP "Coin Selection Algorithms for Cardano". CIP2 - Coin Selection Algorithms May 11, 2020
@KtorZ KtorZ self-assigned this May 11, 2020
CIP2/CIP2.md Show resolved Hide resolved
CIP2/CIP2.md Show resolved Hide resolved
CIP2/CIP2.md Show resolved Hide resolved

In particular:

* **_v_**<sub>selected</sub>
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you not missing the fee somewhere in here?

Copy link
Member Author

@jonathanknowles jonathanknowles Aug 4, 2020

Choose a reason for hiding this comment

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

@SebastienGllmt wrote:

Are you not missing the fee somewhere in here?

Hi Sebastien

Many thanks for your comment, and apologies for the long delay in responding. (Until this week, we were focused quite heavily on getting things ready for Shelley.)

To answer your question:

As you might be aware, coin selection (in Cardano Wallet) proceeds in two distinct and consecutive phases:

  1. Phase 1: Selecting coins from a UTxO set to form an initial coin selection.
    The goal of this phase is to produce a selection where sum (inputs) = sum (outputs).
  2. Phase 2: Adjusting the coin selection generated in the previous phase in order to pay for the network fee.
    The goal of this phase is to produce a selection where sum (inputs) = sum (outputs) + fee.

Both the former and the latter phases are complex and worthy of discussion in their own right, so we took the decision quite early on to separate the descriptions of these two phases into two different specification documents.

This document deliberately focuses on the former phase: the algorithms involved in creating an initial coin selection. These algorithms are (more or less) independent of any fee adjustment that happens later on. As for fee adjustment, I expect that we'll eventually create another specification document to cover this topic, as soon as time allows.

The fact that you've raised this point is really useful, as it indicates that we ought to make the scope of this document clearer.

I will adjust the abstract and motivation sections and let you know when the document is updated. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @SebastienGllmt.

As promised, I've revised the abstract and motivation sections, in light of your feedback.

See 5c685d6.

Many thanks again for your feedback!

Of course, let me know if you think we should make further changes.


* #### UTxO Balance Insufficient

This failure occurs when the total value of the entries within the [initial
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be worth noting that this can also happen in the case that UTXO in your wallet contains less ADA than the cost of including it in the input. The sum of the dust may have a sufficient balance, but each input individually is not worth adding.

Copy link
Member Author

Choose a reason for hiding this comment

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

@SebastienGllmt wrote:

It may be worth noting that this can also happen in the case that UTXO in your wallet contains less ADA than the cost of including it in the input. The sum of the dust may have a sufficient balance, but each input individually is not worth adding.

That is indeed true, and a very good point.

However, in Cardano Wallet, dust elimination is mainly the responsibility of the fee adjustment algorithm, whereas this document describes the algorithms involved in making an initial coin selection.

See #2 (comment) for an explanation of these two phases (which are consecutive and distinct from one another).


If the [available UTxO list](#available-utxo-list) is _empty_:

* Terminate with a [UTxO Balance
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, this is the wrong condition. It should be until the available utxo list has no UTXO with an amount greater than the fee of adding it to the transaction (I know for some currencies this is now straight forward to detect)

Copy link
Member Author

@jonathanknowles jonathanknowles Aug 4, 2020

Choose a reason for hiding this comment

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

@SebastienGllmt wrote:

Again, this is the wrong condition. It should be until the available utxo list has no UTXO with an amount greater than the fee of adding it to the transaction (I know for some currencies this is now straight forward to detect)

It would be the wrong condition if we were considering fees.

However, fees are handled by a later phase (the fee adjustment phase), which is (currently) outside the scope of this document.

See #2 (comment) for a longer explanation.


Represents the set of payments to be made to recipient addresses.

* A set of **_change values_** (see [change output](#change-output)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a minimum size for the value of a change? For example, if the change is just one lovelace, maybe it should just be added to the fee instead.

Copy link
Member Author

@jonathanknowles jonathanknowles Aug 4, 2020

Choose a reason for hiding this comment

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

Do we have a minimum size for the value of a change? For example, if the change is just one lovelace, maybe it should just be added to the fee instead.

Yes, we do. We have the concept of a dust threshold.

Here's an extract from the API docs for cardano-coin-selection:

A DustThreshold defines the maximum size of a dust coin.

Functions that accept a DustThreshold argument will generally exclude values that are less than or equal to this threshold from the change sets of generated selections, coalescing such coins together into larger coins that exceed the threshold.

This dust threshold parameter is used in the fee adjustment phase (see #2 (comment)), which is mainly concerned with adjusting a coin selection in order to pay for the network fee, but also concerned with the process of coalescing dust coins into larger coins that exceed the dust threshold.

Copy link
Contributor

@dcoutts dcoutts left a comment

Choose a reason for hiding this comment

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

I think this is in a good enough state to merge in draft status. Further comments and edits in draft status are fine.

@dcoutts dcoutts marked this pull request as ready for review August 4, 2020 08:48
Copy link
Contributor

@crptmppt crptmppt left a comment

Choose a reason for hiding this comment

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

Good to move to Draft

This change also adds a "Scope" section, to clarify and delineate the
scope of this article.

In response to review feedback:

#2 (comment)
@crptmppt crptmppt merged commit 2569aaa into cardano-foundation:master Aug 4, 2020
@KtorZ KtorZ mentioned this pull request Apr 13, 2021
GeorgeFlerovsky pushed a commit to GeorgeFlerovsky/CIPs that referenced this pull request Feb 27, 2023
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.

5 participants