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

TxBuilder: Implement CIP-0002 Coin Selection #232

Merged
merged 4 commits into from
Nov 7, 2021

Conversation

rooooooooob
Copy link
Contributor

https://github.com/cardano-foundation/CIPs/blob/master/CIP-0002/CIP-0002.md

key differences

  1. we take into account adjuting for free (which is out of CIP2's scope)
  2. LargestFirst supports multiassets (naively)

https://github.com/cardano-foundation/CIPs/blob/master/CIP-0002/CIP-0002.md

key differences
1) we take into account adjuting for free (which is out of CIP2's scope)
2) LargestFirst supports multiassets (naively)
Copy link
Contributor Author

@rooooooooob rooooooooob left a comment

Choose a reason for hiding this comment

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

I still need to update the flow types to add in this method.

},
CoinSelectionStrategyCIP2::RandomImprove => {
use rand::Rng;
if self.outputs.0.iter().any(|output| output.amount.multiasset.is_some()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CIP-0002 was written before multiassets and I think trying to handle mutliassets with random improve would be non-trivial. We can do it for largest first but with possible unintended consequences of huge TXs if the multiasset UTXO you need has a very low ADA value, causing this algorithm to stuff in many others.

Do we want to handle multiasset separately? Or as an extra stage at the end if there are any that just adds in inputs containing enough without much concern which should be fine as long as people aren't stuffing in many different multiassets into one tx and need to optimize the selection in a better way.

@@ -222,7 +316,7 @@ impl TransactionBuilder {
}

/// calculates how much the fee would increase if you added a given output
pub fn fee_for_input(&mut self, address: &Address, input: &TransactionInput, amount: &Value) -> Result<Coin, JsError> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This wasn't strictly needed for this PR I think (add_inputs_from is mut), but this shouldn't have been mut in the first place.

for utxo in available_inputs.0.iter() {
input_values.insert(utxo.input.transaction_id(), utxo.output.amount.clone());
}
let mut encountered = std::collections::HashSet::new();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

due to the RNG I'm not sure what else we can test

@vsubhuman vsubhuman self-requested a review October 27, 2021 08:32
@vsubhuman
Copy link
Contributor

Thank you, @rooooooooob, this is great. Resolve conflicts, please, when possible

@rooooooooob
Copy link
Contributor Author

@vsubhuman I fixed the merge conflict.

@vsubhuman vsubhuman added this to the 9.2.0 milestone Nov 1, 2021
Copy link
Contributor

@pedromtcosta pedromtcosta left a comment

Choose a reason for hiding this comment

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

Looks good! Nice tests

Copy link
Contributor

@vsubhuman vsubhuman left a comment

Choose a reason for hiding this comment

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

/check

@rooooooooob
Copy link
Contributor Author

rooooooooob commented Dec 7, 2021

@BlakeBrown please see #264 which implements multiasset for both. The current (i.e. this PR) implementation for multiasset (largest-first) is very sub-optimal and will almost certainly add too many inputs in unless you have a lot of inputs with those desired multiassets. I should have thrown an error for the CIP-2 largest-first if there were multiassets too, and I do now in #264 if you're using the non-multiasset enum variants.

Keep in mind CIP-2 only targets ADA-only since it was written before Cardano had multiassets. I tried to adapt both to multiasset in the linked PR, but I didn't before as it was additional work and how well they would work when applied (and even how you would adapt them) is/was unknown.

@vsubhuman vsubhuman mentioned this pull request Feb 6, 2022
40 tasks
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.

4 participants