2024-08-03 11:36:47 +00:00
|
|
|
package main
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
2022-05-04 22:58:14 +00:00
|
|
|
"strings"
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2024-02-06 22:54:10 +00:00
|
|
|
cometbftdb "github.com/cometbft/cometbft-db"
|
|
|
|
"github.com/cometbft/cometbft/libs/log"
|
|
|
|
tmtypes "github.com/cometbft/cometbft/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
servertypes "github.com/cosmos/cosmos-sdk/server/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/snapshots"
|
2023-04-04 00:08:45 +00:00
|
|
|
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/crisis"
|
2023-04-04 00:08:45 +00:00
|
|
|
ethermintflags "github.com/evmos/ethermint/server/flags"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
|
|
|
"github.com/0glabs/0g-chain/app/params"
|
|
|
|
metricstypes "github.com/0glabs/0g-chain/x/metrics/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagMempoolEnableAuth = "mempool.enable-authentication"
|
|
|
|
flagMempoolAuthAddresses = "mempool.authorized-addresses"
|
2024-03-01 00:22:09 +00:00
|
|
|
flagSkipLoadLatest = "skip-load-latest"
|
2022-01-08 00:39:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// appCreator holds functions used by the sdk server to control the kava app.
|
|
|
|
// The methods implement types in cosmos-sdk/server/types
|
|
|
|
type appCreator struct {
|
|
|
|
encodingConfig params.EncodingConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// newApp loads config from AppOptions and returns a new app.
|
|
|
|
func (ac appCreator) newApp(
|
|
|
|
logger log.Logger,
|
2024-02-06 22:54:10 +00:00
|
|
|
db cometbftdb.DB,
|
2022-01-08 00:39:27 +00:00
|
|
|
traceStore io.Writer,
|
|
|
|
appOpts servertypes.AppOptions,
|
|
|
|
) servertypes.Application {
|
|
|
|
var cache sdk.MultiStorePersistentCache
|
|
|
|
if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) {
|
|
|
|
cache = store.NewCommitKVStoreCacheManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
skipUpgradeHeights := make(map[int64]bool)
|
|
|
|
for _, h := range cast.ToIntSlice(appOpts.Get(server.FlagUnsafeSkipUpgrades)) {
|
|
|
|
skipUpgradeHeights[int64(h)] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
pruningOpts, err := server.GetPruningOptionsFromFlags(appOpts)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
|
|
|
|
snapshotDir := filepath.Join(homeDir, "data", "snapshots") // TODO can these directory names be imported from somewhere?
|
2024-02-06 22:54:10 +00:00
|
|
|
snapshotDB, err := cometbftdb.NewDB("metadata", server.GetAppDBBackend(appOpts), snapshotDir)
|
2022-01-08 00:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mempoolEnableAuth := cast.ToBool(appOpts.Get(flagMempoolEnableAuth))
|
|
|
|
mempoolAuthAddresses, err := accAddressesFromBech32(
|
|
|
|
cast.ToStringSlice(appOpts.Get(flagMempoolAuthAddresses))...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("could not get authorized address from config: %v", err))
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
iavlDisableFastNode := appOpts.Get(server.FlagDisableIAVLFastNode)
|
2022-10-19 18:11:17 +00:00
|
|
|
if iavlDisableFastNode == nil {
|
|
|
|
iavlDisableFastNode = true
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
snapshotOptions := snapshottypes.NewSnapshotOptions(
|
|
|
|
cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval)),
|
|
|
|
cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent)),
|
|
|
|
)
|
|
|
|
|
2024-02-06 22:54:10 +00:00
|
|
|
// Setup chainId
|
|
|
|
chainID := cast.ToString(appOpts.Get(flags.FlagChainID))
|
|
|
|
if len(chainID) == 0 {
|
|
|
|
// fallback to genesis chain-id
|
|
|
|
appGenesis, err := tmtypes.GenesisDocFromFile(filepath.Join(homeDir, "config", "genesis.json"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
chainID = appGenesis.ChainID
|
|
|
|
}
|
|
|
|
|
2024-03-01 00:22:09 +00:00
|
|
|
skipLoadLatest := false
|
|
|
|
if appOpts.Get(flagSkipLoadLatest) != nil {
|
|
|
|
skipLoadLatest = cast.ToBool(appOpts.Get(flagSkipLoadLatest))
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
return app.NewApp(
|
|
|
|
logger, db, homeDir, traceStore, ac.encodingConfig,
|
|
|
|
app.Options{
|
2024-03-01 00:22:09 +00:00
|
|
|
SkipLoadLatest: skipLoadLatest,
|
2022-01-08 00:39:27 +00:00
|
|
|
SkipUpgradeHeights: skipUpgradeHeights,
|
|
|
|
SkipGenesisInvariants: cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants)),
|
|
|
|
InvariantCheckPeriod: cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
|
|
|
|
MempoolEnableAuth: mempoolEnableAuth,
|
|
|
|
MempoolAuthAddresses: mempoolAuthAddresses,
|
2022-04-21 20:16:28 +00:00
|
|
|
EVMTrace: cast.ToString(appOpts.Get(ethermintflags.EVMTracer)),
|
|
|
|
EVMMaxGasWanted: cast.ToUint64(appOpts.Get(ethermintflags.EVMMaxTxGasWanted)),
|
2023-08-25 19:23:53 +00:00
|
|
|
TelemetryOptions: metricstypes.TelemetryOptionsFromAppOpts(appOpts),
|
2022-01-08 00:39:27 +00:00
|
|
|
},
|
|
|
|
baseapp.SetPruning(pruningOpts),
|
2022-05-04 22:58:14 +00:00
|
|
|
baseapp.SetMinGasPrices(strings.Replace(cast.ToString(appOpts.Get(server.FlagMinGasPrices)), ";", ",", -1)),
|
2022-01-08 00:39:27 +00:00
|
|
|
baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))),
|
|
|
|
baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))),
|
|
|
|
baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))), // TODO what is this?
|
|
|
|
baseapp.SetInterBlockCache(cache),
|
|
|
|
baseapp.SetTrace(cast.ToBool(appOpts.Get(server.FlagTrace))),
|
|
|
|
baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents))),
|
2023-04-04 00:08:45 +00:00
|
|
|
baseapp.SetSnapshot(snapshotStore, snapshotOptions),
|
2022-10-19 18:11:17 +00:00
|
|
|
baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))),
|
|
|
|
baseapp.SetIAVLDisableFastNode(cast.ToBool(iavlDisableFastNode)),
|
2023-04-04 00:08:45 +00:00
|
|
|
baseapp.SetIAVLLazyLoading(cast.ToBool(appOpts.Get(server.FlagIAVLLazyLoading))),
|
2024-02-06 22:54:10 +00:00
|
|
|
baseapp.SetChainID(chainID),
|
2022-01-08 00:39:27 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// appExport writes out an app's state to json.
|
|
|
|
func (ac appCreator) appExport(
|
|
|
|
logger log.Logger,
|
2024-02-06 22:54:10 +00:00
|
|
|
db cometbftdb.DB,
|
2022-01-08 00:39:27 +00:00
|
|
|
traceStore io.Writer,
|
|
|
|
height int64,
|
|
|
|
forZeroHeight bool,
|
|
|
|
jailAllowedAddrs []string,
|
|
|
|
appOpts servertypes.AppOptions,
|
2024-02-06 22:54:10 +00:00
|
|
|
modulesToExport []string,
|
2022-01-08 00:39:27 +00:00
|
|
|
) (servertypes.ExportedApp, error) {
|
|
|
|
homePath, ok := appOpts.Get(flags.FlagHome).(string)
|
|
|
|
if !ok || homePath == "" {
|
|
|
|
return servertypes.ExportedApp{}, errors.New("application home not set")
|
|
|
|
}
|
|
|
|
|
2022-04-21 20:16:28 +00:00
|
|
|
options := app.DefaultOptions
|
|
|
|
options.SkipLoadLatest = true
|
|
|
|
options.InvariantCheckPeriod = cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod))
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var tempApp *app.App
|
|
|
|
if height != -1 {
|
2022-04-21 20:16:28 +00:00
|
|
|
tempApp = app.NewApp(logger, db, homePath, traceStore, ac.encodingConfig, options)
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
if err := tempApp.LoadHeight(height); err != nil {
|
|
|
|
return servertypes.ExportedApp{}, err
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-21 20:16:28 +00:00
|
|
|
tempApp = app.NewApp(logger, db, homePath, traceStore, ac.encodingConfig, options)
|
2022-01-08 00:39:27 +00:00
|
|
|
}
|
2024-02-06 22:54:10 +00:00
|
|
|
return tempApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
|
2022-01-08 00:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// addStartCmdFlags adds flags to the server start command.
|
|
|
|
func (ac appCreator) addStartCmdFlags(startCmd *cobra.Command) {
|
|
|
|
crisis.AddModuleInitFlags(startCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// accAddressesFromBech32 converts a slice of bech32 encoded addresses into a slice of address types.
|
|
|
|
func accAddressesFromBech32(addresses ...string) ([]sdk.AccAddress, error) {
|
|
|
|
var decodedAddresses []sdk.AccAddress
|
|
|
|
for _, s := range addresses {
|
|
|
|
a, err := sdk.AccAddressFromBech32(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decodedAddresses = append(decodedAddresses, a)
|
|
|
|
}
|
|
|
|
return decodedAddresses, nil
|
|
|
|
}
|