Skip to content

Commit

Permalink
Add context to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandofcampos committed Aug 17, 2024
1 parent 429b05f commit 70989fc
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 30 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/cosmos/cosmos-sdk/x/gov"
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
Expand Down Expand Up @@ -267,7 +268,7 @@ func NewAlloraApp(
app.SetInitChainer(func(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
err := app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap())
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to set module version map")
}
return app.App.InitChainer(ctx, req)
})
Expand Down
14 changes: 7 additions & 7 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (app *AlloraApp) ExportAppStateAndValidators(
height = 0
err := app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
if err != nil {
return servertypes.ExportedApp{}, err
return servertypes.ExportedApp{}, fmt.Errorf("error preparing for zero height genesis: %w", err)
}
}

Expand All @@ -40,7 +40,7 @@ func (app *AlloraApp) ExportAppStateAndValidators(

appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
return servertypes.ExportedApp{}, fmt.Errorf("failed to marshal application state: %w", err)
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
Expand All @@ -67,7 +67,7 @@ func (app *AlloraApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
for _, addr := range jailAllowedAddrs {
_, err := sdk.ValAddressFromBech32(addr)
if err != nil {
return err
return fmt.Errorf("failed to parse address %s: %w", addr, err)
}
allowedAddrsMap[addr] = true
}
Expand All @@ -87,7 +87,7 @@ func (app *AlloraApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
return err != nil
})
if err != nil {
return err
return fmt.Errorf("failed to reset redelegation creation heights: %w", err)
}

// iterate through unbonding delegations, reset creation height
Expand All @@ -99,7 +99,7 @@ func (app *AlloraApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
return err != nil
})
if err != nil {
return err
return fmt.Errorf("failed to reset unbonding delegation creation heights: %w", err)
}

// Iterate through validators by power descending, reset bond heights, and
Expand All @@ -124,7 +124,7 @@ func (app *AlloraApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs

err = app.StakingKeeper.SetValidator(ctx, validator)
if err != nil {
return err
return fmt.Errorf("failed to update validator: %w", err)
}
counter++
}
Expand All @@ -136,7 +136,7 @@ func (app *AlloraApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs

_, err = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
return err
return fmt.Errorf("failed to update validator set: %w", err)
}
return nil
}
2 changes: 1 addition & 1 deletion app/upgrades/v0_4_0/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0_4_0
package v0_4_0 //nolint:revive // var-naming: don't use an underscore in package name

import (
"context"
Expand Down
18 changes: 9 additions & 9 deletions test/testutil/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ func SetRegretsFromPreviousEpoch(
emissionstypes.TimestampedValue{BlockHeight: blockHeight, Value: regret},
)
if err != nil {
return err
return fmt.Errorf("error setting inferer network regret: %v", err)
}
}

Expand All @@ -1508,7 +1508,7 @@ func SetRegretsFromPreviousEpoch(
emissionstypes.TimestampedValue{BlockHeight: blockHeight, Value: regret},
)
if err != nil {
return err
return fmt.Errorf("error setting forecaster network regret: %v", err)
}
}

Expand All @@ -1526,7 +1526,7 @@ func SetRegretsFromPreviousEpoch(
emissionstypes.TimestampedValue{BlockHeight: blockHeight, Value: regret},
)
if err != nil {
return err
return fmt.Errorf("error setting naive inferer network regret: %v", err)
}
}

