Skip to content

Commit

Permalink
genesis fix + lint + others
Browse files Browse the repository at this point in the history
  • Loading branch information
xmariachi committed Feb 18, 2025
1 parent 3f3ae63 commit 44f4640
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 38 deletions.
8 changes: 5 additions & 3 deletions test/integration/update_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions utils/migutils/migutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package migutils_test
import (
"fmt"
rand "math/rand/v2"
"strings"
"testing"

"cosmossdk.io/collections"
Expand Down Expand Up @@ -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")
}
}
44 changes: 22 additions & 22 deletions x/emissions/keeper/actor_utils/losses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcForecastImpliedInferencesTwoWorker
EpsilonTopic: epsilon,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
},
)
s.Require().NoError(err)
Expand Down Expand Up @@ -223,6 +224,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcForecastImpliedInferencesTwoWorker
EpsilonTopic: epsilon,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
},
)
s.Require().NoError(err)
Expand Down Expand Up @@ -317,6 +319,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcForecastImpliedInferencesThreeWork
EpsilonTopic: epsilon,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
},
)
s.Require().NoError(err)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -732,6 +734,7 @@ func (s *InferenceSynthesisTestSuite) testCorrectOneOutInfererValuesForEpoch(epo
EpsilonSafeDiv: epsilonSafeDiv,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
})
s.Require().NoError(err)

Expand Down Expand Up @@ -778,6 +781,7 @@ func (s *InferenceSynthesisTestSuite) testCorrectOneOutForecasterValuesForEpoch(
EpsilonSafeDiv: epsilonSafeDiv,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
})
s.Require().NoError(err)

Expand Down Expand Up @@ -836,6 +840,7 @@ func (s *InferenceSynthesisTestSuite) testCorrectOneInForecasterValuesForEpoch(e
EpsilonSafeDiv: epsilonSafeDiv,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
})
s.Require().NoError(err)

Expand Down Expand Up @@ -943,6 +948,7 @@ func (s *InferenceSynthesisTestSuite) TestBuildNetworkInferencesIncompleteData()
EpsilonSafeDiv: epsilonSafeDiv,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
}

valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(calcArgs)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1197,6 +1204,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesThreeWorkerThreeF
EpsilonSafeDiv: epsilonSafeDiv,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
}

valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(
Expand Down Expand Up @@ -1343,6 +1351,7 @@ func (s *InferenceSynthesisTestSuite) TestCalcNetworkInferencesThreeWorkerTwoFor
EpsilonSafeDiv: epsilonSafeDiv,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
}

valueBundle, weights, err := inferencesynthesis.CalcNetworkInferences(
Expand Down Expand Up @@ -1450,6 +1459,7 @@ func (s *InferenceSynthesisTestSuite) TestCalc0neInInferencesTwoForecastersOldTw
EpsilonSafeDiv: epsilonSafeDiv,
PNorm: pNorm,
CNorm: cNorm,
StdDevPlusEpsilon: alloraMath.ZeroDec(),
}

valueBundle, _, err := inferencesynthesis.CalcNetworkInferences(calcArgs)
Expand Down
8 changes: 2 additions & 6 deletions x/emissions/keeper/inference_synthesis/weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/keeper/inference_synthesis/weights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions x/emissions/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4002,6 +4002,7 @@ func mockUninitializedParams() types.Params {
GlobalReputerWhitelistEnabled: true,
GlobalAdminWhitelistAppended: true,
MaxWhitelistInputArrayLength: uint64(10),
MinWeightThresholdForStdnorm: alloraMath.ZeroDec(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions x/emissions/keeper/msgserver/msg_server_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ func (s *MsgServerTestSuite) TestMsgInsertWorkerPayloadWithFewTopElementsPerFore
GlobalReputerWhitelistEnabled: nil,
GlobalAdminWhitelistAppended: nil,
MaxWhitelistInputArrayLength: nil,
MinWeightThresholdForStdnorm: nil,
}

updateMsg := &types.UpdateParamsRequest{
Expand Down Expand Up @@ -778,6 +779,7 @@ func (s *MsgServerTestSuite) TestMsgInsertWorkerPayloadWithLowScoreForecastsAreR
GlobalReputerWhitelistEnabled: nil,
GlobalAdminWhitelistAppended: nil,
MaxWhitelistInputArrayLength: nil,
MinWeightThresholdForStdnorm: nil,
}

updateMsg := &types.UpdateParamsRequest{
Expand Down
9 changes: 5 additions & 4 deletions x/emissions/module/rewards/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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(
Expand Down Expand Up @@ -2361,6 +2361,7 @@ func (s *RewardsTestSuite) SetParamsForTest() {
GlobalReputerWhitelistEnabled: nil,
GlobalAdminWhitelistAppended: nil,
MaxWhitelistInputArrayLength: nil,
MinWeightThresholdForStdnorm: nil,
}

updateMsg := &types.UpdateParamsRequest{
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 44f4640

Please sign in to comment.