Repository navigation
mempool: expose dust limits from script details - #2675
Harshil-1603 wants to merge 2 commits into
Conversation
b10561c to
4e5c189
Compare
agammann
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
| 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, | ||
| ), | ||
| }) | ||
| } |
There was a problem hiding this comment.
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.
| 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) | ||
| } |
There was a problem hiding this comment.
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)
}
}
}
| // 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 { |
There was a problem hiding this comment.
We can remove the Get prefix here.
| } | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
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
49c0527 to
a7b6de1
Compare
Closes #2672.
GetDustThresholddescribes the cost model in bytes, but callers that know a future output's script length and witness form cannot use it until they allocate awire.TxOut. This adds the requested size-based API and the minimum non-dust amount at a relay fee.DustThresholdForScriptSizematches the existing serialized-output calculation, including CompactSize boundaries and witness discounting.MinNonDustAmountcomputes the smallest value that fails the existing strict dust comparison, rounds up, and clamps above the Bitcoin money range.IsDustkeeps 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 fullmake unit-race;make build;go vet ./...;golangci-lint run --new-from-rev=origin/master(0 new issues);git diff --check.