Skip to content

Commit

Permalink
ORA-1238 Test correct number of results given number of actors (#236)
Browse files Browse the repository at this point in the history
Co-authored-by: Kenny P <[email protected]>
  • Loading branch information
br4e and kpeluso authored May 11, 2024
1 parent f4c69ff commit a53b6d4
Show file tree
Hide file tree
Showing 3 changed files with 477 additions and 21 deletions.
194 changes: 194 additions & 0 deletions x/emissions/keeper/inference_synthesis/network_inferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1274,3 +1274,197 @@ func TestSelectTopNWorkerNonces(t *testing.T) {
})
}
}

func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesTwoWorkerTwoForecasters() {
k := s.emissionsKeeper
ctx := s.ctx
topicId := uint64(1)

worker1 := "worker1"
worker2 := "worker2"
worker3 := "worker3"
worker4 := "worker4"
worker1Add := sdk.AccAddress(worker1)
worker2Add := sdk.AccAddress(worker2)
worker3Add := sdk.AccAddress(worker3)
worker4Add := sdk.AccAddress(worker4)

// Set up input data
inferences := &emissions.Inferences{
Inferences: []*emissions.Inference{
{Inferer: worker1, Value: alloraMath.MustNewDecFromString("0.5")},
{Inferer: worker2, Value: alloraMath.MustNewDecFromString("0.7")},
},
}

forecasts := &emissions.Forecasts{
Forecasts: []*emissions.Forecast{
{
Forecaster: worker3,
ForecastElements: []*emissions.ForecastElement{
{Inferer: worker1, Value: alloraMath.MustNewDecFromString("0.6")},
{Inferer: worker2, Value: alloraMath.MustNewDecFromString("0.8")},
},
},
{
Forecaster: worker4,
ForecastElements: []*emissions.ForecastElement{
{Inferer: worker1, Value: alloraMath.MustNewDecFromString("0.4")},
{Inferer: worker2, Value: alloraMath.MustNewDecFromString("0.9")},
},
},
},
}

networkCombinedLoss := alloraMath.MustNewDecFromString("0.2")
epsilon := alloraMath.MustNewDecFromString("0.001")
pInferenceSynthesis := alloraMath.MustNewDecFromString("2")

// Set inferer network regrets
err := k.SetInfererNetworkRegret(ctx, topicId, worker1Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.2")})
s.Require().NoError(err)
err = k.SetInfererNetworkRegret(ctx, topicId, worker2Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.3")})
s.Require().NoError(err)

// Set forecaster network regrets
err = k.SetForecasterNetworkRegret(ctx, topicId, worker3Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.4")})
s.Require().NoError(err)
err = k.SetForecasterNetworkRegret(ctx, topicId, worker4Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.5")})
s.Require().NoError(err)

// Set one-in forecaster network regrets
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, worker3Add, worker1Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.2")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, worker3Add, worker2Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.3")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, worker4Add, worker1Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.6")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, worker4Add, worker2Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.4")})
s.Require().NoError(err)

// Call the function
valueBundle, err := inference_synthesis.CalcNetworkInferences(ctx, k, topicId, inferences, forecasts, networkCombinedLoss, epsilon, pInferenceSynthesis)
s.Require().NoError(err)

// Check the results
s.Require().NotNil(valueBundle)
s.Require().NotNil(valueBundle.CombinedValue)
s.Require().NotNil(valueBundle.NaiveValue)

s.Require().Len(valueBundle.OneOutInfererValues, 2)
s.Require().Len(valueBundle.OneOutForecasterValues, 2)
s.Require().Len(valueBundle.OneInForecasterValues, 2)
}

