|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + dbm "github.com/cosmos/cosmos-db" |
| 9 | + "github.com/cosmos/cosmos-sdk/baseapp" |
| 10 | + "github.com/cosmos/cosmos-sdk/client/flags" |
| 11 | + "github.com/cosmos/cosmos-sdk/server" |
| 12 | + servertypes "github.com/cosmos/cosmos-sdk/server/types" |
| 13 | + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" |
| 14 | + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" |
| 15 | + "github.com/cosmos/evm/evmd" |
| 16 | + evmdconfig "github.com/cosmos/evm/evmd/cmd/evmd/config" |
| 17 | + "github.com/spf13/cast" |
| 18 | + "github.com/spf13/viper" |
| 19 | + |
| 20 | + "cosmossdk.io/log" |
| 21 | + "cosmossdk.io/store" |
| 22 | + "cosmossdk.io/store/snapshots" |
| 23 | + snapshottypes "cosmossdk.io/store/snapshots/types" |
| 24 | + storetypes "cosmossdk.io/store/types" |
| 25 | +) |
| 26 | + |
| 27 | +type appCreator struct{} |
| 28 | + |
| 29 | +func (a appCreator) newApp( |
| 30 | + logger log.Logger, |
| 31 | + db dbm.DB, |
| 32 | + traceStore io.Writer, |
| 33 | + appOpts servertypes.AppOptions, |
| 34 | +) servertypes.Application { |
| 35 | + var cache storetypes.MultiStorePersistentCache |
| 36 | + if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) { |
| 37 | + cache = store.NewCommitKVStoreCacheManager() |
| 38 | + } |
| 39 | + pruningOpts, err := server.GetPruningOptionsFromFlags(appOpts) |
| 40 | + if err != nil { |
| 41 | + panic(err) |
| 42 | + } |
| 43 | + |
| 44 | + skipUpgradeHeights := make(map[int64]bool) |
| 45 | + for _, h := range cast.ToIntSlice(appOpts.Get(server.FlagUnsafeSkipUpgrades)) { |
| 46 | + skipUpgradeHeights[int64(h)] = true |
| 47 | + } |
| 48 | + |
| 49 | + homeDir := cast.ToString(appOpts.Get(flags.FlagHome)) |
| 50 | + chainID := cast.ToString(appOpts.Get(flags.FlagChainID)) |
| 51 | + if chainID == "" { |
| 52 | + // fallback to genesis chain-id |
| 53 | + genDocFile := filepath.Join(homeDir, cast.ToString(appOpts.Get("genesis_file"))) |
| 54 | + appGenesis, err := genutiltypes.AppGenesisFromFile(genDocFile) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + |
| 59 | + chainID = appGenesis.ChainID |
| 60 | + } |
| 61 | + |
| 62 | + snapshotDir := filepath.Join(homeDir, "data", "snapshots") |
| 63 | + snapshotDB, err := dbm.NewDB("metadata", server.GetAppDBBackend(appOpts), snapshotDir) |
| 64 | + if err != nil { |
| 65 | + panic(err) |
| 66 | + } |
| 67 | + snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir) |
| 68 | + if err != nil { |
| 69 | + panic(err) |
| 70 | + } |
| 71 | + |
| 72 | + // BaseApp Opts |
| 73 | + snapshotOptions := snapshottypes.NewSnapshotOptions( |
| 74 | + cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval)), |
| 75 | + cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent)), |
| 76 | + ) |
| 77 | + baseappOptions := []func(*baseapp.BaseApp){ |
| 78 | + baseapp.SetChainID(chainID), |
| 79 | + baseapp.SetPruning(pruningOpts), |
| 80 | + baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))), |
| 81 | + baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))), |
| 82 | + baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))), |
| 83 | + baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))), |
| 84 | + baseapp.SetInterBlockCache(cache), |
| 85 | + baseapp.SetTrace(cast.ToBool(appOpts.Get(server.FlagTrace))), |
| 86 | + baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents))), |
| 87 | + baseapp.SetSnapshot(snapshotStore, snapshotOptions), |
| 88 | + baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))), |
| 89 | + } |
| 90 | + |
| 91 | + return evmd.NewExampleApp( |
| 92 | + logger, |
| 93 | + db, |
| 94 | + traceStore, |
| 95 | + true, |
| 96 | + simtestutil.EmptyAppOptions{}, |
| 97 | + evmdconfig.EVMChainID, |
| 98 | + evmdconfig.EvmAppOptions, |
| 99 | + baseappOptions..., |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +func (a appCreator) appExport( |
| 104 | + logger log.Logger, |
| 105 | + db dbm.DB, |
| 106 | + traceStore io.Writer, |
| 107 | + height int64, |
| 108 | + forZeroHeight bool, |
| 109 | + jailAllowedAddrs []string, |
| 110 | + appOpts servertypes.AppOptions, |
| 111 | + modulesToExport []string, |
| 112 | +) (servertypes.ExportedApp, error) { |
| 113 | + var evmApp *evmd.EVMD |
| 114 | + |
| 115 | + homePath, ok := appOpts.Get(flags.FlagHome).(string) |
| 116 | + if !ok || homePath == "" { |
| 117 | + return servertypes.ExportedApp{}, errors.New("application home is not set") |
| 118 | + } |
| 119 | + |
| 120 | + // InvCheckPeriod |
| 121 | + viperAppOpts, ok := appOpts.(*viper.Viper) |
| 122 | + if !ok { |
| 123 | + return servertypes.ExportedApp{}, errors.New("appOpts is not viper.Viper") |
| 124 | + } |
| 125 | + // overwrite the FlagInvCheckPeriod |
| 126 | + viperAppOpts.Set(server.FlagInvCheckPeriod, 1) |
| 127 | + appOpts = viperAppOpts |
| 128 | + |
| 129 | + var loadLatest bool |
| 130 | + if height == -1 { |
| 131 | + loadLatest = true |
| 132 | + } |
| 133 | + |
| 134 | + evmApp = evmd.NewExampleApp( |
| 135 | + logger, |
| 136 | + db, |
| 137 | + traceStore, |
| 138 | + loadLatest, |
| 139 | + appOpts, |
| 140 | + evmdconfig.EVMChainID, |
| 141 | + evmdconfig.EvmAppOptions, |
| 142 | + ) |
| 143 | + |
| 144 | + if height != -1 { |
| 145 | + if err := evmApp.LoadHeight(height); err != nil { |
| 146 | + return servertypes.ExportedApp{}, err |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return evmApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport) |
| 151 | +} |
0 commit comments