Skip to content

Commit 1b932e0

Browse files
authored
Additional validations + use inferenceBlockheight in NetworkInferences (#804)
<!-- < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ☺ v ✰ Thanks for creating a PR! You're awesome! ✰ v Please note that maintainers will only review those PRs with a completed PR template. ☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > --> ## Purpose of Changes and their Description * Additional validations on `validations.go` * nonempty `infererValues` is required * input data `blockheight` is required to be non-nil and non-zero * inference and forecasts `blockheight` need to be aligned with bundle `blockheight` * fix usage of current block `ctx.Blockheight()` in `inferenceBlockheight` in `NetworkInferences` responses. ## Are these changes tested and documented? - [x] If tested, please describe how. If not, why tests are not needed. -- unit tests fixed + cases added. - [x] If documented, please describe where. If not, describe why docs are not needed. -- no need to document, not relevant functional changes. - [x] Added to `Unreleased` section of `CHANGELOG.md`?
1 parent 56b42c0 commit 1b932e0

14 files changed

Lines changed: 388 additions & 131 deletions

x/emissions/keeper/inference_synthesis/network_inference_builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ type CalcNetworkInferencesArgs struct {
856856
PNorm alloraMath.Dec
857857
CNorm alloraMath.Dec
858858
StdDevPlusEpsilon alloraMath.Dec
859+
InferenceBlockHeight BlockHeight
859860
}
860861

861862
// Calculates all network inferences in the set I_i given historical state (e.g. regrets)
@@ -1048,7 +1049,7 @@ func CalcNetworkInferences(
10481049
return &emissions.ValueBundle{
10491050
TopicId: args.TopicId,
10501051
ReputerRequestNonce: &emissions.ReputerRequestNonce{
1051-
ReputerNonce: &emissions.Nonce{BlockHeight: args.Ctx.BlockHeight()},
1052+
ReputerNonce: &emissions.Nonce{BlockHeight: args.InferenceBlockHeight},
10521053
},
10531054
Reputer: "allo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqas6usy",
10541055
ExtraData: nil,

x/emissions/keeper/inference_synthesis/network_inference_builder_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,23 @@ func (s *InferenceSynthesisTestSuite) mockTopic() emissionstypes.Topic {
188188
}
189189
}
190190

191+
// ConvertInferencesToWorkerAttributedValues converts an Inferences type to a slice of WorkerAttributedValue
192+
func (s *InferenceSynthesisTestSuite) ConvertInferencesToWorkerAttributedValues(inferences emissionstypes.Inferences) []*emissionstypes.WorkerAttributedValue {
193+
result := make([]*emissionstypes.WorkerAttributedValue, 0, len(inferences.Inferences))
194+
195+
for _, inference := range inferences.Inferences {
196+
result = append(result, &emissionstypes.WorkerAttributedValue{
197+
Worker: inference.Inferer,
198+
Value: inference.Value,
199+
})
200+
}
201+
202+
return result
203+
}
204+
191205
func (s *InferenceSynthesisTestSuite) mockEmptyValueBundle(
192206
combinedAndNaiveValue alloraMath.Dec,
207+
infererValues []*emissionstypes.WorkerAttributedValue,
193208
) emissionstypes.ValueBundle {
194209
return emissionstypes.ValueBundle{
195210
TopicId: uint64(1),
@@ -199,7 +214,7 @@ func (s *InferenceSynthesisTestSuite) mockEmptyValueBundle(
199214
Reputer: s.addrsStr[9],
200215
ExtraData: nil,
201216
CombinedValue: combinedAndNaiveValue,
202-
InfererValues: nil,
217+
InfererValues: infererValues,
203218
ForecasterValues: nil,
204219
NaiveValue: combinedAndNaiveValue,
205220
OneOutInfererValues: nil,
@@ -548,6 +563,7 @@ func (s *InferenceSynthesisTestSuite) getEpochValueBundleByEpoch(epochNumber int
548563
topic,
549564
valueBundle,
550565
moduleParams,
566+
blockHeight,
551567
)
552568
s.Require().NoError(err)
553569
return ctx, k, ctx.Logger(), topicId, calcArgs.AllInferersAreNew,
@@ -599,6 +615,7 @@ func (s *InferenceSynthesisTestSuite) getNetworkCalcArgs(
599615
topic,
600616
networkLosses,
601617
moduleParams,
618+
blockHeight,
602619
)
603620
s.Require().NoError(err)
604621
return calcArgs.Inferers, calcArgs.InfererToInference, calcArgs.InfererToRegret, calcArgs.AllInferersAreNew,
@@ -949,6 +966,7 @@ func (s *InferenceSynthesisTestSuite) TestBuildNetworkInferencesIncompleteData()
949966
PNorm: pNorm,
950967
CNorm: cNorm,
951968
StdDevPlusEpsilon: alloraMath.ZeroDec(),
969+
InferenceBlockHeight: blockHeightInferences,
952970
}
953971

954972
valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(calcArgs)
@@ -1062,6 +1080,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesTwoWorkerTwoForec
10621080
PNorm: pNorm,
10631081
CNorm: cNorm,
10641082
StdDevPlusEpsilon: alloraMath.ZeroDec(),
1083+
InferenceBlockHeight: blockHeight,
10651084
}
10661085
valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(calcArgs)
10671086
s.Require().NoError(err)
@@ -1205,6 +1224,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesThreeWorkerThreeF
12051224
PNorm: pNorm,
12061225
CNorm: cNorm,
12071226
StdDevPlusEpsilon: alloraMath.ZeroDec(),
1227+
InferenceBlockHeight: blockHeight,
12081228
}
12091229

12101230
valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(
@@ -1352,6 +1372,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesThreeWorkerTwoFor
13521372
PNorm: pNorm,
13531373
CNorm: cNorm,
13541374
StdDevPlusEpsilon: alloraMath.ZeroDec(),
1375+
InferenceBlockHeight: blockHeight,
13551376
}
13561377

13571378
valueBundle, weights, err := inferencesynthesis.CalcNetworkInferences(
@@ -1460,6 +1481,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcOneInInferencesTwoForecastersOldTw
14601481
PNorm: pNorm,
14611482
CNorm: cNorm,
14621483
StdDevPlusEpsilon: alloraMath.ZeroDec(),
1484+
InferenceBlockHeight: blockHeight,
14631485
}
14641486

14651487
valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(calcArgs)

x/emissions/keeper/inference_synthesis/network_inferences.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func GetNetworkInferences(
5151
networkLosses, err := k.GetLatestNetworkLossBundle(ctx, topicId)
5252
if errors.Is(err, emissions.ErrNotFound) {
5353
// 2a. If we have no network losses, fallback to using the median of the inferences.
54-
return calcNetworkInferencesMultipleByMedian(ctx, topicId, inferences, *inferencesNonce)
54+
return calcNetworkInferencesMultipleByMedian(topicId, inferences, *inferencesNonce)
5555
} else if err != nil {
5656
return nil, errorsmod.Wrap(err, "while getting latest network loss bundle")
5757
}
@@ -67,7 +67,6 @@ func GetNetworkInferences(
6767
}
6868

6969
func calcNetworkInferencesMultipleByMedian(
70-
ctx sdk.Context,
7170
topicId TopicId,
7271
inferences *emissions.Inferences,
7372
inferenceBlockHeight BlockHeight,
@@ -83,7 +82,7 @@ func calcNetworkInferencesMultipleByMedian(
8382
TopicId: topicId,
8483
ExtraData: nil,
8584
ReputerRequestNonce: &emissions.ReputerRequestNonce{
86-
ReputerNonce: &emissions.Nonce{BlockHeight: ctx.BlockHeight()},
85+
ReputerNonce: &emissions.Nonce{BlockHeight: inferenceBlockHeight},
8786
},
8887
Reputer: "allo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqas6usy",
8988
CombinedValue: medianValue,
@@ -144,6 +143,7 @@ func calcNetworkInferencesMultiple(
144143
topic,
145144
*networkLosses,
146145
moduleParams,
146+
inferenceBlockHeight,
147147
)
148148
if err != nil {
149149
return nil, errorsmod.Wrap(err, "while getting network inference args")
@@ -215,6 +215,7 @@ func GetCalcNetworkInferenceArgs(
215215
topic emissions.Topic,
216216
networkLosses emissions.ValueBundle,
217217
moduleParams emissions.Params,
218+
inferenceBlockHeight BlockHeight,
218219
) (
219220
calcArgs CalcNetworkInferencesArgs,
220221
err error,
@@ -296,6 +297,7 @@ func GetCalcNetworkInferenceArgs(
296297
PNorm: topic.PNorm,
297298
CNorm: moduleParams.CNorm,
298299
StdDevPlusEpsilon: stdDevPlusEpsilon,
300+
InferenceBlockHeight: inferenceBlockHeight,
299301
}
300302

301303
// If there are forecast-implied inferences, add forecasters info

x/emissions/keeper/inference_synthesis/network_inferences_test.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,15 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlock() {
136136
forecaster2 := s.addrsStr[7]
137137
forecasterAddresses := []string{forecaster0, forecaster1, forecaster2}
138138

139+
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeight, infererAddresses, epoch3Get)
140+
s.Require().NoError(err)
141+
infererValues := s.ConvertInferencesToWorkerAttributedValues(inferences)
142+
139143
// Set Previous Loss
140-
valueBundlePrevious := s.mockEmptyValueBundle(epoch2Get("network_loss"))
144+
valueBundlePrevious := s.mockEmptyValueBundle(epoch2Get("network_loss"), infererValues)
141145
err = keeper.InsertNetworkLossBundleAtBlock(s.ctx, topicId, blockHeightPreviousLosses, valueBundlePrevious)
142146
require.NoError(err)
143147

144-
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeight, infererAddresses, epoch3Get)
145-
s.Require().NoError(err)
146-
147148
err = keeper.InsertActiveInferences(s.ctx, topicId, simpleNonce.BlockHeight, inferences)
148149
s.Require().NoError(err)
149150

@@ -376,15 +377,15 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlockWithOneOldI
376377
inferer4 := s.addrsStr[4]
377378
infererAddresses := []string{inferer0, inferer1, inferer2, inferer3, inferer4}
378379

380+
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeight, infererAddresses, epoch2Get)
381+
s.Require().NoError(err)
382+
infererValues := s.ConvertInferencesToWorkerAttributedValues(inferences)
379383
// Set Previous Loss
380-
valueBundlePrevious := s.mockEmptyValueBundle(epoch1Get("network_loss"))
384+
valueBundlePrevious := s.mockEmptyValueBundle(epoch1Get("network_loss"), infererValues)
381385
err = s.emissionsKeeper.InsertNetworkLossBundleAtBlock(
382386
s.ctx, topicId, blockHeightPreviousLosses, valueBundlePrevious)
383387
s.Require().NoError(err)
384388

385-
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeight, infererAddresses, epoch2Get)
386-
s.Require().NoError(err)
387-
388389
err = s.emissionsKeeper.InsertActiveInferences(s.ctx, topicId, simpleNonce.BlockHeight, inferences)
389390
s.Require().NoError(err)
390391

@@ -455,12 +456,13 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlockWithOldInfe
455456
forecaster2 := s.addrsStr[7]
456457
forecasterAddresses := []string{forecaster0, forecaster1, forecaster2}
457458

458-
// Set Previous Loss
459-
emptyValueBundle := s.mockEmptyValueBundle(epoch1Get("network_loss"))
460-
err = s.emissionsKeeper.InsertNetworkLossBundleAtBlock(s.ctx, topicId, blockHeightPreviousLosses, emptyValueBundle)
459+
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeight, infererAddresses, epoch2Get)
461460
s.Require().NoError(err)
461+
infererValues := s.ConvertInferencesToWorkerAttributedValues(inferences)
462462

463-
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeight, infererAddresses, epoch2Get)
463+
// Set Previous Loss
464+
emptyValueBundle := s.mockEmptyValueBundle(epoch1Get("network_loss"), infererValues)
465+
err = s.emissionsKeeper.InsertNetworkLossBundleAtBlock(s.ctx, topicId, blockHeightPreviousLosses, emptyValueBundle)
464466
s.Require().NoError(err)
465467

466468
err = s.emissionsKeeper.InsertActiveInferences(s.ctx, topicId, simpleNonce.BlockHeight, inferences)
@@ -601,17 +603,18 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlockWithOldInfe
601603
forecaster2 := s.addrsStr[7]
602604
forecasterAddresses := []string{forecaster0, forecaster1, forecaster2}
603605

606+
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeight, infererAddresses, epoch2Get)
607+
s.Require().NoError(err)
608+
infererValues := s.ConvertInferencesToWorkerAttributedValues(inferences)
609+
604610
// Set Previous Loss
605-
emptyValueBundle := s.mockEmptyValueBundle(epoch1Get("network_loss"))
611+
emptyValueBundle := s.mockEmptyValueBundle(epoch1Get("network_loss"), infererValues)
606612
err = s.emissionsKeeper.InsertNetworkLossBundleAtBlock(
607613
s.ctx, topicId, blockHeightPreviousLosses,
608614
emptyValueBundle,
609615
)
610616
s.Require().NoError(err)
611617

612-
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeight, infererAddresses, epoch2Get)
613-
s.Require().NoError(err)
614-
615618
err = s.emissionsKeeper.InsertActiveInferences(s.ctx, topicId, simpleNonce.BlockHeight, inferences)
616619
s.Require().NoError(err)
617620

