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

add import cmd #10

Open
wants to merge 1 commit into
base: import-api
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
86 changes: 86 additions & 0 deletions cmd/cronosd/cmd/memiavl.go
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
}
}
Comment on lines +40 to +79

Check failure

Code scanning / gosec

the value in the range statement should be _ unless copying a map: want: for key := range m

expected exactly 1 statement (either append, delete, or copying to another map) in a range with a map, got 9
return nil
},
}
cmd.Flags().Int64(flagSnapshotVersion, 0, "Snapshot version")
cmd.Flags().String(flagOutputDir, "", "Output directory")
return cmd
}
5 changes: 5 additions & 0 deletions cmd/cronosd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
rootCmd.AddCommand(changeSetCmd)
}

importCmd := ImportCmd()
if importCmd != nil {
rootCmd.AddCommand(importCmd)
}

// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
rpc.StatusCommand(),
Expand Down
8 changes: 8 additions & 0 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,14 @@ def changeset_verify(self, changeset_dir, **kwargs):
hash, commit_info = output.split("\n")
return binascii.unhexlify(hash), json.loads(commit_info)

def import_snapshot(self, dir, version=None):
return self.raw(
"import",
dir,
"--snapshot-version",
"0" if version is None else version,
)

def changeset_restore_app_db(self, snapshot_dir, app_db, **kwargs):
return self.raw(
"changeset", "restore-app-db", snapshot_dir, app_db, **kwargs
Expand Down
25 changes: 25 additions & 0 deletions integration_tests/test_versiondb.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,28 @@ def test_versiondb_migration(cronos: Cronos):
"value": 1000,
},
)


def test_import(cronos: Cronos):
w3 = cronos.w3
community = ADDRS["community"]
tx = {
"from": ADDRS["validator"],
"to": community,
"value": 1000,
}
send_transaction(w3, tx)

# stop the network first
print("stop all nodes")
print(cronos.supervisorctl("stop", "all"))
cli0 = cronos.cosmos_cli(i=0)

changeset_dir = tempfile.mkdtemp(dir=cronos.base_dir)
print("dump to:", changeset_dir)
print(cli0.changeset_dump(changeset_dir))
snapshot_dir = tempfile.mkdtemp(dir=cronos.base_dir)
print("verify and save to snapshot:", snapshot_dir)
_, commit_info = cli0.changeset_verify(changeset_dir, save_snapshot=snapshot_dir)
print("commit_info", commit_info)
cli0.import_snapshot(snapshot_dir)