Skip to content

Commit

Permalink
fix scorum module BeginBlocker interface implementation (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikluke authored Oct 3, 2024
1 parent 2d579d2 commit 1856405
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
6 changes: 4 additions & 2 deletions x/aviatrix/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
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"
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions x/scorum/keeper/validator_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

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

0 comments on commit 1856405

Please sign in to comment.