Skip to content

mempool: expose dust limits from script details - #2675

Open
Harshil-1603 wants to merge 2 commits into
btcsuite:masterfrom
Harshil-1603:fix/dust-threshold-from-script-size
Open

Harshil-1603 wants to merge 2 commits into
btcsuite:masterfrom
Harshil-1603:fix/dust-threshold-from-script-size

Conversation

@Harshil-1603

@Harshil-1603 Harshil-1603 commented Nov 1, 2026 •

Copy link
Copy Markdown

Closes #2672.

GetDustThreshold describes the cost model in bytes, but callers that know a future output's script length and witness form cannot use it until they allocate a wire.TxOut. This adds the requested size-based API and the minimum non-dust amount at a relay fee.

  • DustThresholdForScriptSize matches the existing serialized-output calculation, including CompactSize boundaries and witness discounting.
  • MinNonDustAmount computes the smallest value that fails the existing strict dust comparison, rounds up, and clamps above the Bitcoin money range.
  • IsDust keeps its signed comparison and existing behavior unchanged.

The regression coverage compares the size-based calculation with real outputs across standard scripts and CompactSize boundaries, verifies the non-dust boundary against IsDust, and covers rounding and multiplication-overflow cases independently with arbitrary-precision arithmetic.

Validation: make unit; final full make unit-race; make build; go vet ./...; golangci-lint run --new-from-rev=origin/master (0 new issues); git diff --check.

@allocz allocz 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.

Commit message is too vague, patch does not follow the formatting rules and the tests are also hard to understand.

@Harshil-1603
Harshil-1603 force-pushed the fix/dust-threshold-from-script-size branch from b10561c to 4e5c189 Compare November 7, 2026 16:13

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The test matrix crosses each CompactSize length boundary (252/253 and 65535/65536) and compares the size-only helper to an actually serialized TxOut. The separate big.Int reference for MinNonDustAmount is useful here because it checks rounding and overflow without reusing the production bits.Mul64/Div64 path.

@allocz allocz 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.

Checked the behavior of GetDustThreshold against the behavior before the changes and the results still the same. Also checked the results of MinNonDustAmount against GetDustThreshold for different min relay feerates. The main issue that I see here is that you've modified the GetDustThreshold code without adding a test table to it first, for this reason I had to spend some time reverting the changes and writing a test to double check if the behavior still the same, I would suggest to add the test table for GetDustThreshold before modifying the code, then you can reuse the table to test the new procedure.

Comment thread mempool/policy_test.go
Comment on lines +794 to +837
p2pkh := append(
[]byte{txscript.OP_DUP, txscript.OP_HASH160, 20},
make([]byte, 20)...,
)
p2pkh = append(p2pkh, txscript.OP_EQUALVERIFY, txscript.OP_CHECKSIG)
p2sh := append([]byte{txscript.OP_HASH160, 20}, make([]byte, 20)...)
p2sh = append(p2sh, txscript.OP_EQUAL)

scripts := []struct {
name string
script []byte
}{{
name: "P2PKH",
script: p2pkh,
}, {
name: "P2SH",
script: p2sh,
}, {
name: "P2WPKH",
script: append(
[]byte{txscript.OP_0, 20}, make([]byte, 20)...,
),
}, {
name: "P2WSH",
script: append(
[]byte{txscript.OP_0, 32}, make([]byte, 32)...,
),
}, {
name: "P2TR",
script: append(
[]byte{txscript.OP_1, 32}, make([]byte, 32)...,
),
}}
for _, size := range []int{252, 253, 65535, 65536} {
scripts = append(scripts, struct {
name string
script []byte
}{
name: fmt.Sprintf("script-%d", size),
script: bytes.Repeat(
[]byte{txscript.OP_TRUE}, size,
),
})
}

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.

What do you think about renaming scripts to tests? Also, consistency can be improved here by creating all scripts as variables above the test tables. I would use

[]struct{
        {
        },
        {
        },
}

To format the test table entries.

Comment thread mempool/policy_test.go Outdated
Comment on lines +842 to +860
witness := txscript.IsWitnessProgram(test.script)

// Check the size against actual serialization.
inputSize := 148
if witness {
inputSize = 67
}

want := uint64(3 * (txOut.SerializeSize() + inputSize))
got := GetDustThresholdForScriptSize(
uint32(len(test.script)), witness,
)
if got != want {
t.Fatalf("threshold = %d, want %d", got, want)
}
legacy := uint64(GetDustThreshold(txOut))
if legacy != got {
t.Fatalf("legacy = %d, want %d", legacy, got)
}

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.

