Genesis import/export fixes (#871)

* panic on export if prev accrual time not set

* on export if interest factor not set, set to 1.0

* fix prev accrual time in cdp export

* panic on export if prev accrual time not set

* export hard reward denom accumulation times

* init genesis starts usdx reward indexes at 0.0

* update incentive migration

* update incentive tests
This commit is contained in:
Denali Marsh 2021-03-11 05:32:35 +01:00 committed by GitHub
parent 509d2edbca
commit 8744d3210c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 31 deletions

View File

@ -17,6 +17,7 @@ import (
func (app *App) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteList []string, func (app *App) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteList []string,
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { ) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
// as if they could withdraw from the start of the next block // as if they could withdraw from the start of the next block
// block time is not available and defaults to Jan 1st, 0001
ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()}) ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
if forZeroHeight { if forZeroHeight {

View File

@ -296,24 +296,24 @@ func Incentive(hardGS v0_11hard.GenesisState, incentiveGS v0_11incentive.Genesis
usdxGenAccumulationTimes := v0_13incentive.GenesisAccumulationTimes{} usdxGenAccumulationTimes := v0_13incentive.GenesisAccumulationTimes{}
for _, rp := range params.USDXMintingRewardPeriods { for _, rp := range params.USDXMintingRewardPeriods {
gat := v0_13incentive.NewGenesisAccumulationTime(rp.CollateralType, GenesisTime, sdk.ZeroDec()) gat := v0_13incentive.NewGenesisAccumulationTime(rp.CollateralType, GenesisTime)
usdxGenAccumulationTimes = append(usdxGenAccumulationTimes, gat) usdxGenAccumulationTimes = append(usdxGenAccumulationTimes, gat)
} }
hardSupplyGenAccumulationTimes := v0_13incentive.GenesisAccumulationTimes{} hardSupplyGenAccumulationTimes := v0_13incentive.GenesisAccumulationTimes{}
for _, rp := range params.HardSupplyRewardPeriods { for _, rp := range params.HardSupplyRewardPeriods {
gat := v0_13incentive.NewGenesisAccumulationTime(rp.CollateralType, GenesisTime, sdk.ZeroDec()) gat := v0_13incentive.NewGenesisAccumulationTime(rp.CollateralType, GenesisTime)
hardSupplyGenAccumulationTimes = append(hardSupplyGenAccumulationTimes, gat) hardSupplyGenAccumulationTimes = append(hardSupplyGenAccumulationTimes, gat)
} }
hardBorrowGenAccumulationTimes := v0_13incentive.GenesisAccumulationTimes{} hardBorrowGenAccumulationTimes := v0_13incentive.GenesisAccumulationTimes{}
for _, rp := range params.HardBorrowRewardPeriods { for _, rp := range params.HardBorrowRewardPeriods {
gat := v0_13incentive.NewGenesisAccumulationTime(rp.CollateralType, GenesisTime, sdk.ZeroDec()) gat := v0_13incentive.NewGenesisAccumulationTime(rp.CollateralType, GenesisTime)
hardBorrowGenAccumulationTimes = append(hardBorrowGenAccumulationTimes, gat) hardBorrowGenAccumulationTimes = append(hardBorrowGenAccumulationTimes, gat)
} }
hardDelegatorGenAccumulationTimes := v0_13incentive.GenesisAccumulationTimes{} hardDelegatorGenAccumulationTimes := v0_13incentive.GenesisAccumulationTimes{}
for _, rp := range params.HardDelegatorRewardPeriods { for _, rp := range params.HardDelegatorRewardPeriods {
gat := v0_13incentive.NewGenesisAccumulationTime(rp.CollateralType, GenesisTime, sdk.ZeroDec()) gat := v0_13incentive.NewGenesisAccumulationTime(rp.CollateralType, GenesisTime)
hardDelegatorGenAccumulationTimes = append(hardDelegatorGenAccumulationTimes, gat) hardDelegatorGenAccumulationTimes = append(hardDelegatorGenAccumulationTimes, gat)
} }

View File

@ -115,7 +115,14 @@ func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
if !found { if !found {
interestFactor = sdk.OneDec() interestFactor = sdk.OneDec()
} }
previousAccumTime, _ := k.GetPreviousAccrualTime(ctx, cp.Type) // Governance param changes happen in the end blocker. If a new collateral type is added and then the chain
// is exported before the BeginBlocker can run, previous accrual time won't be found. We can't set it to
// current block time because it is not available in the export ctx. We should panic instead of exporting
// bad state.
previousAccumTime, f := k.GetPreviousAccrualTime(ctx, cp.Type)
if !f {
panic(fmt.Sprintf("expected previous accrual time to be set in state for %s", cp.Type))
}
previousAccumTimes = append(previousAccumTimes, types.NewGenesisAccumulationTime(cp.Type, previousAccumTime, interestFactor)) previousAccumTimes = append(previousAccumTimes, types.NewGenesisAccumulationTime(cp.Type, previousAccumTime, interestFactor))
tp := k.GetTotalPrincipal(ctx, cp.Type, types.DefaultStableDenom) tp := k.GetTotalPrincipal(ctx, cp.Type, types.DefaultStableDenom)

View File

@ -89,15 +89,20 @@ func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
for _, mm := range params.MoneyMarkets { for _, mm := range params.MoneyMarkets {
supplyFactor, f := k.GetSupplyInterestFactor(ctx, mm.Denom) supplyFactor, f := k.GetSupplyInterestFactor(ctx, mm.Denom)
if !f { if !f {
supplyFactor = sdk.ZeroDec() supplyFactor = sdk.OneDec()
} }
borrowFactor, f := k.GetBorrowInterestFactor(ctx, mm.Denom) borrowFactor, f := k.GetBorrowInterestFactor(ctx, mm.Denom)
if !f { if !f {
borrowFactor = sdk.ZeroDec() borrowFactor = sdk.OneDec()
} }
previousAccrualTime, f := k.GetPreviousAccrualTime(ctx, mm.Denom) previousAccrualTime, f := k.GetPreviousAccrualTime(ctx, mm.Denom)
if !f { if !f {
previousAccrualTime = ctx.BlockTime() // Goverance adds new params at end of block, but mm's previous accrual time is set in begin blocker.
// If a new money market is added and chain is exported before begin blocker runs, then the previous
// accrual time will not be found. We can't set it here because our ctx doesn't contain current block
// time; if we set it to ctx.BlockTime() then on the next block it could accrue interest from Jan 1st
// 0001 to now. To avoid setting up a bad state, we panic.
panic(fmt.Sprintf("expected previous accrual time to be set in state for %s", mm.Denom))
} }
gat := types.NewGenesisAccumulationTime(mm.Denom, previousAccrualTime, supplyFactor, borrowFactor) gat := types.NewGenesisAccumulationTime(mm.Denom, previousAccrualTime, supplyFactor, borrowFactor)
gats = append(gats, gat) gats = append(gats, gat)

View File

@ -56,7 +56,6 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, supplyKeeper types.SupplyKeep
for _, gat := range gs.USDXAccumulationTimes { for _, gat := range gs.USDXAccumulationTimes {
k.SetPreviousUSDXMintingAccrualTime(ctx, gat.CollateralType, gat.PreviousAccumulationTime) k.SetPreviousUSDXMintingAccrualTime(ctx, gat.CollateralType, gat.PreviousAccumulationTime)
k.SetUSDXMintingRewardFactor(ctx, gat.CollateralType, gat.RewardFactor)
} }
for _, gat := range gs.HardSupplyAccumulationTimes { for _, gat := range gs.HardSupplyAccumulationTimes {
@ -147,20 +146,46 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
synchronizedHardClaims = append(synchronizedHardClaims, claim) synchronizedHardClaims = append(synchronizedHardClaims, claim)
} }
var gats GenesisAccumulationTimes var usdxMintingGats GenesisAccumulationTimes
for _, rp := range params.USDXMintingRewardPeriods { for _, rp := range params.USDXMintingRewardPeriods {
pat, found := k.GetPreviousUSDXMintingAccrualTime(ctx, rp.CollateralType) pat, found := k.GetPreviousUSDXMintingAccrualTime(ctx, rp.CollateralType)
if !found { if !found {
pat = ctx.BlockTime() panic(fmt.Sprintf("expected previous usdx minting reward accrual time to be set in state for %s", rp.CollateralType))
} }
factor, found := k.GetUSDXMintingRewardFactor(ctx, rp.CollateralType) gat := types.NewGenesisAccumulationTime(rp.CollateralType, pat)
if !found { usdxMintingGats = append(usdxMintingGats, gat)
factor = sdk.ZeroDec()
}
gat := types.NewGenesisAccumulationTime(rp.CollateralType, pat, factor)
gats = append(gats, gat)
} }
return types.NewGenesisState(params, gats, DefaultGenesisAccumulationTimes, DefaultGenesisAccumulationTimes, DefaultGenesisAccumulationTimes, synchronizedUsdxClaims, synchronizedHardClaims) var hardSupplyGats GenesisAccumulationTimes
for _, rp := range params.HardSupplyRewardPeriods {
pat, found := k.GetPreviousHardSupplyRewardAccrualTime(ctx, rp.CollateralType)
if !found {
panic(fmt.Sprintf("expected previous hard supply reward accrual time to be set in state for %s", rp.CollateralType))
}
gat := types.NewGenesisAccumulationTime(rp.CollateralType, pat)
hardSupplyGats = append(hardSupplyGats, gat)
}
var hardBorrowGats GenesisAccumulationTimes
for _, rp := range params.HardBorrowRewardPeriods {
pat, found := k.GetPreviousHardBorrowRewardAccrualTime(ctx, rp.CollateralType)
if !found {
panic(fmt.Sprintf("expected previous hard borrow reward accrual time to be set in state for %s", rp.CollateralType))
}
gat := types.NewGenesisAccumulationTime(rp.CollateralType, pat)
hardBorrowGats = append(hardBorrowGats, gat)
}
var hardDelegatorGats GenesisAccumulationTimes
for _, rp := range params.HardDelegatorRewardPeriods {
pat, found := k.GetPreviousHardDelegatorRewardAccrualTime(ctx, rp.CollateralType)
if !found {
panic(fmt.Sprintf("expected previous hard delegator reward accrual time to be set in state for %s", rp.CollateralType))
}
gat := types.NewGenesisAccumulationTime(rp.CollateralType, pat)
hardDelegatorGats = append(hardDelegatorGats, gat)
}
return types.NewGenesisState(params, usdxMintingGats, hardSupplyGats,
hardBorrowGats, hardDelegatorGats, synchronizedUsdxClaims, synchronizedHardClaims)
} }

View File

@ -149,7 +149,6 @@ func NewIncentiveGenState(previousAccumTime, endTime time.Time, rewardPeriods ..
incentive.NewGenesisAccumulationTime( incentive.NewGenesisAccumulationTime(
rp.CollateralType, rp.CollateralType,
previousAccumTime, previousAccumTime,
sdk.ZeroDec(),
), ),
) )
} }

