Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2446 from bandprotocol/migrate-guanyu
Browse files Browse the repository at this point in the history
chain: Add migrate command to guanyu version
  • Loading branch information
taobun authored Aug 13, 2020
2 parents a9aea78 + cf124f4 commit 6ddb1a7
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Chain (Non-consensus)

- (feat) [\#2446](https://github.com/bandprotocol/bandchain/pull/2446) Add migrate command to guanyu version.
- (patch) [\#2465](https://github.com/bandprotocol/bandchain/pull/2465) Upgrade to Cosmos-SDK v0.39.1. + [\#2396](https://github.com/bandprotocol/bandchain/pull/2396) Upgrade to Cosmos-SDK v0.39.1-rc3.
- (chore) [\#2381](https://github.com/bandprotocol/bandchain/pull/2381) test: Add more unit tests in keeper.go.

Expand Down
2 changes: 1 addition & 1 deletion chain/cmd/bandd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
// Add subcommands to bandd root cmd.
rootCmd.AddCommand(InitCmd(ctx, cdc, app.NewDefaultGenesisState(), app.DefaultNodeHome))
rootCmd.AddCommand(genutilcli.CollectGenTxsCmd(ctx, cdc, auth.GenesisAccountIterator{}, app.DefaultNodeHome))
rootCmd.AddCommand(genutilcli.MigrateGenesisCmd(ctx, cdc))
rootCmd.AddCommand(MigrateGenesisCmd(ctx, cdc))
rootCmd.AddCommand(genutilcli.GenTxCmd(ctx, cdc, app.ModuleBasics, staking.AppModuleBasic{}, auth.GenesisAccountIterator{}, app.DefaultNodeHome, app.DefaultCLIHome))
rootCmd.AddCommand(genutilcli.ValidateGenesisCmd(ctx, cdc, app.ModuleBasics))
rootCmd.AddCommand(AddGenesisAccountCmd(ctx, cdc, app.DefaultNodeHome, app.DefaultCLIHome))
Expand Down
129 changes: 129 additions & 0 deletions chain/cmd/bandd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"fmt"
"io/ioutil"
"time"

"github.com/bandprotocol/bandchain/chain/app"
"github.com/bandprotocol/bandchain/chain/x/oracle"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
errors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/evidence"
extypes "github.com/cosmos/cosmos-sdk/x/genutil"
v038 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_38"
v039 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_39"
"github.com/cosmos/cosmos-sdk/x/upgrade"
"github.com/spf13/cobra"
tmtypes "github.com/tendermint/tendermint/types"
)

const (
flagGenesisTime = "genesis-time"
flagChainID = "chain-id"
)

// GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
func GenesisDocFromFile(genDocFile string, cdc *codec.Codec) (*tmtypes.GenesisDoc, error) {
jsonBlob, err := ioutil.ReadFile(genDocFile)
if err != nil {
return nil, errors.Wrap(err, "Couldn't read GenesisDoc file")
}

var genDoc tmtypes.GenesisDoc
err = cdc.UnmarshalJSON(jsonBlob, &genDoc)
if err != nil {
return nil, err
}

// Set up Tendermint consensus params with default value.
genDoc.ConsensusParams.Evidence = tmtypes.DefaultEvidenceParams()

if err := genDoc.ValidateAndComplete(); err != nil {
return nil, err
}

return &genDoc, nil
}

// MigrateGenesisCmd returns a command to execute genesis state migration.
// nolint: funlen
func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "migrate [genesis-file]",
Short: "Migrate Wenchang genesis to Guanyu version",
Long: fmt.Sprintf(`Migrate the Wenchang genesis into the Guanyu version and print to STDOUT.
Example:
$ %s migrate /path/to/genesis.json --chain-id=band-guanyu --genesis-time=2020-08-11T17:00:00Z
`, version.ServerName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error

importGenesis := args[0]
genDoc, err := GenesisDocFromFile(importGenesis, cdc)
if err != nil {
return errors.Wrapf(err, "failed to read genesis document from file %s", importGenesis)
}

var state extypes.AppMap
if err := cdc.UnmarshalJSON(genDoc.AppState, &state); err != nil {
return errors.Wrap(err, "failed to JSON unmarshal initial genesis state")
}

// Migrate from Wenchang (0.36 like genesis file) to cosmos-sdk v0.39
state = v039.Migrate(v038.Migrate(state))

// Add genesis state of the new modules get state from `app` default genesis.
defaultGuanyu := app.NewDefaultGenesisState()
state[oracle.ModuleName] = cdc.MustMarshalJSON(defaultGuanyu[oracle.ModuleName])
state[evidence.ModuleName] = cdc.MustMarshalJSON(defaultGuanyu[evidence.ModuleName])
state[upgrade.ModuleName] = cdc.MustMarshalJSON(defaultGuanyu[upgrade.ModuleName])

genDoc.AppState, err = cdc.MarshalJSON(state)
if err != nil {
return errors.Wrap(err, "failed to JSON marshal migrated genesis state")
}
if err := genDoc.ValidateAndComplete(); err != nil {
return err
}

genesisTime := cmd.Flag(flagGenesisTime).Value.String()
if genesisTime != "" {
var t time.Time

err := t.UnmarshalText([]byte(genesisTime))
if err != nil {
return errors.Wrap(err, "failed to unmarshal genesis time")
}

genDoc.GenesisTime = t
}

chainID := cmd.Flag(flagChainID).Value.String()
if chainID != "" {
genDoc.ChainID = chainID
}

bz, err := cdc.MarshalJSONIndent(genDoc, "", " ")
if err != nil {
return errors.Wrap(err, "failed to marshal genesis doc")
}

sortedBz, err := sdk.SortJSON(bz)
if err != nil {
return errors.Wrap(err, "failed to sort JSON genesis doc")
}

fmt.Println(string(sortedBz))
return nil
},
}
cmd.Flags().String(flagGenesisTime, "", "override genesis_time with this flag")
cmd.Flags().String(flagChainID, "band-guanyu", "override chain_id with this flag")
return cmd
}

0 comments on commit 6ddb1a7

Please sign in to comment.