From 44f4640ee338d08afeafde3798d6ac9f529544a8 Mon Sep 17 00:00:00 2001 From: Diego C Date: Tue, 18 Feb 2025 23:18:02 +0100 Subject: [PATCH] genesis fix + lint + others --- test/integration/update_params_test.go | 8 ++-- utils/migutils/migutils_test.go | 3 +- x/emissions/keeper/actor_utils/losses.go | 44 +++++++++---------- .../forecast_implied_test.go | 5 +++ .../network_inference_builder_test.go | 10 +++++ .../keeper/inference_synthesis/weight.go | 8 +--- .../inference_synthesis/weights_test.go | 2 +- x/emissions/keeper/keeper_test.go | 1 + .../msgserver/msg_server_params_test.go | 2 + .../msg_server_worker_payload_test.go | 2 + x/emissions/module/rewards/rewards_test.go | 9 ++-- x/emissions/types/genesis.go | 3 ++ 12 files changed, 59 insertions(+), 38 deletions(-) diff --git a/test/integration/update_params_test.go b/test/integration/update_params_test.go index 3e464e43b..cc8ce2071 100644 --- a/test/integration/update_params_test.go +++ b/test/integration/update_params_test.go @@ -2,7 +2,6 @@ package integration_test import ( "context" - "fmt" alloraMath "github.com/allora-network/allora-chain/math" testCommon "github.com/allora-network/allora-chain/test/common" @@ -28,8 +27,8 @@ func checkIfAdmin(m testCommon.TestConfig, address string) bool { func UpdateParamsChecks(m testCommon.TestConfig) { ctx := context.Background() // Ensure Alice is in the whitelist and Bob is not - require.True(m.T, checkIfAdmin(m, m.AliceAddr), fmt.Sprintf("Alice %s should be a whitelist admin", m.AliceAddr)) - require.False(m.T, checkIfAdmin(m, m.BobAddr), fmt.Sprintf("Bob %s should not be a whitelist admin", m.BobAddr)) + require.True(m.T, checkIfAdmin(m, m.AliceAddr), "Alice %s should be a whitelist admin", m.AliceAddr) + require.False(m.T, checkIfAdmin(m, m.BobAddr), "Bob %s should not be a whitelist admin", m.BobAddr) // Keep old params to revert back to oldParams := GetEmissionsParams(m) @@ -96,6 +95,7 @@ func UpdateParamsChecks(m testCommon.TestConfig) { GlobalReputerWhitelistEnabled: nil, GlobalAdminWhitelistAppended: nil, MaxWhitelistInputArrayLength: nil, + MinWeightThresholdForStdnorm: nil, }, } txResp, err := m.Client.BroadcastTx(ctx, m.AliceAcc, updateParamRequest) @@ -162,6 +162,7 @@ func UpdateParamsChecks(m testCommon.TestConfig) { GlobalReputerWhitelistEnabled: nil, GlobalAdminWhitelistAppended: nil, MaxWhitelistInputArrayLength: nil, + MinWeightThresholdForStdnorm: nil, }, } _, err = m.Client.BroadcastTx(ctx, m.BobAcc, updateParamRequest) @@ -232,6 +233,7 @@ func UpdateParamsChecks(m testCommon.TestConfig) { GlobalReputerWhitelistEnabled: nil, GlobalAdminWhitelistAppended: nil, MaxWhitelistInputArrayLength: nil, + MinWeightThresholdForStdnorm: nil, }, } txResp, err = m.Client.BroadcastTx(ctx, m.AliceAcc, updateParamRequest) diff --git a/utils/migutils/migutils_test.go b/utils/migutils/migutils_test.go index f0bcef5ee..6ad8e4adc 100644 --- a/utils/migutils/migutils_test.go +++ b/utils/migutils/migutils_test.go @@ -3,7 +3,6 @@ package migutils_test import ( "fmt" rand "math/rand/v2" - "strings" "testing" "cosmossdk.io/collections" @@ -133,6 +132,6 @@ func runSafelyClearWholeMapCase[K, V any]( for _, key := range keys { _, err := m.Get(testDB.TestCtx.Ctx, key) require.Error(t, err) - require.True(t, strings.Contains(err.Error(), "collections: not found")) + require.Contains(t, err.Error(), "collections: not found") } } diff --git a/x/emissions/keeper/actor_utils/losses.go b/x/emissions/keeper/actor_utils/losses.go index a6f685db8..bda2a4066 100644 --- a/x/emissions/keeper/actor_utils/losses.go +++ b/x/emissions/keeper/actor_utils/losses.go @@ -145,7 +145,8 @@ func CloseReputerNonce( return err } - // 0 get inferer and forecaster regrets + // Calculate the regret_stdnorm and the weights (multistep process). + // 0. Get inferer and forecaster regrets infererRegrets := regrets.InfererRegrets inferers := make([]synth.Worker, 0) for inferer := range infererRegrets { @@ -157,23 +158,25 @@ func CloseReputerNonce( forecasters = append(forecasters, forecaster) } - // 2. Calculate the regret_stdnorm based on the previous weights. If not, apply stddev. - stdDevPlusEpsilon, err := synth.CalcStdDevForWeights(synth.CalcStdDevForWeightsArgs{ - Ctx: ctx, - K: k, - Logger: ctx.Logger(), - TopicId: topic.Id, - Inferers: inferers, - Forecasters: forecasters, - InfererToRegret: infererRegrets, - ForecasterToRegret: forecasterRegrets, - NegligibleThreshold: params.MinWeightThresholdForStdnorm, - EpsilonTopic: topic.Epsilon, - }) + // 2. Calculate the regret_stdnorm to be used in + // 2.a Calculate the regret_stdnorm filtered by ∫the previous weights. If not, apply stddev. + stdDevPlusEpsilon, err := synth.CalcRegretStdDevFilteredByWeights( + synth.CalcRegretStdDevFilteredByWeightsArgs{ + Ctx: ctx, + K: k, + Logger: ctx.Logger(), + TopicId: topic.Id, + Inferers: inferers, + Forecasters: forecasters, + InfererToRegret: infererRegrets, + ForecasterToRegret: forecasterRegrets, + NegligibleThreshold: params.MinWeightThresholdForStdnorm, + EpsilonTopic: topic.Epsilon, + }, + ) if err != nil { return err } - ctx.Logger().Info("CloseReputerNonce: stdDevPlusEpsilon", "stdDevPlusEpsilon", stdDevPlusEpsilon) // 2.b ... and store it. err = k.SetLatestRegretStdNorm(ctx, topic.Id, stdDevPlusEpsilon) if err != nil { @@ -198,30 +201,27 @@ func CloseReputerNonce( return err } - // 4. Normalize weights! + // 4. Normalize weights! This was not done before, but it is needed for the filter of non-negligible weights. err = newWeights.NormalizeWeights() if err != nil { return err } - // 5. Clean previous weights for this topic - // TODO and TBD - - // 6. Store the new weights + // 5. Store the new weights err = synth.StoreLatestNormalizedWeights(ctx, *k, topic.Id, newWeights) if err != nil { return err } - // Emit the regret stdnorm set event + // Emit events: the regret stdnorm set event types.EmitNewRegretStdNormSetEvent(ctx, topic.Id, nonce.BlockHeight, stdDevPlusEpsilon) - // Emit weights events, one per actor. for _, inferer := range inferers { types.EmitNewInfererWeightSetEvent(ctx, topic.Id, nonce.BlockHeight, inferer, newWeights.Inferers[inferer]) } for _, forecaster := range forecasters { types.EmitNewForecasterWeightSetEvent(ctx, topic.Id, nonce.BlockHeight, forecaster, newWeights.Forecasters[forecaster]) } + // -- end of regrets_stdnorm and weights multistep process _, err = k.FulfillReputerNonce(ctx, topic.Id, &nonce) if err != nil { diff --git a/x/emissions/keeper/inference_synthesis/forecast_implied_test.go b/x/emissions/keeper/inference_synthesis/forecast_implied_test.go index b5ee68916..26ff9aaf0 100644 --- a/x/emissions/keeper/inference_synthesis/forecast_implied_test.go +++ b/x/emissions/keeper/inference_synthesis/forecast_implied_test.go @@ -147,6 +147,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcForecastImpliedInferencesTwoWorker EpsilonTopic: epsilon, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }, ) s.Require().NoError(err) @@ -223,6 +224,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcForecastImpliedInferencesTwoWorker EpsilonTopic: epsilon, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }, ) s.Require().NoError(err) @@ -317,6 +319,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcForecastImpliedInferencesThreeWork EpsilonTopic: epsilon, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }, ) s.Require().NoError(err) @@ -417,6 +420,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcForcastImpliedInferencesEpoch2() { EpsilonTopic: epsilon, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }) s.Require().NoError(err) for key, expectedValue := range expected { @@ -509,6 +513,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcForcastImpliedInferencesEpoch3() { EpsilonTopic: epsilon, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }) s.Require().NoError(err) for key, expectedValue := range expected { diff --git a/x/emissions/keeper/inference_synthesis/network_inference_builder_test.go b/x/emissions/keeper/inference_synthesis/network_inference_builder_test.go index 9ac7f2850..d361147de 100644 --- a/x/emissions/keeper/inference_synthesis/network_inference_builder_test.go +++ b/x/emissions/keeper/inference_synthesis/network_inference_builder_test.go @@ -643,6 +643,7 @@ func (s *InferenceSynthesisTestSuite) testCorrectCombinedInitialValueForEpoch(ep EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }) s.Require().NoError(err) alloratestutil.InEpsilon5(s.T(), combinedValue, epochGet[epoch]("network_inference").String()) @@ -681,6 +682,7 @@ func (s *InferenceSynthesisTestSuite) testCorrectNaiveValueForEpoch(epoch int) { EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }) s.Require().NoError(err) alloratestutil.InEpsilon5(s.T(), naiveValue, epochGet[epoch]("network_naive_inference").String()) @@ -732,6 +734,7 @@ func (s *InferenceSynthesisTestSuite) testCorrectOneOutInfererValuesForEpoch(epo EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }) s.Require().NoError(err) @@ -778,6 +781,7 @@ func (s *InferenceSynthesisTestSuite) testCorrectOneOutForecasterValuesForEpoch( EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }) s.Require().NoError(err) @@ -836,6 +840,7 @@ func (s *InferenceSynthesisTestSuite) testCorrectOneInForecasterValuesForEpoch(e EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), }) s.Require().NoError(err) @@ -943,6 +948,7 @@ func (s *InferenceSynthesisTestSuite) TestBuildNetworkInferencesIncompleteData() EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), } valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(calcArgs) @@ -1055,6 +1061,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesTwoWorkerTwoForec EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), } valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(calcArgs) s.Require().NoError(err) @@ -1197,6 +1204,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesThreeWorkerThreeF EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), } valueBundle, _, err := inferencesynthesis.CalcNetworkInferences( @@ -1343,6 +1351,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesThreeWorkerTwoFor EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), } valueBundle, weights, err := inferencesynthesis.CalcNetworkInferences( @@ -1450,6 +1459,7 @@ func (s *InferenceSynthesisTestSuite) TestCalc0neInInferencesTwoForecastersOldTw EpsilonSafeDiv: epsilonSafeDiv, PNorm: pNorm, CNorm: cNorm, + StdDevPlusEpsilon: alloraMath.ZeroDec(), } valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(calcArgs) diff --git a/x/emissions/keeper/inference_synthesis/weight.go b/x/emissions/keeper/inference_synthesis/weight.go index 3edb649ec..d460653e7 100644 --- a/x/emissions/keeper/inference_synthesis/weight.go +++ b/x/emissions/keeper/inference_synthesis/weight.go @@ -26,7 +26,7 @@ type CalcWeightsGivenWorkersArgs struct { StdDevPlusEpsilon alloraMath.Dec } -type CalcStdDevForWeightsArgs struct { +type CalcRegretStdDevFilteredByWeightsArgs struct { Ctx sdk.Context K *keeper.Keeper Logger log.Logger @@ -42,7 +42,7 @@ type CalcStdDevForWeightsArgs struct { // Calculates the standard deviation of the regrets provided plus epsilon // It uses previous epoch's weights to filter the regrets of workers that had a negligible weight // If there are less than 2 non-negligible weights, it uses all regrets. -func CalcStdDevForWeights(args CalcStdDevForWeightsArgs) (alloraMath.Dec, error) { +func CalcRegretStdDevFilteredByWeights(args CalcRegretStdDevFilteredByWeightsArgs) (alloraMath.Dec, error) { // Combine all weights and regrets var filteredRegrets []alloraMath.Dec nonNegligibleCount := 0 @@ -85,10 +85,8 @@ func CalcStdDevForWeights(args CalcStdDevForWeightsArgs) (alloraMath.Dec, error) if err != nil { return alloraMath.ZeroDec(), errorsmod.Wrapf(err, "Error gathering worker regrets") } - args.Logger.Debug("calcStdDevForWeights() using all regrets") return CalcStdDevPlusEpsilon(regrets, args.EpsilonTopic) } - args.Logger.Debug("calcStdDevForWeights() using filtered regrets") return CalcStdDevPlusEpsilon(filteredRegrets, args.EpsilonTopic) } @@ -162,8 +160,6 @@ func CalcWeightsGivenWorkers(args CalcWeightsGivenWorkersArgs) (RegretInformedWe } } - args.Logger.Info("In CalcWeightsGivenWorkers(): stdDevRegretsPlusEpsilon", "stdDevRegretsPlusEpsilon", stdDevRegretsPlusEpsilon) - // Normalize the regrets and find the max normalized regret among them normalizedInfererRegrets := make(map[Worker]Regret) maxRegret := alloraMath.ZeroDec() diff --git a/x/emissions/keeper/inference_synthesis/weights_test.go b/x/emissions/keeper/inference_synthesis/weights_test.go index 406a3a513..708bdc12a 100644 --- a/x/emissions/keeper/inference_synthesis/weights_test.go +++ b/x/emissions/keeper/inference_synthesis/weights_test.go @@ -471,7 +471,7 @@ func (s *WeightsTestSuite) TestCalcStdDevForWeights() { s.Require().NoError(err) } - result, err := synth.CalcStdDevForWeights(synth.CalcStdDevForWeightsArgs{ + result, err := synth.CalcRegretStdDevFilteredByWeights(synth.CalcRegretStdDevFilteredByWeightsArgs{ Ctx: s.ctx, K: &s.emissionsKeeper, Logger: s.ctx.Logger(), diff --git a/x/emissions/keeper/keeper_test.go b/x/emissions/keeper/keeper_test.go index 09368738e..e672b23da 100644 --- a/x/emissions/keeper/keeper_test.go +++ b/x/emissions/keeper/keeper_test.go @@ -4002,6 +4002,7 @@ func mockUninitializedParams() types.Params { GlobalReputerWhitelistEnabled: true, GlobalAdminWhitelistAppended: true, MaxWhitelistInputArrayLength: uint64(10), + MinWeightThresholdForStdnorm: alloraMath.ZeroDec(), } } diff --git a/x/emissions/keeper/msgserver/msg_server_params_test.go b/x/emissions/keeper/msgserver/msg_server_params_test.go index c28d71f56..000e0adb2 100644 --- a/x/emissions/keeper/msgserver/msg_server_params_test.go +++ b/x/emissions/keeper/msgserver/msg_server_params_test.go @@ -73,6 +73,7 @@ func (s *MsgServerTestSuite) TestUpdateAllParams() { GlobalReputerWhitelistEnabled: []bool{true}, GlobalAdminWhitelistAppended: []bool{true}, MaxWhitelistInputArrayLength: []uint64{10}, + MinWeightThresholdForStdnorm: []alloraMath.Dec{alloraMath.MustNewDecFromString("0.000001")}, } updateMsg := &types.UpdateParamsRequest{ @@ -206,6 +207,7 @@ func (s *MsgServerTestSuite) TestUpdateParamsNonWhitelistedUser() { GlobalReputerWhitelistEnabled: nil, GlobalAdminWhitelistAppended: nil, MaxWhitelistInputArrayLength: nil, + MinWeightThresholdForStdnorm: nil, } // Creating the UpdateParamsRequest message with a non-whitelisted user diff --git a/x/emissions/keeper/msgserver/msg_server_worker_payload_test.go b/x/emissions/keeper/msgserver/msg_server_worker_payload_test.go index 03afcd7dd..875464dd0 100644 --- a/x/emissions/keeper/msgserver/msg_server_worker_payload_test.go +++ b/x/emissions/keeper/msgserver/msg_server_worker_payload_test.go @@ -371,6 +371,7 @@ func (s *MsgServerTestSuite) TestMsgInsertWorkerPayloadWithFewTopElementsPerFore GlobalReputerWhitelistEnabled: nil, GlobalAdminWhitelistAppended: nil, MaxWhitelistInputArrayLength: nil, + MinWeightThresholdForStdnorm: nil, } updateMsg := &types.UpdateParamsRequest{ @@ -778,6 +779,7 @@ func (s *MsgServerTestSuite) TestMsgInsertWorkerPayloadWithLowScoreForecastsAreR GlobalReputerWhitelistEnabled: nil, GlobalAdminWhitelistAppended: nil, MaxWhitelistInputArrayLength: nil, + MinWeightThresholdForStdnorm: nil, } updateMsg := &types.UpdateParamsRequest{ diff --git a/x/emissions/module/rewards/rewards_test.go b/x/emissions/module/rewards/rewards_test.go index 7477cf90c..57b4fc3ce 100644 --- a/x/emissions/module/rewards/rewards_test.go +++ b/x/emissions/module/rewards/rewards_test.go @@ -1649,7 +1649,7 @@ func (s *RewardsTestSuite) TestMultipleEpochsWeightAndStdNormEvolution() { require.NoError(err) // Track weights and stdnorm over epochs - var workerWeights map[string][]alloraMath.Dec = make(map[string][]alloraMath.Dec) + var workerWeights = make(map[string][]alloraMath.Dec) var stdNorms []alloraMath.Dec numEpochs := 5 @@ -1691,7 +1691,7 @@ func (s *RewardsTestSuite) TestMultipleEpochsWeightAndStdNormEvolution() { } // Verify weight evolution - require.Equal(3, len(workerWeights)) // one entry per worker + require.Len(workerWeights, 3) // one entry per worker for i := 1; i < len(workerWeights); i++ { // Weight for the same worker should change between epochs currentWorker := s.addrsStr[i] @@ -1708,7 +1708,7 @@ func (s *RewardsTestSuite) TestMultipleEpochsWeightAndStdNormEvolution() { } // Verify stdnorm evolution - require.Equal(numEpochs, len(stdNorms)) + require.Len(stdNorms, numEpochs) for i := 1; i < len(stdNorms); i++ { // StdNorm should adapt based on predictions require.NotEqual( @@ -2361,6 +2361,7 @@ func (s *RewardsTestSuite) SetParamsForTest() { GlobalReputerWhitelistEnabled: nil, GlobalAdminWhitelistAppended: nil, MaxWhitelistInputArrayLength: nil, + MinWeightThresholdForStdnorm: nil, } updateMsg := &types.UpdateParamsRequest{ @@ -3167,7 +3168,7 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() { s.Require().NoError(err) inDelta, err := alloraMath.InDelta(totalSumPreviousTopicWeights, sumWeights, alloraMath.MustNewDecFromString("0.0001")) s.Require().NoError(err) - s.Require().True(inDelta, fmt.Sprintf("Total sum of previous topic weights %s + %s = %s is not equal to the sum of the weights of the two topics %s", topic0Weight1, topic1Weight0, totalSumPreviousTopicWeights, sumWeights)) + s.Require().True(inDelta, "Total sum of previous topic weights %s + %s = %s is not equal to the sum of the weights of the two topics %s", topic0Weight1, topic1Weight0, totalSumPreviousTopicWeights, sumWeights) err = s.emissionsKeeper.SetRewardCurrentBlockEmission(s.ctx, cosmosMath.NewInt(100)) s.Require().NoError(err) diff --git a/x/emissions/types/genesis.go b/x/emissions/types/genesis.go index 1eb55647c..b6e746c69 100644 --- a/x/emissions/types/genesis.go +++ b/x/emissions/types/genesis.go @@ -96,6 +96,9 @@ func NewGenesisState() *GenesisState { TopicReputerWhitelistEnabled: []uint64{}, LastMedianInferences: []*TopicIdAndDec{}, MadInferences: []*TopicIdAndDec{}, + LatestRegretStdNorm: []*TopicIdAndDec{}, + LatestInfererWeights: []*TopicIdActorIdDec{}, + LatestForecasterWeights: []*TopicIdActorIdDec{}, } }