Skip to content

Commit

Permalink
chore: remove panic usage in keeper methods (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency authored Apr 26, 2024
1 parent 6a7913e commit ee953c5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
5 changes: 4 additions & 1 deletion x/manifest/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 18 additions & 12 deletions x/manifest/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))

Expand All @@ -134,15 +137,18 @@ func (k Keeper) CalculateShareHolderTokenPayout(ctx context.Context, c sdk.Coin)

}

return pairs
return pairs, nil
}

// PayoutStakeholders mints coins and sends them to the stakeholders.
// This is called from the endblocker, so panics should never happen.
// 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
Expand All @@ -164,27 +170,27 @@ 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
blocksPerYear := mkParams.BlocksPerYear

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
}
3 changes: 2 additions & 1 deletion x/manifest/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ee953c5

Please sign in to comment.