Skip to content

Commit

Permalink
add RemovePerformanceDelegation and change logic for RemoveZoneAndAss…
Browse files Browse the repository at this point in the history
…ociated… (quicksilver-zone#758)

* add RemovePertDelegation and change logic for RemoveZoneAndAssociatedRecords

* add test

* chore: add more checking

---------

Co-authored-by: ThanhNhann <[email protected]>
  • Loading branch information
DongLieu and ThanhNhann authored Nov 1, 2023
1 parent e2b8d51 commit 1399c5e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
9 changes: 9 additions & 0 deletions x/interchainstaking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ func (k *Keeper) RemoveDelegation(ctx sdk.Context, chainID string, delegation ty
return nil
}

// RemovePerformanceDelegation removes a performance delegation.
func (k *Keeper) RemovePerformanceDelegation(ctx sdk.Context, chainID string, delegation types.Delegation) error {
delegatorAddress := delegation.GetDelegatorAddr()

store := ctx.KVStore(k.storeKey)
store.Delete(types.GetPerformanceDelegationKey(chainID, delegatorAddress, delegation.GetValidatorAddr()))
return nil
}

// IterateDelegatorDelegations iterates through one delegator's delegations.
func (k *Keeper) IterateDelegatorDelegations(ctx sdk.Context, chainID string, delegator sdk.AccAddress, cb func(delegation types.Delegation) (stop bool)) {
store := ctx.KVStore(k.storeKey)
Expand Down
65 changes: 65 additions & 0 deletions x/interchainstaking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkmath "cosmossdk.io/math"

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

"github.com/quicksilver-zone/quicksilver/app"
"github.com/quicksilver-zone/quicksilver/utils/addressutils"
Expand Down Expand Up @@ -630,6 +631,70 @@ func (suite *KeeperTestSuite) TestFlushOutstandingDelegations() {
}
}

func (suite *KeeperTestSuite) TestPerformanceDelegation() {
quicksilver := suite.GetQuicksilverApp(suite.chainA)
ctx := suite.chainA.GetContext()
chainID := "quicksilver-1"

// Set zone
quicksilver.InterchainstakingKeeper.SetZone(ctx, &icstypes.Zone{
ConnectionId: "connection-test",
ChainId: chainID,
LocalDenom: "uqck",
BaseDenom: "qck",
DelegationAddress: &icstypes.ICAAccount{
Address: addressutils.GenerateAddressForTestWithPrefix("quicksilver"),
},
PerformanceAddress: &icstypes.ICAAccount{
Address: addressutils.GenerateAddressForTestWithPrefix("quicksilver"),
},
})
// Check set zone
zone, ok := quicksilver.InterchainstakingKeeper.GetZone(ctx, chainID)
suite.True(ok, "expected to retrieve a zone")
suite.NotEqual(icstypes.Zone{}, zone, "Expecting a non-blank zone")

// set val
val0 := icstypes.Validator{ValoperAddress: "cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0", CommissionRate: sdk.MustNewDecFromStr("1"), VotingPower: sdk.NewInt(2000), Status: stakingtypes.BondStatusBonded}
err := quicksilver.InterchainstakingKeeper.SetValidator(ctx, zone.ChainId, val0)
suite.NoError(err)

val1 := icstypes.Validator{ValoperAddress: "cosmosvaloper156gqf9837u7d4c4678yt3rl4ls9c5vuursrrzf", CommissionRate: sdk.MustNewDecFromStr("1"), VotingPower: sdk.NewInt(2000), Status: stakingtypes.BondStatusBonded}
err = quicksilver.InterchainstakingKeeper.SetValidator(ctx, zone.ChainId, val1)
suite.NoError(err)

val2 := icstypes.Validator{ValoperAddress: "cosmosvaloper14lultfckehtszvzw4ehu0apvsr77afvyju5zzy", CommissionRate: sdk.MustNewDecFromStr("1"), VotingPower: sdk.NewInt(2000), Status: stakingtypes.BondStatusBonded}
err = quicksilver.InterchainstakingKeeper.SetValidator(ctx, zone.ChainId, val2)
suite.NoError(err)

val3 := icstypes.Validator{ValoperAddress: "cosmosvaloper1z8zjv3lntpwxua0rtpvgrcwl0nm0tltgpgs6l7", CommissionRate: sdk.MustNewDecFromStr("1"), VotingPower: sdk.NewInt(2000), Status: stakingtypes.BondStatusBonded}
err = quicksilver.InterchainstakingKeeper.SetValidator(ctx, zone.ChainId, val3)
suite.NoError(err)

vals := quicksilver.InterchainstakingKeeper.GetValidators(ctx, chainID)

// create perf delegation
performanceAddress := zone.PerformanceAddress
perfDelegation := icstypes.Delegation{
DelegationAddress: performanceAddress.Address,
ValidatorAddress: vals[1].ValoperAddress,
Amount: sdk.NewCoin(zone.BaseDenom, sdk.NewInt(1000)),
}

// set and check get perf delegation
quicksilver.InterchainstakingKeeper.SetPerformanceDelegation(ctx, zone.ChainId, perfDelegation)
_, found := quicksilver.InterchainstakingKeeper.GetPerformanceDelegation(ctx, chainID, performanceAddress, perfDelegation.ValidatorAddress)
suite.True(found)

// Handle remove perf delegation
err = quicksilver.InterchainstakingKeeper.RemovePerformanceDelegation(ctx, chainID, perfDelegation)
suite.NoError(err)

// check perf delegation
_, found = quicksilver.InterchainstakingKeeper.GetPerformanceDelegation(ctx, chainID, performanceAddress, perfDelegation.ValidatorAddress)
suite.False(found, "Not found pert delegation stored in the keeper")
}

func (suite *KeeperTestSuite) TestDelegationPlan() {
jsonDelegations, _ := base64.RawStdEncoding.DecodeString(delegationFixture)
quicksilver := suite.GetQuicksilverApp(suite.chainA)
Expand Down
2 changes: 1 addition & 1 deletion x/interchainstaking/keeper/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (k *Keeper) RemoveZoneAndAssociatedRecords(ctx sdk.Context, chainID string)

// remove performance delegation records
k.IterateAllPerformanceDelegations(ctx, chainID, func(delegation types.Delegation) (stop bool) {
err := k.RemoveDelegation(ctx, chainID, delegation)
err := k.RemovePerformanceDelegation(ctx, chainID, delegation)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 1399c5e

Please sign in to comment.