From c26945ae0a0a42ea1be4b21bc00112d34573cb52 Mon Sep 17 00:00:00 2001 From: Phuong Do Date: Fri, 28 May 2021 14:40:13 +0700 Subject: [PATCH] Create migrate to fix staking bug --- cmd/shareledger/migrate-genesis.go | 100 ++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/cmd/shareledger/migrate-genesis.go b/cmd/shareledger/migrate-genesis.go index 25de51f2..17caf1fb 100644 --- a/cmd/shareledger/migrate-genesis.go +++ b/cmd/shareledger/migrate-genesis.go @@ -6,8 +6,12 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution" "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/cosmos/cosmos-sdk/x/slashing" + "github.com/cosmos/cosmos-sdk/x/staking" gentlemint "github.com/sharering/shareledger/x/gentlemint" + "github.com/sharering/shareledger/x/identity" oldId "github.com/sharering/shareledger/x/identity" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -67,6 +71,95 @@ func FromV0_0_1(oldGenesisFile, newGenesisFile, mergeGenesisFile string, cdc *co // Copy id data: Id data is totally different, can not copy } +func FromV1_1_0(inputFilePath, outputFilePath string, cdc *codec.Codec) error { + appState, genDoc, err := genutil.GenesisStateFromGenFile(cdc, inputFilePath) + if err != nil { + return fmt.Errorf("failed to unmarshal old genesis state: %w", err) + } + + // Staking module + var stakingState staking.GenesisState + if appState[staking.ModuleName] != nil { + cdc.MustUnmarshalJSON(appState[staking.ModuleName], &stakingState) + } + + for i := 0; i < len(stakingState.Validators); i++ { + stakingState.Validators[i].UnbondingHeight = 0 + } + + for i := 0; i < len(stakingState.UnbondingDelegations); i++ { + for j := 0; j < len(stakingState.UnbondingDelegations[i].Entries); j++ { + stakingState.UnbondingDelegations[i].Entries[j].CreationHeight = 0 + } + } + + for i := 0; i < len(stakingState.Redelegations); i++ { + for j := 0; j < len(stakingState.Redelegations[i].Entries); j++ { + stakingState.Redelegations[i].Entries[j].CreationHeight = 0 + } + } + + stakingStateBz, err := cdc.MarshalJSON(stakingState) + if err != nil { + return fmt.Errorf("failed to marshal auth genesis state: %w", err) + } + appState[staking.ModuleName] = stakingStateBz + + // Distribution module + var distState distribution.GenesisState + if appState[distribution.ModuleName] != nil { + cdc.MustUnmarshalJSON(appState[distribution.ModuleName], &distState) + } + + for i := 0; i < len(distState.DelegatorStartingInfos); i++ { + distState.DelegatorStartingInfos[i].StartingInfo.Height = 0 + } + + for i := 0; i < len(distState.ValidatorSlashEvents); i++ { + distState.ValidatorSlashEvents[i].Height = 0 + } + + distStateBz, err := cdc.MarshalJSON(distState) + if err != nil { + return fmt.Errorf("failed to marshal auth genesis state: %w", err) + } + appState[distribution.ModuleName] = distStateBz + + // Slashing module + var slashingState slashing.GenesisState + if appState[slashing.ModuleName] != nil { + cdc.MustUnmarshalJSON(appState[slashing.ModuleName], &slashingState) + } + + for k, v := range slashingState.SigningInfos { + v.StartHeight = 0 + slashingState.SigningInfos[k] = v + } + + slashingStateBz, err := cdc.MarshalJSON(slashingState) + if err != nil { + return fmt.Errorf("failed to marshal auth genesis state: %w", err) + } + appState[slashing.ModuleName] = slashingStateBz + + // Reset old id module + var identityState identity.GenesisState + identityStateBz, err := cdc.MarshalJSON(identityState) + if err != nil { + return fmt.Errorf("failed to marshal auth genesis state: %w", err) + } + appState[oldId.ModuleName] = identityStateBz + + appStateJSON, err := cdc.MarshalJSON(appState) + if err != nil { + return fmt.Errorf("failed to marshal application genesis state: %w", err) + } + + genDoc.AppState = appStateJSON + + return genutil.ExportGenesisFile(genDoc, outputFilePath) +} + func AddGenesisCustomMigrate( ctx *server.Context, cdc *codec.Codec, defaultNodeHome, defaultClientHome string, ) *cobra.Command { @@ -74,7 +167,10 @@ func AddGenesisCustomMigrate( cmd := &cobra.Command{ Use: "custom-migrate ", Short: "Copy data from old genesis file the new one.", - Long: `Copy data from old genesis file the new one. This command generates merge file.`, + Long: `Copy data from old genesis file the new one. This command generates merge file. + version 0.0.1: custom-migrate 0.0.1 + version 1.1.0: custom-migrate 1.1.0 + `, // Args: cobra.ExactArgs(1), RunE: func(_ *cobra.Command, args []string) error { config := ctx.Config @@ -83,6 +179,8 @@ func AddGenesisCustomMigrate( switch v := args[0]; v { case "0.0.1": return FromV0_0_1(args[1], args[2], args[3], cdc) + case "1.1.0": + return FromV1_1_0(args[1], args[2], cdc) default: fmt.Println("No match version " + v) }