@@ -693,7 +696,6 @@ func (s *InferenceSynthesisTestSuite) TestGetNetworkInferencesAtBlockWithOldInfe
693696
case forecaster1:
694697
testutil.InEpsilon5(s.T(), oneOutForecasterValue.Value, "0.13368285139309616")
695698
case forecaster2:
696-
697699
testutil.InEpsilon5(s.T(), oneOutForecasterValue.Value, "0.1339967078008638")
698700
default:
699701
s.Require().Fail("Unexpected worker %v", oneOutForecasterValue.Worker)
@@ -745,14 +747,14 @@ func (s *InferenceSynthesisTestSuite) TestGetLatestNetworkInferenceFromCsv() {
745747
forecaster2 := s.addrsStr[7]
746748
forecasterAddresses := []string{forecaster0, forecaster1, forecaster2}
747749

750+
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeightInferences, infererAddresses, epoch3Get)
751+
require.NoError(err)
752+
infererValues := s.ConvertInferencesToWorkerAttributedValues(inferences)
748753
// Set Previous Loss
749-
valueBundlePrevious := s.mockEmptyValueBundle(epoch2Get("network_loss"))
754+
valueBundlePrevious := s.mockEmptyValueBundle(epoch2Get("network_loss"), infererValues)
750755
err = keeper.InsertNetworkLossBundleAtBlock(s.ctx, topicId, blockHeightPreviousLosses, valueBundlePrevious)
751756
require.NoError(err)
752757

