Skip to content

Commit

Permalink
Add IBC markets to pricefeed migration (Kava-Labs#1131)
Browse files Browse the repository at this point in the history
* Add ibc markets to pricefeed

* Default empty oracles if no markets

* Update pricefeed migration tests

* Add ibc markets to testdata genesis pricefeed

* Add existing pricefeed markets and new ibc markets to committee permissions

* Remove stray empty branch

* Panic on committee migration if pricefeed app state is missing

* Remove redundant import
  • Loading branch information
drklee3 authored Jan 14, 2022
1 parent d71f406 commit 1a51d1f
Show file tree
Hide file tree
Showing 10 changed files with 959 additions and 34 deletions.
9 changes: 8 additions & 1 deletion migrate/v0_16/kava.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,19 @@ func migrateKavaAppState(appState genutiltypes.AppMap, clientCtx client.Context)

// Migrate x/committee
if appState[v015committee.ModuleName] != nil {
if appState[v015pricefeed.ModuleName] == nil {
panic("pricefeed app state is missing, committee migration requires pricefeed app state")
}

var pricefeedGenState v015pricefeed.GenesisState
v15Codec.MustUnmarshalJSON(appState[v015pricefeed.ModuleName], &pricefeedGenState)

// unmarshal relative source genesis application state
var genState v015committee.GenesisState
v15Codec.MustUnmarshalJSON(appState[v015committee.ModuleName], &genState)

// replace migrated genstate with previous genstate
appState[v015committee.ModuleName] = v16Codec.MustMarshalJSON(v016committee.Migrate(genState))
appState[v015committee.ModuleName] = v16Codec.MustMarshalJSON(v016committee.Migrate(genState, pricefeedGenState))
}

// Migrate x/bep3
Expand Down
3 changes: 1 addition & 2 deletions migrate/v0_16/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
v036supply "github.com/cosmos/cosmos-sdk/x/bank/legacy/v036"
v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v038"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
genutil "github.com/cosmos/cosmos-sdk/x/genutil/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/types"
Expand Down Expand Up @@ -46,7 +45,7 @@ func TestMigrateFull(t *testing.T) {
assert.NoError(t, err)

encodingConfig := app.MakeEncodingConfig()
var appMap genutil.AppMap
var appMap genutiltypes.AppMap
err = tmjson.Unmarshal(newGenDoc.AppState, &appMap)
assert.NoError(t, err)
err = app.ModuleBasics.ValidateGenesis(encodingConfig.Marshaler, encodingConfig.TxConfig, appMap)
Expand Down
120 changes: 120 additions & 0 deletions migrate/v0_16/testdata/genesis-v16.json
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,76 @@
"market_id": "bnb:usd:30",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "atom",
"market_id": "atom:usd",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "atom",
"market_id": "atom:usd:30",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "akt",
"market_id": "akt:usd",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "akt",
"market_id": "akt:usd:30",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "luna",
"market_id": "luna:usd",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "luna",
"market_id": "luna:usd:30",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "osmo",
"market_id": "osmo:usd",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "osmo",
"market_id": "osmo:usd:30",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "ust",
"market_id": "ust:usd",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
},
{
"active": true,
"base_asset": "ust",
"market_id": "ust:usd:30",
"oracles": ["kava1acge4tcvhf3q6fh53fgwaa7vsq40wvx6wn50em"],
"quote_asset": "usd"
}
]
},
Expand Down Expand Up @@ -1382,6 +1452,56 @@
"key": "market_id",
"val": "busd:usd:30",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "atom:usd",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "atom:usd:30",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "akt:usd",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "akt:usd:30",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "luna:usd",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "luna:usd:30",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "osmo:usd",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "osmo:usd:30",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "ust:usd",
"allowed_subparam_attr_changes": ["active"]
},
{
"key": "market_id",
"val": "ust:usd:30",
"allowed_subparam_attr_changes": ["active"]
}
]
},
Expand Down
72 changes: 56 additions & 16 deletions x/committee/legacy/v0_16/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
v016hardtypes "github.com/kava-labs/kava/x/hard/types"
v015kavadist "github.com/kava-labs/kava/x/kavadist/legacy/v0_15"
v016kavadist "github.com/kava-labs/kava/x/kavadist/types"
v015pricefeed "github.com/kava-labs/kava/x/pricefeed/legacy/v0_15"
v016pricefeedmigration "github.com/kava-labs/kava/x/pricefeed/legacy/v0_16"
v016pricefeedtypes "github.com/kava-labs/kava/x/pricefeed/types"
)

Expand Down Expand Up @@ -61,7 +63,11 @@ type subspaceKeyPair struct {
}

// migrateSubParamPermissions converts v15 SubParamChangePermissions to v16 ParamsChangePermission
func migrateSubParamPermissions(permission v015committee.SubParamChangePermission, isStabilityCommittee bool) *v016committee.ParamsChangePermission {
func migrateSubParamPermissions(
permission v015committee.SubParamChangePermission,
isStabilityCommittee bool,
oldPricefeedState v015pricefeed.GenesisState,
) *v016committee.ParamsChangePermission {
changes := v016committee.AllowedParamsChanges{}

// migrate allowed params
Expand Down Expand Up @@ -171,6 +177,39 @@ func migrateSubParamPermissions(permission v015committee.SubParamChangePermissio
requirement.AllowedSubparamAttrChanges = allowed
requirements = append(requirements, requirement)
}

if isStabilityCommittee {
// Add permissions for existing pricefeed markets that are missing in allowed_markets
outer:
for _, oldPricefeedMarket := range oldPricefeedState.Params.Markets {
// Skip if this oldPricefeedMarket already exists in requirements
for _, marketRequirement := range requirements {
if marketRequirement.Val == oldPricefeedMarket.MarketID {
continue outer
}
}

requirement := v016committee.SubparamRequirement{
Key: "market_id",
Val: oldPricefeedMarket.MarketID,
AllowedSubparamAttrChanges: []string{"active"},
}

requirements = append(requirements, requirement)
}

// Add new IBC denoms for stability committee
for _, market := range v016pricefeedmigration.NewIBCMarkets {
requirement := v016committee.SubparamRequirement{
Key: "market_id",
Val: market.MarketID,
AllowedSubparamAttrChanges: []string{"active"},
}

requirements = append(requirements, requirement)
}
}

change.MultiSubparamsRequirements = requirements
changes = append(changes, change)
}
Expand Down Expand Up @@ -215,7 +254,11 @@ func migrateSubParamPermissions(permission v015committee.SubParamChangePermissio
}
}

