-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Module Wired up and created MsgSetValidatorSetPreference
- Loading branch information
1 parent
76a99c7
commit 634f626
Showing
17 changed files
with
1,133 additions
and
22 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
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,16 @@ | ||
syntax = "proto3"; | ||
package osmosis.validatorpreference.v1beta1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "osmosis/validator-preference/v1beta1/state.proto"; | ||
|
||
option go_package = "github.com/osmosis-labs/osmosis/v12/x/validator-preference/types"; | ||
|
||
// Params defines the parameters for the module. | ||
message Params { | ||
// the users validator-set, {valAddr, weight} to unstake the tokens from. | ||
repeated ValidatorPreference preferences = 1 [ | ||
(gogoproto.moretags) = "yaml:\"preferences\"", | ||
(gogoproto.nullable) = false | ||
]; | ||
} |
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,45 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"github.com/osmosis-labs/osmosis/v12/x/validator-preference/types" | ||
) | ||
|
||
// ValidateValidator checks if the validator address is valid and the validator provided exists onchain. | ||
func (k Keeper) ValidateValidator(ctx sdk.Context, valOperAddress string) (sdk.ValAddress, stakingtypes.Validator, error) { | ||
valAddr, err := sdk.ValAddressFromBech32(valOperAddress) | ||
if err != nil { | ||
return nil, stakingtypes.Validator{}, fmt.Errorf("validator address not formatted") | ||
} | ||
|
||
validator, found := k.stakingKeeper.GetValidator(ctx, valAddr) | ||
if !found { | ||
return nil, stakingtypes.Validator{}, fmt.Errorf("validator not found %s", validator) | ||
} | ||
|
||
return valAddr, validator, nil | ||
} | ||
|
||
// ValidatePreferences checks if the sum of the validator set equals 1. | ||
func (k Keeper) ValidatePreferences(ctx sdk.Context, preferences []types.ValidatorPreference) error { | ||
total_weight := sdk.NewDec(0) | ||
for _, val := range preferences { | ||
// validation to check that the validator given is valid | ||
_, _, err := k.ValidateValidator(ctx, val.ValOperAddress) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
total_weight = total_weight.Add(val.Weight) | ||
} | ||
|
||
// check if the total validator distribution weights equal 1 | ||
if !total_weight.Equal(sdk.NewDec(1)) { | ||
return fmt.Errorf("The weights allocated to the validators do not add up to 1, %d", total_weight) | ||
} | ||
|
||
return nil | ||
} |
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,57 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tendermint/tendermint/libs/log" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/osmosis-labs/osmosis/v12/osmoutils" | ||
"github.com/osmosis-labs/osmosis/v12/x/validator-preference/types" | ||
) | ||
|
||
type Keeper struct { | ||
storeKey sdk.StoreKey | ||
paramSpace paramtypes.Subspace | ||
stakingKeeper types.StakingInterface | ||
} | ||
|
||
func NewKeeper(storeKey sdk.StoreKey, paramSpace paramtypes.Subspace, stakingKeeper types.StakingInterface) Keeper { | ||
if !paramSpace.HasKeyTable() { | ||
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable()) | ||
} | ||
|
||
return Keeper{ | ||
storeKey: storeKey, | ||
paramSpace: paramSpace, | ||
stakingKeeper: stakingKeeper, | ||
} | ||
} | ||
|
||
func (k Keeper) Logger(ctx sdk.Context) log.Logger { | ||
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) | ||
} | ||
|
||
func (k Keeper) SetValidatorSetPreferences(ctx sdk.Context, delegator string, validators types.ValidatorSetPreferences) { | ||
store := ctx.KVStore(k.storeKey) | ||
osmoutils.MustSet(store, []byte(delegator), &validators) | ||
} | ||
|
||
func (k Keeper) GetValidatorSetPreference(ctx sdk.Context, delegator string) (types.ValidatorSetPreferences, bool) { | ||
validatorSet := types.ValidatorSetPreferences{} | ||
|
||
store := ctx.KVStore(k.storeKey) | ||
b := store.Get([]byte(delegator)) | ||
if b == nil { | ||
return types.ValidatorSetPreferences{}, false | ||
} | ||
|
||
err := proto.Unmarshal(b, &validatorSet) | ||
if err != nil { | ||
return types.ValidatorSetPreferences{}, false | ||
} | ||
|
||
return validatorSet, true | ||
} |
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,67 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"github.com/osmosis-labs/osmosis/v12/app/apptesting" | ||
"github.com/osmosis-labs/osmosis/v12/x/validator-preference/types" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type KeeperTestSuite struct { | ||
apptesting.KeeperTestHelper | ||
|
||
cleanup func() | ||
} | ||
|
||
func (suite *KeeperTestSuite) SetupTest() { | ||
suite.Setup() | ||
} | ||
|
||
func (suite *KeeperTestSuite) Cleanup() { | ||
suite.cleanup() | ||
} | ||
|
||
func (suite *KeeperTestSuite) SetupValidators(bondStatuses []stakingtypes.BondStatus) []sdk.ValAddress { | ||
valAddrs := []sdk.ValAddress{} | ||
for _, status := range bondStatuses { | ||
valAddr := suite.SetupValidator(status) | ||
valAddrs = append(valAddrs, valAddr) | ||
} | ||
return valAddrs | ||
} | ||
|
||
// SetupMultipleValidators setups "numValidator" validators and returns their address in string | ||
func (suite *KeeperTestSuite) SetupMultipleValidators(numValidator int) []string { | ||
valAddrs := []string{} | ||
for i := 0; i < numValidator; i++ { | ||
valAddr := suite.SetupValidators([]stakingtypes.BondStatus{stakingtypes.Bonded}) | ||
valAddrs = append(valAddrs, valAddr[0].String()) | ||
} | ||
return valAddrs | ||
} | ||
|
||
func (suite *KeeperTestSuite) PrepareDelegateToValidatorSet() []types.ValidatorPreference { | ||
valAddrs := suite.SetupMultipleValidators(3) | ||
valPreferences := []types.ValidatorPreference{ | ||
{ | ||
ValOperAddress: valAddrs[0], | ||
Weight: sdk.NewDecWithPrec(5, 1), | ||
}, | ||
{ | ||
ValOperAddress: valAddrs[1], | ||
Weight: sdk.NewDecWithPrec(3, 1), | ||
}, | ||
{ | ||
ValOperAddress: valAddrs[2], | ||
Weight: sdk.NewDecWithPrec(2, 1), | ||
}, | ||
} | ||
return valPreferences | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(KeeperTestSuite)) | ||
} |
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 ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/osmosis-labs/osmosis/v12/x/validator-preference/types" | ||
) | ||
|
||
type msgServer struct { | ||
keeper *Keeper | ||
} | ||
|
||
// NewMsgServerImpl returns an implementation of the MsgServer interface | ||
// for the provided Keeper. | ||
func NewMsgServerImpl(keeper *Keeper) types.MsgServer { | ||
return &msgServer{ | ||
keeper: keeper, | ||
} | ||
} | ||
|
||
var _ types.MsgServer = msgServer{} | ||
|
||
func (server msgServer) SetValidatorSetPreference(goCtx context.Context, msg *types.MsgSetValidatorSetPreference) (*types.MsgSetValidatorSetPreferenceResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
preferences := msg.Preferences | ||
|
||
// check if a user already have a validator-set created | ||
existingValidator, found := server.keeper.GetValidatorSetPreference(ctx, msg.Delegator) | ||
if found { | ||
preferences = existingValidator.Preferences | ||
} | ||
|
||
// check if the distribution weights equals 1 | ||
err := server.keeper.ValidatePreferences(ctx, preferences) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// update the validator-set based on what user provides | ||
setMsg := types.ValidatorSetPreferences{ | ||
Preferences: msg.Preferences, | ||
} | ||
|
||
server.keeper.SetValidatorSetPreferences(ctx, msg.Delegator, setMsg) | ||
return &types.MsgSetValidatorSetPreferenceResponse{}, nil | ||
} | ||
|
||
func (server msgServer) DelegateToValidatorSet(goCtx context.Context, msg *types.MsgDelegateToValidatorSet) (*types.MsgDelegateToValidatorSetResponse, error) { | ||
return &types.MsgDelegateToValidatorSetResponse{}, nil | ||
} | ||
|
||
func (server msgServer) UndelegateFromValidatorSet(goCtx context.Context, msg *types.MsgUndelegateFromValidatorSet) (*types.MsgUndelegateFromValidatorSetResponse, error) { | ||
return &types.MsgUndelegateFromValidatorSetResponse{}, nil | ||
} | ||
|
||
func (server msgServer) WithdrawDelegationRewards(goCtx context.Context, msg *types.MsgWithdrawDelegationRewards) (*types.MsgWithdrawDelegationRewardsResponse, error) { | ||
return &types.MsgWithdrawDelegationRewardsResponse{}, nil | ||
} |
Oops, something went wrong.