Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arijitAD committed Nov 10, 2021
1 parent fde34e7 commit e37f306
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 153 deletions.
36 changes: 21 additions & 15 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,7 @@ func NewApp(
homePath string, appOpts servertypes.AppOptions, options ...func(*bam.BaseApp),
) *AkashApp {
// find out the genesis time, to be used later in inflation calculation
if v := appOpts.Get("GenesisTime"); v != nil {
// in tests, GenesisTime is supplied using appOpts
genTime, ok := v.(time.Time)
if !ok {
panic("expected GenesisTime to be a Time value")
}
inflationtypes.GenesisTime = genTime
} else if genDoc, err := tmtypes.GenesisDocFromFile(filepath.Join(homePath, "config/genesis.json")); err != nil {
panic(err)
} else {
inflationtypes.GenesisTime = genDoc.GenesisTime
}
genesisTime := getGenesisTime(appOpts, homePath)

// TODO: Remove cdc in favor of appCodec once all modules are migrated.
encodingConfig := MakeEncodingConfig()
Expand Down Expand Up @@ -353,7 +342,6 @@ func NewApp(
app.keeper.evidence = *evidenceKeeper

app.setAkashKeepers()
inflationtypes.InflationParamSubspace = app.GetSubspace(inflation.ModuleName)

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
// we prefer to be more strict in what arguments the modules expect.
Expand All @@ -369,7 +357,7 @@ func NewApp(
capability.NewAppModule(appCodec, *app.keeper.cap),
crisis.NewAppModule(&app.keeper.crisis, skipGenesisInvariants),
gov.NewAppModule(appCodec, app.keeper.gov, app.keeper.acct, app.keeper.bank),
mint.NewAppModule(appCodec, app.keeper.mint, app.keeper.acct, inflationtypes.InflationCalculator),
mint.NewAppModule(appCodec, app.keeper.mint, app.keeper.acct, inflationtypes.GetInflationCalculator(genesisTime, app.GetSubspace(inflation.ModuleName))),
slashing.NewAppModule(appCodec, app.keeper.slashing, app.keeper.acct, app.keeper.bank, app.keeper.staking),
distr.NewAppModule(appCodec, app.keeper.distr, app.keeper.acct, app.keeper.bank, app.keeper.staking),
staking.NewAppModule(appCodec, app.keeper.staking, app.keeper.acct, app.keeper.bank),
Expand Down Expand Up @@ -436,7 +424,7 @@ func NewApp(
bank.NewAppModule(appCodec, app.keeper.bank, app.keeper.acct),
capability.NewAppModule(appCodec, *app.keeper.cap),
gov.NewAppModule(appCodec, app.keeper.gov, app.keeper.acct, app.keeper.bank),
mint.NewAppModule(appCodec, app.keeper.mint, app.keeper.acct, inflationtypes.InflationCalculator),
mint.NewAppModule(appCodec, app.keeper.mint, app.keeper.acct, inflationtypes.GetInflationCalculator(genesisTime, app.GetSubspace(inflation.ModuleName))),
staking.NewAppModule(appCodec, app.keeper.staking, app.keeper.acct, app.keeper.bank),
distr.NewAppModule(appCodec, app.keeper.distr, app.keeper.acct, app.keeper.bank, app.keeper.staking),
slashing.NewAppModule(appCodec, app.keeper.slashing, app.keeper.acct, app.keeper.bank, app.keeper.staking),
Expand Down Expand Up @@ -538,6 +526,24 @@ func (app *AkashApp) registerUpgradeHandlers() {
}
}

func getGenesisTime(appOpts servertypes.AppOptions, homePath string) time.Time {
if v := appOpts.Get("GenesisTime"); v != nil {
// in tests, GenesisTime is supplied using appOpts
genTime, ok := v.(time.Time)
if !ok {
panic("expected GenesisTime to be a Time value")
}
return genTime
}

genDoc, err := tmtypes.GenesisDocFromFile(filepath.Join(homePath, "config/genesis.json"))
if err != nil {
panic(err)
}

return genDoc.GenesisTime
}

// MakeCodecs constructs the *std.Codec and *codec.LegacyAmino instances used by
// simapp. It is useful for tests and clients who do not want to construct the
// full simapp
Expand Down
2 changes: 1 addition & 1 deletion proto/akash/inflation/v1beta2/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ option go_package = "github.com/ovrclk/akash/x/inflation/types/v1beta2";

// GenesisState stores slice of genesis deployment instance
message GenesisState {
Params params = 2 [
Params params = 1 [
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "params",
(gogoproto.moretags) = "yaml:\"params\""
Expand Down
10 changes: 6 additions & 4 deletions proto/akash/inflation/v1beta2/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ message Params {
];

// InitialInflation is the rate at which inflation starts at genesis.
// It is a float value. Maximum: 100.0
// Amino has issues marshalling float, so type is marked string.
// It is a decimal value in the range [0.0, 100.0].
string initial_inflation = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.customname) = "InitialInflation",
(gogoproto.jsontag) = "initial_inflation",
(gogoproto.moretags) = "yaml:\"initial_inflation\""
];

// Variance defines the fraction by which inflation can vary from its previous value in a block.
// It is a float value in the range [0.0, 1.0].
// Amino has issues marshalling float, so type is marked string.
// It is a decimal value in the range [0.0, 1.0].
string variance = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.customname) = "Variance",
(gogoproto.jsontag) = "variance",
(gogoproto.moretags) = "yaml:\"variance\""
Expand Down
5 changes: 3 additions & 2 deletions x/inflation/simulation/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simulation

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
types "github.com/ovrclk/akash/x/inflation/types/v1beta2"
)
Expand All @@ -10,8 +11,8 @@ func RandomizedGenState(simState *module.SimulationState) {
deploymentGenesis := &types.GenesisState{
Params: types.Params{
InflationDecayFactor: 2,
InitialInflation: "100.0",
Variance: "0.05",
InitialInflation: sdk.NewDec(100),
Variance: sdk.MustNewDecFromStr("0.05"),
},
}

Expand Down
10 changes: 5 additions & 5 deletions x/inflation/types/v1beta2/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 46 additions & 54 deletions x/inflation/types/v1beta2/inflation_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,51 @@ import (
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

var (
// these are set during NewApp and used in InflationCalculator

GenesisTime time.Time
InflationParamSubspace paramstypes.Subspace
)

func InflationCalculator(ctx sdk.Context, minter minttypes.Minter, params minttypes.Params, bondedRatio sdk.Dec) sdk.Dec {
var inflationParams Params
InflationParamSubspace.GetParamSet(ctx, &inflationParams)
tHalf := float64(inflationParams.InflationDecayFactor)
initialInflation, err := strconv.ParseFloat(inflationParams.InitialInflation, 32)
if err != nil {
panic(err)
func GetInflationCalculator(
genesisTime time.Time,
inflationParamSubspace paramstypes.Subspace,
) minttypes.InflationCalculationFn {
return func(ctx sdk.Context, minter minttypes.Minter, params minttypes.Params, bondedRatio sdk.Dec) sdk.Dec {
var inflationParams Params
inflationParamSubspace.GetParamSet(ctx, &inflationParams)
tHalf := float64(inflationParams.InflationDecayFactor)

// Number of hours in an year = 8766 (averaging the leap year hours)
// Number of minutes in an hour = 60
// Number of seconds in a minute = 60
// => Number of seconds per year = 60 * 60 * 8766 = 31557600
t := ctx.BlockTime().Sub(genesisTime).Seconds() / 31557600 // years passed (can be a fraction, eg: 0.5)
idealInflation := inflationParams.InitialInflation.Mul(sdk.MustNewDecFromStr(
strconv.FormatFloat(math.Pow(2, -t/tHalf), 'f', -1, 64),
))

// (1 - bondedRatio/GoalBonded) * InflationRateChange
inflationRateChangePerYear := sdk.OneDec().
Sub(bondedRatio.Quo(params.GoalBonded)).
Mul(params.InflationRateChange)
inflationRateChange := inflationRateChangePerYear.Quo(sdk.NewDec(int64(params.BlocksPerYear)))

// note inflationRateChange may be negative
currentInflation := minter.Inflation.Add(inflationRateChange)

// min, max currentInflation based on a defined range parameter 'r'
// currentInflation range = [I(t) - I(t) * R, I(t) + I(t) * R]
r := inflationParams.Variance
minInflation := idealInflation.Sub(idealInflation.Mul(r))
maxInflation := idealInflation.Add(idealInflation.Mul(r))

// minInflation >= minimumMinInflation
minimumMinInflation := sdk.ZeroDec() // 0 for now
if minInflation.LT(minimumMinInflation) {
minInflation = minimumMinInflation
}

if currentInflation.LT(minInflation) {
currentInflation = minInflation
} else if currentInflation.GT(maxInflation) {
currentInflation = maxInflation
}

return currentInflation
}

// Number of hours in an year = 8766 (averaging the leap year hours)
// Number of minutes in an hour = 60
// Number of seconds in a minute = 60
// => 60 * 60 * 8766 = Number of seconds per year
t := ctx.BlockTime().Sub(GenesisTime).Seconds() / (60 * 60 * 8766) // years passed
i := initialInflation * math.Pow(2, -t/tHalf)
idealInflation := sdk.NewDec(int64(i))

// (1 - bondedRatio/GoalBonded) * InflationRateChange
inflationRateChangePerYear := sdk.OneDec().
Sub(bondedRatio.Quo(params.GoalBonded)).
Mul(params.InflationRateChange)
inflationRateChange := inflationRateChangePerYear.Quo(sdk.NewDec(int64(params.BlocksPerYear)))

// note inflationRateChange may be negative
currentInflation := minter.Inflation.Add(inflationRateChange)

// min, max currentInflation based on a defined range parameter 'r'
// currentInflation range = [I(t) - I(t) * R, I(t) + I(t) * R]
r, err := sdk.NewDecFromStr(inflationParams.Variance)
if err != nil {
panic(err)
}
minInflation := idealInflation.Sub(idealInflation.Mul(r))
maxInflation := idealInflation.Add(idealInflation.Mul(r))

// minInflation >= minimumMinInflation
minimumMinInflation := sdk.ZeroDec() // 0 for now
if minInflation.LT(minimumMinInflation) {
minInflation = minimumMinInflation
}

if currentInflation.LT(minInflation) {
currentInflation = minInflation
} else if currentInflation.GT(maxInflation) {
currentInflation = maxInflation
}

return currentInflation
}
35 changes: 18 additions & 17 deletions x/inflation/types/v1beta2/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v1beta2

import (
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/pkg/errors"
Expand All @@ -11,14 +11,23 @@ var _ paramtypes.ParamSet = (*Params)(nil)

const (
DefaultInflationDecayFactor uint32 = 2 // years
DefaultInitialInflation string = "100.0"
DefaultVarince string = "0.05"

keyInflationDecayFactor = "InflationDecayFactor"
keyInitialInflation = "InitialInflation"
keyVariance = "Variance"
)

var (
DefaultInitialInflation = sdk.NewDec(100)
DefaultVarince = sdk.MustNewDecFromStr("0.05")

MaxInitialInflation = sdk.NewDec(100)
MinInitialInflation = sdk.ZeroDec()

MaxVariance = sdk.NewDec(1)
MinVariance = sdk.ZeroDec()
)

func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
Expand Down Expand Up @@ -63,32 +72,24 @@ func validateInflationDecayFactor(i interface{}) error {
}

func validateInitialInflation(i interface{}) error {
v, ok := i.(string)
v, ok := i.(sdk.Dec)
if !ok {
return errors.Wrapf(ErrInvalidParam, "%T", i)
}
initialInflation, err := strconv.ParseFloat(v, 32)
if err != nil {
return err
}
if initialInflation > 100.0 {
return errors.Wrapf(ErrInvalidInitialInflation, "%v", initialInflation)
if v.GT(MaxInitialInflation) || v.LT(MinInitialInflation) {
return errors.Wrapf(ErrInvalidInitialInflation, "%v", v)
}

return nil
}

func validateVariance(i interface{}) error {
v, ok := i.(string)
v, ok := i.(sdk.Dec)
if !ok {
return errors.Wrapf(ErrInvalidParam, "%T", i)
}
variance, err := strconv.ParseFloat(v, 32)
if err != nil {
return err
}
if variance < 0.0 || variance > 1.0 {
return errors.Wrapf(ErrInvalidVariance, "%v", variance)
if v.GT(MaxVariance) || v.LT(MinVariance) {
return errors.Wrapf(ErrInvalidVariance, "%v", v)
}

return nil
Expand Down
Loading

0 comments on commit e37f306

Please sign in to comment.