Skip to content

Commit

Permalink
ORA1289 Test delegators cant easily game the system (#302)
Browse files Browse the repository at this point in the history
Test some of the ways that delegators might be able to game the system,
and ensure that they are not possible.

---------

Co-authored-by: T <relyt29@users.noreply.github.com>
Co-authored-by: Kenny P <17100641+kpeluso@users.noreply.github.com>
Co-authored-by: Kenny <pelusoken@gmail.com>
  • Loading branch information
4 people authored Jun 2, 2024
1 parent b3a3c7e commit 370d61c
Show file tree
Hide file tree
Showing 5 changed files with 397 additions and 64 deletions.
49 changes: 49 additions & 0 deletions test/testutil/inEpsilon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package testutil

import (
"testing"

alloraMath "github.com/allora-network/allora-chain/math"
require "github.com/stretchr/testify/require"
)

func InEpsilon(t *testing.T, value alloraMath.Dec, target string, epsilon string) {
epsilonDec := alloraMath.MustNewDecFromString(epsilon)
targetDec := alloraMath.MustNewDecFromString(target)
one := alloraMath.MustNewDecFromString("1")

lowerMultiplier, err := one.Sub(epsilonDec)
require.NoError(t, err)
lowerBound, err := targetDec.Mul(lowerMultiplier)
require.NoError(t, err)

upperMultiplier, err := one.Add(epsilonDec)
require.NoError(t, err)
upperBound, err := targetDec.Mul(upperMultiplier)
require.NoError(t, err)

if lowerBound.Lt(upperBound) { // positive values, lower < value < upper
require.True(t, value.Gte(lowerBound), "value: %s, lowerBound: %s", value.String(), lowerBound.String())
require.True(t, value.Lte(upperBound), "value: %s, upperBound: %s", value.String(), upperBound.String())
} else { // negative values, upper < value < lower
require.True(t, value.Lte(lowerBound), "value: %s, lowerBound: %s", value.String(), lowerBound.String())
require.True(t, value.Gte(upperBound), "value: %s, upperBound: %s", value.String(), upperBound.String())
}
}

func InEpsilon2(t *testing.T, value alloraMath.Dec, target string) {
InEpsilon(t, value, target, "0.01")
}

func InEpsilon3(t *testing.T, value alloraMath.Dec, target string) {
InEpsilon(t, value, target, "0.001")
}

/*unused
func InEpsilon4(t *testing.T, value alloraMath.Dec, target string) {
s.inEpsilon(t, value, target, "0.0001")
}*/

func InEpsilon5(t *testing.T, value alloraMath.Dec, target string) {
InEpsilon(t, value, target, "0.00001")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inference_synthesis_test

import (
alloraMath "github.com/allora-network/allora-chain/math"
"github.com/allora-network/allora-chain/test/testutil"
inference_synthesis "github.com/allora-network/allora-chain/x/emissions/keeper/inference_synthesis"
)

Expand Down Expand Up @@ -36,7 +37,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcWeightFromRegret() {
weight, err := inference_synthesis.CalcWeightFromRegret(regretFrac, maxRegret, pNorm, cNorm)
s.Require().NoError(err)

s.inEpsilon5(weight, tc.expectedWeight)
testutil.InEpsilon5(s.T(), weight, tc.expectedWeight)
}
}

Expand Down
43 changes: 0 additions & 43 deletions x/emissions/keeper/inference_synthesis/inference_synthesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
// cosmosMath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
"github.com/allora-network/allora-chain/app/params"
alloraMath "github.com/allora-network/allora-chain/math"
"github.com/allora-network/allora-chain/x/emissions/keeper"
"github.com/allora-network/allora-chain/x/emissions/keeper/msgserver"
"github.com/allora-network/allora-chain/x/emissions/module"
Expand Down Expand Up @@ -124,48 +123,6 @@ func (s *InferenceSynthesisTestSuite) SetupTest() {
}
}

