Skip to content

Commit

Permalink
uncomment test
Browse files Browse the repository at this point in the history
  • Loading branch information
akremstudy committed Nov 27, 2024
1 parent 1bc2eea commit bfa03fd
Show file tree
Hide file tree
Showing 2 changed files with 449 additions and 454 deletions.
96 changes: 39 additions & 57 deletions x/oracle/keeper/weighted_median.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,55 @@ package keeper

import (
"context"
"fmt"
"errors"
"math/big"
"sort"

"github.com/tellor-io/layer/x/oracle/types"

"cosmossdk.io/collections"
cosmomath "cosmossdk.io/math"
)

func (k Keeper) WeightedMedian(ctx context.Context, reports []types.MicroReport, metaId uint64) (*types.Aggregate, error) {
var medianReport types.Aggregate
// values := make(map[string]cosmomath.LegacyDec)

// for _, r := range reports {
// val, ok := new(big.Int).SetString(r.Value, 16)
// if !ok {
// k.Logger(ctx).Error("WeightedMedian", "error", "failed to parse value")
// return nil, errors.New("failed to parse value")
// }
// values[r.Reporter] = cosmomath.LegacyNewDecFromBigInt(val)
// }

// sort.SliceStable(reports, func(i, j int) bool {
// return values[reports[i].Reporter].BigInt().Cmp(values[reports[j].Reporter].BigInt()) < 0
// })

// totalReporterPower := cosmomath.LegacyZeroDec()
// for _, r := range reports {
// totalReporterPower = totalReporterPower.Add(cosmomath.LegacyNewDec(int64(r.Power)))
// medianReport.Reporters = append(medianReport.Reporters, &types.AggregateReporter{Reporter: r.Reporter, Power: r.Power, BlockNumber: r.BlockNumber})
// }
values := make(map[string]cosmomath.LegacyDec)

for _, r := range reports {
val, ok := new(big.Int).SetString(r.Value, 16)
if !ok {
k.Logger(ctx).Error("WeightedMedian", "error", "failed to parse value")
return nil, errors.New("failed to parse value")
}
values[r.Reporter] = cosmomath.LegacyNewDecFromBigInt(val)
}

// halfTotalPower := totalReporterPower.Quo(cosmomath.LegacyNewDec(2))
// cumulativePower := cosmomath.LegacyZeroDec()
sort.SliceStable(reports, func(i, j int) bool {
return values[reports[i].Reporter].BigInt().Cmp(values[reports[j].Reporter].BigInt()) < 0
})

// // Find the weighted median
// for i, s := range reports {
// cumulativePower = cumulativePower.Add(cosmomath.LegacyNewDec(int64(s.Power)))
// if cumulativePower.BigInt().Cmp(halfTotalPower.BigInt()) >= 0 {
// medianReport.ReporterPower = uint64(totalReporterPower.TruncateInt64())
// medianReport.AggregateReporter = s.Reporter
// medianReport.AggregateValue = median.Value
// medianReport.QueryId = s.QueryId
// medianReport.AggregateReportIndex = uint64(i)
// medianReport.MicroHeight = s.BlockNumber
// medianReport.MetaId = metaId
// break
// }
// }
median, err := k.Median.Get(ctx, metaId)
if err != nil {
// print error
fmt.Printf("Error getting median value from store: %v\n", err)
totalReporterPower := cosmomath.LegacyZeroDec()
for _, r := range reports {
totalReporterPower = totalReporterPower.Add(cosmomath.LegacyNewDec(int64(r.Power)))
medianReport.Reporters = append(medianReport.Reporters, &types.AggregateReporter{Reporter: r.Reporter, Power: r.Power, BlockNumber: r.BlockNumber})
}
valuesStored, err := k.Values.Get(ctx, collections.Join(metaId, median.Value))
if err != nil {
// print error
fmt.Printf("Error getting median value from store: %v\n", err)
}
tPower, err := k.ValuesWeightSum.Get(ctx, metaId)
if err != nil {
// print error
fmt.Printf("Error getting median value from store: %v\n", err)

halfTotalPower := totalReporterPower.Quo(cosmomath.LegacyNewDec(2))
cumulativePower := cosmomath.LegacyZeroDec()

// Find the weighted median
for i, s := range reports {
cumulativePower = cumulativePower.Add(cosmomath.LegacyNewDec(int64(s.Power)))
if cumulativePower.BigInt().Cmp(halfTotalPower.BigInt()) >= 0 {
medianReport.ReporterPower = uint64(totalReporterPower.TruncateInt64())
medianReport.AggregateReporter = s.Reporter
medianReport.AggregateValue = s.Value
medianReport.QueryId = s.QueryId
medianReport.AggregateReportIndex = uint64(i)
medianReport.MicroHeight = s.BlockNumber
medianReport.MetaId = metaId
break
}
}
medianReport.AggregateValue = median.Value
medianReport.AggregateReporter = valuesStored.Report.Reporter
medianReport.MicroHeight = valuesStored.Report.BlockNumber
medianReport.QueryId = valuesStored.Report.QueryId
medianReport.MetaId = metaId
medianReport.ReporterPower = tPower

return &medianReport, nil
}
Loading

0 comments on commit bfa03fd

Please sign in to comment.