func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesThreeWorkerThreeForecasters() {
k := s.emissionsKeeper
ctx := s.ctx
topicId := uint64(1)

worker1 := "worker1"
worker2 := "worker2"
worker3 := "worker3"
worker1Add := sdk.AccAddress(worker1)
worker2Add := sdk.AccAddress(worker2)
worker3Add := sdk.AccAddress(worker3)

forecaster1 := "forecaster1"
forecaster2 := "forecaster2"
forecaster3 := "forecaster3"
forecaster1Add := sdk.AccAddress(forecaster1)
forecaster2Add := sdk.AccAddress(forecaster2)
forecaster3Add := sdk.AccAddress(forecaster3)

// Set up input data
inferences := &emissions.Inferences{
Inferences: []*emissions.Inference{
{Inferer: worker1, Value: alloraMath.MustNewDecFromString("0.1")},
{Inferer: worker2, Value: alloraMath.MustNewDecFromString("0.2")},
{Inferer: worker3, Value: alloraMath.MustNewDecFromString("0.3")},
},
}

forecasts := &emissions.Forecasts{
Forecasts: []*emissions.Forecast{
{
Forecaster: forecaster1,
ForecastElements: []*emissions.ForecastElement{
{Inferer: worker1, Value: alloraMath.MustNewDecFromString("0.4")},
{Inferer: worker2, Value: alloraMath.MustNewDecFromString("0.5")},
{Inferer: worker3, Value: alloraMath.MustNewDecFromString("0.6")},
},
},
{
Forecaster: forecaster2,
ForecastElements: []*emissions.ForecastElement{
{Inferer: worker1, Value: alloraMath.MustNewDecFromString("0.7")},
{Inferer: worker2, Value: alloraMath.MustNewDecFromString("0.8")},
{Inferer: worker3, Value: alloraMath.MustNewDecFromString("0.9")},
},
},
{
Forecaster: forecaster3,
ForecastElements: []*emissions.ForecastElement{
{Inferer: worker1, Value: alloraMath.MustNewDecFromString("0.1")},
{Inferer: worker2, Value: alloraMath.MustNewDecFromString("0.2")},
{Inferer: worker3, Value: alloraMath.MustNewDecFromString("0.3")},
},
},
},
}

networkCombinedLoss := alloraMath.MustNewDecFromString("0.3")
epsilon := alloraMath.MustNewDecFromString("0.001")
pInferenceSynthesis := alloraMath.MustNewDecFromString("2")

// Set inferer network regrets
err := k.SetInfererNetworkRegret(ctx, topicId, worker1Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.1")})
s.Require().NoError(err)
err = k.SetInfererNetworkRegret(ctx, topicId, worker2Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.2")})
s.Require().NoError(err)
err = k.SetInfererNetworkRegret(ctx, topicId, worker3Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.3")})
s.Require().NoError(err)

// Set forecaster network regrets
err = k.SetForecasterNetworkRegret(ctx, topicId, forecaster1Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.4")})
s.Require().NoError(err)
err = k.SetForecasterNetworkRegret(ctx, topicId, forecaster2Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.5")})
s.Require().NoError(err)
err = k.SetForecasterNetworkRegret(ctx, topicId, forecaster3Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.6")})
s.Require().NoError(err)

// Set one-in forecaster network regrets
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster1Add, worker1Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.7")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster1Add, worker2Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.8")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster1Add, worker3Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.9")})
s.Require().NoError(err)

err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster2Add, worker1Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.1")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster2Add, worker2Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.2")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster2Add, worker3Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.3")})
s.Require().NoError(err)

err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster3Add, worker1Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.4")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster3Add, worker2Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.5")})
s.Require().NoError(err)
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, forecaster3Add, worker3Add, emissions.TimestampedValue{Value: alloraMath.MustNewDecFromString("0.6")})
s.Require().NoError(err)

// Call the function
valueBundle, err := inference_synthesis.CalcNetworkInferences(ctx, k, topicId, inferences, forecasts, networkCombinedLoss, epsilon, pInferenceSynthesis)
s.Require().NoError(err)

// Check the results
s.Require().NotNil(valueBundle)
s.Require().NotNil(valueBundle.CombinedValue)
s.Require().NotNil(valueBundle.NaiveValue)

s.Require().Len(valueBundle.OneOutInfererValues, 3)
s.Require().Len(valueBundle.OneOutForecasterValues, 3)
s.Require().Len(valueBundle.OneInForecasterValues, 3)
}
142 changes: 140 additions & 2 deletions x/emissions/keeper/inference_synthesis/network_losses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,117 @@ func (s *InferenceSynthesisTestSuite) TestCalcCombinedNetworkLoss() {
}
}