What you named legacy here, is not really legacy, as it is calling the new procedure. To make easier for reviewers, you can add a test table for GetDustThreshold in a previous commit (before modifying it) and then reusing the same table to test GetDustThresholdForSize, for example:

type dustThresholdTest struct {
        name     string
        script   []byte
        isSegwit bool
        wantSize int64
}

func dustThresholdTestVector() []dustThresholdTest {
        p2pkh := append(
                []byte{txscript.OP_DUP, txscript.OP_HASH160, 20},
                make([]byte, 20)...,
        )
        p2sh := append(
                append([]byte{txscript.OP_HASH160, 20}, make([]byte, 20)...),
                txscript.OP_EQUAL,
        )
        p2wpkh := append([]byte{txscript.OP_0, 20}, make([]byte, 20)...)
        p2wsh := append([]byte{txscript.OP_0, 32}, make([]byte, 32)...)
        p2tr := append([]byte{txscript.OP_1, 32}, make([]byte, 32)...)
        script252 := bytes.Repeat([]byte{txscript.OP_TRUE}, 252)
        script253 := bytes.Repeat([]byte{txscript.OP_TRUE}, 253)
        script65535 := bytes.Repeat([]byte{txscript.OP_TRUE}, 65535)
        script65536 := bytes.Repeat([]byte{txscript.OP_TRUE}, 65536)
        script4MB := bytes.Repeat([]byte{txscript.OP_TRUE}, 4_000_000)

        return []dustThresholdTest{
                {"p2pkh", p2pkh, false, 540},
                {"p2sh", p2sh, false, 540},
                {"p2wpkh", p2wpkh, true, 294},
                {"p2wsh", p2wsh, true, 330},
                {"p2tr", p2tr, true, 330},
                {"script-252", script252, false, 1227},
                {"script-253", script253, false, 1236},
                {"script-65535", script65535, false, 197082},
                {"script-65536", script65536, false, 197091},
                {"script-4MB", script4MB, false, 12000483},
        }
}

// TestGetDustThreshold checks the size outputs of TestGetDustThreshold to
// prevent regressions in future changes.
func TestGetDustThreshold(t *testing.T) {
        tests := dustThresholdTestVector()

        for _, test := range tests {
                gotSize := GetDustThreshold(&wire.TxOut{
                        Value:    0,
                        PkScript: test.script,
                })

                if test.wantSize != gotSize {
                        t.Errorf("test %s; want size %d, got %d", test.name,
                                test.wantSize, gotSize)
                }
        }
}

Comment thread mempool/policy.go Outdated
Comment on lines +237 to +242
// GetDustThresholdForScriptSize returns the dust threshold, in bytes, for an
// output with a scriptSize-byte public key script. When isWitness is true,
// the typical spending input's signature script receives the witness discount.
// The result is a size, not a satoshi amount, and matches GetDustThreshold for
// an output with the same script size and witness classification.
func GetDustThresholdForScriptSize(scriptSize uint32, isWitness bool) uint64 {

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.

We can remove the Get prefix here.

Comment thread mempool/dust_threshold_test.go Outdated
}
})
}
}

@allocz allocz Nov 11, 2026 •

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.

There's no reason to create another file for this test. Also, don't you think that would be easier to confirm that the behavior of the function don't changed with the first commit being only adding this test? Because this way we can just checkout to the first commit and confirm that this was the behavior before the change and then confirm that stays the same by checking out to the next commit, after the change. In the second and last commit you can add the changes to the code/tests and reuse the same test vectors of the previous commit in the TestDustThresholdForScriptSize.

Add explicit standard-script and CompactSize boundary vectors to the
existing policy tests before changing the output-based implementation.
Include the four-megabyte script case and the full P2PKH script.

Suggested-by: allocz
Assisted-by: OpenAI Codex
Add DustThresholdForScriptSize for callers that know the future output
script length and witness form without allocating a wire.TxOut. Keep the
existing output-based threshold behavior pinned by the preceding tests
and reuse their vectors to check the size-only implementation.

Add MinNonDustAmount with upward fee rounding and overflow clamping,
checked independently with arbitrary-precision arithmetic. Preserve the
existing signed IsDust comparison.

Closes btcsuite#2672.

Suggested-by: allocz
Assisted-by: OpenAI Codex
@Harshil-1603
Harshil-1603 force-pushed the fix/dust-threshold-from-script-size branch from 49c0527 to a7b6de1 Compare November 12, 2026 21:39

@allocz allocz 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.

Confirmed that all tests are passing in both commits.

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.

mempool: expose the dust limit from script size and witness status

3 participants