forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 154
Policy: Penalize effective fee for sub-dust outputs #272
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
Open
kwsantiago
wants to merge
4
commits into
bitcoinknots:29.x-knots
Choose a base branch
from
privkeyio:policy-subdust-fee-penalty
base: 29.x-knots
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9dae552
validation: penalize effective fee for sub-dust outputs
kwsantiago 00fa59e
qt: add subdustfeepenalty GUI option
kwsantiago a0b0011
test: add mempool_subdust_fee_penalty functional test
kwsantiago af7d6f6
policy: enable subdustfeepenalty by default
kwsantiago File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) 2026 The Bitcoin Knots developers | ||
| # Distributed under the MIT software license, see the accompanying | ||
| # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
| """Test sub-dust output fee penalty (-subdustfeepenalty).""" | ||
|
|
||
| from math import ceil | ||
|
|
||
| from test_framework.messages import ( | ||
| COIN, | ||
| COutPoint, | ||
| CTransaction, | ||
| CTxIn, | ||
| CTxOut, | ||
| ) | ||
| from test_framework.script import ( | ||
| CScript, | ||
| OP_RETURN, | ||
| ) | ||
| from test_framework.test_framework import BitcoinTestFramework | ||
| from test_framework.util import assert_equal | ||
| from test_framework.wallet import MiniWallet | ||
|
|
||
|
|
||
| DUST_THRESHOLD = 330 # P2TR: (43 + 67) * 3000 / 1000 = 330 sats | ||
|
|
||
|
|
||
| class SubDustFeePenaltyTest(BitcoinTestFramework): | ||
| def set_test_params(self): | ||
| self.num_nodes = 1 | ||
| self.extra_args = [["-acceptnonstdtxn=1", "-subdustfeepenalty=1"]] | ||
|
|
||
| def run_test(self): | ||
| self.wallet = MiniWallet(self.nodes[0]) | ||
|
|
||
| info = self.nodes[0].getmempoolinfo() | ||
| self.minrelay_per_kvb = info['minrelaytxfee'] * COIN # sats per kvB | ||
|
|
||
| self.test_dust_output_increases_required_fee() | ||
| self.test_partial_dust_proportional_penalty() | ||
| self.test_multiple_dust_outputs_stack() | ||
| self.test_penalty_disabled() | ||
| self.test_op_return_not_penalized() | ||
| self.test_above_dust_not_penalized() | ||
|
|
||
| def build_tx_with_dust(self, dust_values, fee): | ||
| utxo = self.wallet.get_utxo() | ||
| tx = CTransaction() | ||
| tx.version = 2 | ||
| tx.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]))] | ||
| input_value = int(utxo["value"] * COIN) | ||
| change = input_value - sum(dust_values) - fee | ||
| assert change > 0, f"Not enough funds: input={input_value}, dust={dust_values}, fee={fee}" | ||
| tx.vout = [CTxOut(v, self.wallet.get_output_script()) for v in dust_values] | ||
| tx.vout.append(CTxOut(change, self.wallet.get_output_script())) | ||
| self.wallet.sign_tx(tx) | ||
| return tx | ||
|
|
||
| def get_min_relay_fee(self, tx): | ||
| """Calculate the minimum relay fee for a transaction.""" | ||
| decoded = self.nodes[0].decoderawtransaction(tx.serialize().hex()) | ||
| return int(ceil(self.minrelay_per_kvb * decoded['vsize'] / 1000)) | ||
|
|
||
| def test_dust_output_increases_required_fee(self): | ||
| self.log.info("Test: sub-dust output penalizes effective fee") | ||
|
|
||
| # Build a probe tx to determine the exact min relay fee | ||
| probe = self.build_tx_with_dust([0], fee=1000) | ||
| min_fee = self.get_min_relay_fee(probe) | ||
|
|
||
| # fee just covers min relay + penalty - 1: rejected | ||
| reject_fee = DUST_THRESHOLD + min_fee - 1 | ||
| tx_low = self.build_tx_with_dust([0], fee=reject_fee) | ||
| result = self.nodes[0].testmempoolaccept([tx_low.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], False) | ||
| assert_equal(result[0]["reject-reason"], "min relay fee not met") | ||
|
|
||
| # fee covers min relay + penalty: accepted | ||
| accept_fee = DUST_THRESHOLD + min_fee | ||
| tx_high = self.build_tx_with_dust([0], fee=accept_fee) | ||
| result = self.nodes[0].testmempoolaccept([tx_high.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], True) | ||
|
|
||
| def test_partial_dust_proportional_penalty(self): | ||
| self.log.info("Test: partial dust value gets proportional penalty") | ||
|
|
||
| partial_value = 100 | ||
| penalty = DUST_THRESHOLD - partial_value # 230 sats | ||
|
|
||
| probe = self.build_tx_with_dust([partial_value], fee=1000) | ||
| min_fee = self.get_min_relay_fee(probe) | ||
|
|
||
| reject_fee = penalty + min_fee - 1 | ||
| tx_low = self.build_tx_with_dust([partial_value], fee=reject_fee) | ||
| result = self.nodes[0].testmempoolaccept([tx_low.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], False) | ||
| assert_equal(result[0]["reject-reason"], "min relay fee not met") | ||
|
|
||
| accept_fee = penalty + min_fee | ||
| tx_high = self.build_tx_with_dust([partial_value], fee=accept_fee) | ||
| result = self.nodes[0].testmempoolaccept([tx_high.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], True) | ||
|
|
||
| def test_multiple_dust_outputs_stack(self): | ||
| self.log.info("Test: multiple dust outputs stack penalties") | ||
|
|
||
| total_penalty = DUST_THRESHOLD * 2 # 660 sats | ||
|
|
||
| probe = self.build_tx_with_dust([0, 0], fee=1000) | ||
| min_fee = self.get_min_relay_fee(probe) | ||
|
|
||
| reject_fee = total_penalty + min_fee - 1 | ||
| tx_low = self.build_tx_with_dust([0, 0], fee=reject_fee) | ||
| result = self.nodes[0].testmempoolaccept([tx_low.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], False) | ||
| assert_equal(result[0]["reject-reason"], "min relay fee not met") | ||
|
|
||
| accept_fee = total_penalty + min_fee | ||
| tx_high = self.build_tx_with_dust([0, 0], fee=accept_fee) | ||
| result = self.nodes[0].testmempoolaccept([tx_high.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], True) | ||
|
|
||
| def test_penalty_disabled(self): | ||
| self.log.info("Test: penalty disabled with -subdustfeepenalty=0") | ||
| self.restart_node(0, extra_args=["-acceptnonstdtxn=1", "-subdustfeepenalty=0"]) | ||
| self.wallet.rescan_utxos() | ||
|
|
||
| probe = self.build_tx_with_dust([0], fee=1000) | ||
| min_fee = self.get_min_relay_fee(probe) | ||
|
|
||
| # Without penalty, just the min relay fee suffices | ||
| tx = self.build_tx_with_dust([0], fee=min_fee) | ||
| result = self.nodes[0].testmempoolaccept([tx.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], True) | ||
|
|
||
| def test_op_return_not_penalized(self): | ||
| self.log.info("Test: OP_RETURN outputs are not penalized (threshold=0)") | ||
| self.restart_node(0, extra_args=["-acceptnonstdtxn=1", "-subdustfeepenalty=1"]) | ||
| self.wallet.rescan_utxos() | ||
|
|
||
| utxo = self.wallet.get_utxo() | ||
| tx = CTransaction() | ||
| tx.version = 2 | ||
| tx.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]))] | ||
| input_value = int(utxo["value"] * COIN) | ||
| fee = 400 | ||
| tx.vout = [ | ||
| CTxOut(0, CScript([OP_RETURN, b"test data"])), | ||
| CTxOut(input_value - fee, self.wallet.get_output_script()), | ||
| ] | ||
| self.wallet.sign_tx(tx) | ||
|
|
||
| result = self.nodes[0].testmempoolaccept([tx.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], True) | ||
|
|
||
| def test_above_dust_not_penalized(self): | ||
| self.log.info("Test: outputs above dust threshold are not penalized") | ||
|
|
||
| probe = self.build_tx_with_dust([DUST_THRESHOLD + 100], fee=1000) | ||
| min_fee = self.get_min_relay_fee(probe) | ||
|
|
||
| tx = self.build_tx_with_dust([DUST_THRESHOLD + 100], fee=min_fee) | ||
| result = self.nodes[0].testmempoolaccept([tx.serialize().hex()]) | ||
| assert_equal(result[0]["allowed"], True) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| SubDustFeePenaltyTest(__file__).main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.