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

Add Script:dust_value() to get minimum output value for a spk #566

Merged
merged 1 commit into from Feb 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/blockdata/script.rs
Expand Up @@ -325,6 +325,26 @@ impl Script {
opcodes::All::from(self.0[0]).classify() == opcodes::Class::IllegalOp)
}

/// The minimum value an output to a witness script must have in order to be
/// broadcastable on today's bitcoin network.
pub const WITNESS_OUTPUT_DUST_THRESHOLD: u64 = 294;

/// The minimum value an output to a non-witness script must have in order to be
/// broadcastable on today's bitcoin network.
pub const LEGACY_OUTPUT_DUST_THRESHOLD: u64 = 546;

/// Gets the minimum value an output with this script should have in order to be
/// broadcastable on today's bitcoin network.
pub fn dust_value(&self) -> u64 {
if self.is_op_return() {
Copy link
Contributor

Choose a reason for hiding this comment

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

When looking at core it also captures the case where a script is too big making the UTXO unspendable. But that would only make this implementation more conservative which might be ok since it's such a fringe use case and not consensus critical.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, right - are those scripts broadcastable at all or are they nonstandard?

Copy link
Member

@sanket1729 sanket1729 Feb 5, 2021

Choose a reason for hiding this comment

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

Those scripts are not possible by consensus rules all scripts(bare, p2sh, witness_v0). But post tapscript, those scripts will be broadcastable.

https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#resource-limits

https://github.com/bitcoin/bitcoin/blob/6c6140846f37de8c132b3b6abf09f3d7940554a7/src/script/interpreter.cpp#L452

We can change the isWitnessProgram check to check only v0 programs, but this can also be done in taproot upgrade #503. But this could have easily been one of the things we forgot to add.

Copy link
Member

@sanket1729 sanket1729 Feb 5, 2021

Choose a reason for hiding this comment

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

@sgeisler Nevermind, I was wrong. This function is about scriptPubkey and not about the witness Program inside it. The witness program can be more 10,000 in Tapscript, but scriptpubkey is always 35.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I'm not even talking about if its valid (its obviously not possible to spend), but is it standard - can you broadcast a transaction with such a script in an output and have it be mined, even if the output is unspendable.

0
} else if self.is_witness_program() {
Self::WITNESS_OUTPUT_DUST_THRESHOLD
} else {
Self::LEGACY_OUTPUT_DUST_THRESHOLD
}
}

/// Iterate over the script in the form of `Instruction`s, which are an enum covering
/// opcodes, datapushes and errors. At most one error will be returned and then the
/// iterator will end. To instead iterate over the script as sequence of bytes, treat
Expand Down