View File

@ -4,8 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"time" "time"
sdk "github.com/cosmos/cosmos-sdk/types"
) )
// GenesisState is the state that must be provided at genesis. // GenesisState is the state that must be provided at genesis.
@ -86,15 +84,13 @@ func (gs GenesisState) IsEmpty() bool {
type GenesisAccumulationTime struct { type GenesisAccumulationTime struct {
CollateralType string `json:"collateral_type" yaml:"collateral_type"` CollateralType string `json:"collateral_type" yaml:"collateral_type"`
PreviousAccumulationTime time.Time `json:"previous_accumulation_time" yaml:"previous_accumulation_time"` PreviousAccumulationTime time.Time `json:"previous_accumulation_time" yaml:"previous_accumulation_time"`
RewardFactor sdk.Dec `json:"reward_factor" yaml:"reward_factor"`
} }
// NewGenesisAccumulationTime returns a new GenesisAccumulationTime // NewGenesisAccumulationTime returns a new GenesisAccumulationTime
func NewGenesisAccumulationTime(ctype string, prevTime time.Time, factor sdk.Dec) GenesisAccumulationTime { func NewGenesisAccumulationTime(ctype string, prevTime time.Time) GenesisAccumulationTime {
return GenesisAccumulationTime{ return GenesisAccumulationTime{
CollateralType: ctype, CollateralType: ctype,
PreviousAccumulationTime: prevTime, PreviousAccumulationTime: prevTime,
RewardFactor: factor,
} }
} }
@ -113,8 +109,8 @@ func (gats GenesisAccumulationTimes) Validate() error {
// Validate performs validation of GenesisAccumulationTime // Validate performs validation of GenesisAccumulationTime
func (gat GenesisAccumulationTime) Validate() error { func (gat GenesisAccumulationTime) Validate() error {
if gat.RewardFactor.LT(sdk.ZeroDec()) { if len(gat.CollateralType) == 0 {
return fmt.Errorf("reward factor should be ≥ 0.0, is %s for %s", gat.RewardFactor, gat.CollateralType) return fmt.Errorf("genesis accumulation time's collateral type must be defined")
} }
return nil return nil
} }

View File

@ -63,7 +63,6 @@ func TestGenesisStateValidate(t *testing.T) {
genAccTimes: GenesisAccumulationTimes{GenesisAccumulationTime{ genAccTimes: GenesisAccumulationTimes{GenesisAccumulationTime{
CollateralType: "bnb-a", CollateralType: "bnb-a",
PreviousAccumulationTime: time.Date(2020, 10, 15, 14, 0, 0, 0, time.UTC), PreviousAccumulationTime: time.Date(2020, 10, 15, 14, 0, 0, 0, time.UTC),
RewardFactor: sdk.ZeroDec(),
}}, }},
claims: USDXMintingClaims{ claims: USDXMintingClaims{
{ {
@ -91,15 +90,15 @@ func TestGenesisStateValidate(t *testing.T) {
params: DefaultParams(), params: DefaultParams(),
genAccTimes: GenesisAccumulationTimes{ genAccTimes: GenesisAccumulationTimes{
{ {
CollateralType: "btcb-a", CollateralType: "",
RewardFactor: sdk.MustNewDecFromStr("-0.1"), PreviousAccumulationTime: time.Date(2020, 10, 15, 14, 0, 0, 0, time.UTC),
}, },
}, },
claims: DefaultUSDXClaims, claims: DefaultUSDXClaims,
}, },
errArgs: errArgs{ errArgs: errArgs{
expectPass: false, expectPass: false,
contains: "reward factor should be ≥ 0.0", contains: "collateral type must be defined",
}, },
}, },
{ {