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

fix scorum module BeginBlocker interface implementation #26

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
6 changes: 4 additions & 2 deletions x/aviatrix/module.go
Original file line number Diff line number Diff line change
@@ -154,10 +154,12 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return 1 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ sdk.Context) {}
func (am AppModule) BeginBlock(_ context.Context) error {
return nil
}

// EndBlock contains the logic that is automatically triggered at the end of each block
func (am AppModule) EndBlock(ctx context.Context) ([]abci.ValidatorUpdate, error) {
func (am AppModule) EndBlock(_ context.Context) ([]abci.ValidatorUpdate, error) {
return []abci.ValidatorUpdate{}, nil
}

15 changes: 11 additions & 4 deletions x/scorum/keeper/gas.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"fmt"
"slices"

"cosmossdk.io/math"
@@ -17,7 +18,7 @@ func (k Keeper) SetAddressToRestoreGas(ctx sdk.Context, addr sdk.AccAddress) {
s.Set(addr.Bytes(), []byte{})
}

func (k Keeper) RestoreGasForAddress(ctx sdk.Context, addr sdk.AccAddress, avgStakedBalance math.LegacyDec, params types.Params) {
func (k Keeper) RestoreGasForAddress(ctx sdk.Context, addr sdk.AccAddress, avgStakedBalance math.LegacyDec, params types.Params) error {
s := prefix.NewStore(ctx.KVStore(k.storeKey), gasConsumedAddressesPrefix)

gasBalance := k.bankKeeper.GetBalance(ctx, addr, types.GasDenom).Amount
@@ -56,20 +57,26 @@ func (k Keeper) RestoreGasForAddress(ctx sdk.Context, addr sdk.AccAddress, avgSt

if gasAdjust.IsPositive() {
if err := k.Mint(ctx, addr, sdk.NewCoin(types.GasDenom, gasAdjust)); err != nil {
panic(err)
return fmt.Errorf("failed to mint: %w", err)
}
}

return nil
}

func (k Keeper) RestoreGas(ctx sdk.Context) {
func (k Keeper) RestoreGas(ctx sdk.Context) error {
s := prefix.NewStore(ctx.KVStore(k.storeKey), gasConsumedAddressesPrefix)
it := s.Iterator(nil, nil)
defer it.Close()

avgStakedBalance, params := k.GetAverageStakedBalance(ctx), k.GetParams(ctx)
for ; it.Valid(); it.Next() {
k.RestoreGasForAddress(ctx, it.Key(), avgStakedBalance, params)
if err := k.RestoreGasForAddress(ctx, it.Key(), avgStakedBalance, params); err != nil {
return fmt.Errorf("failed to restore gas for address %s: %w", sdk.AccAddress(it.Key()), err)
}
}

return nil
}

func calculateGasAdjustAmount(stakedBalance, gasLimit, gasUnconditionedAmount, avgStakedBalance, gasAdjustCoefficient math.LegacyDec) math.LegacyDec {
14 changes: 8 additions & 6 deletions x/scorum/keeper/validator_reward.go
Original file line number Diff line number Diff line change
@@ -7,14 +7,14 @@ import (
"github.com/scorum/cosmos-network/x/scorum/types"
)

func (k Keeper) PrepareValidatorsReward(ctx sdk.Context) {
func (k Keeper) PrepareValidatorsReward(ctx sdk.Context) error {
feeCollectorAddr := k.accountKeeper.GetModuleAddress(k.feeCollectorName)

ctx.Logger().Debug("burn collected gas from fee_collector")
b := k.bankKeeper.GetBalance(ctx, feeCollectorAddr, types.GasDenom)
if b.IsPositive() {
if err := k.Burn(ctx, feeCollectorAddr, b); err != nil {
panic(fmt.Errorf("failed to burn gas coins: %w", err))
return fmt.Errorf("failed to burn gas coins: %w", err)
}
}

@@ -23,15 +23,15 @@ func (k Keeper) PrepareValidatorsReward(ctx sdk.Context) {
ctx.Logger().Info("validator rewards pool address is empty")
ctx.Logger().Debug("skip pouring rewards to fee_collector")

return
return nil
}

blockReward := validatorRewardsParams.BlockReward
if !blockReward.IsValid() || !blockReward.IsPositive() {
ctx.Logger().Info("validators reward amount is invalid or not positive")
ctx.Logger().Debug("skip pouring rewards to fee_collector")

return
return nil
}

poolAddress := sdk.MustAccAddressFromBech32(validatorRewardsParams.PoolAddress)
@@ -43,12 +43,14 @@ func (k Keeper) PrepareValidatorsReward(ctx sdk.Context) {
if blockReward.IsZero() {
ctx.Logger().Error("validators reward pool is fully drained")

return
return nil
}

if err := k.bankKeeper.SendCoins(ctx, poolAddress, feeCollectorAddr, sdk.NewCoins(blockReward)); err != nil {
panic(fmt.Errorf("failed to send coins from validators reward pool to fee_collector: %w", err))
return fmt.Errorf("failed to send coins from validators reward pool to fee_collector: %w", err)
}

ctx.Logger().Debug("validators reward pool is successfully poured")

return nil
}
15 changes: 12 additions & 3 deletions x/scorum/module.go
Original file line number Diff line number Diff line change
@@ -152,9 +152,18 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return 1 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(ctx sdk.Context) {
am.keeper.RestoreGas(ctx)
am.keeper.PrepareValidatorsReward(ctx)
func (am AppModule) BeginBlock(ctx context.Context) error {
sdkContext := sdk.UnwrapSDKContext(ctx)

if err := am.keeper.RestoreGas(sdkContext); err != nil {
return fmt.Errorf("failed to restore gas: %w", err)
}

if err := am.keeper.PrepareValidatorsReward(sdkContext); err != nil {
return fmt.Errorf("failed to prepare validators reward: %w", err)
}

return nil
}

// EndBlock contains the logic that is automatically triggered at the end of each block