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

Feat: don't include dust btc amounts on rotation #4063

Merged
merged 3 commits into from
Oct 3, 2023

Conversation

msgmaxim
Copy link
Contributor

Pull Request

Checklist

Please conduct a thorough self-review before opening the PR.

  • I am confident that the code works.
  • I have updated documentation where appropriate.

Summary

Noticed that we used "all or nothing" approach when sweeping utxos on rotation, when it is better to skip dust utxos to avoid paying more for their fees than they are worth. Another potential benefit of this is reducing the number of payloads we would need to sign on rotation. (Already discussed this with @ramizhasan111 to make sure that we want this change.)

@codecov
Copy link

codecov bot commented Sep 29, 2023

Codecov Report

Merging #4063 (7a6fda7) into main (b3adda0) will increase coverage by 0%.
The diff coverage is 100%.

@@          Coverage Diff          @@
##            main   #4063   +/-   ##
=====================================
  Coverage     71%     71%           
=====================================
  Files        376     376           
  Lines      59662   59672   +10     
  Branches   59662   59672   +10     
=====================================
+ Hits       42554   42563    +9     
- Misses     14961   14962    +1     
  Partials    2147    2147           
Files Coverage Δ
state-chain/pallets/cf-environment/src/lib.rs 74% <100%> (+1%) ⬆️
state-chain/pallets/cf-environment/src/tests.rs 100% <100%> (ø)

... and 3 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Comment on lines 440 to 445
non_dust_utxos
.iter()
.map(|utxo| utxo.amount)
.sum::<u64>()
.checked_sub(total_fee)
.map(|change_amount| (non_dust_utxos, change_amount))
Copy link
Collaborator

Choose a reason for hiding this comment

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

We want to return None if this is empty, right?

Could we not just do the same as before except:

let available_utxos = BitcoinAvailableUtxos::<T>::take()
	.into_iter()
	.filter(|utxo| utxo.amount > fee_per_input_utxo)
	.collect();

// Same as before
(!available_utxos.is_empty()).then_some(available_utxos).and_then(
//etc.

Copy link
Contributor Author

@msgmaxim msgmaxim Oct 2, 2023

Choose a reason for hiding this comment

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

We want to return None if this is empty, right?

New code also returns None if the list is empty, and we even have a test for that. Is your suggestion purely due to style preference?

I'm not convinced that "no utxo" needs to be handled separately. Conceptually it is really not any different from "not enough utxo". If you want, I could inline total_fee and combine the first two expressions, but personally don't really mind giving names to these intermediate expressions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok I see that now. I guess it's stylistic thing then.

I think I missed it because the new code reads to me like "Return None if the utxo sum minus the total fee saturates", which (indirectly, because we filter the list above) implies that the list is empty. This seems less obvious than simply "return None if there are no utxos above the dust amount".

I don't mind breaking this up in some way / adding intermediate expressions, but it would be easier to follow if we handle the empty case explicitly (if x.is_empty() or whatever, doesn't have to be !available_utxos.is_empty()).then_some( which I admit is not easy to read either).

Copy link
Contributor Author

@msgmaxim msgmaxim Oct 2, 2023

Choose a reason for hiding this comment

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

I would say the unnecessary check for is_empty() is the cause of the initial confusion. It made it seem like it was a case that needed a special treatment (different from the more general "not enough funds" case) when it doesn't.

Anyway, it doesn't make a big difference to me, so I will make the change you are suggesting before merging.

Comment on lines 440 to 445
non_dust_utxos
.iter()
.map(|utxo| utxo.amount)
.sum::<u64>()
.checked_sub(total_fee)
.map(|change_amount| (non_dust_utxos, change_amount))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok I see that now. I guess it's stylistic thing then.

I think I missed it because the new code reads to me like "Return None if the utxo sum minus the total fee saturates", which (indirectly, because we filter the list above) implies that the list is empty. This seems less obvious than simply "return None if there are no utxos above the dust amount".

I don't mind breaking this up in some way / adding intermediate expressions, but it would be easier to follow if we handle the empty case explicitly (if x.is_empty() or whatever, doesn't have to be !available_utxos.is_empty()).then_some( which I admit is not easy to read either).

@msgmaxim msgmaxim enabled auto-merge (squash) October 2, 2023 23:56
@msgmaxim msgmaxim merged commit 73f4bc8 into main Oct 3, 2023
@msgmaxim msgmaxim deleted the feat/skip-dust-on-rotation branch October 3, 2023 00:26
syan095 added a commit that referenced this pull request Oct 3, 2023
* origin/main:
  Feat: don't include dust btc amounts on rotation (#4063)
  chore: update dependency and config.toml for RUSTSEC-2023-0065 (#4066)
  fix: loop_select conditions (PRO-587) (#4061)
  chore: remove unused config items (#4064)
  feat: size limit for CCM (#4015)
  fix: warn -> info (#4060)
  Fix: correctly handle peer updates while waiting to reconnect (#4052)

# Conflicts:
#	state-chain/chains/src/lib.rs
dandanlen added a commit that referenced this pull request Oct 4, 2023
* feat: don't include dust btc amounts on rotation

* chore: return None early on empty utxo list

---------

Co-authored-by: dandanlen <3168260+dandanlen@users.noreply.github.com>
dandanlen added a commit that referenced this pull request Oct 9, 2023
* feat: don't include dust btc amounts on rotation

* chore: return None early on empty utxo list

---------

Co-authored-by: dandanlen <3168260+dandanlen@users.noreply.github.com>
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.

3 participants