forked from quicksilver-zone/quicksilver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add val denylist (quicksilver-zone#1268)
* initial work * refactor * remove new msg type * [wip]: add query type * add grpc query * add crud tests for val deny list * refactor deny list storage Now store validator address bytes instead of serialized form of the Validator struct * filter validator based on denylist * linting * add initial supply if user doesn't have balance * add comment * Add grpc test for query deny list * Update x/interchainstaking/keeper/intent_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * refactor based on review, return string instead of validator object, using sdk.ValAddress as argument * linting * refactor: remove unused marshal code --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
87683af
commit 47d8bc1
Showing
12 changed files
with
797 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/quicksilver-zone/quicksilver/utils/addressutils" | ||
"github.com/quicksilver-zone/quicksilver/x/interchainstaking/types" | ||
) | ||
|
||
// SetZoneValidatorToDenyList sets the zone validator deny list | ||
func (k *Keeper) SetZoneValidatorToDenyList(ctx sdk.Context, chainID string, validatorAddress sdk.ValAddress) error { | ||
store := ctx.KVStore(k.storeKey) | ||
|
||
key := types.GetDeniedValidatorKey(chainID, validatorAddress) | ||
store.Set(key, validatorAddress) | ||
return nil | ||
} | ||
|
||
// GetZoneValidatorDenyList get the validator deny list of a specific zone | ||
func (k *Keeper) GetZoneValidatorDenyList(ctx sdk.Context, chainID string) (denyList []string) { | ||
zone, found := k.GetZone(ctx, chainID) | ||
if !found { | ||
return denyList | ||
} | ||
k.IterateZoneDeniedValidator(ctx, chainID, func(validator sdk.ValAddress) bool { | ||
denyList = append(denyList, addressutils.MustEncodeAddressToBech32(zone.GetValoperPrefix(), validator)) | ||
return false | ||
}) | ||
|
||
return denyList | ||
} | ||
|
||
func (k *Keeper) GetDeniedValidatorInDenyList(ctx sdk.Context, chainID string, validatorAddress sdk.ValAddress) bool { | ||
key := types.GetDeniedValidatorKey(chainID, validatorAddress) | ||
store := ctx.KVStore(k.storeKey) | ||
bz := store.Get(key) | ||
return bz != nil | ||
} | ||
|
||
// RemoveValidatorFromDenyList removes a validator from the deny list. Panic if the validator is not in the deny list | ||
func (k *Keeper) RemoveValidatorFromDenyList(ctx sdk.Context, chainID string, validator sdk.ValAddress) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.GetDeniedValidatorKey(chainID, validator) | ||
store.Delete(key) | ||
} | ||
|
||
func (k *Keeper) IterateZoneDeniedValidator(ctx sdk.Context, chainID string, cb func(validator sdk.ValAddress) (stop bool)) { | ||
store := ctx.KVStore(k.storeKey) | ||
deniedValPrefixKey := types.GetZoneDeniedValidatorKey(chainID) | ||
|
||
iterator := sdk.KVStorePrefixIterator(store, deniedValPrefixKey) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
validator := sdk.ValAddress(iterator.Value()) | ||
if cb(validator) { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package keeper_test | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/quicksilver-zone/quicksilver/utils/addressutils" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestStoreGetDeleteDenyList() { | ||
suite.Run("deny list - store / get / delete single", func() { | ||
suite.SetupTest() | ||
suite.setupTestZones() | ||
|
||
qApp := suite.GetQuicksilverApp(suite.chainA) | ||
ctx := suite.chainA.GetContext() | ||
|
||
zone, found := qApp.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID) | ||
suite.True(found) | ||
vals := qApp.InterchainstakingKeeper.GetValidators(ctx, zone.ChainId) | ||
suite.Len(vals, 4) | ||
val := vals[0] | ||
validator := sdk.ValAddress(val.ValoperAddress) | ||
// Initially the deny list should be empty | ||
found = qApp.InterchainstakingKeeper.GetDeniedValidatorInDenyList(ctx, zone.ChainId, validator) | ||
suite.False(found) | ||
// Add a validator to the deny list | ||
err := qApp.InterchainstakingKeeper.SetZoneValidatorToDenyList(ctx, zone.ChainId, validator) | ||
suite.NoError(err) | ||
|
||
// Ensure the deny list contains the validator | ||
|
||
denyList := qApp.InterchainstakingKeeper.GetZoneValidatorDenyList(ctx, zone.ChainId) | ||
suite.Len(denyList, 1) | ||
suite.Equal(addressutils.MustEncodeAddressToBech32(zone.GetValoperPrefix(), validator), denyList[0]) | ||
|
||
found = qApp.InterchainstakingKeeper.GetDeniedValidatorInDenyList(ctx, zone.ChainId, validator) | ||
suite.True(found) | ||
|
||
// Remove the validator from the deny list | ||
qApp.InterchainstakingKeeper.RemoveValidatorFromDenyList(ctx, zone.ChainId, validator) | ||
found = qApp.InterchainstakingKeeper.GetDeniedValidatorInDenyList(ctx, zone.ChainId, validator) | ||
suite.False(found) | ||
|
||
denyList = qApp.InterchainstakingKeeper.GetZoneValidatorDenyList(ctx, zone.ChainId) | ||
suite.Len(denyList, 0) | ||
}) | ||
|
||
suite.Run("deny list - store / get / delete multiple", func() { | ||
suite.SetupTest() | ||
suite.setupTestZones() | ||
|
||
qApp := suite.GetQuicksilverApp(suite.chainA) | ||
ctx := suite.chainA.GetContext() | ||
|
||
zone, found := qApp.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID) | ||
suite.True(found) | ||
vals := qApp.InterchainstakingKeeper.GetValidators(ctx, zone.ChainId) | ||
suite.Len(vals, 4) | ||
valAddr := make([]sdk.ValAddress, 4) | ||
for i, v := range vals { | ||
valAddr[i] = sdk.ValAddress(v.ValoperAddress) | ||
} | ||
// Initially the deny list should be empty | ||
found = qApp.InterchainstakingKeeper.GetDeniedValidatorInDenyList(ctx, zone.ChainId, valAddr[0]) | ||
suite.False(found) | ||
|
||
// Add three validators to the deny list | ||
err := qApp.InterchainstakingKeeper.SetZoneValidatorToDenyList(ctx, zone.ChainId, valAddr[0]) | ||
suite.NoError(err) | ||
err = qApp.InterchainstakingKeeper.SetZoneValidatorToDenyList(ctx, zone.ChainId, valAddr[1]) | ||
suite.NoError(err) | ||
err = qApp.InterchainstakingKeeper.SetZoneValidatorToDenyList(ctx, zone.ChainId, valAddr[2]) | ||
suite.NoError(err) | ||
|
||
denyList := qApp.InterchainstakingKeeper.GetZoneValidatorDenyList(ctx, zone.ChainId) | ||
suite.Len(denyList, 3) | ||
|
||
// Ensure the deny list contains the three validators | ||
suite.Contains(denyList, addressutils.MustEncodeAddressToBech32(zone.GetValoperPrefix(), valAddr[0])) | ||
suite.Contains(denyList, addressutils.MustEncodeAddressToBech32(zone.GetValoperPrefix(), valAddr[1])) | ||
suite.Contains(denyList, addressutils.MustEncodeAddressToBech32(zone.GetValoperPrefix(), valAddr[2])) | ||
|
||
// Remove the validator from the deny list | ||
qApp.InterchainstakingKeeper.RemoveValidatorFromDenyList(ctx, zone.ChainId, valAddr[1]) | ||
found = qApp.InterchainstakingKeeper.GetDeniedValidatorInDenyList(ctx, zone.ChainId, valAddr[1]) | ||
suite.False(found) | ||
|
||
// Ensure the deny list contains the two remaining validators | ||
denyList = qApp.InterchainstakingKeeper.GetZoneValidatorDenyList(ctx, zone.ChainId) | ||
suite.Len(denyList, 2) | ||
suite.NotContains(denyList, addressutils.MustEncodeAddressToBech32(zone.GetValoperPrefix(), valAddr[1])) | ||
suite.Contains(denyList, addressutils.MustEncodeAddressToBech32(zone.GetValoperPrefix(), valAddr[0])) | ||
suite.Contains(denyList, addressutils.MustEncodeAddressToBech32(zone.GetValoperPrefix(), valAddr[2])) | ||
|
||
// Remove the remaining two validators from the deny list | ||
qApp.InterchainstakingKeeper.RemoveValidatorFromDenyList(ctx, zone.ChainId, valAddr[0]) | ||
qApp.InterchainstakingKeeper.RemoveValidatorFromDenyList(ctx, zone.ChainId, valAddr[2]) | ||
|
||
// Ensure the deny list is empty | ||
denyList = qApp.InterchainstakingKeeper.GetZoneValidatorDenyList(ctx, zone.ChainId) | ||
suite.Len(denyList, 0) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package types_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.