753-
inferences, err := testutil.GetInferencesFromCsv(topicId, blockHeightInferences, infererAddresses, epoch3Get)
754-
require.NoError(err)
755-
756758
err = keeper.InsertActiveInferences(s.ctx, topicId, simpleNonce.BlockHeight, inferences)
757759
require.NoError(err)
758760

x/emissions/keeper/keeper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ func (k *Keeper) GetLatestNetworkInferences(ctx context.Context, topicId TopicId
541541

542542
/// NONCES
543543

544-
// GetTopicIds returns the TopicIds for a given BlockHeight.
544+
// GetWorkerWindowTopicIds returns the TopicIds for a given BlockHeight.
545545
// If no TopicIds are found for the BlockHeight, it returns an empty slice.
546546
func (k *Keeper) GetWorkerWindowTopicIds(ctx sdk.Context, height BlockHeight) types.TopicIds {
547547
topicIds, err := k.openWorkerWindows.Get(ctx, height)
@@ -551,7 +551,7 @@ func (k *Keeper) GetWorkerWindowTopicIds(ctx sdk.Context, height BlockHeight) ty
551551
return topicIds
552552
}
553553

554-
// SetTopicId appends a new TopicId to the list of TopicIds for a given BlockHeight.
554+
// AddWorkerWindowTopicId appends a new TopicId to the list of TopicIds for a given BlockHeight.
555555
// If no entry exists for the BlockHeight, it creates a new entry with the TopicId.
556556
func (k *Keeper) AddWorkerWindowTopicId(ctx sdk.Context, height BlockHeight, topicId TopicId) error {
557557
if err := types.ValidateTopicId(topicId); err != nil {

0 commit comments

Comments
 (0)