func (s *InferenceSynthesisTestSuite) inEpsilon(value alloraMath.Dec, target string, epsilon string) {
require := s.Require()
epsilonDec := alloraMath.MustNewDecFromString(epsilon)
targetDec := alloraMath.MustNewDecFromString(target)
one := alloraMath.MustNewDecFromString("1")

lowerMultiplier, err := one.Sub(epsilonDec)
require.NoError(err)
lowerBound, err := targetDec.Mul(lowerMultiplier)
require.NoError(err)

upperMultiplier, err := one.Add(epsilonDec)
require.NoError(err)
upperBound, err := targetDec.Mul(upperMultiplier)
require.NoError(err)

if lowerBound.Lt(upperBound) { // positive values, lower < value < upper
require.True(value.Gte(lowerBound), "value: %s, lowerBound: %s", value.String(), lowerBound.String())
require.True(value.Lte(upperBound), "value: %s, upperBound: %s", value.String(), upperBound.String())
} else { // negative values, upper < value < lower
require.True(value.Lte(lowerBound), "value: %s, lowerBound: %s", value.String(), lowerBound.String())
require.True(value.Gte(upperBound), "value: %s, upperBound: %s", value.String(), upperBound.String())
}
}

func (s *InferenceSynthesisTestSuite) inEpsilon2(value alloraMath.Dec, target string) {
s.inEpsilon(value, target, "0.01")
}

func (s *InferenceSynthesisTestSuite) inEpsilon3(value alloraMath.Dec, target string) {
s.inEpsilon(value, target, "0.001")
}

/*unused
func (s *InferenceSynthesisTestSuite) inEpsilon4(value alloraMath.Dec, target string) {
s.inEpsilon(value, target, "0.0001")
}*/

func (s *InferenceSynthesisTestSuite) inEpsilon5(value alloraMath.Dec, target string) {
s.inEpsilon(value, target, "0.00001")
}

