Skip to content

Commit

Permalink
add: add straths suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
fragwuerdig committed Sep 26, 2023
1 parent 201f0a0 commit 940f53f
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 124 deletions.
5 changes: 3 additions & 2 deletions proto/terra/dyncomm/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ option go_package = "github.com/classic-terra/core/v2/x/dyncomm/types";
message GenesisState {
// params defines all the paramaters of the module.
Params params = 1 [(gogoproto.nullable) = false];
repeated MinCommissionRate min_commission_rates = 2 [(gogoproto.nullable) = false];
repeated ValidatorCommissionRate validator_commission_rates = 2 [(gogoproto.nullable) = false];
}

// MinDynCommission defines a validator - min commission rate
// pair to be enforced by the blockchain
message MinCommissionRate {
message ValidatorCommissionRate {
string validator_address = 1;
string min_commission_rate = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"];
string target_commission_rate = 3 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"];
}
1 change: 1 addition & 0 deletions proto/terra/dyncomm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ message QueryRateRequest {
// QueryRateResponse is the response type for the Query/Rate RPC method.
message QueryRateResponse {
string rate = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"];
string target = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"];
}
39 changes: 28 additions & 11 deletions x/dyncomm/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ func NewDyncommDecorator(dk dyncommkeeper.Keeper, sk stakingkeeper.Keeper) Dynco
}
}

// IsMsgSubmitProposal checks whether the input msg is a MsgSubmitProposal
func IsMsgEditValidator(msg sdk.Msg) bool {
_, ok := msg.(*stakingtypes.MsgEditValidator)
return ok
}

