cosmos-sdk/server/v2/store/server.go
2024-09-28 07:18:08 +00:00

78 lines
1.7 KiB
Go

package store
import (
"context"
"fmt"
"github.com/spf13/cobra"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"
)
var (
_ serverv2.ServerComponent[transaction.Tx] = (*Server[transaction.Tx])(nil)
_ serverv2.HasConfig = (*Server[transaction.Tx])(nil)
_ serverv2.HasCLICommands = (*Server[transaction.Tx])(nil)
)
const ServerName = "store"
// Server manages store config and contains prune & snapshot commands
type Server[T transaction.Tx] struct {
config *Config
// saving appCreator for only RestoreSnapshotCmd
appCreator serverv2.AppCreator[T]
}
func New[T transaction.Tx](appCreator serverv2.AppCreator[T]) *Server[T] {
return &Server[T]{appCreator: appCreator}
}
func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.Logger) error {
serverCfg := s.Config().(*Config)
if len(cfg) > 0 {
if err := serverv2.UnmarshalSubConfig(cfg, s.Name(), &serverCfg); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}
s.config = serverCfg
return nil
}
func (s *Server[T]) Name() string {
return ServerName
}
func (s *Server[T]) Start(ctx context.Context) error {
return nil
}
func (s *Server[T]) Stop(ctx context.Context) error {
return nil
}
func (s *Server[T]) CLICommands() serverv2.CLIConfig {
return serverv2.CLIConfig{
Commands: []*cobra.Command{
s.PrunesCmd(),
s.ExportSnapshotCmd(),
s.DeleteSnapshotCmd(),
s.ListSnapshotsCmd(),
s.DumpArchiveCmd(),
s.LoadArchiveCmd(),
s.RestoreSnapshotCmd(s.appCreator),
},
}
}
func (s *Server[T]) Config() any {
if s.config == nil || s.config.AppDBBackend == "" {
return DefaultConfig()
}
return s.config
}