Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adding calls to util.Panic #1764

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions util/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package util

// Panic panics on error
func Panic(err error) {
if err != nil {
panic(err)
}
}
9 changes: 3 additions & 6 deletions x/leverage/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
util.Panic(k.AccrueAllInterest(ctx))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

return []abci.ValidatorUpdate{}
}
59 changes: 14 additions & 45 deletions x/leverage/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
31 changes: 7 additions & 24 deletions x/leverage/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
13 changes: 4 additions & 9 deletions x/leverage/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
toteki marked this conversation as resolved.
Show resolved Hide resolved
if val.Value < 0 {
panic(types.ErrGetAmount.Wrapf("%d is below the minimum LastInterestTime of zero", val.Value))
}
Expand Down
22 changes: 6 additions & 16 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
14 changes: 5 additions & 9 deletions x/oracle/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions x/oracle/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

return []abci.ValidatorUpdate{}
}
Expand Down
Loading