Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cylinder] Fix cylinder db #563

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions cmd/cylinder/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

const (
flagAll = "all"
flagOutput = "output"
)

Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
21 changes: 18 additions & 3 deletions cmd/cylinder/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions cmd/cylinder/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,7 +16,6 @@ func main() {
ctx := &context.Context{}
rootCmd := NewRootCmd(ctx)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
7 changes: 6 additions & 1 deletion cmd/cylinder/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 26 additions & 9 deletions cylinder/context/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package context

import (
"fmt"
"path/filepath"
"time"

Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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
}

Expand All @@ -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
}
6 changes: 3 additions & 3 deletions cylinder/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading