Skip to content

Commit

Permalink
EndBlocker to burn NotBonded tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Feb 22, 2024
1 parent 1855e2a commit 26c2814
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 30 deletions.
30 changes: 20 additions & 10 deletions keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ func (k Keeper) UpdateBondedPoolPower(ctx context.Context) error {
return err
}

addr := authtypes.NewModuleAddress(stakingtypes.BondedPoolName)

prevBal := k.bankKeeper.GetBalance(ctx, addr, bondDenom).Amount
prevBal := k.bankKeeper.GetBalance(ctx, authtypes.NewModuleAddress(stakingtypes.BondedPoolName), bondDenom).Amount

if newTotal.Equal(prevBal) {
return nil
Expand All @@ -175,15 +173,27 @@ func (k Keeper) UpdateBondedPoolPower(ctx context.Context) error {
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, stakingtypes.BondedPoolName, coins); err != nil {
return err
}

} else {
diff := prevBal.Sub(newTotal)
coins := sdk.NewCoins(sdk.NewCoin(bondDenom, diff))
fmt.Println("Burning coins from the bonded pool", coins)

if err := k.bankKeeper.BurnCoins(ctx, stakingtypes.BondedPoolName, coins); err != nil {
return err
}
other := k.bankKeeper.GetBalance(ctx, authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName), bondDenom).Amount
fmt.Println("other", other)

Check failure on line 179 in keeper/keeper.go

View workflow job for this annotation

GitHub Actions / golangci-lint

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

// diff := prevBal.Sub(newTotal)
// fmt.Println("prevBal", prevBal, "newTotal", newTotal, "diff", diff)
// coins := sdk.NewCoins(sdk.NewCoin(bondDenom, diff))
// fmt.Println("Burning coins from the bonded pool", coins)

// if err := k.bankKeeper.BurnCoins(ctx, stakingtypes.BondedPoolName, coins); err != nil {
// return err
// }

// if err := k.bankKeeper.MintCoins(ctx, minttypes.ModuleName, coins); err != nil {
// return err
// }

// if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, stakingtypes.BondedPoolName, coins); err != nil {
// return err
// }
}

return nil
Expand Down
45 changes: 25 additions & 20 deletions keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

// minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
// "github.com/cosmos/cosmos-sdk/x/staking/types"
sdkmath "cosmossdk.io/math"

Check failure on line 15 in keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ST1019: package "cosmossdk.io/math" is being imported more than once (stylecheck)
Expand Down Expand Up @@ -336,23 +339,23 @@ func TestRemoveValidator(t *testing.T) {
expectErrMsg: poa.ErrValidatorSelfRemoval.Error(),
isSelfRemovalAllowed: false,
},
// {
// name: "remove validator as itself",
// request: &poa.MsgRemoveValidator{
// Sender: sdk.AccAddress(MustValAddressFromBech32(vals[1].OperatorAddress)).String(),
// ValidatorAddress: vals[1].OperatorAddress,
// },
// isSelfRemovalAllowed: true,
// },
// {
// name: "fail; try again (no longer exist)",
// request: &poa.MsgRemoveValidator{
// Sender: sdk.AccAddress(MustValAddressFromBech32(vals[1].OperatorAddress)).String(),
// ValidatorAddress: vals[1].OperatorAddress,
// },
// expectErrMsg: "is not bonded",
// isSelfRemovalAllowed: true,
// },
{
name: "success; remove validator as itself",
request: &poa.MsgRemoveValidator{
Sender: sdk.AccAddress(MustValAddressFromBech32(vals[1].OperatorAddress)).String(),
ValidatorAddress: vals[1].OperatorAddress,
},
isSelfRemovalAllowed: true,
},
{
name: "fail; try again (no longer exist)",
request: &poa.MsgRemoveValidator{
Sender: sdk.AccAddress(MustValAddressFromBech32(vals[1].OperatorAddress)).String(),
ValidatorAddress: vals[1].OperatorAddress,
},
expectErrMsg: "is not bonded",
isSelfRemovalAllowed: true,
},
}

for _, tc := range testCases {
Expand All @@ -367,6 +370,9 @@ func TestRemoveValidator(t *testing.T) {

_, err = f.msgServer.RemoveValidator(f.ctx, tc.request)

other := f.bankkeeper.GetBalance(f.ctx, authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName), "stake").Amount
fmt.Println("other1", other)

Check failure on line 374 in keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

if tc.expectErrMsg != "" {
require.Error(err)
require.ErrorContains(err, tc.expectErrMsg)
Expand All @@ -381,13 +387,12 @@ func TestRemoveValidator(t *testing.T) {
require.NoError(err)
fmt.Println("total bonded tokens", amt)

// difference

// BondedRatio
bondRatio, err := f.stakingKeeper.BondedRatio(f.ctx)
require.NoError(err)
fmt.Println("bonded ratio", bondRatio)
require.EqualValues(bondRatio, sdkmath.LegacyOneDec())
require.True(bondRatio.GTE(sdkmath.LegacyNewDecWithPrec(6666, 18)))
// This will be handled in the next endblock
})
}
}
Expand Down
25 changes: 25 additions & 0 deletions module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/strangelove-ventures/poa"
Expand Down Expand Up @@ -60,6 +61,30 @@ func (am AppModule) BeginBlocker(ctx context.Context) error {
return nil
}

func (am AppModule) EndBlocker(ctx context.Context) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
defer telemetry.ModuleMeasureSince(poa.ModuleName, sdkCtx.BlockTime(), telemetry.MetricKeyEndBlocker)

bondDenom, err := am.keeper.GetStakingKeeper().BondDenom(ctx)
if err != nil {
return err
}

notBondedAcc := authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName)

bal := am.keeper.GetBankKeeper().
GetBalance(ctx, notBondedAcc, bondDenom).Amount

if bal.IsPositive() {
coins := sdk.NewCoins(sdk.NewCoin(bondDenom, bal))
if err := am.keeper.GetBankKeeper().BurnCoins(ctx, stakingtypes.NotBondedPoolName, coins); err != nil {
return nil

Check failure on line 81 in module/abci.go

View workflow job for this annotation

GitHub Actions / golangci-lint

error is not nil (line 80) but it returns nil (nilerr)
}
}

return nil
}

// resetCachedTotalPower resets the block power index to the current total power.
func (am AppModule) resetCachedTotalPower(ctx context.Context) error {
currValPower, err := am.keeper.GetStakingKeeper().GetLastTotalPower(ctx)
Expand Down
5 changes: 5 additions & 0 deletions module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
_ module.AppModule = AppModule{}
_ module.AppModuleGenesis = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
)

// ConsensusVersion defines the current module consensus version.
Expand Down Expand Up @@ -132,3 +133,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (am AppModule) BeginBlock(ctx context.Context) error {
return am.BeginBlocker(ctx)
}

func (am AppModule) EndBlock(ctx context.Context) error {
return am.EndBlocker(ctx)
}

0 comments on commit 26c2814

Please sign in to comment.