func (dd DyncommDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
if simulate {
return next(ctx, tx, simulate)
}

msgs := tx.GetMsgs()
err := dd.FilterMsgsAndCheckEditValidator(ctx, msgs...)
err := dd.FilterMsgsAndProcessMsgs(ctx, msgs...)

if err != nil {
return ctx, err
Expand All @@ -46,23 +40,29 @@ func (dd DyncommDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,

}

func (dd DyncommDecorator) FilterMsgsAndCheckEditValidator(ctx sdk.Context, msgs ...sdk.Msg) (err error) {
func (dd DyncommDecorator) FilterMsgsAndProcessMsgs(ctx sdk.Context, msgs ...sdk.Msg) (err error) {

for _, msg := range msgs {
if !IsMsgEditValidator(msg) {

switch msg.(type) {
case *stakingtypes.MsgEditValidator:
err = dd.ProcessEditValidator(ctx, msg)
case *stakingtypes.MsgCreateValidator:
err = dd.ProcessCreateValidator(ctx, msg)
default:
continue
}

err := dd.CheckEditValidator(ctx, msg)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, err.Error())
}

}
return nil

}

func (dd DyncommDecorator) CheckEditValidator(ctx sdk.Context, msg sdk.Msg) (err error) {
func (dd DyncommDecorator) ProcessEditValidator(ctx sdk.Context, msg sdk.Msg) (err error) {

msgEditValidator := msg.(*stakingtypes.MsgEditValidator)

Expand All @@ -79,6 +79,23 @@ func (dd DyncommDecorator) CheckEditValidator(ctx sdk.Context, msg sdk.Msg) (err
return fmt.Errorf("commission for %s must be at least %f", operator, dynMinRate.MustFloat64())
}

// set new target rate from intendet
dd.dyncommKeeper.SetTargetCommissionRate(ctx, msgEditValidator.ValidatorAddress, *newIntendedRate)

return nil

}

func (dd DyncommDecorator) ProcessCreateValidator(ctx sdk.Context, msg sdk.Msg) (err error) {

msgEditValidator := msg.(*stakingtypes.MsgCreateValidator)
newIntendedRate := msgEditValidator.Commission.Rate

// we don't know yet what the validators VP
// is gonna be. So let's just set the intended
// target rate here
dd.dyncommKeeper.SetTargetCommissionRate(ctx, msgEditValidator.ValidatorAddress, newIntendedRate)

return nil

}
15 changes: 12 additions & 3 deletions x/dyncomm/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import (

"github.com/classic-terra/core/v2/x/dyncomm/keeper"
"github.com/classic-terra/core/v2/x/dyncomm/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// InitGenesis initializes default parameters
// and the keeper's address to pubkey map
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState) {
keeper.SetParams(ctx, data.Params)

//iterate validators and set target rates
keeper.StakingKeeper.IterateValidators(ctx, func(index int64, validator stakingtypes.ValidatorI) (stop bool) {
val := validator.(stakingtypes.Validator)
keeper.SetTargetCommissionRate(ctx, val.OperatorAddress, val.Commission.Rate)
return false
})

err := keeper.UpdateAllBondedValidatorRates(ctx)
if err != nil {
panic("could not initialize genesis")
Expand All @@ -23,10 +32,10 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState
// with InitGenesis
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) (data *types.GenesisState) {
params := keeper.GetParams(ctx)
var rates []types.MinCommissionRate
var rates []types.ValidatorCommissionRate

rates = append(rates)
keeper.IterateDynCommissionRates(ctx, func(rate types.MinCommissionRate) (stop bool) {
//rates = append(rates)
keeper.IterateDynCommissionRates(ctx, func(rate types.ValidatorCommissionRate) (stop bool) {
rates = append(rates, rate)
return false
})
Expand Down
83 changes: 70 additions & 13 deletions x/dyncomm/keeper/dyncomm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,42 @@ func (k Keeper) CalculateDynCommission(ctx sdk.Context, validator stakingtypes.V
}

func (k Keeper) SetDynCommissionRate(ctx sdk.Context, validator string, rate sdk.Dec) {

var preSetRate types.ValidatorCommissionRate
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(
&types.MinCommissionRate{
ValidatorAddress: validator,
MinCommissionRate: &rate,
bz := store.Get(types.GetMinCommissionRatesKey(validator))
targetRate := sdk.ZeroDec()

if bz != nil {
k.cdc.MustUnmarshal(bz, &preSetRate)
targetRate = *preSetRate.TargetCommissionRate
}
bz = k.cdc.MustMarshal(
&types.ValidatorCommissionRate{
ValidatorAddress: validator,
MinCommissionRate: &rate,
TargetCommissionRate: &targetRate,
},
)
store.Set(types.GetMinCommissionRatesKey(validator), bz)
}

func (k Keeper) SetTargetCommissionRate(ctx sdk.Context, validator string, rate sdk.Dec) {

var preSetRate types.ValidatorCommissionRate
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMinCommissionRatesKey(validator))
minRate := sdk.ZeroDec()

if bz != nil {
k.cdc.MustUnmarshal(bz, &preSetRate)
minRate = *preSetRate.MinCommissionRate
}
bz = k.cdc.MustMarshal(
&types.ValidatorCommissionRate{
ValidatorAddress: validator,
MinCommissionRate: &minRate,
TargetCommissionRate: &rate,
},
)
store.Set(types.GetMinCommissionRatesKey(validator), bz)
Expand All @@ -63,19 +94,31 @@ func (k Keeper) GetDynCommissionRate(ctx sdk.Context, validator string) (rate sd
return sdk.ZeroDec()
}

var validatorRate types.MinCommissionRate
var validatorRate types.ValidatorCommissionRate
k.cdc.MustUnmarshal(bz, &validatorRate)
return *validatorRate.MinCommissionRate
}

func (k Keeper) GetTargetCommissionRate(ctx sdk.Context, validator string) (rate sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMinCommissionRatesKey(validator))
if bz == nil {
return sdk.ZeroDec()
}

var validatorRate types.ValidatorCommissionRate
k.cdc.MustUnmarshal(bz, &validatorRate)
return *validatorRate.TargetCommissionRate
}

// IterateDynCommissionRates iterates over dyn commission rates in the store
func (k Keeper) IterateDynCommissionRates(ctx sdk.Context, cb func(types.MinCommissionRate) bool) {
func (k Keeper) IterateDynCommissionRates(ctx sdk.Context, cb func(types.ValidatorCommissionRate) bool) {
store := ctx.KVStore(k.storeKey)
it := store.Iterator(nil, nil)
defer it.Close()

for ; it.Valid(); it.Next() {
var entry types.MinCommissionRate
var entry types.ValidatorCommissionRate
if err := entry.Unmarshal(it.Value()); err != nil {
panic(err)
}
Expand All @@ -86,13 +129,24 @@ func (k Keeper) IterateDynCommissionRates(ctx sdk.Context, cb func(types.MinComm
}
}

func (k Keeper) UpdateValidatorRates(ctx sdk.Context, validator stakingtypes.Validator) {
func (k Keeper) UpdateValidatorMinRates(ctx sdk.Context, validator stakingtypes.Validator) {

newRate := k.CalculateDynCommission(ctx, validator)
var newRate sdk.Dec
minRate := k.CalculateDynCommission(ctx, validator)
newMaxRate := validator.Commission.MaxRate
targetRate := k.GetTargetCommissionRate(ctx, validator.OperatorAddress)

// assume the newRate will be the target rate ...
newRate = targetRate

// ... but enforce min rate
if newRate.LT(minRate) {
newRate = minRate
}

if newMaxRate.LT(newRate) {
newMaxRate = newRate
// new min rate pushes max rate
if newMaxRate.LT(minRate) {
newMaxRate = minRate
}

newValidator := validator
Expand All @@ -105,7 +159,10 @@ func (k Keeper) UpdateValidatorRates(ctx sdk.Context, validator stakingtypes.Val
k.StakingKeeper.SetValidator(ctx, newValidator)
k.SetDynCommissionRate(ctx, validator.OperatorAddress, newRate)

ctx.Logger().Info("dyncomm:", "val", validator.OperatorAddress, "rate", k.GetDynCommissionRate(ctx, validator.OperatorAddress))
// Debug
minRate = k.GetDynCommissionRate(ctx, validator.OperatorAddress)
targetRate = k.GetTargetCommissionRate(ctx, validator.OperatorAddress)
ctx.Logger().Info("dyncomm:", "val", validator.OperatorAddress, "min_rate", minRate, "target_rate", targetRate)

}

Expand All @@ -120,7 +177,7 @@ func (k Keeper) UpdateAllBondedValidatorRates(ctx sdk.Context) (err error) {
return false
}

k.UpdateValidatorRates(ctx, val)
k.UpdateValidatorMinRates(ctx, val)

return false

Expand Down
3 changes: 2 additions & 1 deletion x/dyncomm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ func (q querier) Params(c context.Context, _ *types.QueryParamsRequest) (*types.
func (q querier) Rate(c context.Context, req *types.QueryRateRequest) (*types.QueryRateResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
rate := q.GetDynCommissionRate(ctx, req.ValidatorAddr)
return &types.QueryRateResponse{Rate: &rate}, nil
target := q.GetTargetCommissionRate(ctx, req.ValidatorAddr)
return &types.QueryRateResponse{Rate: &rate, Target: &target}, nil
}
12 changes: 6 additions & 6 deletions x/dyncomm/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package types

// NewGenesisState creates a new GenesisState object
func NewGenesisState(params Params, rates []MinCommissionRate) *GenesisState {
func NewGenesisState(params Params, rates []ValidatorCommissionRate) *GenesisState {
return &GenesisState{
Params: params,
MinCommissionRates: rates,
Params: params,
ValidatorCommissionRates: rates,
}
}

// DefaultGenesisState gets raw genesis raw message for testing
func DefaultGenesisState() *GenesisState {
emptySet := []MinCommissionRate{}
emptySet := []ValidatorCommissionRate{}
return &GenesisState{
Params: DefaultParams(),
MinCommissionRates: emptySet,
Params: DefaultParams(),
ValidatorCommissionRates: emptySet,
}
}
Loading

0 comments on commit 940f53f

Please sign in to comment.