From 172354793d2b9fa9db0c205ddf1f7c0fad8ac5b3 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 30 Jan 2023 22:38:07 +0100 Subject: [PATCH 1/2] adding calls to util.Panic --- x/leverage/abci.go | 9 ++---- x/leverage/keeper/genesis.go | 59 ++++++++-------------------------- x/leverage/keeper/iter.go | 31 ++++-------------- x/leverage/keeper/store.go | 13 +++----- x/oracle/genesis.go | 22 ++++--------- x/oracle/keeper/slash.go | 14 +++----- x/oracle/module.go | 11 +++---- x/uibc/module/abci.go | 9 ++---- x/uibc/module/module.go | 9 +++--- x/uibc/quota/keeper/genesis.go | 21 ++++-------- 10 files changed, 58 insertions(+), 140 deletions(-) diff --git a/x/leverage/abci.go b/x/leverage/abci.go index 37900bc3c1..3371ae5040 100644 --- a/x/leverage/abci.go +++ b/x/leverage/abci.go @@ -4,17 +4,14 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" abci "github.com/tendermint/tendermint/abci/types" + "github.com/umee-network/umee/v4/util" "github.com/umee-network/umee/v4/x/leverage/keeper" ) // EndBlocker implements EndBlock for the x/leverage module. func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate { - if err := k.SweepBadDebts(ctx); err != nil { - panic(err) - } - if err := k.AccrueAllInterest(ctx); err != nil { - panic(err) - } + util.Panic(k.SweepBadDebts(ctx)) + util.Panic(k.AccrueAllInterest(ctx)) return []abci.ValidatorUpdate{} } diff --git a/x/leverage/keeper/genesis.go b/x/leverage/keeper/genesis.go index fca8065214..1060a598c5 100644 --- a/x/leverage/keeper/genesis.go +++ b/x/leverage/keeper/genesis.go @@ -4,6 +4,7 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/umee-network/umee/v4/util" "github.com/umee-network/umee/v4/x/leverage/types" ) @@ -12,58 +13,35 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { k.SetParams(ctx, genState.Params) for _, token := range genState.Registry { - if err := k.SetTokenSettings(ctx, token); err != nil { - panic(err) - } + util.Panic(k.SetTokenSettings(ctx, token)) } for _, borrow := range genState.AdjustedBorrows { borrower, err := sdk.AccAddressFromBech32(borrow.Address) - if err != nil { - panic(err) - } - - if err = k.setAdjustedBorrow(ctx, borrower, borrow.Amount); err != nil { - panic(err) - } + util.Panic(err) + util.Panic(k.setAdjustedBorrow(ctx, borrower, borrow.Amount)) } for _, collateral := range genState.Collateral { borrower, err := sdk.AccAddressFromBech32(collateral.Address) - if err != nil { - panic(err) - } - - if err = k.setCollateral(ctx, borrower, collateral.Amount); err != nil { - panic(err) - } + util.Panic(err) + util.Panic(k.setCollateral(ctx, borrower, collateral.Amount)) } for _, reserve := range genState.Reserves { - if err := k.setReserves(ctx, reserve); err != nil { - panic(err) - } + util.Panic(k.setReserves(ctx, reserve)) } - if err := k.setLastInterestTime(ctx, genState.LastInterestTime); err != nil { - panic(err) - } + util.Panic(k.setLastInterestTime(ctx, genState.LastInterestTime)) for _, badDebt := range genState.BadDebts { borrower, err := sdk.AccAddressFromBech32(badDebt.Address) - if err != nil { - panic(err) - } - - if err := k.setBadDebtAddress(ctx, borrower, badDebt.Denom, true); err != nil { - panic(err) - } + util.Panic(err) + util.Panic(k.setBadDebtAddress(ctx, borrower, badDebt.Denom, true)) } for _, rate := range genState.InterestScalars { - if err := k.setInterestScalar(ctx, rate.Denom, rate.Scalar); err != nil { - panic(err) - } + util.Panic(k.setInterestScalar(ctx, rate.Denom, rate.Scalar)) } } @@ -102,10 +80,7 @@ func (k Keeper) getAllAdjustedBorrows(ctx sdk.Context) []types.AdjustedBorrow { return nil } - err := k.iterate(ctx, prefix, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, prefix, iterator)) return borrows } @@ -130,10 +105,7 @@ func (k Keeper) getAllCollateral(ctx sdk.Context) []types.Collateral { return nil } - err := k.iterate(ctx, prefix, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, prefix, iterator)) return collateral } @@ -157,10 +129,7 @@ func (k Keeper) getAllInterestScalars(ctx sdk.Context) []types.InterestScalar { return nil } - err := k.iterate(ctx, prefix, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, prefix, iterator)) return interestScalars } diff --git a/x/leverage/keeper/iter.go b/x/leverage/keeper/iter.go index 0721040165..fe7d730bff 100644 --- a/x/leverage/keeper/iter.go +++ b/x/leverage/keeper/iter.go @@ -4,6 +4,7 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/umee-network/umee/v4/util" "github.com/umee-network/umee/v4/x/leverage/types" ) @@ -40,10 +41,7 @@ func (k Keeper) getAllBadDebts(ctx sdk.Context) []types.BadDebt { return nil } - err := k.iterate(ctx, prefix, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, prefix, iterator)) return badDebts } @@ -64,10 +62,7 @@ func (k Keeper) GetAllRegisteredTokens(ctx sdk.Context) []types.Token { return nil } - err := k.iterate(ctx, types.KeyPrefixRegisteredToken, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, types.KeyPrefixRegisteredToken, iterator)) return tokens } @@ -90,10 +85,7 @@ func (k Keeper) GetAllReserves(ctx sdk.Context) sdk.Coins { return nil } - err := k.iterate(ctx, prefix, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, prefix, iterator)) return reserves } @@ -123,10 +115,7 @@ func (k Keeper) GetBorrowerBorrows(ctx sdk.Context, borrowerAddr sdk.AccAddress) return nil } - err := k.iterate(ctx, prefix, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, prefix, iterator)) return totalBorrowed } @@ -151,10 +140,7 @@ func (k Keeper) GetBorrowerCollateral(ctx sdk.Context, borrowerAddr sdk.AccAddre return nil } - err := k.iterate(ctx, prefix, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, prefix, iterator)) return totalCollateral } @@ -262,10 +248,7 @@ func (k Keeper) GetAllUTokenSupply(ctx sdk.Context) sdk.Coins { return nil } - err := k.iterate(ctx, prefix, iterator) - if err != nil { - panic(err) - } + util.Panic(k.iterate(ctx, prefix, iterator)) return supplies } diff --git a/x/leverage/keeper/store.go b/x/leverage/keeper/store.go index d53ea2b27a..41d4b996c8 100644 --- a/x/leverage/keeper/store.go +++ b/x/leverage/keeper/store.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" gogotypes "github.com/gogo/protobuf/types" + "github.com/umee-network/umee/v4/util" "github.com/umee-network/umee/v4/x/leverage/types" ) @@ -14,9 +15,7 @@ import ( func (k Keeper) getStoredDec(ctx sdk.Context, key []byte, minimum sdk.Dec, desc string) sdk.Dec { if bz := ctx.KVStore(k.storeKey).Get(key); bz != nil { val := sdk.ZeroDec() - if err := val.Unmarshal(bz); err != nil { - panic(err) - } + util.Panic(val.Unmarshal(bz)) if val.LTE(minimum) { panic(types.ErrGetAmount.Wrapf("%s is not above the minimum %s of %s", val, desc, minimum)) } @@ -52,9 +51,7 @@ func (k Keeper) setStoredDec(ctx sdk.Context, key []byte, val, minimum sdk.Dec, func (k Keeper) getStoredInt(ctx sdk.Context, key []byte, desc string) sdkmath.Int { if bz := ctx.KVStore(k.storeKey).Get(key); bz != nil { val := sdk.ZeroInt() - if err := val.Unmarshal(bz); err != nil { - panic(err) - } + util.Panic(val.Unmarshal(bz)) if val.LTE(sdk.ZeroInt()) { panic(types.ErrGetAmount.Wrapf("%s is not above the minimum %s of zero", val, desc)) } @@ -176,9 +173,7 @@ func (k Keeper) getLastInterestTime(ctx sdk.Context) int64 { } val := gogotypes.Int64Value{} - if err := k.cdc.Unmarshal(bz, &val); err != nil { - panic(err) - } + k.cdc.MustUnmarshal(bz, &val) if val.Value < 0 { panic(types.ErrGetAmount.Wrapf("%d is below the minimum LastInterestTime of zero", val.Value)) } diff --git a/x/oracle/genesis.go b/x/oracle/genesis.go index 6e35841652..c7c2e79972 100644 --- a/x/oracle/genesis.go +++ b/x/oracle/genesis.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/umee-network/umee/v4/util" "github.com/umee-network/umee/v4/x/oracle/keeper" "github.com/umee-network/umee/v4/x/oracle/types" ) @@ -14,14 +15,9 @@ import ( func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, genState types.GenesisState) { for _, d := range genState.FeederDelegations { voter, err := sdk.ValAddressFromBech32(d.ValidatorAddress) - if err != nil { - panic(err) - } - + util.Panic(err) feeder, err := sdk.AccAddressFromBech32(d.FeederAddress) - if err != nil { - panic(err) - } + util.Panic(err) keeper.SetFeederDelegation(ctx, voter, feeder) } @@ -32,27 +28,21 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, genState types.GenesisSt for _, mc := range genState.MissCounters { operator, err := sdk.ValAddressFromBech32(mc.ValidatorAddress) - if err != nil { - panic(err) - } + util.Panic(err) keeper.SetMissCounter(ctx, operator, mc.MissCounter) } for _, ap := range genState.AggregateExchangeRatePrevotes { valAddr, err := sdk.ValAddressFromBech32(ap.Voter) - if err != nil { - panic(err) - } + util.Panic(err) keeper.SetAggregateExchangeRatePrevote(ctx, valAddr, ap) } for _, av := range genState.AggregateExchangeRateVotes { valAddr, err := sdk.ValAddressFromBech32(av.Voter) - if err != nil { - panic(err) - } + util.Panic(err) keeper.SetAggregateExchangeRateVote(ctx, valAddr, av) } diff --git a/x/oracle/keeper/slash.go b/x/oracle/keeper/slash.go index 13aa3b9d88..086604c2f4 100644 --- a/x/oracle/keeper/slash.go +++ b/x/oracle/keeper/slash.go @@ -11,19 +11,15 @@ import ( // If the valid vote rate is below the minValidPerWindow, the validator will be // slashed and jailed. func (k Keeper) SlashAndResetMissCounters(ctx sdk.Context) { - height := ctx.BlockHeight() - distributionHeight := height - sdk.ValidatorUpdateDelay - 1 - var ( + height = ctx.BlockHeight() + distributionHeight = height - sdk.ValidatorUpdateDelay - 1 slashWindow = int64(k.SlashWindow(ctx)) votePeriod = int64(k.VotePeriod(ctx)) votePeriodsPerWindow = sdk.NewDec(slashWindow).QuoInt64(votePeriod).TruncateInt64() - ) - - var ( - minValidPerWindow = k.MinValidPerWindow(ctx) - slashFraction = k.SlashFraction(ctx) - powerReduction = k.StakingKeeper.PowerReduction(ctx) + minValidPerWindow = k.MinValidPerWindow(ctx) + slashFraction = k.SlashFraction(ctx) + powerReduction = k.StakingKeeper.PowerReduction(ctx) ) k.IterateMissCounters(ctx, func(operator sdk.ValAddress, missCounter uint64) bool { diff --git a/x/oracle/module.go b/x/oracle/module.go index 074cc4a7eb..7717963df2 100644 --- a/x/oracle/module.go +++ b/x/oracle/module.go @@ -18,6 +18,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/umee-network/umee/v4/util" "github.com/umee-network/umee/v4/x/oracle/client/cli" "github.com/umee-network/umee/v4/x/oracle/keeper" simulation "github.com/umee-network/umee/v4/x/oracle/simulations" @@ -83,9 +84,8 @@ func (AppModuleBasic) RegisterRESTRoutes(client.Context, *mux.Router) {} // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the x/oracle // module. func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { - if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { - panic(err) - } + err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) + util.Panic(err) } // GetTxCmd returns the x/oracle module's root tx command. @@ -180,9 +180,8 @@ func (am AppModule) BeginBlock(sdk.Context, abci.RequestBeginBlock) {} // EndBlock executes all ABCI EndBlock logic respective to the x/oracle module. // It returns no validator updates. func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { - if err := EndBlocker(ctx, am.keeper); err != nil { - panic(err) - } + err := EndBlocker(ctx, am.keeper) + util.Panic(err) return []abci.ValidatorUpdate{} } diff --git a/x/uibc/module/abci.go b/x/uibc/module/abci.go index 432a2cf9de..09eae84078 100644 --- a/x/uibc/module/abci.go +++ b/x/uibc/module/abci.go @@ -3,21 +3,18 @@ package uibc import ( sdk "github.com/cosmos/cosmos-sdk/types" abci "github.com/tendermint/tendermint/abci/types" + "github.com/umee-network/umee/v4/util" "github.com/umee-network/umee/v4/x/uibc/quota/keeper" ) // BeginBlock implements BeginBlock for the x/uibc module. func BeginBlock(ctx sdk.Context, keeper keeper.Keeper) { quotaExpires, err := keeper.GetExpire(ctx) - if err != nil { - panic(err) - } + util.Panic(err) // reset quotas if quotaExpires == nil || quotaExpires.Before(ctx.BlockTime()) { - if err := keeper.ResetQuota(ctx); err != nil { - panic(err) - } + util.Panic(keeper.ResetQuota(ctx)) } } diff --git a/x/uibc/module/module.go b/x/uibc/module/module.go index 7f0fd42a5b..9ef0946f3d 100644 --- a/x/uibc/module/module.go +++ b/x/uibc/module/module.go @@ -13,6 +13,7 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" + "github.com/umee-network/umee/v4/util" ibctransfer "github.com/umee-network/umee/v4/x/uibc" "github.com/umee-network/umee/v4/x/uibc/client/cli" "github.com/umee-network/umee/v4/x/uibc/quota/keeper" @@ -54,11 +55,9 @@ func (AppModuleBasic) Name() string { // RegisterGRPCGatewayRoutes implements module.AppModuleBasic func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { - if err := ibctransfer.RegisterQueryHandlerClient( - context.Background(), mux, ibctransfer.NewQueryClient(clientCtx), - ); err != nil { - panic(err) - } + err := ibctransfer.RegisterQueryHandlerClient( + context.Background(), mux, ibctransfer.NewQueryClient(clientCtx)) + util.Panic(err) } // RegisterInterfaces implements module.AppModuleBasic diff --git a/x/uibc/quota/keeper/genesis.go b/x/uibc/quota/keeper/genesis.go index 85a87635b2..f4526e8f4a 100644 --- a/x/uibc/quota/keeper/genesis.go +++ b/x/uibc/quota/keeper/genesis.go @@ -2,36 +2,29 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/umee-network/umee/v4/util" "github.com/umee-network/umee/v4/x/uibc" ) // InitGenesis initializes the x/uibc module's state from a provided genesis // state. func (k Keeper) InitGenesis(ctx sdk.Context, genState uibc.GenesisState) { - if err := k.SetParams(ctx, genState.Params); err != nil { - panic(err) - } + err := k.SetParams(ctx, genState.Params) + util.Panic(err) k.SetDenomQuotas(ctx, genState.Quotas) - k.SetTotalOutflowSum(ctx, genState.TotalOutflowSum) - if err := k.SetExpire(ctx, genState.QuotaExpires); err != nil { - panic(err) - } + err = k.SetExpire(ctx, genState.QuotaExpires) + util.Panic(err) } // ExportGenesis returns the x/uibc module's exported genesis state. func (k Keeper) ExportGenesis(ctx sdk.Context) *uibc.GenesisState { quotas, err := k.GetQuotaOfIBCDenoms(ctx) - if err != nil { - panic(err) - } - + util.Panic(err) quotaExpires, err := k.GetExpire(ctx) - if err != nil { - panic(err) - } + util.Panic(err) return &uibc.GenesisState{ Params: k.GetParams(ctx), From fe94e69caf9907c7dec97237cad1221336f83380 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 31 Jan 2023 11:55:05 +0100 Subject: [PATCH 2/2] add missing file --- util/error.go | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 util/error.go diff --git a/util/error.go b/util/error.go new file mode 100644 index 0000000000..ad182db1c9 --- /dev/null +++ b/util/error.go @@ -0,0 +1,8 @@ +package util + +// Panic panics on error +func Panic(err error) { + if err != nil { + panic(err) + } +}