diff --git a/cmd/liked/cmd/cmd.go b/cmd/liked/cmd/cmd.go index cc36b29f36..6c283869cb 100644 --- a/cmd/liked/cmd/cmd.go +++ b/cmd/liked/cmd/cmd.go @@ -174,7 +174,6 @@ func NewRootCmd() (*cobra.Command, app.EncodingConfig) { rootCmd.AddCommand( genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome), genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome), - MigrateGenesisCmd(), genutilcli.GenTxCmd( app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome, diff --git a/cmd/liked/cmd/migrate.go b/cmd/liked/cmd/migrate.go deleted file mode 100644 index d777de15b9..0000000000 --- a/cmd/liked/cmd/migrate.go +++ /dev/null @@ -1,188 +0,0 @@ -package cmd - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "strings" - "time" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/version" - captypes "github.com/cosmos/cosmos-sdk/x/capability/types" - evtypes "github.com/cosmos/cosmos-sdk/x/evidence/types" - "github.com/cosmos/cosmos-sdk/x/genutil/types" - ibcxfertypes "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types" - host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host" - "github.com/cosmos/cosmos-sdk/x/ibc/core/exported" - ibccoretypes "github.com/cosmos/cosmos-sdk/x/ibc/core/types" - "github.com/cosmos/cosmos-sdk/x/params" - paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/cosmos/cosmos-sdk/x/upgrade" - upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" - "github.com/pkg/errors" - "github.com/spf13/cobra" - - tmjson "github.com/tendermint/tendermint/libs/json" - tmtypes "github.com/tendermint/tendermint/types" - - v038 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v038" - v039 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v039" - v040 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v040" - - "github.com/likecoin/likechain/cmd/liked/cmd/oldgenesis" - - iscntypes "github.com/likecoin/likechain/x/iscn/types" -) - -const ( - flagGenesisTime = "genesis-time" - flagInitialHeight = "initial-height" - flagIscnRegistryName = "iscn-registry-name" - flagIscnFeePerByte = "iscn-fee-per-byte" - flagOutput = "output" -) - -func migrateState(initialState types.AppMap, ctx client.Context, iscnParams iscntypes.Params) types.AppMap { - state := initialState - state = v038.Migrate(state, ctx) - state = v039.Migrate(state, ctx) - state = v040.Migrate(state, ctx) - delete(state, "whitelist") - - var stakingGenesis stakingtypes.GenesisState - ctx.JSONMarshaler.MustUnmarshalJSON(state[stakingtypes.ModuleName], &stakingGenesis) - stakingGenesis.Params.HistoricalEntries = 10000 - - ibcTransferGenesis := ibcxfertypes.DefaultGenesisState() - ibcCoreGenesis := ibccoretypes.DefaultGenesisState() - capGenesis := captypes.DefaultGenesis() - evGenesis := evtypes.DefaultGenesisState() - - ibcTransferGenesis.Params.ReceiveEnabled = false - ibcTransferGenesis.Params.SendEnabled = false - - ibcCoreGenesis.ClientGenesis.Params.AllowedClients = []string{exported.Tendermint} - - iscnGenesis := iscntypes.NewGenesisState(iscnParams, nil, nil) - - state[ibcxfertypes.ModuleName] = ctx.JSONMarshaler.MustMarshalJSON(ibcTransferGenesis) - state[host.ModuleName] = ctx.JSONMarshaler.MustMarshalJSON(ibcCoreGenesis) - state[captypes.ModuleName] = ctx.JSONMarshaler.MustMarshalJSON(capGenesis) - state[evtypes.ModuleName] = ctx.JSONMarshaler.MustMarshalJSON(evGenesis) - state[stakingtypes.ModuleName] = ctx.JSONMarshaler.MustMarshalJSON(&stakingGenesis) - state[iscntypes.ModuleName] = ctx.JSONMarshaler.MustMarshalJSON(iscnGenesis) - state[paramstypes.ModuleName] = params.AppModuleBasic{}.DefaultGenesis(ctx.JSONMarshaler) - state[upgradetypes.ModuleName] = upgrade.AppModuleBasic{}.DefaultGenesis(ctx.JSONMarshaler) - return state -} - -func MigrateGenesisCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "migrate [genesis-file-from-sheungwan]", - Short: "Migrate genesis from SheungWan to FoTan", - Long: strings.TrimSpace( - fmt.Sprintf(`Migrate the source genesis into the target version and print to STDOUT. - -Example: -$ %s migrate /path/to/genesis.json --%s=1000000 --%s=likecoin-chain-fotan --%s=2021-12-31T04:00:00Z --%s=likecoin-chain --%s=1234.560000000000000000nanolike -`, - version.AppName, flagInitialHeight, flags.FlagChainID, flagGenesisTime, flagIscnRegistryName, flagIscnFeePerByte)), - - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := client.GetClientContextFromCmd(cmd) - - importGenesis := args[0] - - oldGenDoc, err := oldgenesis.GenesisDocFromFile(importGenesis) - if err != nil { - return errors.Wrapf(err, "failed to read genesis document from file %s", importGenesis) - } - - var initialState types.AppMap - if err := json.Unmarshal(oldGenDoc.AppState, &initialState); err != nil { - return errors.Wrap(err, "failed to JSON unmarshal initial genesis state") - } - - newGenDoc := tmtypes.GenesisDoc{} - newGenDoc.AppHash = oldGenDoc.AppHash - newGenDoc.ConsensusParams = tmtypes.DefaultConsensusParams() - newGenDoc.ConsensusParams.Block = oldGenDoc.ConsensusParams.Block - newGenDoc.ConsensusParams.Validator = oldGenDoc.ConsensusParams.Validator - newGenDoc.Validators = oldGenDoc.Validators - - iscnRegistryName, _ := cmd.Flags().GetString(flagIscnRegistryName) - iscnFeePerByteStr, _ := cmd.Flags().GetString(flagIscnFeePerByte) - iscnFeePerByte, err := sdk.ParseDecCoin(iscnFeePerByteStr) - if err != nil { - return errors.Wrap(err, "failed to parse ISCN fee per byte parameter") - } - iscnParam := iscntypes.Params{ - RegistryName: iscnRegistryName, - FeePerByte: iscnFeePerByte, - } - newGenState := migrateState(initialState, clientCtx, iscnParam) - - newGenDoc.AppState, err = json.Marshal(newGenState) - if err != nil { - return errors.Wrap(err, "failed to JSON marshal migrated genesis state") - } - - genesisTime, _ := cmd.Flags().GetString(flagGenesisTime) - if genesisTime != "" { - var t time.Time - - err := t.UnmarshalText([]byte(genesisTime)) - if err != nil { - return errors.Wrap(err, "failed to unmarshal genesis time") - } - - newGenDoc.GenesisTime = t - } else { - newGenDoc.GenesisTime = oldGenDoc.GenesisTime - } - - chainID, _ := cmd.Flags().GetString(flags.FlagChainID) - if chainID != "" { - newGenDoc.ChainID = chainID - } else { - newGenDoc.ChainID = oldGenDoc.ChainID - } - - initialHeight, _ := cmd.Flags().GetUint64(flagInitialHeight) - newGenDoc.InitialHeight = int64(initialHeight) - - bz, err := tmjson.Marshal(newGenDoc) - 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") - } - - outputPath, _ := cmd.Flags().GetString(flagOutput) - err = ioutil.WriteFile(outputPath, sortedBz, 0o644) - if err != nil { - return errors.Wrap(err, "failed to write JSON genesis doc") - } - - fmt.Printf("Genesis doc written to %s.\n", outputPath) - return nil - }, - } - - cmd.Flags().Uint64(flagInitialHeight, 0, "initial height of the new chain") - cmd.Flags().String(flagGenesisTime, "", "override genesis_time with this flag") - cmd.Flags().String(flags.FlagChainID, "", "override chain_id with this flag") - cmd.Flags().String(flagIscnRegistryName, iscntypes.DefaultRegistryName, "ISCN registry ID parameter in the migrated genesis state") - cmd.Flags().String(flagIscnFeePerByte, iscntypes.DefaultFeePerByte.String(), "ISCN fee per byte parameter in the migrated genesis state") - cmd.Flags().String(flagOutput, "genesis.json", "output path") - - return cmd -} diff --git a/cmd/liked/cmd/oldgenesis/consensus.go b/cmd/liked/cmd/oldgenesis/consensus.go deleted file mode 100644 index 2690e55eb0..0000000000 --- a/cmd/liked/cmd/oldgenesis/consensus.go +++ /dev/null @@ -1,89 +0,0 @@ -package oldgenesis - -import ( - "github.com/tendermint/tendermint/crypto/ed25519" - - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/tendermint/tendermint/types" - - "github.com/pkg/errors" -) - -// ConsensusParams contains consensus critical parameters that determine the -// validity of blocks. -type ConsensusParams struct { - Block tmproto.BlockParams `json:"block"` - Evidence EvidenceParams `json:"evidence"` - Validator tmproto.ValidatorParams `json:"validator"` -} - -// EvidenceParams determine how we handle evidence of malfeasance. -type EvidenceParams struct { - MaxAge int64 `json:"max_age"` // only accept new evidence more recent than this -} - -// DefaultEvidenceParams Params returns a default EvidenceParams. -func DefaultEvidenceParams() EvidenceParams { - return EvidenceParams{ - MaxAge: 100000, // 27.8 hrs at 1block/s - } -} - -func DefaultConsensusParams() *ConsensusParams { - return &ConsensusParams{ - types.DefaultBlockParams(), - DefaultEvidenceParams(), - types.DefaultValidatorParams(), - } -} - -const ( - ABCIPubKeyTypeEd25519 = "ed25519" -) - -var ABCIPubKeyTypesToAminoNames = map[string]string{ - ABCIPubKeyTypeEd25519: ed25519.PubKeyName, -} - -// Validate validates the ConsensusParams to ensure all values are within their -// allowed limits, and returns an error if they are not. -func (params *ConsensusParams) Validate() error { - if params.Block.MaxBytes <= 0 { - return errors.Errorf("Block.MaxBytes must be greater than 0. Got %d", - params.Block.MaxBytes) - } - if params.Block.MaxBytes > types.MaxBlockSizeBytes { - return errors.Errorf("Block.MaxBytes is too big. %d > %d", - params.Block.MaxBytes, types.MaxBlockSizeBytes) - } - - if params.Block.MaxGas < -1 { - return errors.Errorf("Block.MaxGas must be greater or equal to -1. Got %d", - params.Block.MaxGas) - } - - if params.Block.TimeIotaMs <= 0 { - return errors.Errorf("Block.TimeIotaMs must be greater than 0. Got %v", - params.Block.TimeIotaMs) - } - - if params.Evidence.MaxAge <= 0 { - return errors.Errorf("EvidenceParams.MaxAge must be greater than 0. Got %d", - params.Evidence.MaxAge) - } - - if len(params.Validator.PubKeyTypes) == 0 { - return errors.New("len(Validator.PubKeyTypes) must be greater than 0") - } - - // Check if keyType is a known ABCIPubKeyType - for i := 0; i < len(params.Validator.PubKeyTypes); i++ { - keyType := params.Validator.PubKeyTypes[i] - if _, ok := ABCIPubKeyTypesToAminoNames[keyType]; !ok { - return errors.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type", - i, keyType) - } - } - - return nil -} diff --git a/cmd/liked/cmd/oldgenesis/genesis.go b/cmd/liked/cmd/oldgenesis/genesis.go deleted file mode 100644 index 57ff93d0b9..0000000000 --- a/cmd/liked/cmd/oldgenesis/genesis.go +++ /dev/null @@ -1,132 +0,0 @@ -package oldgenesis - -import ( - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "time" - - "github.com/pkg/errors" - - tmcrypto "github.com/tendermint/tendermint/crypto" - "github.com/tendermint/tendermint/crypto/ed25519" - "github.com/tendermint/tendermint/crypto/secp256k1" - "github.com/tendermint/tendermint/crypto/sr25519" - tmbytes "github.com/tendermint/tendermint/libs/bytes" - tmtypes "github.com/tendermint/tendermint/types" - tmtime "github.com/tendermint/tendermint/types/time" - - "github.com/cosmos/cosmos-sdk/codec" - kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" -) - -const ( - // MaxChainIDLen is a maximum length of the chain ID. - MaxChainIDLen = 50 -) - -var cdc = codec.NewLegacyAmino() - -//------------------------------------------------------------ -// core types for a genesis definition -// NOTE: any changes to the genesis definition should -// be reflected in the documentation: -// docs/tendermint-core/using-tendermint.md - -// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set. -type GenesisDoc struct { - GenesisTime time.Time `json:"genesis_time"` - ChainID string `json:"chain_id"` - ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"` - Validators []tmtypes.GenesisValidator `json:"validators,omitempty"` - AppHash tmbytes.HexBytes `json:"app_hash"` - AppState json.RawMessage `json:"app_state,omitempty"` -} - -// ValidateAndComplete checks that all necessary fields are present -// and fills in defaults for optional fields left empty -func (genDoc *GenesisDoc) ValidateAndComplete() error { - if genDoc.ChainID == "" { - return errors.New("Genesis doc must include non-empty chain_id") - } - if len(genDoc.ChainID) > MaxChainIDLen { - return errors.Errorf("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen) - } - - if genDoc.ConsensusParams == nil { - genDoc.ConsensusParams = DefaultConsensusParams() - } else if err := genDoc.ConsensusParams.Validate(); err != nil { - return err - } - - for i, v := range genDoc.Validators { - if v.Power == 0 { - return errors.Errorf("The genesis file cannot contain validators with no voting power: %v", v) - } - if len(v.Address) > 0 && !bytes.Equal(v.PubKey.Address(), v.Address) { - return errors.Errorf("Incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address()) - } - if len(v.Address) == 0 { - genDoc.Validators[i].Address = v.PubKey.Address() - } - } - - if genDoc.GenesisTime.IsZero() { - genDoc.GenesisTime = tmtime.Now() - } - - return nil -} - -//------------------------------------------------------------ -// Make genesis state from file - -// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc. -func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) { - genDoc := GenesisDoc{} - err := cdc.UnmarshalJSON(jsonBlob, &genDoc) - if err != nil { - return nil, err - } - - if err := genDoc.ValidateAndComplete(); err != nil { - return nil, err - } - - return &genDoc, err -} - -// GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc. -func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) { - jsonBlob, err := ioutil.ReadFile(genDocFile) - if err != nil { - return nil, errors.Wrap(err, "Couldn't read GenesisDoc file") - } - genDoc, err := GenesisDocFromJSON(jsonBlob) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("Error reading GenesisDoc at %v", genDocFile)) - } - return genDoc, nil -} - -func init() { - // register tendermint crypto - cdc.RegisterInterface((*tmcrypto.PubKey)(nil), nil) - cdc.RegisterConcrete(sr25519.PubKey{}, - sr25519.PubKeyName, nil) - cdc.RegisterConcrete(&ed25519.PubKey{}, - ed25519.PubKeyName, nil) - cdc.RegisterConcrete(&secp256k1.PubKey{}, - secp256k1.PubKeyName, nil) - cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{}, - kmultisig.PubKeyAminoRoute, nil) - - cdc.RegisterInterface((*tmcrypto.PrivKey)(nil), nil) - cdc.RegisterConcrete(sr25519.PrivKey{}, - sr25519.PrivKeyName, nil) - cdc.RegisterConcrete(&ed25519.PrivKey{}, - ed25519.PrivKeyName, nil) - cdc.RegisterConcrete(&secp256k1.PrivKey{}, - secp256k1.PrivKeyName, nil) -}