Skip to content

Commit

Permalink
fix(oracle): Remove float SQRT calculation to avoid determinism issues (
Browse files Browse the repository at this point in the history
#1700)

* fix: remove float sqrt calculation

* chore: changelog

* fix(oracle): exact sqrts (common.SqrtDec) gives more accurate math

---------

Co-authored-by: Unique-Divine <[email protected]>
  • Loading branch information
matthiasmatt and Unique-Divine authored Dec 11, 2023
1 parent f4d0933 commit b400eed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ v0.47.5.
* [#1614](https://github.com/NibiruChain/nibiru/pull/1614) - refactor(proto): Use explicit namespacing on proto imports for #1608
* [#1610](https://github.com/NibiruChain/nibiru/pull/1610) - refactor(app): Simplify app.go with less redundant imports using struct embedding.
* [#1606](https://github.com/NibiruChain/nibiru/pull/1606) - fix(perp): emit `MarketUpdatedEvent` in the absence of index price
* [#1700](https://github.com/NibiruChain/nibiru/pull/1700) - fix(oracle): remove non deterministic sqrt calculation

### Dependencies

Expand Down
11 changes: 5 additions & 6 deletions x/oracle/types/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package types

import (
"encoding/json"
"fmt"
"math"
"sort"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/common/asset"
)

Expand Down Expand Up @@ -149,9 +147,10 @@ func (pb ExchangeRateVotes) StandardDeviation(median sdk.Dec) (standardDeviation

variance := sum.QuoInt64(int64(n))

floatNum, _ := strconv.ParseFloat(variance.String(), 64)
floatNum = math.Sqrt(floatNum)
standardDeviation, _ = sdk.NewDecFromStr(fmt.Sprintf("%f", floatNum))
standardDeviation, err := common.SqrtDec(variance)
if err != nil {
return sdk.ZeroDec()
}

return
}
Expand Down
8 changes: 4 additions & 4 deletions x/oracle/types/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,21 @@ func TestPBStandardDeviation(t *testing.T) {
[]float64{1.0, 2.0, 10.0, 100000.0},
[]int64{1, 1, 100, 1},
[]bool{true, true, true, true},
sdk.NewDecWithPrec(4999500036300, types.OracleDecPrecision),
sdk.MustNewDecFromStr("49995.000362536000000000"),
},
{
// Adding fake validator doesn't change outcome
[]float64{1.0, 2.0, 10.0, 100000.0, 10000000000},
[]int64{1, 1, 100, 1, 10000},
[]bool{true, true, true, true, false},
sdk.NewDecWithPrec(447213595075100600, types.OracleDecPrecision),
sdk.MustNewDecFromStr("4472135950.751005519000000000"),
},
{
// Tie votes
[]float64{1.0, 2.0, 3.0, 4.0},
[]int64{1, 100, 100, 1},
[]bool{true, true, true, true},
sdk.NewDecWithPrec(122474500, types.OracleDecPrecision),
sdk.MustNewDecFromStr("1.224744871000000000"),
},
{
// No votes
Expand All @@ -270,7 +270,7 @@ func TestPBStandardDeviation(t *testing.T) {
[]float64{1.0, 2.0, 10.0, 100000.0, -99999999999.0, 0},
[]int64{1, 1, 100, 1, 1, 1},
[]bool{true, true, true, true, true, true},
sdk.NewDecWithPrec(4999500036300, types.OracleDecPrecision),
sdk.MustNewDecFromStr("49995.000362536000000000"),
},
}

Expand Down

0 comments on commit b400eed

Please sign in to comment.