forked from crypto-org-chain/cronos
-
Notifications
You must be signed in to change notification settings - Fork 1
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
add import cmd #10
Open
mmsqe
wants to merge
1
commit into
yihuang:import-api
Choose a base branch
from
mmsqe:import-api-cmd
base: import-api
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add import cmd #10
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/cosmos/iavl" | ||
"github.com/crypto-org-chain/cronos/memiavl" | ||
"github.com/crypto-org-chain/cronos/v2/app" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
flagSnapshotVersion = "snapshot-version" | ||
flagOutputDir = "output-dir" | ||
) | ||
|
||
func ImportCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "import [input_dir]", | ||
Short: "import snapshot to convert state-sync snapshot to memiavl snapshot", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
dir := args[0] | ||
outputDir, err := cmd.Flags().GetString(flagOutputDir) | ||
if err != nil { | ||
return err | ||
} | ||
if outputDir == "" { | ||
outputDir = fmt.Sprintf("%s-output", dir) | ||
} else if dir == outputDir { | ||
return errors.New("output same as import dir") | ||
} | ||
version, err := cmd.Flags().GetInt64(flagSnapshotVersion) | ||
if err != nil { | ||
return err | ||
} | ||
keys, _, _ := app.StoreKeys() | ||
for name := range keys { | ||
moduleDir := fmt.Sprintf("%s/%s", dir, name) | ||
// read file to snapshot | ||
snapshot, err := memiavl.OpenSnapshot(moduleDir) | ||
if err != nil { | ||
return err | ||
} | ||
ch := make(chan *iavl.ExportNode) | ||
chanErr := make(chan error) | ||
go func() { | ||
defer close(ch) | ||
exporter := snapshot.Export() | ||
for { | ||
node, err := exporter.Next() | ||
if err == iavl.ExportDone { | ||
break | ||
} | ||
if err != nil { | ||
chanErr <- err | ||
return | ||
} | ||
ch <- node | ||
} | ||
}() | ||
go func() { | ||
moduleDir = fmt.Sprintf("%s/%s", outputDir, name) | ||
err := os.MkdirAll(moduleDir, os.ModePerm) | ||
if err == nil { | ||
err = memiavl.Import(moduleDir, version, ch, true) | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
chanErr <- err | ||
}() | ||
err = <-chanErr | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().Int64(flagSnapshotVersion, 0, "Snapshot version") | ||
cmd.Flags().String(flagOutputDir, "", "Output directory") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / gosec
the value in the range statement should be _ unless copying a map: want: for key := range m