Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
chore(BUX-461): filter out invalid fee units
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain authored and dorzepowski committed Jan 12, 2024
1 parent 9a22645 commit 2410e8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions chainstate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chainstate
import (
"context"
"errors"
"fmt"
"time"

"github.com/BuxOrg/bux/logging"
Expand Down Expand Up @@ -200,6 +201,8 @@ func (c *Client) checkFeeUnit() error {
switch {
case feeUnit == nil:
return errors.New("no fee unit found")
case !feeUnit.IsValid():
return fmt.Errorf("invalid fee unit found: %s", feeUnit)
case feeUnit.IsZero():
c.options.logger.Warn().Msg("fee unit suggests no fees (free)")
default:
Expand Down
27 changes: 22 additions & 5 deletions utils/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,32 @@ func (f *FeeUnit) IsZero() bool {
return f.Satoshis == 0
}

// IsValid returns true if the Bytes in fee are greater than 0
func (f *FeeUnit) IsValid() bool {
return f.Bytes > 0
}

// ValidFees filters out invalid fees from a list of fee units
func ValidFees(feeUnits []FeeUnit) []FeeUnit {
validFees := []FeeUnit{}
for _, fee := range feeUnits {
if fee.IsValid() {
validFees = append(validFees, fee)
}
}
return validFees
}

// LowestFee get the lowest fee from a list of fee units, if defaultValue exists and none is found, return defaultValue
func LowestFee(feeUnits []FeeUnit, defaultValue *FeeUnit) *FeeUnit {
if len(feeUnits) == 0 {
validFees := ValidFees(feeUnits)
if len(validFees) == 0 {
return defaultValue
}
minFee := feeUnits[0]
for i := 1; i < len(feeUnits); i++ {
if feeUnits[i].IsLowerThan(&minFee) {
minFee = feeUnits[i]
minFee := validFees[0]
for i := 1; i < len(validFees); i++ {
if validFees[i].IsLowerThan(&minFee) {
minFee = validFees[i]
}
}
return &minFee
Expand Down

0 comments on commit 2410e8e

Please sign in to comment.