Skip to content

Commit

Permalink
add Equals for aggAlgorithm
Browse files Browse the repository at this point in the history
return nil for empty list when do median
  • Loading branch information
leonz789 committed Feb 8, 2025
1 parent 0b19739 commit e1628fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions x/oracle/keeper/feedermanagement/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ func (a *aggregator) Equals(a2 *aggregator) bool {
if !a.t.Equals(a2.t) {
return false
}

if !a.v.Equals(a2.v) {
return false
}

if !a.ds.Equals(a2.ds) {
return false
}

if !a.algo.Equals(a2.algo) {
return false
}

return true
}

Expand Down
20 changes: 20 additions & 0 deletions x/oracle/keeper/feedermanagement/algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
"strings"
)

type AlgoType string

const (
AlgoMedian AlgoType = "median"
)

type BigIntList []*big.Int

func (b BigIntList) Len() int {
Expand All @@ -21,6 +27,9 @@ func (b BigIntList) Swap(i, j int) {
}

func (b BigIntList) Median() *big.Int {
if len(b) == 0 {
return nil
}
sort.Sort(b)
l := len(b)
if l%2 == 1 {
Expand All @@ -33,6 +42,8 @@ type AggAlgorithm interface {
Add(*PriceResult) bool
GetResult() *PriceResult
Reset()
Type() AlgoType
Equals(AggAlgorithm) bool
}

type priceType int
Expand Down Expand Up @@ -105,6 +116,7 @@ func (a *AggMedian) GetResult() *PriceResult {
return nil
}
if a.t == number {
// when a.t is set to number, the length of a.list must be bigger than 0, so the Median() must return a non-nil result
result := BigIntList(a.list).Median().String()
decimal := a.decimal
return &PriceResult{
Expand All @@ -128,5 +140,13 @@ func (a *AggMedian) Reset() {
a.finalString = ""
}

func (a *AggMedian) Type() AlgoType {
return AlgoMedian
}

func (a *AggMedian) Equals(a2 AggAlgorithm) bool {
return a.Type() == a2.Type()
}

//nolint:unused
var defaultAggMedian = NewAggMedian()

0 comments on commit e1628fc

Please sign in to comment.