Skip to content

Commit

Permalink
prepare software upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Sep 24, 2024
1 parent 973f949 commit 0a509c4
Show file tree
Hide file tree
Showing 6 changed files with 1,392 additions and 131 deletions.
2 changes: 1 addition & 1 deletion app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
)

const upgradeName = "0.2.2"
const upgradeName = "0.4.11-initation.0"

// RegisterUpgradeHandlers returns upgrade handlers
func (app *InitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) {
Expand Down
70 changes: 70 additions & 0 deletions proto/initia/gov/v1/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,73 @@ message Proposal {
// Since: cosmos-sdk 0.50
string failed_reason = 18;
}

// LegacyProposal defines the core field members of a governance proposal.
message LegacyProposal {
// id defines the unique id of the proposal.
uint64 id = 1;

// messages are the arbitrary messages to be executed if the proposal passes.
repeated google.protobuf.Any messages = 2;

// status defines the proposal status.
cosmos.gov.v1.ProposalStatus status = 3;

// final_tally_result is the final tally result of the proposal. When
// querying a proposal via gRPC, this field is not populated until the
// proposal's voting period has ended.
cosmos.gov.v1.TallyResult final_tally_result = 4;

// submit_time is the time of proposal submission.
google.protobuf.Timestamp submit_time = 5 [(gogoproto.stdtime) = true];

// deposit_end_time is the end time for deposition.
google.protobuf.Timestamp deposit_end_time = 6 [(gogoproto.stdtime) = true];

// total_deposit is the total deposit on the proposal.
repeated cosmos.base.v1beta1.Coin total_deposit = 7 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];

// voting_start_time is the starting time to vote on a proposal.
google.protobuf.Timestamp voting_start_time = 8 [(gogoproto.stdtime) = true];

// voting_end_time is the end time of voting on a proposal.
google.protobuf.Timestamp voting_end_time = 9 [(gogoproto.stdtime) = true];

google.protobuf.Timestamp emergency_start_time = 10 [(gogoproto.stdtime) = true];
google.protobuf.Timestamp emergency_next_tally_time = 11 [(gogoproto.stdtime) = true];

// metadata is any arbitrary metadata attached to the proposal.
// the recommended format of the metadata is to be found here:
// https://docs.cosmos.network/v0.47/modules/gov#proposal-3
string metadata = 12;

// title is the title of the proposal
//
// Since: cosmos-sdk 0.47
string title = 13;

// summary is a short summary of the proposal
//
// Since: cosmos-sdk 0.47
string summary = 14;

// proposer is the address of the proposal sumbitter
//
// Since: cosmos-sdk 0.47
string proposer = 15 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// expedited defines if the proposal is expedited
//
// Since: cosmos-sdk 0.50
bool expedited = 16;

bool emergency = 17;

// failed_reason defines the reason why the proposal failed
//
// Since: cosmos-sdk 0.50
string failed_reason = 18;
}
24 changes: 24 additions & 0 deletions x/gov/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v2 "github.com/initia-labs/initia/x/gov/migrations/v2"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper *Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper *Keeper) Migrator {
return Migrator{
keeper: keeper,
}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.Proposals, m.keeper.storeService, m.keeper.cdc)
}
66 changes: 66 additions & 0 deletions x/gov/migrations/v2/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package v1

import (
"context"
"fmt"

"cosmossdk.io/collections"
corestoretypes "cosmossdk.io/core/store"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/gov/types"
customtypes "github.com/initia-labs/initia/x/gov/types"
)

const (
ModuleName = "gov"
)

func ConvertLegacyProposalToProposal(proposal customtypes.LegacyProposal) customtypes.Proposal {
return customtypes.Proposal{
Id: proposal.Id,
Messages: proposal.Messages,
EmergencyStartTime: proposal.EmergencyStartTime,
EmergencyNextTallyTime: proposal.EmergencyNextTallyTime,
Metadata: proposal.Metadata,
Title: proposal.Title,
Summary: proposal.Summary,
Proposer: proposal.Proposer,
Expedited: proposal.Expedited,
Emergency: proposal.Emergency,
FailedReason: proposal.FailedReason,
Status: proposal.Status,
SubmitTime: proposal.SubmitTime,
DepositEndTime: proposal.DepositEndTime,
TotalDeposit: proposal.TotalDeposit,
VotingStartTime: proposal.VotingStartTime,
VotingEndTime: proposal.VotingEndTime,

// Convert the final tally result
FinalTallyResult: customtypes.TallyResult{
TallyHeight: 0,
TotalStakingPower: "0",
TotalVestingPower: "0",
V1TallyResult: proposal.FinalTallyResult,
},
}
}

func MigrateStore(
ctx context.Context,
proposals collections.Map[uint64, customtypes.Proposal],
storeService corestoretypes.KVStoreService, cdc codec.BinaryCodec,
) error {
sb := collections.NewSchemaBuilder(storeService)
legacyProposals := collections.NewMap(sb, types.ProposalsKeyPrefix, "proposals", collections.Uint64Key, codec.CollValue[customtypes.LegacyProposal](cdc))
_, err := sb.Build()
if err != nil {
return err
}

fmt.Println("SIBONG")
return legacyProposals.Walk(ctx, nil, func(pid uint64, lp customtypes.LegacyProposal) (bool, error) {
p := ConvertLegacyProposalToProposal(lp)
return false, proposals.Set(ctx, pid, p)
})
}
41 changes: 23 additions & 18 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
"github.com/cosmos/cosmos-sdk/x/gov/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

