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

Fix Script::dust_value()'s calculation for non-P2*PKH script_pubkeys #579

Merged
merged 2 commits into from May 6, 2021

Conversation

TheBlueMatt
Copy link
Member

The dust calculations added were only valid for P2WPKH and P2PKH
outputs, and somehow this fact was missed in review, despite the
upstream Core code being linked to and looked at by two reviewers
and the author (me).

Someday I will grow eyeballs, but that day is not today.

Comment on lines 417 to 427
3 * // The default dust relay fee is 3000 satoshi/kB (ie 1 sat/vByte)
if self.is_op_return() {
0
} else if self.is_witness_program() {
Self::WITNESS_OUTPUT_DUST_THRESHOLD
32 + 4 + 1 + (107 / 4) + 4 + // The spend cost copied from Core
8 + // The serialized size of the TxOut's amount field
self.consensus_encode(&mut ::std::io::sink()).unwrap() as u64 // The serialized size of this script_pubkey
} else {
Self::LEGACY_OUTPUT_DUST_THRESHOLD
32 + 4 + 1 + 107 + 4 + // The spend cost copied from Core
8 + // The serialized size of the TxOut's amount field
self.consensus_encode(&mut ::std::io::sink()).unwrap() as u64 // The serialized size of this script_pubkey
Copy link
Collaborator

Choose a reason for hiding this comment

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

I will suggest improving readability of this fragment

       if self.is_op_return() {
             return 0
       }
       let script_pubkey_len =  self.consensus_encode(&mut ::std::io::sink()).expect("I/O sink never fails") as u64;
       let witness_reducer = (self.is_witness_program() as u8) * 4;
       DEFAULT_DUST_RELAY_FEE * // The default dust relay fee is 3000 satoshi/kB (ie 1 sat/vByte)
            (32 + 4 + 1 + 107 / witness_reducer + 4 // The spend cost copied from Core
             + 8  // The serialized size of the TxOut's amount field
             + script_pubkey_len)

And introduce DEFAULT_DUST_RELAY_FEE const additionally to other values introduced by #584

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure that DUST_RELAY_FEE is a useful constant - it is only used when calculating the dust threshold, not in policy in any other context. I'm not sure what a user would do with such a constant. As for your proposed change, note that witness_reducer can be 0 causing a divide-by-0 :).

The current code was written so as to make it clear its directly comparable to Core.

apoelstra
apoelstra previously approved these changes Apr 21, 2021
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ack 45d48c5

@dr-orlovsky dr-orlovsky added the minor API Change This PR should get a minor version bump label Apr 28, 2021
@dr-orlovsky dr-orlovsky added this to the 0.26.1 milestone Apr 28, 2021
dr-orlovsky
dr-orlovsky previously approved these changes Apr 28, 2021
Copy link
Collaborator

@dr-orlovsky dr-orlovsky 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 that it's quite important to get this bugfix in the upcoming 0.26.1, so I withdraw my nits and do a second ACK to this PR such that it can be merged

sgeisler
sgeisler previously approved these changes Apr 30, 2021
Copy link
Contributor

@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

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

utACK 45d48c5

I was kinda scared of this PR 😅 it's just so obvious in hindsight what went wrong in #566, I guess I should get a pair of eyes too at some point 👀

/// broadcastable on today's bitcoin network.
pub const WITNESS_OUTPUT_DUST_THRESHOLD: u64 = 294;
pub const P2WPKH_OUTPUT_DUST_THRESHOLD: u64 = 294;
Copy link
Contributor

@sgeisler sgeisler Apr 30, 2021

Choose a reason for hiding this comment

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

Are these constants actually still all that useful now? If we fell for it someone else might do the same and just use one of the constants, not paying attention to the script type 😬

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably not, admittedly, I'm happy to remove them?

Copy link
Contributor

Choose a reason for hiding this comment

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

Do if you want, I don't have too strong feelings either way. It just seemed somewhat redundant and I wanted to bring up the question.

Copy link
Member Author

Choose a reason for hiding this comment

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

I do, in fact. Pushed.

The dust calculations added were only valid for P2WPKH and P2PKH
outputs, and somehow this fact was missed in review, despite the
upstream Core code being linked to and looked at by two reviewers
and the author (me).

Someday I will grow eyeballs, but that day is not today.
dr-orlovsky
dr-orlovsky previously approved these changes May 1, 2021
sgeisler
sgeisler previously approved these changes May 1, 2021
It doesn't really make sense to have a constant for every common
script type's dust limit, instead we should just use the
`Script::dust_value()` function to have users calculate it.
@TheBlueMatt TheBlueMatt dismissed stale reviews from sgeisler and dr-orlovsky via fc6f23f May 5, 2021 14:54
@TheBlueMatt
Copy link
Member Author

Ok, sorry about the churn, this should be good now, assuming we agree to drop the useless constants.

@apoelstra
Copy link
Member

If we drop the constants this is a breaking change.

@sgeisler
Copy link
Contributor

sgeisler commented May 6, 2021

Right, didn't think of that 😬 so maybe only deprecate now and remove with 0.27.0?

@RCasatta
Copy link
Collaborator

RCasatta commented May 6, 2021

I think released 0.26.0 hadn't those constants yet, so removing them shouldn't be breaking

@TheBlueMatt
Copy link
Member Author

Indeed, none is this has appeared in a release yet, so I figured it was fine to change.

@apoelstra
Copy link
Member

Ah! My bad

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ack fc6f23f

@apoelstra apoelstra merged commit 4620c64 into rust-bitcoin:master May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor API Change This PR should get a minor version bump
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants