Skip to content

Commit

Permalink
Fix leastOverFundWithLimit bug
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp committed Jul 13, 2023
1 parent ed0c78a commit d06f5db
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client/asset/dcr/coin_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func subsetWithLeastOverFund(enough func(uint64, uint32, *compositeUTXO) (bool,
totalSize += shuffledUTXOs[i].input.Size()

if e, _ := enough(totalBefore, sizeBefore, shuffledUTXOs[i]); 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
39 changes: 39 additions & 0 deletions client/asset/dcr/coin_selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,45 @@ func Test_leastOverFund(t *testing.T) {
}
}

func Test_leastOverFundWithLimit(t *testing.T) {
newU := func(amt float64) *compositeUTXO {
return &compositeUTXO{
rpc: &walletjson.ListUnspentResult{Amount: amt},
input: &dexdcr.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(reserveEnough(10e8), tt.limit, tt.utxos)
sort.Slice(got, func(i int, j int) bool {
return got[i].rpc.Amount < got[j].rpc.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

0 comments on commit d06f5db

Please sign in to comment.