Skip to content

Commit

Permalink
Removed token burning in slashing (#18)
Browse files Browse the repository at this point in the history
Removed token burning in slashing: Added hooks
  • Loading branch information
EduardMikhrin authored Dec 10, 2024
1 parent d3145c3 commit bf47b9e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ func NewSimApp(
app.BankKeeper = bankkeeper.NewBaseKeeper(
appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.GetSubspace(banktypes.ModuleName), app.ModuleAccountAddrs(),
)

app.StakingKeeper = stakingkeeper.NewKeeper(
appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName),
)
Expand Down Expand Up @@ -299,6 +298,7 @@ func NewSimApp(
appCodec, keys[distrtypes.StoreKey], app.GetSubspace(distrtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
app.StakingKeeper, app.NFTKeeper, authtypes.FeeCollectorName,
)

app.SlashingKeeper = slashingkeeper.NewKeeper(
appCodec, keys[slashingtypes.StoreKey], app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName),
)
Expand Down
5 changes: 5 additions & 0 deletions x/distribution/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, f
return nil
}

// fund community pool from module account
func (h Hooks) FundCommunityPoolFromModule(ctx sdk.Context, amount sdk.Coins, senderModuleName string) error {
return h.k.FundCommunityPoolFromModule(ctx, amount, senderModuleName)
}

func (h Hooks) BeforeValidatorModified(_ sdk.Context, _ sdk.ValAddress) error { return nil }
func (h Hooks) AfterValidatorBonded(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
return nil
Expand Down
15 changes: 15 additions & 0 deletions x/distribution/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,25 @@ func (k Keeper) FundCommunityPool(ctx sdk.Context, amount sdk.Coins, sender sdk.
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, amount); err != nil {
return err
}
feePool := k.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...)
k.SetFeePool(ctx, feePool)

return nil
}

// FundCommunityPoolFromModule is used to send tokens to community pool from other modules
func (k Keeper) FundCommunityPoolFromModule(ctx sdk.Context, amount sdk.Coins, senderModuleName string) error {
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, senderModuleName, types.ModuleName, amount); err != nil {
err = sdkerrors.Wrap(err, "failed to fund community pool")
k.Logger(ctx).Error(err.Error())
return err
}
feePool := k.GetFeePool(ctx)
k.Logger(ctx).Info(fmt.Sprintf("amount of tokens in community pool before slashing is %s", feePool.CommunityPool.String()))
feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...)
k.SetFeePool(ctx, feePool)
k.Logger(ctx).Info(fmt.Sprintf("amount of tokens in community pool after slashing is %s", feePool.CommunityPool.String()))

return nil
}
4 changes: 4 additions & 0 deletions x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (h Hooks) AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) er
return h.k.AfterValidatorCreated(ctx, valAddr)
}

func (h Hooks) FundCommunityPoolFromModule(_ sdk.Context, _ sdk.Coins, _ string) error {
return nil
}

func (h Hooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions x/staking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func (k Keeper) AfterValidatorRemoved(ctx sdk.Context, consAddr sdk.ConsAddress,
return nil
}

// FundCommunityPoolFromModule - call hook if registered
func (k Keeper) FundCommunityPoolFromModule(ctx sdk.Context, amount sdk.Coins, senderModuleName string) error {
if k.hooks != nil {
return k.hooks.FundCommunityPoolFromModule(ctx, amount, senderModuleName)
}
return nil
}

// AfterValidatorBonded - call hook if registered
func (k Keeper) AfterValidatorBonded(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
if k.hooks != nil {
Expand Down
12 changes: 4 additions & 8 deletions x/staking/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ func (k Keeper) burnBondedTokens(ctx sdk.Context, amt math.Int) error {
// skip as no coins need to be burned
return nil
}

coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amt))

return k.bankKeeper.BurnCoins(ctx, types.BondedPoolName, coins)
//Replaced burning to transfer tokens to community pool
return k.FundCommunityPoolFromModule(ctx, sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amt)), types.BondedPoolName)
}

// burnNotBondedTokens removes coins from the not bonded pool module account
Expand All @@ -51,10 +49,8 @@ func (k Keeper) burnNotBondedTokens(ctx sdk.Context, amt math.Int) error {
// skip as no coins need to be burned
return nil
}

coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amt))

return k.bankKeeper.BurnCoins(ctx, types.NotBondedPoolName, coins)
//Replaced burning to transfer tokens to community pool
return k.FundCommunityPoolFromModule(ctx, sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amt)), types.NotBondedPoolName)
}

// TotalBondedTokens total staking tokens supply which is bonded
Expand Down
3 changes: 2 additions & 1 deletion x/staking/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type DistributionKeeper interface {
GetFeePoolCommunityCoins(ctx sdk.Context) sdk.DecCoins
GetValidatorOutstandingRewardsCoins(ctx sdk.Context, val sdk.ValAddress) sdk.DecCoins
FundCommunityPoolFromModule(ctx sdk.Context, amount sdk.Coins, senderModuleName string) error
}

// AccountKeeper defines the expected account keeper (noalias)
Expand Down Expand Up @@ -96,7 +97,7 @@ type StakingHooks interface {

AfterValidatorBonded(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error // Must be called when a validator is bonded
AfterValidatorBeginUnbonding(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error // Must be called when a validator begins unbonding

FundCommunityPoolFromModule(ctx sdk.Context, amount sdk.Coins, senderModuleName string) error
BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error // Must be called when a delegation is created
BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error // Must be called when a delegation's shares are modified
BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error // Must be called when a delegation is removed
Expand Down
10 changes: 10 additions & 0 deletions x/staking/types/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ func (h MultiStakingHooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.
return nil
}

// hooks to distribution module to process token sending after validator is slashed
func (h MultiStakingHooks) FundCommunityPoolFromModule(ctx sdk.Context, amount sdk.Coins, senderModuleName string) error {
for i := range h {
if err := h[i].FundCommunityPoolFromModule(ctx, amount, senderModuleName); err != nil {
return err
}
}
return nil
}

func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) error {
for i := range h {
if err := h[i].BeforeValidatorSlashed(ctx, valAddr, fraction); err != nil {
Expand Down

0 comments on commit bf47b9e

Please sign in to comment.