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: dust share #109

Merged
merged 2 commits into from
Jan 8, 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
10 changes: 10 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"github.com/xpladev/xpla/docs"

evmupgrade "github.com/xpladev/xpla/app/upgrades/evm"
"github.com/xpladev/xpla/app/upgrades/v1_4"
"github.com/xpladev/xpla/app/upgrades/volunteer"
xplareward "github.com/xpladev/xpla/app/upgrades/xpla_reward"
)
Expand Down Expand Up @@ -422,6 +423,12 @@ func (app *XplaApp) setUpgradeHandlers() {
volunteer.CreateUpgradeHandler(app.mm, app.configurator, &app.AppKeepers),
)

// v1_4 upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(
v1_4.UpgradeName,
v1_4.CreateUpgradeHandler(app.mm, app.configurator, &app.AppKeepers.StakingKeeper),
)

// When a planned update height is reached, the old binary will panic
// writing on disk the height and name of the update that triggered it
// This will read that value, and execute the preparations for the upgrade.
Expand Down Expand Up @@ -449,6 +456,9 @@ func (app *XplaApp) setUpgradeHandlers() {
storeUpgrades = &storetypes.StoreUpgrades{
Added: volunteer.AddModules,
}
case v1_4.UpgradeName:
storeUpgrades = &storetypes.StoreUpgrades{}

}

if storeUpgrades != nil {
Expand Down
5 changes: 5 additions & 0 deletions app/upgrades/v1_4/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package v1_4

const (
UpgradeName = "v1_4"
)
41 changes: 41 additions & 0 deletions app/upgrades/v1_4/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package v1_4

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

stakingkeeper "github.com/xpladev/xpla/x/staking/keeper"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
stakingKeeper *stakingkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {

validators := stakingKeeper.GetAllValidators(ctx)
for _, validator := range validators {
tolerance, err := validator.SharesFromTokens(sdk.OneInt())
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrLogic, "validator must have valid share")
}

delegations := stakingKeeper.GetValidatorDelegations(ctx, validator.GetOperator())
for _, delegation := range delegations {
if delegation.Shares.GTE(tolerance) {
continue
}

_, err := stakingKeeper.Unbond(ctx, delegation.GetDelegatorAddr(), validator.GetOperator(), delegation.GetShares())
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrLogic, "dust delegation must be unbond")
}
}
}

return mm.RunMigrations(ctx, configurator, fromVM)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ replace (
// TODO: Simapp dependency, review removing when updating to SDK with backported update https://github.com/cosmos/cosmos-sdk/issues/13423
github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.22.2 // indirect

github.com/cosmos/cosmos-sdk => github.com/xpladev/cosmos-sdk v0.45.19-xpla
github.com/cosmos/cosmos-sdk => github.com/xpladev/cosmos-sdk v0.45.20-xpla

// enforce same SDK and IBC on all dependencies
github.com/cosmos/ibc-go/v4 => github.com/cosmos/ibc-go/v4 v4.5.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,8 @@ github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQ
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/xpladev/cosmos-sdk v0.45.19-xpla h1:d7ZV4ioj+OfHiVsQZvWlyHoPmvVez/7mI0kfDyUc+wo=
github.com/xpladev/cosmos-sdk v0.45.19-xpla/go.mod h1:3MSZtH7mYNdRrybrpw8aJi4lnjQPJgg7Suw9K4fUh/w=
github.com/xpladev/cosmos-sdk v0.45.20-xpla h1:BkogYrm3LgFqbCvJsHI6W9dWPx5YIvLIVSccpgVaXd0=
github.com/xpladev/cosmos-sdk v0.45.20-xpla/go.mod h1:3MSZtH7mYNdRrybrpw8aJi4lnjQPJgg7Suw9K4fUh/w=
github.com/xpladev/ethermint v0.19.4-xpla h1:F7SdxazMYXhVz37u4XyyXa4nGZ0ad/g6uifZX+quAvk=
github.com/xpladev/ethermint v0.19.4-xpla/go.mod h1:yVEHdlFKDd/ZOfkR30ZCK0yyQ3KufwmTTwvk1IYz6qY=
github.com/xpladev/evmos/v9 v9.2.0-xpla h1:UwuEqik0RyEsxMJzlrbE9u+hfmnY1VZXQMfprJebI3U=
Expand Down
53 changes: 53 additions & 0 deletions tests/integration/staking/staking_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package staking_test

import (
"testing"

"github.com/stretchr/testify/assert"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/xpladev/xpla/tests/integration/testutil"
"github.com/xpladev/xpla/x/staking"
)

func TestDustShare(t *testing.T) {
input := testutil.CreateTestInput(t)

sdk.DefaultPowerReduction = sdk.NewInt(1)
for i := 0; i < 2; i++ {
err := input.InitAccountWithCoins(sdk.AccAddress(testutil.Pks[i].Address()), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))))
assert.NoError(t, err)
}

// 1 validator & 100 self delegation
_, err := input.StakingHandler(
input.Ctx,
testutil.NewMsgCreateValidator(
sdk.ValAddress(testutil.Pks[0].Address()),
testutil.Pks[0],
sdk.NewInt(100)))
assert.NoError(t, err)

staking.EndBlocker(input.Ctx, input.StakingKeeper)
input.Ctx = input.Ctx.WithBlockHeight(1)

// slash for dust share
input.SlashingKeeper.Slash(input.Ctx, sdk.ConsAddress(testutil.Pks[0].Address()), sdk.NewDecWithPrec(1, 2), 100, 1)

// new 1stake delegator
_, err = input.StakingHandler(
input.Ctx,
testutil.NewMsgDelegate(sdk.AccAddress(testutil.Pks[1].Address()), sdk.ValAddress(testutil.Pks[0].Address()), sdk.NewInt(1)),
)

assert.NoError(t, err)

// try to remove all delegation
_, err = input.StakingHandler(input.Ctx, stakingtypes.NewMsgUndelegate(sdk.AccAddress(testutil.Pks[0].Address()), sdk.ValAddress(testutil.Pks[0].Address()), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(99))))
assert.NoError(t, err)

delegations := input.StakingKeeper.GetValidatorDelegations(input.Ctx, sdk.ValAddress(testutil.Pks[0].Address()))
assert.Equal(t, 1, len(delegations))
assert.Equal(t, sdk.AccAddress(testutil.Pks[1].Address()).String(), delegations[0].DelegatorAddress)
}