func (s *InferenceSynthesisTestSuite) TestCalcNetworkLosses() {
tests := []struct {
func getTestCasesOneWorker() []struct {
name string
stakesByReputer map[inference_synthesis.Worker]cosmosMath.Uint
reportedLosses emissions.ReputerValueBundles
epsilon alloraMath.Dec
expectedOutput emissions.ValueBundle
expectedError error
} {
return []struct {
name string
stakesByReputer map[inference_synthesis.Worker]cosmosMath.Uint
reportedLosses emissions.ReputerValueBundles
epsilon alloraMath.Dec
expectedOutput emissions.ValueBundle
expectedError error
}{
{
name: "simple one reputer combined loss",
stakesByReputer: map[inference_synthesis.Worker]cosmosMath.Uint{
"worker1": inference_synthesis.CosmosUintOneE18(), // 1 token
},
reportedLosses: emissions.ReputerValueBundles{
ReputerValueBundles: []*emissions.ReputerValueBundle{
{
ValueBundle: &emissions.ValueBundle{
Reputer: "worker1",
CombinedValue: alloraMath.MustNewDecFromString("0.1"),
NaiveValue: alloraMath.MustNewDecFromString("0.1"),
InfererValues: []*emissions.WorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1"),
},
},
ForecasterValues: []*emissions.WorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1"),
},
},
OneOutInfererValues: []*emissions.WithheldWorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1"),
},
},
OneOutForecasterValues: []*emissions.WithheldWorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1"),
},
},
OneInForecasterValues: []*emissions.WorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1"),
},
},
},
},
},
},
epsilon: alloraMath.MustNewDecFromString("1e-4"),
expectedOutput: emissions.ValueBundle{
CombinedValue: alloraMath.MustNewDecFromString("0.1587401051968199"),
NaiveValue: alloraMath.MustNewDecFromString("0.1587401051968199"),
InfererValues: []*emissions.WorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1587401051968199"),
},
},
ForecasterValues: []*emissions.WorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1587401051968199"),
},
},
OneOutInfererValues: []*emissions.WithheldWorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1587401051968199"),
},
},
OneOutForecasterValues: []*emissions.WithheldWorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1587401051968199"),
},
},
OneInForecasterValues: []*emissions.WorkerAttributedValue{
{
Worker: "worker1",
Value: alloraMath.MustNewDecFromString("0.1587401051968199"),
},
},
},
expectedError: nil,
},
}
}

func getTestCasesTwoWorkers() []struct {
name string
stakesByReputer map[inference_synthesis.Worker]cosmosMath.Uint
reportedLosses emissions.ReputerValueBundles
epsilon alloraMath.Dec
expectedOutput emissions.ValueBundle
expectedError error
} {
return []struct {
name string
stakesByReputer map[inference_synthesis.Worker]cosmosMath.Uint
reportedLosses emissions.ReputerValueBundles
Expand Down Expand Up @@ -383,6 +492,10 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkLosses() {
expectedError: nil,
},
}
}

func (s *InferenceSynthesisTestSuite) TestCalcNetworkLosses() {
tests := getTestCasesTwoWorkers()

require := s.Require()

Expand Down Expand Up @@ -432,3 +545,28 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkLosses() {
})
}
}

func (s *InferenceSynthesisTestSuite) TestCalcNetworkLossesCombined() {
tests := append(getTestCasesOneWorker(), getTestCasesTwoWorkers()...)

require := s.Require()

for _, tc := range tests {
s.Run(tc.name, func() {
output, err := inference_synthesis.CalcNetworkLosses(tc.stakesByReputer, tc.reportedLosses, tc.epsilon)
if tc.expectedError != nil {
require.Error(err)
require.EqualError(err, tc.expectedError.Error())
} else {
require.NoError(err)

// Verify the length of each attribute in the ValueBundle
require.Len(output.InfererValues, len(tc.expectedOutput.InfererValues), "Mismatch in number of InfererValues")
require.Len(output.ForecasterValues, len(tc.expectedOutput.ForecasterValues), "Mismatch in number of ForecasterValues")
require.Len(output.OneInForecasterValues, len(tc.expectedOutput.OneInForecasterValues), "Mismatch in number of OneInForecasterValues")
require.Len(output.OneOutInfererValues, len(tc.expectedOutput.OneOutInfererValues), "Mismatch in number of OneOutInfererValues")
require.Len(output.OneOutForecasterValues, len(tc.expectedOutput.OneOutForecasterValues), "Mismatch in number of OneOutForecasterValues")
}
})
}
}
Loading

0 comments on commit a53b6d4

Please sign in to comment.