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: remove panic usage in keeper methods #49

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@
}

// 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

Check warning on line 38 in x/manifest/abci.go

View check run for this annotation

Codecov / codecov/patch

x/manifest/abci.go#L38

Added line #L38 was not covered by tests
}
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) 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

Check warning on line 93 in x/manifest/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/manifest/keeper/keeper.go#L93

Added line #L93 was not covered by tests
}

return params.StakeHolders
return params.StakeHolders, nil
}

// IsManualMintingEnabled returns nil if inflation mint is 0% (disabled)
Expand All @@ -112,8 +112,11 @@
}

// 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

Check warning on line 118 in x/manifest/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/manifest/keeper/keeper.go#L118

Added line #L118 was not covered by tests
}

pairs := make([]StakeHolderPayout, 0, len(sh))

Expand All @@ -134,15 +137,18 @@

}

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

Check warning on line 150 in x/manifest/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/manifest/keeper/keeper.go#L150

Added line #L150 was not covered by tests
}

if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(c)); err != nil {
return err
Expand All @@ -164,27 +170,27 @@

// 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

Check warning on line 176 in x/manifest/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/manifest/keeper/keeper.go#L176

Added line #L176 was not covered by tests
}

params, err := k.Params.Get(ctx)
if err != nil {
panic(err)
return sdk.NewCoin(denom, sdkmath.ZeroInt()), err

Check warning on line 181 in x/manifest/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/manifest/keeper/keeper.go#L181

Added line #L181 was not covered by tests
}

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

Check warning on line 189 in x/manifest/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/manifest/keeper/keeper.go#L189

Added line #L189 was not covered by tests
}

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
Loading