Skip to content

Commit

Permalink
added validator selection helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
shellvish committed Jun 21, 2022
1 parent 56cb805 commit 19c3642
Show file tree
Hide file tree
Showing 6 changed files with 610 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
build/*
state/*
scripts/state/*
scripts/testnet/*/state/keys.txt
scripts/logs/*.log
scripts/temp.sh
vue/*
Expand Down
53 changes: 53 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31630,6 +31630,22 @@ paths:
reinvest_interval:
type: string
format: uint64
host_zone_validator_weights:
type: object
additionalProperties:
type: object
properties:
validator_weights:
type: object
additionalProperties:
type: string
format: int64
title: >-
This stores a map from "validator address" -> "target
weight" "
title: >-
validator_weights is a map from "hostZone chainId" ->
"ValidatorWeights" (defined below)
title: |-
Params defines the parameters for the module.
next id: 8
Expand Down Expand Up @@ -52482,6 +52498,20 @@ definitions:
reinvest_interval:
type: string
format: uint64
host_zone_validator_weights:
type: object
additionalProperties:
type: object
properties:
validator_weights:
type: object
additionalProperties:
type: string
format: int64
title: This stores a map from "validator address" -> "target weight" "
title: >-
validator_weights is a map from "hostZone chainId" ->
"ValidatorWeights" (defined below)
title: |-
Params defines the parameters for the module.
next id: 8
Expand Down Expand Up @@ -53140,6 +53170,20 @@ definitions:
reinvest_interval:
type: string
format: uint64
host_zone_validator_weights:
type: object
additionalProperties:
type: object
properties:
validator_weights:
type: object
additionalProperties:
type: string
format: int64
title: This stores a map from "validator address" -> "target weight" "
title: >-
validator_weights is a map from "hostZone chainId" ->
"ValidatorWeights" (defined below)
title: |-
Params defines the parameters for the module.
next id: 8
Expand All @@ -53159,3 +53203,12 @@ definitions:
delegationAmt:
type: string
format: int64
Stridelabs.stride.stakeibc.ValidatorWeights:
type: object
properties:
validator_weights:
type: object
additionalProperties:
type: string
format: int64
title: This stores a map from "validator address" -> "target weight" "
7 changes: 6 additions & 1 deletion proto/stakeibc/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ message Params {
// TODO implement this
map<string, string> zone_com_address = 5;
uint64 reinvest_interval = 7;

// validator_weights is a map from "hostZone chainId" -> "ValidatorWeights" (defined below)
map<string, ValidatorWeights> host_zone_validator_weights = 8;
}

// This stores a map from "validator address" -> "target weight" "
message ValidatorWeights {
map<string, int64> validator_weights = 1;
}
46 changes: 46 additions & 0 deletions x/stakeibc/keeper/validator_selection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package keeper

import (
"github.com/Stride-Labs/stride/x/stakeibc/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) GetValidatorAmtDifferences(ctx sdk.Context, hostZone types.HostZone) map[string]int64 {
/*
This function returns a map from Validator Address to how many extra tokens
need to be given to that validator (posit)
*/
validators := hostZone.GetValidators()
scaled_weights := make(map[string]int64)
target_weights := k.GetTargetValAmtsForHostZone(ctx, hostZone)
for _, validator := range validators {
scaled_weights[validator.Address] = target_weights[validator.Address] - validator.DelegationAmt
}
return scaled_weights
}

func (k Keeper) GetTargetValAmtsForHostZone(ctx sdk.Context, hostZone types.HostZone) map[string]int64 {
var out map[string]types.ValidatorWeights
k.paramstore.Get(ctx, types.KeyHostZoneValidatorWeights, &out)
valWeights := out[hostZone.ChainId]
totalWeight := int64(0)
totalDelegatedWeight := k.GetTotalValidatorWeight()
for _, weight := range valWeights.ValidatorWeights {
totalWeight += weight
}
var targetWeight map[string]int64
for valAddr, weight := range valWeights.ValidatorWeights {
targetWeight[valAddr] = int64(float64(weight*totalDelegatedWeight) / float64(totalWeight))
}
return targetWeight
}

