feat: pricefeed migrations (#846)

This commit is contained in:
Kevin Davis 2021-02-22 14:53:00 -07:00 committed by GitHub
parent 4753504a68
commit 3bf40b97f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 1 deletions

View File

@ -20,6 +20,8 @@ import (
v0_13incentive "github.com/kava-labs/kava/x/incentive"
v0_11incentive "github.com/kava-labs/kava/x/incentive/legacy/v0_11"
"github.com/kava-labs/kava/x/kavadist"
v0_11pricefeed "github.com/kava-labs/kava/x/pricefeed"
v0_13pricefeed "github.com/kava-labs/kava/x/pricefeed"
validatorvesting "github.com/kava-labs/kava/x/validator-vesting"
)
@ -441,7 +443,7 @@ func Bep3(genesisState bep3.GenesisState) bep3.GenesisState {
return bep3.NewGenesisState(newParams, newSwaps, newSupplies, genesisState.PreviousBlockTime)
}
// Committee migrates from a v0.11 (or v0.12) committee genesis state to a v0.13 committee genesis stat
// Committee migrates from a v0.11 (or v0.12) committee genesis state to a v0.13 committee genesis state
func Committee(genesisState v0_11committee.GenesisState) v0_13committee.GenesisState {
committees := []v0_13committee.Committee{}
votes := []v0_13committee.Vote{}
@ -556,6 +558,28 @@ func Committee(genesisState v0_11committee.GenesisState) v0_13committee.GenesisS
genesisState.NextProposalID, committees, proposals, votes)
}
// Pricefeed migrates from a v0.11 (or v0.12) pricefeed genesis state to a v0.13 pricefeed genesis state
func Pricefeed(genesisState v0_11pricefeed.GenesisState) v0_13pricefeed.GenesisState {
newMarkets := v0_13pricefeed.Markets{}
oracles := genesisState.Params.Markets[0].Oracles
for _, m := range genesisState.Params.Markets {
newMarkets = append(newMarkets, m)
}
usdx := v0_11pricefeed.NewMarket("usdx:usd", "usdx", "usd", oracles, true)
newMarkets = append(newMarkets, usdx)
newPrices := v0_13pricefeed.PostedPrices{}
for _, p := range genesisState.PostedPrices {
if p.Expiry.After(GenesisTime) {
newPrices = append(newPrices, p)
}
}
return v0_13pricefeed.NewGenesisState(v0_13pricefeed.NewParams(newMarkets), newPrices)
}
func removeIndex(accs authexported.GenesisAccounts, index int) authexported.GenesisAccounts {
ret := make(authexported.GenesisAccounts, 0)
ret = append(ret, accs[:index]...)

View File

@ -23,6 +23,7 @@ import (
v0_11hard "github.com/kava-labs/kava/x/hard/legacy/v0_11"
v0_13incentive "github.com/kava-labs/kava/x/incentive"
v0_11incentive "github.com/kava-labs/kava/x/incentive/legacy/v0_11"
v0_11pricefeed "github.com/kava-labs/kava/x/pricefeed"
validatorvesting "github.com/kava-labs/kava/x/validator-vesting"
"github.com/stretchr/testify/require"
@ -178,6 +179,19 @@ func TestCommittee(t *testing.T) {
require.Equal(t, len(oldSPCP.AllowedMarkets), len(newSPCP.AllowedMarkets))
}
func TestPricefeed(t *testing.T) {
cdc := app.MakeCodec()
bz, err := ioutil.ReadFile(filepath.Join("testdata", "kava-4-pricefeed-state.json"))
require.NoError(t, err)
var oldPricefeedGenState v0_11pricefeed.GenesisState
require.NotPanics(t, func() {
cdc.MustUnmarshalJSON(bz, &oldPricefeedGenState)
})
newGenState := Pricefeed(oldPricefeedGenState)
err = newGenState.Validate()
require.NoError(t, err)
require.Equal(t, len(oldPricefeedGenState.Params.Markets)+1, len(newGenState.Params.Markets))
}
func TestBep3(t *testing.T) {
bz, err := ioutil.ReadFile(filepath.Join("testdata", "kava-4-bep3-state.json"))
require.NoError(t, err)

File diff suppressed because one or more lines are too long