Skip to content

test: add coverage for dust mempool policy (-dustrelayfee setting) - #26631

Merged
maflcko merged 2 commits into
bitcoin:masterfrom
theStack:202212-test-add_dustrelayfee_coverage
Jan 16, 2023
Merged

maflcko merged 2 commits into
bitcoin:masterfrom
theStack:202212-test-add_dustrelayfee_coverage

Conversation

@theStack

@theStack theStack commented Dec 4, 2022 •

Copy link
Copy Markdown
Contributor

This PR adds missing test coverage for the -dustrelayfee setting, which specifies the fee-rate used to define dust. Output scripts for all common types that are treated as standard by default (P2PK, P2(W)PKH, P2(W)SH, P2TR, bare multisig, null data, unknown witness versions v2+) are created and then checked for dust-mempool-policy each via the testmempoolaccept RPC: a tx with an output's nValue equal to the dust threshold should be accepted, one with an nValue of just one 1 satoshi below that should be rejected with reason dust. This is repeatedly done for a fixed (but obviously somewhat arbitrary) list of different -dustrelayfee settings on a single node, including the default and zero (i.e. no dust limit) settings.

Note that the first commit introduces a necessary CScript helper method IsWitnessProgram (using PascalCase in Python is likely controversial; in this case the style for the already existing method GetSigOpCount was followed, which also refers to a method in the core CScript class).

Some historical information about dust, contributed by pablomartin4btc:
"The concept of dust was first introduced in #2577. This commit from #9380 introduced the -dustrelayfee option. Previous to that PR, the dust feerate was whatever -minrelaytxfee was set to."

This is needed in the next commit to calculate the dust threshold
for a given output script and min feerate for defining dust.
@DrahtBot

DrahtBot commented Dec 4, 2022 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK glozow, kouloumos, LarryRuane
Concept ACK pablomartin4btc, rserranon, instagibbs

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Coverage

Coverage Change (pull 26631, 8147f7f7ec5dcfe5295ed2697942b410554b4117) Reference (master, 5055d07)
Lines +0.0036 % 83.9288 %
Functions +0.0000 % 80.9329 %
Branches +0.0040 % 51.4098 %

Updated at: 2022-12-16T12:29:44.053355.

@DrahtBot DrahtBot added the Tests label Dec 4, 2022
@theStack
theStack force-pushed the 202212-test-add_dustrelayfee_coverage branch 2 times, most recently from b3ba31a to 2c697dd Compare December 4, 2022 03:00

@glozow glozow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK

@LarryRuane LarryRuane left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 2c697ddcb1fa4d04f55b024c06116530d7e74598
suggestions are non-blocking

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe add a comment before this definition:

# Equivalent to: CScript::IsWitnessProgram()

Comment thread test/functional/mempool_dust.py Resolved
Comment thread test/functional/mempool_dust.py Resolved

@pablomartin4btc pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tested ACK.

image

All different output scripts ran over the -dustrelayfee list of settings as it's shown on the screenshot above.

Comment thread test/functional/mempool_dust.py Resolved
@rserranon

Copy link
Copy Markdown

Tested ACK

Comment thread test/functional/mempool_dust.py Resolved

@pablomartin4btc pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this statement is very useful as a context for the review:

The concept of dust was first introduced in PR #2577. This commit from PR #9380 introduced the -dustrelayfee option. Previous to that PR, the dust feerate was whatever -minrelaytxfee was set to.

Taken from the notes of the review club. Is it possible to add the label "Review club" to this PR please?

@theStack
theStack force-pushed the 202212-test-add_dustrelayfee_coverage branch from 2c697dd to d6fc1d6 Compare December 16, 2022 14:04
@theStack

Copy link
Copy Markdown
Contributor Author

Force-pushed with most suggestions taken, and another few changes:

  • add both smallest and largest possible (non-nulldata) output scripts that are considered standard to the list (i.e. segwitv2+ with 2-byte witness program, m-of-3 bare multisig with uncompressed pubkeys)
  • extended the dust fee-rate list to test with 333333, to also have one case covering the "~hundreds of sats/vbyte" range
  • add comments about program witness sizes and chosen dust fee-rates to reduce confusion about magic numbers
  • for tx creation: adjust first output with the dust threshold amount, to keep the total output value constant and not risk underpaying fees (or even worse, exceeding the input amount)

I think this statement is very useful as a context for the review:

The concept of dust was first introduced in PR #2577. This commit from PR #9380 introduced the -dustrelayfee option. Previous to that PR, the dust feerate was whatever -minrelaytxfee was set to.

Thanks for researching! 👍 I added this paragraph to the PR description, as it is very helpful for reviewers getting deeper into the topic of dust with some historical context.

Taken from the notes of the review club. Is it possible to add the label "Review club" to this PR please?

+1

kouloumos added a commit to kouloumos/review-club-summaries that referenced this pull request Jan 12, 2023
add coverage for dust mempool policy (-dustrelayfee setting)
bitcoin/bitcoin#26631

@glozow glozow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK d6fc1d6
dust calculation seems to match GetDustThreshold(), IsWitnessProgram() looks correct, don't see any off-by-ones, coverage looks good

@instagibbs instagibbs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

concept ACK

(script_to_p2wsh_script(CScript([OP_TRUE])), "P2WSH"),
(output_key_to_p2tr_script(pubkey[1:]), "P2TR"),
# witness programs for segwitv2+ can be between 2 and 40 bytes
(program_to_witness_script(2, b'\x66' * 2), "P2?? (future witness version 2)"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As I do in #26875 could check v1 at non-32-byte-push lengths as well to make sure they're treated as UNKNOWN

@kouloumos kouloumos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK d6fc1d6
with a few non-blocking comments

def set_test_params(self):
self.num_nodes = 1

def test_dust_output(self, node: TestNode, dust_relay_fee: Decimal,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the node argument may be residue from when the PR used multiple nodes to test different configurations?

tx_size = len(CTxOut(nValue=0, scriptPubKey=output_script).serialize())
tx_size += 67 if output_script.IsWitnessProgram() else 148
dust_threshold = int(get_fee(tx_size, dust_relay_fee) * COIN)
self.log.info(f"-> Test {type_desc} output (size {len(output_script)}, limit {dust_threshold})")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: I've been thinking about ways to formalize the way logging is done in the testing framework and indentation of subcases is in there. The ideal scenario would be for those things to happen automatically, but working with what we have...
I would like to make a minor suggestion that I believe makes the logging more readable:

Suggested change
self.log.info(f"-> Test {type_desc} output (size {len(output_script)}, limit {dust_threshold})")
self.log.info(f" - Test {type_desc} output (size {len(output_script)}, limit {dust_threshold})")

side by side comparison
imgonline-com-ua-twotoone-KxmOPSAP8O

and to remove even more noise:

Suggested change
self.log.info(f"-> Test {type_desc} output (size {len(output_script)}, limit {dust_threshold})")
self.log.info(f"- output (size {len(output_script)}, limit {dust_threshold})")

icon3

I've noticed that this is a format that you have previously used in wallet_fast_rescan.py, but "- ..." is currently used in more place, such as rpc_decodescript.py and rpc_psbt.py and I believe that following the same format across the testing framework is for the better.

@LarryRuane

Copy link
Copy Markdown
Contributor

ACK d6fc1d6

@maflcko
maflcko merged commit 2182149 into bitcoin:master Jan 16, 2023
@theStack
theStack deleted the 202212-test-add_dustrelayfee_coverage branch January 16, 2023 10:41
@bitcoin bitcoin locked and limited conversation to collaborators Jan 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants