[R4R] CDP module migrations from v0.11 -> v0.13 (#769)

* remove references to savings rate

* removing savings rate module account in auth migration

* use compact json

* fix non-determinism is cdp migration
This commit is contained in:
Kevin Davis 2021-01-21 10:37:35 -07:00 committed by GitHub
parent 92afaf6ca0
commit 4eef80b47f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 178 additions and 127 deletions

View File

@ -1,15 +1,19 @@
package v0_13
import (
"sort"
"time"
v0_11cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_11"
v0_13cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_13"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/cosmos/cosmos-sdk/x/supply"
v0_13cdp "github.com/kava-labs/kava/x/cdp"
v0_11cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_11"
)
// MigrateCDP migrates from a v0.9 (or v0.10) cdp genesis state to a v0.11 cdp genesis state
// MigrateCDP migrates from a v0.11 cdp genesis state to a v0.13 cdp genesis state
func MigrateCDP(oldGenState v0_11cdp.GenesisState) v0_13cdp.GenesisState {
var newCDPs v0_13cdp.CDPs
var newDeposits v0_13cdp.Deposits
@ -20,24 +24,28 @@ func MigrateCDP(oldGenState v0_11cdp.GenesisState) v0_13cdp.GenesisState {
newStartingID := oldGenState.StartingCdpID
totalPrincipalMap := make(map[string]sdk.Int)
for _, cp := range oldGenState.Params.CollateralParams {
newCollateralParam := v0_13cdp.NewCollateralParam(cp.Denom, cp.Type, cp.LiquidationRatio, cp.DebtLimit, cp.StabilityFee, cp.AuctionSize, cp.LiquidationPenalty, cp.Prefix, cp.SpotMarketID, cp.LiquidationMarketID, sdk.MustNewDecFromStr("0.01"), sdk.NewInt(10), cp.ConversionFactor)
newCollateralParams = append(newCollateralParams, newCollateralParam)
newGenesisAccumulationTime := v0_13cdp.NewGenesisAccumulationTime(cp.Type, previousAccumulationTime, sdk.OneDec())
newGenesisAccumulationTimes = append(newGenesisAccumulationTimes, newGenesisAccumulationTime)
totalPrincipalMap[cp.Type] = sdk.ZeroInt()
}
for _, cdp := range oldGenState.CDPs {
newCDP := v0_13cdp.NewCDPWithFees(cdp.ID, cdp.Owner, cdp.Collateral, cdp.Type, cdp.Principal, cdp.AccumulatedFees, cdp.FeesUpdated, sdk.OneDec())
if previousAccumulationTime.Before(cdp.FeesUpdated) {
previousAccumulationTime = cdp.FeesUpdated
}
_, found := totalPrincipalMap[cdp.Type]
if !found {
totalPrincipalMap[cdp.Type] = sdk.ZeroInt()
}
totalPrincipalMap[cdp.Type] = totalPrincipalMap[cdp.Type].Add(newCDP.GetTotalPrincipal().Amount)
newCDPs = append(newCDPs, newCDP)
}
for _, cp := range oldGenState.Params.CollateralParams {
newCollateralParam := v0_13cdp.NewCollateralParam(cp.Denom, cp.Type, cp.LiquidationRatio, cp.DebtLimit, cp.StabilityFee, cp.AuctionSize, cp.LiquidationPenalty, cp.Prefix, cp.SpotMarketID, cp.LiquidationMarketID, sdk.MustNewDecFromStr("0.01"), sdk.NewInt(10), cp.ConversionFactor)
newCollateralParams = append(newCollateralParams, newCollateralParam)
newGenesisAccumulationTime := v0_13cdp.NewGenesisAccumulationTime(cp.Type, previousAccumulationTime, sdk.OneDec())
newGenesisAccumulationTimes = append(newGenesisAccumulationTimes, newGenesisAccumulationTime)
}
for _, dep := range oldGenState.Deposits {
newDep := v0_13cdp.NewDeposit(dep.CdpID, dep.Depositor, dep.Amount)
newDeposits = append(newDeposits, newDep)
@ -48,6 +56,8 @@ func MigrateCDP(oldGenState v0_11cdp.GenesisState) v0_13cdp.GenesisState {
totalPrincipals = append(totalPrincipals, totalPrincipal)
}
sort.Slice(totalPrincipals, func(i, j int) bool { return totalPrincipals[i].CollateralType < totalPrincipals[j].CollateralType })
oldDebtParam := oldGenState.Params.DebtParam
newDebtParam := v0_13cdp.NewDebtParam(oldDebtParam.Denom, oldDebtParam.ReferenceAsset, oldDebtParam.ConversionFactor, oldDebtParam.DebtFloor)
@ -67,3 +77,39 @@ func MigrateCDP(oldGenState v0_11cdp.GenesisState) v0_13cdp.GenesisState {
totalPrincipals,
)
}
// MigrateAuth migrates from a v0.11 auth genesis state to a v0.13
func MigrateAuth(genesisState auth.GenesisState) auth.GenesisState {
savingsRateMaccCoins := sdk.NewCoins()
savingsMaccAddr := supply.NewModuleAddress(v0_11cdp.SavingsRateMacc)
savingsRateMaccIndex := 0
liquidatorMaccIndex := 0
for idx, acc := range genesisState.Accounts {
if acc.GetAddress().Equals(savingsMaccAddr) {
savingsRateMaccCoins = acc.GetCoins()
savingsRateMaccIndex = idx
err := acc.SetCoins(acc.GetCoins().Sub(acc.GetCoins()))
if err != nil {
panic(err)
}
}
if acc.GetAddress().Equals(supply.NewModuleAddress(v0_13cdp.LiquidatorMacc)) {
liquidatorMaccIndex = idx
}
}
liquidatorAcc := genesisState.Accounts[liquidatorMaccIndex]
err := liquidatorAcc.SetCoins(liquidatorAcc.GetCoins().Add(savingsRateMaccCoins...))
if err != nil {
panic(err)
}
genesisState.Accounts[liquidatorMaccIndex] = liquidatorAcc
genesisState.Accounts = removeIndex(genesisState.Accounts, savingsRateMaccIndex)
return genesisState
}
func removeIndex(accs authexported.GenesisAccounts, index int) authexported.GenesisAccounts {
ret := make(authexported.GenesisAccounts, 0)
ret = append(ret, accs[:index]...)
return append(ret, accs[index+1:]...)
}

View File

@ -7,6 +7,7 @@ import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/kava-labs/kava/app"
v0_11cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_11"
@ -41,3 +42,18 @@ func TestMigrateCdp(t *testing.T) {
require.Equal(t, sdk.OneDec(), cdp1.InterestFactor)
}
func TestMigrateAuth(t *testing.T) {
bz, err := ioutil.ReadFile(filepath.Join("testdata", "kava-4-auth-state-block-500000.json"))
require.NoError(t, err)
var oldGenState auth.GenesisState
cdc := app.MakeCodec()
require.NotPanics(t, func() {
cdc.MustUnmarshalJSON(bz, &oldGenState)
})
newGenState := MigrateAuth(oldGenState)
err = auth.ValidateGenesis(newGenState)
require.NoError(t, err)
require.Equal(t, len(oldGenState.Accounts), len(newGenState.Accounts)+1)
}

File diff suppressed because one or more lines are too long

View File

@ -8,36 +8,34 @@ import (
)
const (
AttributeKeyCdpID = types.AttributeKeyCdpID
AttributeKeyDeposit = types.AttributeKeyDeposit
AttributeKeyError = types.AttributeKeyError
AttributeValueCategory = types.AttributeValueCategory
DefaultParamspace = types.DefaultParamspace
EventTypeBeginBlockerFatal = types.EventTypeBeginBlockerFatal
EventTypeCdpClose = types.EventTypeCdpClose
EventTypeCdpDeposit = types.EventTypeCdpDeposit
EventTypeCdpDraw = types.EventTypeCdpDraw
EventTypeCdpLiquidation = types.EventTypeCdpLiquidation
EventTypeCdpRepay = types.EventTypeCdpRepay
EventTypeCdpWithdrawal = types.EventTypeCdpWithdrawal
EventTypeCreateCdp = types.EventTypeCreateCdp
LiquidatorMacc = types.LiquidatorMacc
ModuleName = types.ModuleName
QuerierRoute = types.QuerierRoute
QueryGetAccounts = types.QueryGetAccounts
QueryGetCdp = types.QueryGetCdp
QueryGetCdpDeposits = types.QueryGetCdpDeposits
QueryGetCdps = types.QueryGetCdps
QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
QueryGetParams = types.QueryGetParams
QueryGetPreviousSavingsDistributionTime = types.QueryGetPreviousSavingsDistributionTime
QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed
RestCollateralType = types.RestCollateralType
RestOwner = types.RestOwner
RestRatio = types.RestRatio
RouterKey = types.RouterKey
StoreKey = types.StoreKey
AttributeKeyCdpID = types.AttributeKeyCdpID
AttributeKeyDeposit = types.AttributeKeyDeposit
AttributeKeyError = types.AttributeKeyError
AttributeValueCategory = types.AttributeValueCategory
DefaultParamspace = types.DefaultParamspace
EventTypeBeginBlockerFatal = types.EventTypeBeginBlockerFatal
EventTypeCdpClose = types.EventTypeCdpClose
EventTypeCdpDeposit = types.EventTypeCdpDeposit
EventTypeCdpDraw = types.EventTypeCdpDraw
EventTypeCdpLiquidation = types.EventTypeCdpLiquidation
EventTypeCdpRepay = types.EventTypeCdpRepay
EventTypeCdpWithdrawal = types.EventTypeCdpWithdrawal
EventTypeCreateCdp = types.EventTypeCreateCdp
LiquidatorMacc = types.LiquidatorMacc
ModuleName = types.ModuleName
QuerierRoute = types.QuerierRoute
QueryGetAccounts = types.QueryGetAccounts
QueryGetCdp = types.QueryGetCdp
QueryGetCdpDeposits = types.QueryGetCdpDeposits
QueryGetCdps = types.QueryGetCdps
QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
QueryGetParams = types.QueryGetParams
RestCollateralType = types.RestCollateralType
RestOwner = types.RestOwner
RestRatio = types.RestRatio
RouterKey = types.RouterKey
StoreKey = types.StoreKey
)
var (
@ -94,62 +92,61 @@ var (
ValidSortableDec = types.ValidSortableDec
// variable aliases
CdpIDKey = types.CdpIDKey
CdpIDKeyPrefix = types.CdpIDKeyPrefix
CdpKeyPrefix = types.CdpKeyPrefix
CollateralRatioIndexPrefix = types.CollateralRatioIndexPrefix
DebtDenomKey = types.DebtDenomKey
DefaultCdpStartingID = types.DefaultCdpStartingID
DefaultCircuitBreaker = types.DefaultCircuitBreaker
DefaultCollateralParams = types.DefaultCollateralParams
DefaultDebtDenom = types.DefaultDebtDenom
DefaultDebtLot = types.DefaultDebtLot
DefaultDebtParam = types.DefaultDebtParam
DefaultDebtThreshold = types.DefaultDebtThreshold
DefaultGlobalDebt = types.DefaultGlobalDebt
DefaultGovDenom = types.DefaultGovDenom
DefaultSavingsRateDistributed = types.DefaultSavingsRateDistributed
DefaultStableDenom = types.DefaultStableDenom
DefaultSurplusLot = types.DefaultSurplusLot
DefaultSurplusThreshold = types.DefaultSurplusThreshold
DepositKeyPrefix = types.DepositKeyPrefix
ErrAccountNotFound = types.ErrAccountNotFound
ErrBelowDebtFloor = types.ErrBelowDebtFloor
ErrCdpAlreadyExists = types.ErrCdpAlreadyExists
ErrCdpNotAvailable = types.ErrCdpNotAvailable
ErrCdpNotFound = types.ErrCdpNotFound
ErrCollateralNotSupported = types.ErrCollateralNotSupported
ErrDebtNotSupported = types.ErrDebtNotSupported
ErrDenomPrefixNotFound = types.ErrDenomPrefixNotFound
ErrDepositNotAvailable = types.ErrDepositNotAvailable
ErrDepositNotFound = types.ErrDepositNotFound
ErrExceedsDebtLimit = types.ErrExceedsDebtLimit
ErrInsufficientBalance = types.ErrInsufficientBalance
ErrInvalidCollateral = types.ErrInvalidCollateral
ErrInvalidCollateralLength = types.ErrInvalidCollateralLength
ErrInvalidCollateralRatio = types.ErrInvalidCollateralRatio
ErrInvalidDebtRequest = types.ErrInvalidDebtRequest
ErrInvalidDeposit = types.ErrInvalidDeposit
ErrInvalidPayment = types.ErrInvalidPayment
ErrInvalidWithdrawAmount = types.ErrInvalidWithdrawAmount
ErrLoadingAugmentedCDP = types.ErrLoadingAugmentedCDP
ErrNotLiquidatable = types.ErrNotLiquidatable
ErrPricefeedDown = types.ErrPricefeedDown
GovDenomKey = types.GovDenomKey
InterestFactorPrefix = types.InterestFactorPrefix
KeyCircuitBreaker = types.KeyCircuitBreaker
KeyCollateralParams = types.KeyCollateralParams
KeyDebtLot = types.KeyDebtLot
KeyDebtParam = types.KeyDebtParam
KeyDebtThreshold = types.KeyDebtThreshold
KeyGlobalDebtLimit = types.KeyGlobalDebtLimit
KeySurplusLot = types.KeySurplusLot
KeySurplusThreshold = types.KeySurplusThreshold
MaxSortableDec = types.MaxSortableDec
ModuleCdc = types.ModuleCdc
PreviousAccrualTimePrefix = types.PreviousAccrualTimePrefix
PricefeedStatusKeyPrefix = types.PricefeedStatusKeyPrefix
PrincipalKeyPrefix = types.PrincipalKeyPrefix
CdpIDKey = types.CdpIDKey
CdpIDKeyPrefix = types.CdpIDKeyPrefix
CdpKeyPrefix = types.CdpKeyPrefix
CollateralRatioIndexPrefix = types.CollateralRatioIndexPrefix
DebtDenomKey = types.DebtDenomKey
DefaultCdpStartingID = types.DefaultCdpStartingID
DefaultCircuitBreaker = types.DefaultCircuitBreaker
DefaultCollateralParams = types.DefaultCollateralParams
DefaultDebtDenom = types.DefaultDebtDenom
DefaultDebtLot = types.DefaultDebtLot
DefaultDebtParam = types.DefaultDebtParam
DefaultDebtThreshold = types.DefaultDebtThreshold
DefaultGlobalDebt = types.DefaultGlobalDebt
DefaultGovDenom = types.DefaultGovDenom
DefaultStableDenom = types.DefaultStableDenom
DefaultSurplusLot = types.DefaultSurplusLot
DefaultSurplusThreshold = types.DefaultSurplusThreshold
DepositKeyPrefix = types.DepositKeyPrefix
ErrAccountNotFound = types.ErrAccountNotFound
ErrBelowDebtFloor = types.ErrBelowDebtFloor
ErrCdpAlreadyExists = types.ErrCdpAlreadyExists
ErrCdpNotAvailable = types.ErrCdpNotAvailable
ErrCdpNotFound = types.ErrCdpNotFound
ErrCollateralNotSupported = types.ErrCollateralNotSupported
ErrDebtNotSupported = types.ErrDebtNotSupported
ErrDenomPrefixNotFound = types.ErrDenomPrefixNotFound
ErrDepositNotAvailable = types.ErrDepositNotAvailable
ErrDepositNotFound = types.ErrDepositNotFound
ErrExceedsDebtLimit = types.ErrExceedsDebtLimit
ErrInsufficientBalance = types.ErrInsufficientBalance
ErrInvalidCollateral = types.ErrInvalidCollateral
ErrInvalidCollateralLength = types.ErrInvalidCollateralLength
ErrInvalidCollateralRatio = types.ErrInvalidCollateralRatio
ErrInvalidDebtRequest = types.ErrInvalidDebtRequest
ErrInvalidDeposit = types.ErrInvalidDeposit
ErrInvalidPayment = types.ErrInvalidPayment
ErrInvalidWithdrawAmount = types.ErrInvalidWithdrawAmount
ErrLoadingAugmentedCDP = types.ErrLoadingAugmentedCDP
ErrNotLiquidatable = types.ErrNotLiquidatable
ErrPricefeedDown = types.ErrPricefeedDown
GovDenomKey = types.GovDenomKey
InterestFactorPrefix = types.InterestFactorPrefix
KeyCircuitBreaker = types.KeyCircuitBreaker
KeyCollateralParams = types.KeyCollateralParams
KeyDebtLot = types.KeyDebtLot
KeyDebtParam = types.KeyDebtParam
KeyDebtThreshold = types.KeyDebtThreshold
KeyGlobalDebtLimit = types.KeyGlobalDebtLimit
KeySurplusLot = types.KeySurplusLot
KeySurplusThreshold = types.KeySurplusThreshold
MaxSortableDec = types.MaxSortableDec
ModuleCdc = types.ModuleCdc
PreviousAccrualTimePrefix = types.PreviousAccrualTimePrefix
PricefeedStatusKeyPrefix = types.PricefeedStatusKeyPrefix
PrincipalKeyPrefix = types.PrincipalKeyPrefix
)
type (

View File

@ -28,7 +28,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
startingID uint64
debtDenom string
govDenom string
savingsRateDist sdk.Int
genAccumTimes cdp.GenesisAccumulationTimes
genTotalPrincipals cdp.GenesisTotalPrincipals
}
@ -54,7 +53,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
deposits: cdp.Deposits{},
debtDenom: "",
govDenom: cdp.DefaultGovDenom,
savingsRateDist: cdp.DefaultSavingsRateDistributed,
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
},
@ -71,7 +69,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
deposits: cdp.Deposits{},
debtDenom: cdp.DefaultDebtDenom,
govDenom: "",
savingsRateDist: cdp.DefaultSavingsRateDistributed,
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
},
@ -88,7 +85,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
deposits: cdp.Deposits{},
debtDenom: cdp.DefaultDebtDenom,
govDenom: cdp.DefaultGovDenom,
savingsRateDist: sdk.NewInt(100),
genAccumTimes: cdp.GenesisAccumulationTimes{cdp.NewGenesisAccumulationTime("bnb-a", time.Time{}, sdk.OneDec().Sub(sdk.SmallestDec()))},
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
},
@ -105,7 +101,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
deposits: cdp.Deposits{},
debtDenom: cdp.DefaultDebtDenom,
govDenom: cdp.DefaultGovDenom,
savingsRateDist: sdk.NewInt(100),
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
genTotalPrincipals: cdp.GenesisTotalPrincipals{cdp.NewGenesisTotalPrincipal("bnb-a", sdk.NewInt(-1))},
},
@ -152,7 +147,6 @@ func (suite *GenesisTestSuite) TestValidGenState() {
appGS,
)
})
}
func TestGenesisTestSuite(t *testing.T) {

View File

@ -28,18 +28,17 @@ var (
ConversionFactor: sdk.NewInt(6),
DebtFloor: sdk.NewInt(10000000),
}
DefaultCdpStartingID = uint64(1)
DefaultDebtDenom = "debt"
DefaultGovDenom = "ukava"
DefaultStableDenom = "usdx"
DefaultSurplusThreshold = sdk.NewInt(500000000000)
DefaultDebtThreshold = sdk.NewInt(100000000000)
DefaultSurplusLot = sdk.NewInt(10000000000)
DefaultDebtLot = sdk.NewInt(10000000000)
DefaultSavingsRateDistributed = sdk.NewInt(0)
minCollateralPrefix = 0
maxCollateralPrefix = 255
stabilityFeeMax = sdk.MustNewDecFromStr("1.000000051034942716") // 500% APR
DefaultCdpStartingID = uint64(1)
DefaultDebtDenom = "debt"
DefaultGovDenom = "ukava"
DefaultStableDenom = "usdx"
DefaultSurplusThreshold = sdk.NewInt(500000000000)
DefaultDebtThreshold = sdk.NewInt(100000000000)
DefaultSurplusLot = sdk.NewInt(10000000000)
DefaultDebtLot = sdk.NewInt(10000000000)
minCollateralPrefix = 0
maxCollateralPrefix = 255
stabilityFeeMax = sdk.MustNewDecFromStr("1.000000051034942716") // 500% APR
)
// Params governance parameters for cdp module

View File

@ -6,18 +6,16 @@ import (
// Querier routes for the cdp module
const (
QueryGetCdp = "cdp"
QueryGetCdps = "cdps"
QueryGetCdpDeposits = "deposits"
QueryGetCdpsByCollateralization = "ratio" // legacy query, maintained for REST API
QueryGetCdpsByCollateralType = "collateralType" // legacy query, maintained for REST API
QueryGetParams = "params"
QueryGetAccounts = "accounts"
QueryGetSavingsRateDistributed = "savings-rate-dist"
QueryGetPreviousSavingsDistributionTime = "savings-rate-dist-time"
RestOwner = "owner"
RestCollateralType = "collateral-type"
RestRatio = "ratio"
QueryGetCdp = "cdp"
QueryGetCdps = "cdps"
QueryGetCdpDeposits = "deposits"
QueryGetCdpsByCollateralization = "ratio" // legacy query, maintained for REST API
QueryGetCdpsByCollateralType = "collateralType" // legacy query, maintained for REST API
QueryGetParams = "params"
QueryGetAccounts = "accounts"
RestOwner = "owner"
RestCollateralType = "collateral-type"
RestRatio = "ratio"
)
// QueryCdpParams params for query /cdp/cdp