func TestModuleTestSuite(t *testing.T) {
suite.Run(t, new(InferenceSynthesisTestSuite))
}
40 changes: 20 additions & 20 deletions x/emissions/keeper/inference_synthesis/network_inferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcOneOutInferencesMultipleWorkers()
for _, oneOutInference := range oneOutInfererValues {
if expected.Worker == oneOutInference.Worker {
found = true
s.inEpsilon2(oneOutInference.Value, expected.Value)
testutil.InEpsilon2(s.T(), oneOutInference.Value, expected.Value)
}
}
if !found {
Expand All @@ -437,7 +437,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcOneOutInferencesMultipleWorkers()
for _, oneOutImpliedInference := range oneOutForecasterValues {
if expected.Worker == oneOutImpliedInference.Worker {
found = true
s.inEpsilon3(oneOutImpliedInference.Value, expected.Value)
testutil.InEpsilon3(s.T(), oneOutImpliedInference.Value, expected.Value)
}
}
if !found {
Expand Down Expand Up @@ -583,7 +583,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcOneOutInferences5Workers3Forecaste
for _, oneOutInference := range oneOutInfererValues {
if expected.Worker == oneOutInference.Worker {
found = true
s.inEpsilon2(oneOutInference.Value, expected.Value)
testutil.InEpsilon2(s.T(), oneOutInference.Value, expected.Value)
}
}
if !found {
Expand All @@ -596,7 +596,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcOneOutInferences5Workers3Forecaste
for _, oneOutImpliedInference := range oneOutForecasterValues {
if expected.Worker == oneOutImpliedInference.Worker {
found = true
s.inEpsilon3(oneOutImpliedInference.Value, expected.Value)
testutil.InEpsilon3(s.T(), oneOutImpliedInference.Value, expected.Value)
}
}
if !found {
Expand Down Expand Up @@ -1309,8 +1309,8 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlock() {
)
require.NoError(err)
s.inEpsilon5(valueBundle.CombinedValue, "-0.08418238013037833391277949761424359")
s.inEpsilon3(valueBundle.NaiveValue, "-0.09089296942031617121265381217201911")
testutil.InEpsilon5(s.T(), valueBundle.CombinedValue, "-0.08418238013037833391277949761424359")
testutil.InEpsilon3(s.T(), valueBundle.NaiveValue, "-0.09089296942031617121265381217201911")
for _, inference := range inferences.Inferences {
found := false
Expand All @@ -1325,11 +1325,11 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlock() {
for _, forecasterValue := range valueBundle.ForecasterValues {
switch string(forecasterValue.Worker) {
case forecaster0:
s.inEpsilon2(forecasterValue.Value, "-0.07360672083447152549990990449686835")
testutil.InEpsilon2(s.T(), forecasterValue.Value, "-0.07360672083447152549990990449686835")
case forecaster1:
s.inEpsilon2(forecasterValue.Value, "-0.07263773178885971876894786458169429")
testutil.InEpsilon2(s.T(), forecasterValue.Value, "-0.07263773178885971876894786458169429")
case forecaster2:
s.inEpsilon2(forecasterValue.Value, "-0.07333303938740419999999999999997501")
testutil.InEpsilon2(s.T(), forecasterValue.Value, "-0.07333303938740419999999999999997501")
default:
require.Fail("Unexpected forecaster %v", forecasterValue.Worker)
}
Expand All @@ -1338,15 +1338,15 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlock() {
for _, oneOutInfererValue := range valueBundle.OneOutInfererValues {
switch string(oneOutInfererValue.Worker) {
case reputer0:
s.inEpsilon2(oneOutInfererValue.Value, "-0.09112256843970400263910853205529701")
testutil.InEpsilon2(s.T(), oneOutInfererValue.Value, "-0.09112256843970400263910853205529701")
case reputer1:
s.inEpsilon2(oneOutInfererValue.Value, "-0.0849762526781571419651680849185323")
testutil.InEpsilon2(s.T(), oneOutInfererValue.Value, "-0.0849762526781571419651680849185323")
case reputer2:
s.inEpsilon2(oneOutInfererValue.Value, "-0.07508631218815497306553765277306250")
testutil.InEpsilon2(s.T(), oneOutInfererValue.Value, "-0.07508631218815497306553765277306250")
case reputer3:
s.inEpsilon2(oneOutInfererValue.Value, "-0.07762408532626815421861778958359602")
testutil.InEpsilon2(s.T(), oneOutInfererValue.Value, "-0.07762408532626815421861778958359602")
case reputer4:
s.inEpsilon2(oneOutInfererValue.Value, "-0.097732445271841")
testutil.InEpsilon2(s.T(), oneOutInfererValue.Value, "-0.097732445271841")
default:
require.Fail("Unexpected worker %v", oneOutInfererValue.Worker)
}
Expand All @@ -1355,11 +1355,11 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlock() {
for _, oneInForecasterValue := range valueBundle.OneInForecasterValues {
switch string(oneInForecasterValue.Worker) {
case forecaster0:
s.inEpsilon2(oneInForecasterValue.Value, "-0.08562185282145071310963674889631515")
testutil.InEpsilon2(s.T(), oneInForecasterValue.Value, "-0.08562185282145071310963674889631515")
case forecaster1:
s.inEpsilon2(oneInForecasterValue.Value, "-0.0857186447720307")
testutil.InEpsilon2(s.T(), oneInForecasterValue.Value, "-0.0857186447720307")
case forecaster2:
s.inEpsilon2(oneInForecasterValue.Value, "-0.0853937827718047")
testutil.InEpsilon2(s.T(), oneInForecasterValue.Value, "-0.0853937827718047")
default:
require.Fail("Unexpected worker %v", oneInForecasterValue.Worker)
}
Expand All @@ -1368,11 +1368,11 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlock() {
for _, oneOutForecasterValue := range valueBundle.OneOutForecasterValues {
switch string(oneOutForecasterValue.Worker) {
case forecaster0:
s.inEpsilon2(oneOutForecasterValue.Value, "-0.08571218484894173358533220915281566")
testutil.InEpsilon2(s.T(), oneOutForecasterValue.Value, "-0.08571218484894173358533220915281566")
case forecaster1:
s.inEpsilon2(oneOutForecasterValue.Value, "-0.08575177379258356927513673523336433")
testutil.InEpsilon2(s.T(), oneOutForecasterValue.Value, "-0.08575177379258356927513673523336433")
case forecaster2:
s.inEpsilon2(oneOutForecasterValue.Value, "-0.08585235237690237323017634246068422")
testutil.InEpsilon2(s.T(), oneOutForecasterValue.Value, "-0.08585235237690237323017634246068422")
default:
require.Fail("Unexpected worker %v", oneOutForecasterValue.Worker)
}
Expand Down
Loading

0 comments on commit 370d61c

Please sign in to comment.