func (k Keeper) GetTotalValidatorWeight(ctx sdk.Context, hostZone types.HostZone) int64 {
validators := hostZone.GetValidators()
total_weight := int64(0)
for _, validator := range validators {
// QUESTION do we need to do some error handling here if delegaitonAmt is missing?
total_weight += validator.DelegationAmt
}
return total_weight
}
40 changes: 27 additions & 13 deletions x/stakeibc/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ var (
DefaultRewardsInterval uint64 = 5
DefaultExchangeRateInterval uint64 = 2
// you apparantly cannot safely encode floats, so we make commission * 100
DefaultStrideCommission uint64 = 10
DefaultStrideCommission uint64 = 10
DefaultHostZoneValidatorWeights map[string]*ValidatorWeights = map[string]*ValidatorWeights{}

// KeyDepositInterval is store's key for the DepositInterval option
KeyDepositInterval = []byte("DepositInterval")
KeyDelegateInterval = []byte("DelegateInterval")
KeyReinvestInterval = []byte("ReinvestInterval")
KeyRewardsInterval = []byte("RewardsInterval")
KeyExchangeRateInterval = []byte("ExchangeRateInterval")
KeyStrideCommission = []byte("StrideCommission")
KeyDepositInterval = []byte("DepositInterval")
KeyDelegateInterval = []byte("DelegateInterval")
KeyReinvestInterval = []byte("ReinvestInterval")
KeyRewardsInterval = []byte("RewardsInterval")
KeyExchangeRateInterval = []byte("ExchangeRateInterval")
KeyStrideCommission = []byte("StrideCommission")
KeyHostZoneValidatorWeights = []byte("HostZoneValidatorWeights")
)

var _ paramtypes.ParamSet = (*Params)(nil)
Expand All @@ -42,14 +44,16 @@ func NewParams(
exchange_rate_interval uint64,
stride_commission uint64,
withdraw_interval uint64,
hostzone_val_weights map[string]*ValidatorWeights,
) Params {
return Params{
DepositInterval: deposit_interval,
DelegateInterval: delegate_interval,
RewardsInterval: rewards_interval,
ExchangeRateInterval: exchange_rate_interval,
StrideCommission: stride_commission,
ReinvestInterval: withdraw_interval,
DepositInterval: deposit_interval,
DelegateInterval: delegate_interval,
RewardsInterval: rewards_interval,
ExchangeRateInterval: exchange_rate_interval,
StrideCommission: stride_commission,
ReinvestInterval: withdraw_interval,
HostZoneValidatorWeights: hostzone_val_weights,
}
}

Expand All @@ -62,6 +66,7 @@ func DefaultParams() Params {
DefaultExchangeRateInterval,
DefaultStrideCommission,
DefaultReinvestInterval,
DefaultHostZoneValidatorWeights,
)
}

Expand All @@ -74,9 +79,18 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(KeyExchangeRateInterval, &p.ExchangeRateInterval, isPositive),
paramtypes.NewParamSetPair(KeyStrideCommission, &p.StrideCommission, isCommission),
paramtypes.NewParamSetPair(KeyReinvestInterval, &p.ReinvestInterval, isPositive),
paramtypes.NewParamSetPair(KeyHostZoneValidatorWeights, &p.HostZoneValidatorWeights, isValidWeights),
}
}

func isValidWeights(i interface{}) error {
_, ok := i.(map[string]*ValidatorWeights)
if !ok {
return fmt.Errorf("parameter not accepted: %T", i)
}
return nil
}

func isPositive(i interface{}) error {
ival, ok := i.(uint64)
if !ok {
Expand Down
Loading

0 comments on commit 19c3642

Please sign in to comment.