Expand All @@ -39,7 +39,7 @@ import (
customcli "github.com/initia-labs/initia/x/gov/client/cli"
)

const ConsensusVersion = 1
const ConsensusVersion = 2

var (
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -67,7 +67,7 @@ func NewAppModuleBasic(cdc codec.Codec, legacyProposalHandlers ...govclient.Prop

// Name returns the gov module's name.
func (AppModuleBasic) Name() string {
return types.ModuleName
return govtypes.ModuleName
}

// RegisterLegacyAminoCodec registers the gov module's types for the given codec.
Expand All @@ -87,7 +87,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
var data customtypes.GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
return fmt.Errorf("failed to unmarshal %s genesis state: %w", govtypes.ModuleName, err)
}

return customtypes.ValidateGenesis(&data, b.cdc.InterfaceRegistry().SigningContext().AddressCodec())
Expand Down Expand Up @@ -136,16 +136,16 @@ type AppModule struct {
AppModuleBasic

keeper *keeper.Keeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
accountKeeper govtypes.AccountKeeper
bankKeeper govtypes.BankKeeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(
cdc codec.Codec,
keeper *keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
ak govtypes.AccountKeeper,
bk govtypes.BankKeeper,
) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{cdc: cdc},
Expand Down Expand Up @@ -177,10 +177,10 @@ type ModuleInputs struct {
ModuleKey depinject.OwnModuleKey
MsgServiceRouter baseapp.MessageRouter

AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
AccountKeeper govtypes.AccountKeeper
BankKeeper govtypes.BankKeeper
StakingKeeper customtypes.StakingKeeper
DistributionKeeper types.DistributionKeeper
DistributionKeeper govtypes.DistributionKeeper
VestingKeeper customtypes.VestingKeeper
}

Expand All @@ -193,13 +193,13 @@ type ModuleOutputs struct {
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
defaultConfig := types.DefaultConfig()
defaultConfig := govtypes.DefaultConfig()
if in.Config.MaxMetadataLen != 0 {
defaultConfig.MaxMetadataLen = in.Config.MaxMetadataLen
}

// default to governance authority if not provided
authority := authtypes.NewModuleAddress(types.ModuleName)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
if in.Config.Authority != "" {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}
Expand All @@ -218,7 +218,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
)

m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper)
hr := v1beta1.HandlerRoute{Handler: v1beta1.ProposalHandler, RouteKey: types.RouterKey}
hr := v1beta1.HandlerRoute{Handler: v1beta1.ProposalHandler, RouteKey: govtypes.RouterKey}

return ModuleOutputs{Module: m, Keeper: k, HandlerRoute: hr}
}
Expand All @@ -241,7 +241,7 @@ func InvokeAddRoutes(keeper *keeper.Keeper, routes []v1beta1.HandlerRoute) {
keeper.SetLegacyRouter(router)
}

func InvokeSetHooks(keeper *keeper.Keeper, govHooks map[string]types.GovHooksWrapper) error {
func InvokeSetHooks(keeper *keeper.Keeper, govHooks map[string]govtypes.GovHooksWrapper) error {
if keeper == nil || govHooks == nil {
return nil
}
Expand All @@ -252,7 +252,7 @@ func InvokeSetHooks(keeper *keeper.Keeper, govHooks map[string]types.GovHooksWra
order := modNames
sort.Strings(order)

var multiHooks types.MultiGovHooks
var multiHooks govtypes.MultiGovHooks
for _, modName := range order {
hook, ok := govHooks[modName]
if !ok {
Expand All @@ -267,7 +267,7 @@ func InvokeSetHooks(keeper *keeper.Keeper, govHooks map[string]types.GovHooksWra

// Name returns the gov module's name.
func (AppModule) Name() string {
return types.ModuleName
return govtypes.ModuleName
}

// RegisterInvariants registers module invariants
Expand All @@ -278,7 +278,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
msgServer := keeper.NewMsgServerImpl(am.keeper)
v1beta1.RegisterMsgServer(cfg.MsgServer(), keeper.NewLegacyMsgServerImpl(am.accountKeeper.GetModuleAddress(types.ModuleName).String(), msgServer))
v1beta1.RegisterMsgServer(cfg.MsgServer(), keeper.NewLegacyMsgServerImpl(am.accountKeeper.GetModuleAddress(govtypes.ModuleName).String(), msgServer))
v1.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))

legacyQueryServer := keeper.NewLegacyQueryServer(am.keeper)
Expand All @@ -287,6 +287,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {

customtypes.RegisterMsgServer(cfg.MsgServer(), keeper.NewCustomMsgServerImpl(am.keeper))
customtypes.RegisterQueryServer(cfg.QueryServer(), keeper.NewCustomQueryServer(am.keeper))

m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(govtypes.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/gov from version 1 to 2: %v", err))
}
}

// InitGenesis performs genesis initialization for the gov module. It returns
Expand Down
Loading

0 comments on commit 0a509c4

Please sign in to comment.