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

client/btc: Fix bug in leastOverFundWithLimit #2435

Merged
merged 1 commit into from Jul 13, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion client/asset/btc/coin_selection.go
Expand Up @@ -123,7 +123,7 @@ func subsetWithLeastOverFund(enough func(uint64, uint64) (bool, uint64), maxFund
nTotal += shuffledUTXOs[i].amount
totalSize += uint64(shuffledUTXOs[i].input.VBytes())
if e, _ := enough(totalSize, nTotal); e {
if nTotal < best || (nTotal == best && numIncluded < bestNumIncluded) && nTotal <= maxFund {
if (nTotal < best || (nTotal == best && numIncluded < bestNumIncluded)) && nTotal <= maxFund {
best = nTotal
if bestIncluded == nil {
bestIncluded = make([]bool, len(shuffledUTXOs))
Expand Down
44 changes: 44 additions & 0 deletions client/asset/btc/coin_selection_test.go
Expand Up @@ -96,6 +96,50 @@ func Test_leastOverFund(t *testing.T) {
}
}

func Test_leastOverFundWithLimit(t *testing.T) {
enough := func(_, sum uint64) (bool, uint64) {
return sum >= 10e8, 0
}
newU := func(amt float64) *compositeUTXO {
return &compositeUTXO{
utxo: &utxo{
amount: uint64(amt) * 1e8,
},
input: &dexbtc.SpendInfo{},
}
}
tests := []struct {
name string
limit uint64
utxos []*compositeUTXO
want []*compositeUTXO
}{
{
"1,3",
10e8,
[]*compositeUTXO{newU(1), newU(8), newU(9)},
[]*compositeUTXO{newU(1), newU(9)},
},
{
"max fund too low",
9e8,
[]*compositeUTXO{newU(1), newU(8), newU(9)},
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := leastOverFundWithLimit(enough, tt.limit, tt.utxos)
sort.Slice(got, func(i int, j int) bool {
return got[i].amount < got[j].amount
})
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("subset() = %v, want %v", got, tt.want)
}
})
}
}

func Fuzz_leastOverFund(f *testing.F) {
type seed struct {
amt uint64
Expand Down