func migratePermission(v015permission v015committee.Permission, isStabilityCommittee bool) *codectypes.Any {
func migratePermission(
v015permission v015committee.Permission,
isStabilityCommittee bool,
oldPricefeedState v015pricefeed.GenesisState,
) *codectypes.Any {
var protoProposal proto.Message

switch v015permission := v015permission.(type) {
Expand Down Expand Up @@ -246,7 +289,7 @@ func migratePermission(v015permission v015committee.Permission, isStabilityCommi
}
case v015committee.SubParamChangePermission:
{
protoProposal = migrateSubParamPermissions(v015permission, isStabilityCommittee)
protoProposal = migrateSubParamPermissions(v015permission, isStabilityCommittee, oldPricefeedState)
}
default:
panic(fmt.Errorf("'%s' is not a valid permission", v015permission))
Expand Down Expand Up @@ -274,15 +317,15 @@ func migrateTallyOption(oldTallyOption v015committee.TallyOption) v016committee.
}
}

func migrateCommittee(committee v015committee.Committee) *codectypes.Any {
func migrateCommittee(committee v015committee.Committee, oldPricefeedState v015pricefeed.GenesisState) *codectypes.Any {
var protoProposal proto.Message
switch committee := committee.(type) {
case v015committee.MemberCommittee:
{
permissions := make([]*codectypes.Any, len(committee.Permissions))
for i, permission := range committee.Permissions {
isStabilityCommittee := committee.GetID() == 1
permissions[i] = migratePermission(permission, isStabilityCommittee)
permissions[i] = migratePermission(permission, isStabilityCommittee, oldPricefeedState)
}

protoProposal = &v016committee.MemberCommittee{
Expand All @@ -301,7 +344,7 @@ func migrateCommittee(committee v015committee.Committee) *codectypes.Any {
{
permissions := make([]*codectypes.Any, len(committee.Permissions))
for i, permission := range committee.Permissions {
permissions[i] = migratePermission(permission, false)
permissions[i] = migratePermission(permission, false, oldPricefeedState)
}

protoProposal = &v016committee.TokenCommittee{
Expand All @@ -322,12 +365,6 @@ func migrateCommittee(committee v015committee.Committee) *codectypes.Any {
panic(fmt.Errorf("'%s' is not a valid committee", committee))
}

// Make some updates to the stability committee
if committee.GetID() == 1 {
// Add requirement to collatora params

}

// Convert the content into Any.
contentAny, err := codectypes.NewAnyWithValue(protoProposal)
if err != nil {
Expand All @@ -337,10 +374,10 @@ func migrateCommittee(committee v015committee.Committee) *codectypes.Any {
return contentAny
}

func migrateCommittees(v015committees v015committee.Committees) []*codectypes.Any {
func migrateCommittees(v015committees v015committee.Committees, oldPricefeedState v015pricefeed.GenesisState) []*codectypes.Any {
committees := make([]*codectypes.Any, len(v015committees))
for i, committee := range v015committees {
committees[i] = migrateCommittee(committee)
committees[i] = migrateCommittee(committee, oldPricefeedState)
}
return committees
}
Expand Down Expand Up @@ -477,10 +514,13 @@ func migrateVotes(v15votes []v015committee.Vote) []v016committee.Vote {
return votes
}

func Migrate(oldState v015committee.GenesisState) *v016committee.GenesisState {
func Migrate(
oldState v015committee.GenesisState,
oldPricefeedState v015pricefeed.GenesisState,
) *v016committee.GenesisState {
newState := v016committee.GenesisState{
NextProposalID: oldState.NextProposalID,
Committees: migrateCommittees(oldState.Committees),
Committees: migrateCommittees(oldState.Committees, oldPricefeedState),
Proposals: migrateProposals(oldState.Proposals),
Votes: migrateVotes(oldState.Votes),
}
Expand Down
3 changes: 2 additions & 1 deletion x/committee/legacy/v0_16/migrate_subparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@ func (s *migrateTestSuite) TestMigrate_Committee_SubparamPermissions() {
}
expectedProposal, err := v016committee.NewMemberCommittee(2, "test", s.addresses, []v016committee.Permission{tc.v016permission}, oldCommittee.VoteThreshold, oldCommittee.ProposalDuration, v016committee.TALLY_OPTION_FIRST_PAST_THE_POST)
s.Require().NoError(err)

s.v15genstate.Committees = []v015committee.Committee{oldCommittee}
genState := Migrate(s.v15genstate)
genState := Migrate(s.v15genstate, s.v15pricefeedgenstate)
s.Require().Len(genState.Committees, 1)
s.Equal(expectedProposal, genState.GetCommittees()[0])
})
Expand Down
Loading

0 comments on commit 1a51d1f

Please sign in to comment.