mirror of
https://github.com/0glabs/0g-chain.git
synced 2025-01-12 08:15:18 +00:00
[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:
parent
92afaf6ca0
commit
4eef80b47f
@ -1,15 +1,19 @@
|
|||||||
package v0_13
|
package v0_13
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
"time"
|
"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"
|
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 {
|
func MigrateCDP(oldGenState v0_11cdp.GenesisState) v0_13cdp.GenesisState {
|
||||||
var newCDPs v0_13cdp.CDPs
|
var newCDPs v0_13cdp.CDPs
|
||||||
var newDeposits v0_13cdp.Deposits
|
var newDeposits v0_13cdp.Deposits
|
||||||
@ -20,24 +24,28 @@ func MigrateCDP(oldGenState v0_11cdp.GenesisState) v0_13cdp.GenesisState {
|
|||||||
newStartingID := oldGenState.StartingCdpID
|
newStartingID := oldGenState.StartingCdpID
|
||||||
|
|
||||||
totalPrincipalMap := make(map[string]sdk.Int)
|
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 {
|
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())
|
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) {
|
if previousAccumulationTime.Before(cdp.FeesUpdated) {
|
||||||
previousAccumulationTime = 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)
|
totalPrincipalMap[cdp.Type] = totalPrincipalMap[cdp.Type].Add(newCDP.GetTotalPrincipal().Amount)
|
||||||
|
|
||||||
newCDPs = append(newCDPs, newCDP)
|
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 {
|
for _, dep := range oldGenState.Deposits {
|
||||||
newDep := v0_13cdp.NewDeposit(dep.CdpID, dep.Depositor, dep.Amount)
|
newDep := v0_13cdp.NewDeposit(dep.CdpID, dep.Depositor, dep.Amount)
|
||||||
newDeposits = append(newDeposits, newDep)
|
newDeposits = append(newDeposits, newDep)
|
||||||
@ -48,6 +56,8 @@ func MigrateCDP(oldGenState v0_11cdp.GenesisState) v0_13cdp.GenesisState {
|
|||||||
totalPrincipals = append(totalPrincipals, totalPrincipal)
|
totalPrincipals = append(totalPrincipals, totalPrincipal)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Slice(totalPrincipals, func(i, j int) bool { return totalPrincipals[i].CollateralType < totalPrincipals[j].CollateralType })
|
||||||
|
|
||||||
oldDebtParam := oldGenState.Params.DebtParam
|
oldDebtParam := oldGenState.Params.DebtParam
|
||||||
|
|
||||||
newDebtParam := v0_13cdp.NewDebtParam(oldDebtParam.Denom, oldDebtParam.ReferenceAsset, oldDebtParam.ConversionFactor, oldDebtParam.DebtFloor)
|
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,
|
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:]...)
|
||||||
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/app"
|
"github.com/kava-labs/kava/app"
|
||||||
v0_11cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_11"
|
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)
|
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)
|
||||||
|
|
||||||
|
}
|
||||||
|
1
migrate/v0_13/testdata/kava-4-auth-state-block-500000.json
vendored
Normal file
1
migrate/v0_13/testdata/kava-4-auth-state-block-500000.json
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -31,8 +31,6 @@ const (
|
|||||||
QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType
|
QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType
|
||||||
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
|
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
|
||||||
QueryGetParams = types.QueryGetParams
|
QueryGetParams = types.QueryGetParams
|
||||||
QueryGetPreviousSavingsDistributionTime = types.QueryGetPreviousSavingsDistributionTime
|
|
||||||
QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed
|
|
||||||
RestCollateralType = types.RestCollateralType
|
RestCollateralType = types.RestCollateralType
|
||||||
RestOwner = types.RestOwner
|
RestOwner = types.RestOwner
|
||||||
RestRatio = types.RestRatio
|
RestRatio = types.RestRatio
|
||||||
@ -108,7 +106,6 @@ var (
|
|||||||
DefaultDebtThreshold = types.DefaultDebtThreshold
|
DefaultDebtThreshold = types.DefaultDebtThreshold
|
||||||
DefaultGlobalDebt = types.DefaultGlobalDebt
|
DefaultGlobalDebt = types.DefaultGlobalDebt
|
||||||
DefaultGovDenom = types.DefaultGovDenom
|
DefaultGovDenom = types.DefaultGovDenom
|
||||||
DefaultSavingsRateDistributed = types.DefaultSavingsRateDistributed
|
|
||||||
DefaultStableDenom = types.DefaultStableDenom
|
DefaultStableDenom = types.DefaultStableDenom
|
||||||
DefaultSurplusLot = types.DefaultSurplusLot
|
DefaultSurplusLot = types.DefaultSurplusLot
|
||||||
DefaultSurplusThreshold = types.DefaultSurplusThreshold
|
DefaultSurplusThreshold = types.DefaultSurplusThreshold
|
||||||
|
@ -28,7 +28,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
|
|||||||
startingID uint64
|
startingID uint64
|
||||||
debtDenom string
|
debtDenom string
|
||||||
govDenom string
|
govDenom string
|
||||||
savingsRateDist sdk.Int
|
|
||||||
genAccumTimes cdp.GenesisAccumulationTimes
|
genAccumTimes cdp.GenesisAccumulationTimes
|
||||||
genTotalPrincipals cdp.GenesisTotalPrincipals
|
genTotalPrincipals cdp.GenesisTotalPrincipals
|
||||||
}
|
}
|
||||||
@ -54,7 +53,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
|
|||||||
deposits: cdp.Deposits{},
|
deposits: cdp.Deposits{},
|
||||||
debtDenom: "",
|
debtDenom: "",
|
||||||
govDenom: cdp.DefaultGovDenom,
|
govDenom: cdp.DefaultGovDenom,
|
||||||
savingsRateDist: cdp.DefaultSavingsRateDistributed,
|
|
||||||
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
|
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
|
||||||
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
|
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
|
||||||
},
|
},
|
||||||
@ -71,7 +69,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
|
|||||||
deposits: cdp.Deposits{},
|
deposits: cdp.Deposits{},
|
||||||
debtDenom: cdp.DefaultDebtDenom,
|
debtDenom: cdp.DefaultDebtDenom,
|
||||||
govDenom: "",
|
govDenom: "",
|
||||||
savingsRateDist: cdp.DefaultSavingsRateDistributed,
|
|
||||||
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
|
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
|
||||||
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
|
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
|
||||||
},
|
},
|
||||||
@ -88,7 +85,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
|
|||||||
deposits: cdp.Deposits{},
|
deposits: cdp.Deposits{},
|
||||||
debtDenom: cdp.DefaultDebtDenom,
|
debtDenom: cdp.DefaultDebtDenom,
|
||||||
govDenom: cdp.DefaultGovDenom,
|
govDenom: cdp.DefaultGovDenom,
|
||||||
savingsRateDist: sdk.NewInt(100),
|
|
||||||
genAccumTimes: cdp.GenesisAccumulationTimes{cdp.NewGenesisAccumulationTime("bnb-a", time.Time{}, sdk.OneDec().Sub(sdk.SmallestDec()))},
|
genAccumTimes: cdp.GenesisAccumulationTimes{cdp.NewGenesisAccumulationTime("bnb-a", time.Time{}, sdk.OneDec().Sub(sdk.SmallestDec()))},
|
||||||
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
|
genTotalPrincipals: cdp.DefaultGenesisState().TotalPrincipals,
|
||||||
},
|
},
|
||||||
@ -105,7 +101,6 @@ func (suite *GenesisTestSuite) TestInvalidGenState() {
|
|||||||
deposits: cdp.Deposits{},
|
deposits: cdp.Deposits{},
|
||||||
debtDenom: cdp.DefaultDebtDenom,
|
debtDenom: cdp.DefaultDebtDenom,
|
||||||
govDenom: cdp.DefaultGovDenom,
|
govDenom: cdp.DefaultGovDenom,
|
||||||
savingsRateDist: sdk.NewInt(100),
|
|
||||||
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
|
genAccumTimes: cdp.DefaultGenesisState().PreviousAccumulationTimes,
|
||||||
genTotalPrincipals: cdp.GenesisTotalPrincipals{cdp.NewGenesisTotalPrincipal("bnb-a", sdk.NewInt(-1))},
|
genTotalPrincipals: cdp.GenesisTotalPrincipals{cdp.NewGenesisTotalPrincipal("bnb-a", sdk.NewInt(-1))},
|
||||||
},
|
},
|
||||||
@ -152,7 +147,6 @@ func (suite *GenesisTestSuite) TestValidGenState() {
|
|||||||
appGS,
|
appGS,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenesisTestSuite(t *testing.T) {
|
func TestGenesisTestSuite(t *testing.T) {
|
||||||
|
@ -36,7 +36,6 @@ var (
|
|||||||
DefaultDebtThreshold = sdk.NewInt(100000000000)
|
DefaultDebtThreshold = sdk.NewInt(100000000000)
|
||||||
DefaultSurplusLot = sdk.NewInt(10000000000)
|
DefaultSurplusLot = sdk.NewInt(10000000000)
|
||||||
DefaultDebtLot = sdk.NewInt(10000000000)
|
DefaultDebtLot = sdk.NewInt(10000000000)
|
||||||
DefaultSavingsRateDistributed = sdk.NewInt(0)
|
|
||||||
minCollateralPrefix = 0
|
minCollateralPrefix = 0
|
||||||
maxCollateralPrefix = 255
|
maxCollateralPrefix = 255
|
||||||
stabilityFeeMax = sdk.MustNewDecFromStr("1.000000051034942716") // 500% APR
|
stabilityFeeMax = sdk.MustNewDecFromStr("1.000000051034942716") // 500% APR
|
||||||
|
@ -13,8 +13,6 @@ const (
|
|||||||
QueryGetCdpsByCollateralType = "collateralType" // legacy query, maintained for REST API
|
QueryGetCdpsByCollateralType = "collateralType" // legacy query, maintained for REST API
|
||||||
QueryGetParams = "params"
|
QueryGetParams = "params"
|
||||||
QueryGetAccounts = "accounts"
|
QueryGetAccounts = "accounts"
|
||||||
QueryGetSavingsRateDistributed = "savings-rate-dist"
|
|
||||||
QueryGetPreviousSavingsDistributionTime = "savings-rate-dist-time"
|
|
||||||
RestOwner = "owner"
|
RestOwner = "owner"
|
||||||
RestCollateralType = "collateral-type"
|
RestCollateralType = "collateral-type"
|
||||||
RestRatio = "ratio"
|
RestRatio = "ratio"
|
||||||
|
Loading…
Reference in New Issue
Block a user