From e77fe0521637f2762e6721e8e49b967bdb2e9fe4 Mon Sep 17 00:00:00 2001 From: "Felix C. Morency" <1102868+fmorency@users.noreply.github.com> Date: Thu, 25 Apr 2024 15:45:00 -0400 Subject: [PATCH] chore: remove panic usage in keeper methods --- x/manifest/abci.go | 5 ++++- x/manifest/keeper/keeper.go | 30 +++++++++++++++++----------- x/manifest/keeper/msg_server_test.go | 3 ++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/x/manifest/abci.go b/x/manifest/abci.go index 89287c2..abc5ec9 100644 --- a/x/manifest/abci.go +++ b/x/manifest/abci.go @@ -33,7 +33,10 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper, mk mintkeeper.Keeper) er } // Calculate the per block inflation rewards to pay out in coins - mintedCoin := k.BlockRewardsProvision(ctx, params.Inflation.MintDenom) + mintedCoin, err := k.BlockRewardsProvision(ctx, params.Inflation.MintDenom) + if err != nil { + return err + } mintedCoins := sdk.NewCoins(mintedCoin) // If no inflation payout this block, skip diff --git a/x/manifest/keeper/keeper.go b/x/manifest/keeper/keeper.go index dae5b7b..b46cb56 100644 --- a/x/manifest/keeper/keeper.go +++ b/x/manifest/keeper/keeper.go @@ -87,13 +87,13 @@ func (k *Keeper) ExportGenesis(ctx context.Context) *types.GenesisState { } } -func (k *Keeper) GetShareHolders(ctx context.Context) []*types.StakeHolders { +func (k *Keeper) GetShareHolders(ctx context.Context) ([]*types.StakeHolders, error) { params, err := k.Params.Get(ctx) if err != nil { - panic(err) + return nil, err } - return params.StakeHolders + return params.StakeHolders, nil } // IsManualMintingEnabled returns nil if inflation mint is 0% (disabled) @@ -112,8 +112,11 @@ type StakeHolderPayout struct { } // Returns the amount of coins to be distributed to the holders -func (k Keeper) CalculateShareHolderTokenPayout(ctx context.Context, c sdk.Coin) []StakeHolderPayout { - sh := k.GetShareHolders(ctx) +func (k Keeper) CalculateShareHolderTokenPayout(ctx context.Context, c sdk.Coin) ([]StakeHolderPayout, error) { + sh, err := k.GetShareHolders(ctx) + if err != nil { + return nil, err + } pairs := make([]StakeHolderPayout, 0, len(sh)) @@ -134,7 +137,7 @@ func (k Keeper) CalculateShareHolderTokenPayout(ctx context.Context, c sdk.Coin) } - return pairs + return pairs, nil } // PayoutStakeholders mints coins and sends them to the stakeholders. @@ -142,7 +145,10 @@ func (k Keeper) CalculateShareHolderTokenPayout(ctx context.Context, c sdk.Coin) // If it does, something is very wrong w/ the SDK. Any logic specific to auto minting // should be kept out of this to properly handle and return nil instead. func (k Keeper) PayoutStakeholders(ctx context.Context, c sdk.Coin) error { - pairs := k.CalculateShareHolderTokenPayout(ctx, c) + pairs, err := k.CalculateShareHolderTokenPayout(ctx, c) + if err != nil { + return err + } if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(c)); err != nil { return err @@ -164,15 +170,15 @@ func (k Keeper) PayoutStakeholders(ctx context.Context, c sdk.Coin) error { // BlockRewardsProvision Gets the amount of coins that are automatically minted every block // per the automatic inflation -func (k Keeper) BlockRewardsProvision(ctx context.Context, denom string) sdk.Coin { +func (k Keeper) BlockRewardsProvision(ctx context.Context, denom string) (sdk.Coin, error) { mkParams, err := k.mintKeeper.Params.Get(ctx) if err != nil { - panic(err) + return sdk.NewCoin(denom, sdkmath.ZeroInt()), err } params, err := k.Params.Get(ctx) if err != nil { - panic(err) + return sdk.NewCoin(denom, sdkmath.ZeroInt()), err } amtPerYear := params.Inflation.YearlyAmount @@ -180,11 +186,11 @@ func (k Keeper) BlockRewardsProvision(ctx context.Context, denom string) sdk.Coi if blocksPerYear < 10 { k.logger.Error("x/mint blocks per year param is too low", "blocks", blocksPerYear) - return sdk.NewCoin(denom, sdkmath.ZeroInt()) + return sdk.NewCoin(denom, sdkmath.ZeroInt()), nil } div := amtPerYear / blocksPerYear // return the amount of coins to be minted per block - return sdk.NewCoin(denom, sdkmath.NewIntFromUint64(div)) + return sdk.NewCoin(denom, sdkmath.NewIntFromUint64(div)), nil } diff --git a/x/manifest/keeper/msg_server_test.go b/x/manifest/keeper/msg_server_test.go index 33d8582..18fe7a9 100644 --- a/x/manifest/keeper/msg_server_test.go +++ b/x/manifest/keeper/msg_server_test.go @@ -225,7 +225,8 @@ func TestCalculatePayoutLogic(t *testing.T) { require.NoError(t, err) // validate the full payout of 100 tokens got split up between all fractional shares as expected - res := k.CalculateShareHolderTokenPayout(f.Ctx, sdk.NewCoin("stake", sdkmath.NewInt(100_000_000))) + res, err := k.CalculateShareHolderTokenPayout(f.Ctx, sdk.NewCoin("stake", sdkmath.NewInt(100_000_000))) + require.NoError(t, err) for _, s := range sh { for w, shp := range res { if s.Address == shp.Address {