Expand All @@ -1545,7 +1545,7 @@ func SetRegretsFromPreviousEpoch(
},
)
if err != nil {
return err
return fmt.Errorf("error setting one-out inferer-inferer network regret: %v", err)
}
}
}
Expand All @@ -1565,7 +1565,7 @@ func SetRegretsFromPreviousEpoch(
},
)
if err != nil {
return err
return fmt.Errorf("error setting one-out inferer-forecaster network regret: %v", err)
}
}
}
Expand All @@ -1585,7 +1585,7 @@ func SetRegretsFromPreviousEpoch(
},
)
if err != nil {
return err
return fmt.Errorf("error setting one-out forecaster-inferer network regret: %v", err)
}
}
}
Expand All @@ -1605,7 +1605,7 @@ func SetRegretsFromPreviousEpoch(
},
)
if err != nil {
return err
return fmt.Errorf("error setting one-out forecaster-forecaster network regret: %v", err)
}
}
}
Expand All @@ -1624,7 +1624,7 @@ func SetRegretsFromPreviousEpoch(
},
)
if err != nil {
return err
return fmt.Errorf("error setting one-in forecaster network regret: %v", err)
}
for j := range inferers {
headerName := fmt.Sprintf("inference_regret_worker_%v_onein_%v", j, i)
Expand All @@ -1639,7 +1639,7 @@ func SetRegretsFromPreviousEpoch(
},
)
if err != nil {
return err
return fmt.Errorf("error setting one-in forecaster network regret: %v", err)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions x/emissions/keeper/actor_utils/util_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (pq *PriorityQueue) Pop() any {
// Sorts the given actors by score, desc, breaking ties randomly
// Returns the top N actors as a map with the actor as the key and a boolean (True) as the value
func FindTopNByScoreDesc(n uint64, scoresByActor map[Actor]Score, randSeed BlockHeight) ([]Actor, map[string]bool) {
r := rand.New(rand.NewSource(randSeed))
r := rand.New(rand.NewSource(randSeed)) //nolint:gosec // G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand)
queue := &PriorityQueue{}
i := 0
// Extract and sort the keys
Expand Down Expand Up @@ -96,7 +96,8 @@ func FindTopNByScoreDesc(n uint64, scoresByActor map[Actor]Score, randSeed Block
}
item, ok := heap.Pop(queue).(*SortableItem)
if !ok {
return nil, fmt.Errorf("could not cast to SortableItem")
fmt.Println("Error: Could not cast to SortableItem")
continue
}
topN = append(topN, item.Value)
topNBool[item.Value] = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (p *SynthPalette) CalcForecastImpliedInferences() (map[Worker]*emissionstyp

// Calculate the forecast-implied inferences I_ik
for _, j := range sortedInferersInForecast {
w_ijk := w_ik[j]
w_ijk := w_ik[j] //nolint:revive // var-naming: don't use underscores in Go names
_, ok := p.InferenceByWorker[j]
if ok && !(w_ijk.Equal(alloraMath.ZeroDec())) {
thisDotProduct, err := w_ijk.Mul(p.InferenceByWorker[j].Value)
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ func (k *Keeper) GetLatestNetworkLossBundle(ctx context.Context, topicId TopicId
return &keyValue.Value, nil
}

return nil, types.ErrNoNetworkLossBundleFound
return nil, types.ErrNotFound
}

/// STAKING
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ func (s *KeeperTestSuite) TestGetLatestNetworkLossBundle() {

// Initially, there should be no loss bundle, so we expect a zero result
emptyLossBundle, err := keeper.GetLatestNetworkLossBundle(ctx, topicId)
s.Require().ErrorIs(err, types.ErrNoNetworkLossBundleFound)
s.Require().ErrorIs(err, types.ErrNotFound)
s.Require().Nil(emptyLossBundle, "Expected no network loss bundle initially")

// Insert first network loss bundle
Expand Down
1 change: 1 addition & 0 deletions x/emissions/keeper/msgserver/msg_server_demand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package msgserver

import (
"context"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down
1 change: 1 addition & 0 deletions x/emissions/keeper/msgserver/msg_server_reputer_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package msgserver

import (
"context"

errorsmod "cosmossdk.io/errors"

"github.com/allora-network/allora-chain/x/emissions/types"
Expand Down
5 changes: 3 additions & 2 deletions x/emissions/keeper/msgserver/msg_server_worker_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package msgserver

import (
"context"

errorsmod "cosmossdk.io/errors"
"github.com/allora-network/allora-chain/x/emissions/keeper/actor_utils"
actorutils "github.com/allora-network/allora-chain/x/emissions/keeper/actor_utils"
"github.com/allora-network/allora-chain/x/emissions/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -128,7 +129,7 @@ func (ms msgServer) InsertWorkerPayload(ctx context.Context, msg *types.MsgInser
if err != nil {
return nil, err
}
_, topNInferer := actor_utils.FindTopNByScoreDesc(moduleParams.MaxElementsPerForecast, latestScoresForForecastedInferers, forecast.BlockHeight)
_, topNInferer := actorutils.FindTopNByScoreDesc(moduleParams.MaxElementsPerForecast, latestScoresForForecastedInferers, forecast.BlockHeight)

for _, el := range forecast.ForecastElements {
if !seenInferers[el.Inferer] && topNInferer[el.Inferer] {
Expand Down
5 changes: 3 additions & 2 deletions x/emissions/migrations/v2/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v2

import (
"cosmossdk.io/collections"
"cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
alloraMath "github.com/allora-network/allora-chain/math"
Expand Down Expand Up @@ -138,12 +139,12 @@ func MigrateOffchainNode(store storetypes.KVStore, cdc codec.BinaryCodec) error
workerStore := prefix.NewStore(store, types.WorkerNodesKey)
err := MigrateOffchainStore(workerStore, cdc)
if err != nil {
return err
return errors.Wrap(err, "error migrating worker store")
}
reputerStore := prefix.NewStore(store, types.ReputerNodesKey)
err = MigrateOffchainStore(reputerStore, cdc)
if err != nil {
return err
return errors.Wrap(err, "error migrating reputer store")
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion x/emissions/module/rewards/topic_skimming.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/rand"
"sort"

"cosmossdk.io/errors"
alloraMath "github.com/allora-network/allora-chain/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ func SkimTopTopicsByWeightDesc(ctx sdk.Context, weights map[TopicId]*alloraMath.
})
sortedTopicIds, err := SortTopicsByWeightDescWithRandomTiebreaker(topicIds, weights, block)
if err != nil {
return nil, nil, err
return nil, nil, errors.Wrap(err, "failed to sort topics by weight desc with random tiebreaker")
}

numberToAdd := N
Expand Down
5 changes: 3 additions & 2 deletions x/mint/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mint
import (
"context"

"cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/allora-network/allora-chain/x/mint/keeper"
"github.com/allora-network/allora-chain/x/mint/types"
Expand Down Expand Up @@ -218,11 +219,11 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error {
// set the previous emissions to this block's emissions
err = k.PreviousRewardEmissionPerUnitStakedToken.Set(ctx, e_i)
if err != nil {
return err
return errors.Wrap(err, "error setting previous reward emission per unit staked token")
}
err = k.PreviousBlockEmission.Set(ctx, blockEmission)
if err != nil {
return err
return errors.Wrap(err, "error setting previous block emission")
}
}
return nil
Expand Down

0 comments on commit 70989fc

Please sign in to comment.