-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: versiondb don't support restore from local snapshot
to setup a pruned versiondb node, we need to restore versiondb from local snapshot. Solution: - implement a new subcommand for that.
- Loading branch information
Showing
5 changed files
with
165 additions
and
0 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"math" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"cosmossdk.io/errors" | ||
protoio "github.com/gogo/protobuf/io" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/cosmos-sdk/server" | ||
"github.com/cosmos/cosmos-sdk/snapshots" | ||
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types" | ||
|
||
"github.com/crypto-org-chain/cronos/versiondb" | ||
"github.com/crypto-org-chain/cronos/versiondb/tsrocksdb" | ||
) | ||
|
||
// RestoreVersionDBCmd returns a command to restore a versiondb from local snapshot | ||
func RestoreVersionDBCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "restore <height> <format>", | ||
Short: "Restore initial versiondb from local snapshot", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := server.GetServerContextFromCmd(cmd) | ||
|
||
height, err := strconv.ParseUint(args[0], 10, 63) | ||
if err != nil { | ||
return err | ||
} | ||
format, err := strconv.ParseUint(args[1], 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store, err := server.GetSnapshotStore(ctx.Viper) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
snapshot, chChunks, err := store.Load(height, uint32(format)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if snapshot == nil { | ||
return fmt.Errorf("snapshot doesn't exist, height: %d, format: %d", height, format) | ||
} | ||
|
||
streamReader, err := snapshots.NewStreamReader(chChunks) | ||
if err != nil { | ||
return err | ||
} | ||
defer streamReader.Close() | ||
|
||
home := ctx.Config.RootDir | ||
versionDB, err := tsrocksdb.NewStore(filepath.Join(home, "data", "versiondb")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ch := make(chan versiondb.ImportEntry, 128) | ||
|
||
go func() { | ||
defer close(ch) | ||
|
||
if err := readSnapshotEntries(streamReader, ch); err != nil { | ||
ctx.Logger.Error("failed to read snapshot entries", "err", err) | ||
} | ||
}() | ||
|
||
return versionDB.Import(int64(height), ch) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// readSnapshotEntries reads key-value entries from protobuf reader and feed to the channel | ||
func readSnapshotEntries(protoReader protoio.Reader, ch chan<- versiondb.ImportEntry) error { | ||
var ( | ||
snapshotItem snapshottypes.SnapshotItem | ||
storeKey string | ||
) | ||
|
||
loop: | ||
for { | ||
snapshotItem = snapshottypes.SnapshotItem{} | ||
err := protoReader.ReadMsg(&snapshotItem) | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return errors.Wrap(err, "invalid protobuf message") | ||
} | ||
|
||
switch item := snapshotItem.Item.(type) { | ||
case *snapshottypes.SnapshotItem_Store: | ||
storeKey = item.Store.Name | ||
case *snapshottypes.SnapshotItem_IAVL: | ||
if storeKey == "" { | ||
return errors.Wrap(err, "invalid protobuf message, store name is empty") | ||
} | ||
if item.IAVL.Height > math.MaxInt8 { | ||
return fmt.Errorf("node height %v cannot exceed %v", | ||
item.IAVL.Height, math.MaxInt8) | ||
} | ||
ch <- versiondb.ImportEntry{ | ||
StoreKey: storeKey, | ||
Key: item.IAVL.Key, | ||
Value: item.IAVL.Value, | ||
} | ||
default: | ||
break loop | ||
} | ||
} | ||
|
||
return nil | ||
} |
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