diff --git a/cmd/cylinder/export.go b/cmd/cylinder/export.go index 531312837..fe16ab469 100644 --- a/cmd/cylinder/export.go +++ b/cmd/cylinder/export.go @@ -14,7 +14,6 @@ import ( ) const ( - flagAll = "all" flagOutput = "output" ) @@ -39,10 +38,10 @@ func exportGroupsCmd(ctx *context.Context) *cobra.Command { cmd := &cobra.Command{ Use: "groups [public-key-1] [public-key-2] [public-key-3] [...]", Short: "Export groups data", + Long: "Export groups data by its public key, if no public key is provided, all groups will be exported", Args: cobra.MinimumNArgs(0), - RunE: func(cmd *cobra.Command, args []string) error { - // get 'all' flag - all, err := cmd.Flags().GetBool(flagAll) + RunE: func(cmd *cobra.Command, args []string) (err error) { + ctx, err = ctx.WithGoLevelDB() if err != nil { return err } @@ -54,8 +53,8 @@ func exportGroupsCmd(ctx *context.Context) *cobra.Command { } // get groups information - var groups []store.Group - if all { + groups := []store.Group{} + if len(args) == 0 { groups, err = ctx.Store.GetAllGroups() if err != nil { return err @@ -99,7 +98,6 @@ func exportGroupsCmd(ctx *context.Context) *cobra.Command { }, } - cmd.Flags().Bool(flagAll, false, "To get all groups") cmd.Flags().String(flagOutput, "", "Specific output filename") _ = cmd.MarkFlagRequired(flagOutput) @@ -112,10 +110,10 @@ func exportDKGsCmd(ctx *context.Context) *cobra.Command { cmd := &cobra.Command{ Use: "dkgs [group-id-1] [group-id-2] [group-id-3] [...]", Short: "Export DKGs data", + Long: "Export DKGs data by group id. If not specify any group id, it will export all DKGs data", Args: cobra.MinimumNArgs(0), - RunE: func(cmd *cobra.Command, args []string) error { - // get 'all' flag - all, err := cmd.Flags().GetBool(flagAll) + RunE: func(cmd *cobra.Command, args []string) (err error) { + ctx, err = ctx.WithGoLevelDB() if err != nil { return err } @@ -128,7 +126,7 @@ func exportDKGsCmd(ctx *context.Context) *cobra.Command { // get DKGs information var dkgs []store.DKG - if all { + if len(args) == 0 { dkgs, err = ctx.Store.GetAllDKGs() if err != nil { return err @@ -172,7 +170,6 @@ func exportDKGsCmd(ctx *context.Context) *cobra.Command { }, } - cmd.Flags().Bool(flagAll, false, "To get all DKGs") cmd.Flags().String(flagOutput, "", "Specific output filename") _ = cmd.MarkFlagRequired(flagOutput) @@ -186,7 +183,12 @@ func exportDEsCmd(ctx *context.Context) *cobra.Command { Use: "des", Short: "Export DEs data", Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) (err error) { + ctx, err = ctx.WithGoLevelDB() + if err != nil { + return err + } + // get 'output' flag output, err := cmd.Flags().GetString(flagOutput) if err != nil { diff --git a/cmd/cylinder/import.go b/cmd/cylinder/import.go index 1303b705e..0b964c533 100644 --- a/cmd/cylinder/import.go +++ b/cmd/cylinder/import.go @@ -33,7 +33,12 @@ func importGroupsCmd(ctx *context.Context) *cobra.Command { Use: "groups [path_to_json_file]", Short: "Import groups data", Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) (err error) { + ctx, err = ctx.WithGoLevelDB() + if err != nil { + return err + } + // open the file jsonFile, err := os.Open(args[0]) if err != nil { @@ -74,7 +79,12 @@ func importDKGsCmd(ctx *context.Context) *cobra.Command { Use: "dkgs [path_to_json_file]", Short: "Import DKGs data", Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) (err error) { + ctx, err = ctx.WithGoLevelDB() + if err != nil { + return err + } + // open the file jsonFile, err := os.Open(args[0]) if err != nil { @@ -116,7 +126,12 @@ func importDEsCmd(ctx *context.Context) *cobra.Command { Use: "des [path_to_json_file]", Short: "Import DEs data", Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) (err error) { + ctx, err = ctx.WithGoLevelDB() + if err != nil { + return err + } + // open the file jsonFile, err := os.Open(args[0]) if err != nil { diff --git a/cmd/cylinder/main.go b/cmd/cylinder/main.go index c8d2758fa..18f01e3ca 100644 --- a/cmd/cylinder/main.go +++ b/cmd/cylinder/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" sdk "github.com/cosmos/cosmos-sdk/types" @@ -17,7 +16,6 @@ func main() { ctx := &context.Context{} rootCmd := NewRootCmd(ctx) if err := rootCmd.Execute(); err != nil { - fmt.Println(err) os.Exit(1) } } diff --git a/cmd/cylinder/run.go b/cmd/cylinder/run.go index 7d4e8ddf0..62298875c 100644 --- a/cmd/cylinder/run.go +++ b/cmd/cylinder/run.go @@ -37,7 +37,12 @@ func runCmd(ctx *context.Context) *cobra.Command { Aliases: []string{"r"}, Short: "Run the cylinder process", Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) (err error) { + ctx, err = ctx.WithGoLevelDB() + if err != nil { + return err + } + group, err := group.New(ctx) if err != nil { return err diff --git a/cylinder/context/context.go b/cylinder/context/context.go index e741fcad9..c7b0f3d37 100644 --- a/cylinder/context/context.go +++ b/cylinder/context/context.go @@ -1,6 +1,7 @@ package context import ( + "fmt" "path/filepath" "time" @@ -51,7 +52,8 @@ type Context struct { ErrCh chan error MsgCh chan sdk.Msg - Store *store.Store + DataDir string + Store *store.Store } // NewContext creates a new instance of the Context. @@ -63,13 +65,6 @@ func NewContext( txConfig client.TxConfig, interfaceRegistry types.InterfaceRegistry, ) (*Context, error) { - // Create the store - dataDir := filepath.Join(home, "data") - db, err := dbm.NewDB("cylinder", dbm.GoLevelDBBackend, dataDir) - if err != nil { - return nil, err - } - // Initialize the context return &Context{ Config: cfg, @@ -80,7 +75,7 @@ func NewContext( InterfaceRegistry: interfaceRegistry, ErrCh: make(chan error, 1), MsgCh: make(chan sdk.Msg, 1000), - Store: store.NewStore(db), + DataDir: filepath.Join(home, "data"), }, nil } @@ -93,3 +88,25 @@ func (ctx *Context) InitLog() error { ctx.Logger = logger.NewLogger(allowLevel) return nil } + +// WithGoLevelDB initializes the database of the context with GoLevelDB. +func (ctx *Context) WithGoLevelDB() (*Context, error) { + db, err := dbm.NewDB("cylinder", dbm.GoLevelDBBackend, ctx.DataDir) + if err != nil { + return nil, fmt.Errorf("%w; possibly due to being run in another process", err) + } + + return ctx.WithDB(db) +} + +// WithDB sets the DB for the context. +func (ctx *Context) WithDB(db dbm.DB) (*Context, error) { + if ctx.Store != nil { + if err := ctx.Store.DB.Close(); err != nil { + return nil, fmt.Errorf("failed to close the existing DB: %w", err) + } + } + + ctx.Store = store.NewStore(db) + return ctx, nil +} diff --git a/cylinder/store/store.go b/cylinder/store/store.go index cfde35b1e..c265e478a 100644 --- a/cylinder/store/store.go +++ b/cylinder/store/store.go @@ -42,7 +42,7 @@ func (s *Store) GetAllDKGs() ([]DKG, error) { } defer iterator.Close() - var dkgs []DKG + dkgs := make([]DKG, 0) // prevent nil slice when exporting data. for ; iterator.Valid(); iterator.Next() { var dkg DKG err = json.Unmarshal(iterator.Value(), &dkg) @@ -99,7 +99,7 @@ func (s *Store) GetAllGroups() ([]Group, error) { } defer iterator.Close() - var groups []Group + groups := make([]Group, 0) // prevent nil slice when exporting data. for ; iterator.Valid(); iterator.Next() { var group Group err = json.Unmarshal(iterator.Value(), &group) @@ -151,7 +151,7 @@ func (s *Store) GetAllDEs() ([]DE, error) { } defer iterator.Close() - var des []DE + des := make([]DE, 0) // prevent nil slice when exporting data. for ; iterator.Valid(); iterator.Next() { var de DE err = json.Unmarshal(iterator.Value(), &de)