-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* port iavlviewer to kava v0.26.x to debug app hash * add hash subcommand to iavlviewer additionally, use better error handling * update changelog * separate iavlviewer command into subcommands --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
- Loading branch information
1 parent
0dc3053
commit 1f31621
Showing
8 changed files
with
309 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package iavlviewer | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
|
||
"github.com/cosmos/iavl" | ||
ethermintserver "github.com/evmos/ethermint/server" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newDataCmd(opts ethermintserver.StartOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "data <prefix> [version number]", | ||
Short: "View all keys, hash, & size of tree.", | ||
Args: cobra.RangeArgs(1, 2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
prefix := args[0] | ||
version := 0 | ||
if len(args) == 2 { | ||
var err error | ||
version, err = parseVersion(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
tree, err := openPrefixTree(opts, cmd, prefix, version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printKeys(tree) | ||
hash, err := tree.Hash() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Hash: %X\n", hash) | ||
fmt.Printf("Size: %X\n", tree.Size()) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func printKeys(tree *iavl.MutableTree) { | ||
fmt.Println("Printing all keys with hashed values (to detect diff)") | ||
tree.Iterate(func(key []byte, value []byte) bool { //nolint:errcheck | ||
printKey := parseWeaveKey(key) | ||
digest := sha256.Sum256(value) | ||
fmt.Printf(" %s\n %X\n", printKey, digest) | ||
return false | ||
}) | ||
} |
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,42 @@ | ||
package iavlviewer | ||
|
||
import ( | ||
"fmt" | ||
|
||
ethermintserver "github.com/evmos/ethermint/server" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newHashCmd(opts ethermintserver.StartOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "hash <prefix> [version number]", | ||
Short: "Print the root hash of the iavl tree.", | ||
Args: cobra.RangeArgs(1, 2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
prefix := args[0] | ||
version := 0 | ||
if len(args) == 2 { | ||
var err error | ||
version, err = parseVersion(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
tree, err := openPrefixTree(opts, cmd, prefix, version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hash, err := tree.Hash() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Hash: %X\n", hash) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package iavlviewer | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
dbm "github.com/cometbft/cometbft-db" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/server" | ||
ethermintserver "github.com/evmos/ethermint/server" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/iavl" | ||
) | ||
|
||
const ( | ||
DefaultCacheSize int = 10000 | ||
) | ||
|
||
func NewCmd(opts ethermintserver.StartOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "iavlviewer <data|hash|shape|versions> <prefix> [version number]", | ||
Short: "Output various data, hashes, and calculations for an iavl tree", | ||
} | ||
|
||
cmd.AddCommand(newDataCmd(opts)) | ||
cmd.AddCommand(newHashCmd(opts)) | ||
cmd.AddCommand(newShapeCmd(opts)) | ||
cmd.AddCommand(newVersionsCmd(opts)) | ||
|
||
return cmd | ||
} | ||
|
||
func parseVersion(arg string) (int, error) { | ||
version, err := strconv.Atoi(arg) | ||
if err != nil { | ||
return 0, fmt.Errorf("invalid version number: '%s'", arg) | ||
} | ||
return version, nil | ||
} | ||
|
||
func openPrefixTree(opts ethermintserver.StartOptions, cmd *cobra.Command, prefix string, version int) (*iavl.MutableTree, error) { | ||
clientCtx := client.GetClientContextFromCmd(cmd) | ||
ctx := server.GetServerContextFromCmd(cmd) | ||
ctx.Config.SetRoot(clientCtx.HomeDir) | ||
|
||
db, err := opts.DBOpener(ctx.Viper, clientCtx.HomeDir, server.GetAppDBBackend(ctx.Viper)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open database at %s: %s", clientCtx.HomeDir, err) | ||
} | ||
defer func() { | ||
if err := db.Close(); err != nil { | ||
ctx.Logger.Error("error closing db", "error", err.Error()) | ||
} | ||
}() | ||
|
||
tree, err := readTree(db, version, []byte(prefix)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read tree with prefix %s: %s", prefix, err) | ||
} | ||
return tree, nil | ||
} | ||
|
||
// ReadTree loads an iavl tree from the directory | ||
// If version is 0, load latest, otherwise, load named version | ||
// The prefix represents which iavl tree you want to read. The iaviwer will always set a prefix. | ||
func readTree(db dbm.DB, version int, prefix []byte) (*iavl.MutableTree, error) { | ||
if len(prefix) != 0 { | ||
db = dbm.NewPrefixDB(db, prefix) | ||
} | ||
|
||
tree, err := iavl.NewMutableTree(db, DefaultCacheSize, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ver, err := tree.LoadVersion(int64(version)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fmt.Printf("Latest version: %d\n", ver) | ||
fmt.Printf("Got version: %d\n", version) | ||
return tree, err | ||
} |
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,47 @@ | ||
package iavlviewer | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cosmos/iavl" | ||
ethermintserver "github.com/evmos/ethermint/server" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newShapeCmd(opts ethermintserver.StartOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "shape <prefix> [version number]", | ||
Short: "View shape of iavl tree.", | ||
Args: cobra.RangeArgs(1, 2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
prefix := args[0] | ||
version := 0 | ||
if len(args) == 2 { | ||
var err error | ||
version, err = parseVersion(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
tree, err := openPrefixTree(opts, cmd, prefix, version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printShape(tree) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func printShape(tree *iavl.MutableTree) { | ||
// shape := tree.RenderShape(" ", nil) | ||
// TODO: handle this error | ||
shape, _ := tree.RenderShape(" ", nodeEncoder) | ||
fmt.Println(strings.Join(shape, "\n")) | ||
} |
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,74 @@ | ||
package iavlviewer | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cosmos/iavl" | ||
ethermintserver "github.com/evmos/ethermint/server" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newVersionsCmd(opts ethermintserver.StartOptions) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "versions <prefix>", | ||
Short: "Print all versions of iavl tree", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
prefix := args[0] | ||
tree, err := openPrefixTree(opts, cmd, prefix, 15) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printVersions(tree) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func printVersions(tree *iavl.MutableTree) { | ||
versions := tree.AvailableVersions() | ||
fmt.Println("Available versions:") | ||
for _, v := range versions { | ||
fmt.Printf(" %d\n", v) | ||
} | ||
} | ||
|
||
// parseWeaveKey assumes a separating : where all in front should be ascii, | ||
// and all afterwards may be ascii or binary | ||
func parseWeaveKey(key []byte) string { | ||
cut := bytes.IndexRune(key, ':') | ||
if cut == -1 { | ||
return encodeID(key) | ||
} | ||
prefix := key[:cut] | ||
id := key[cut+1:] | ||
return fmt.Sprintf("%s:%s", encodeID(prefix), encodeID(id)) | ||
} | ||
|
||
// casts to a string if it is printable ascii, hex-encodes otherwise | ||
func encodeID(id []byte) string { | ||
for _, b := range id { | ||
if b < 0x20 || b >= 0x80 { | ||
return strings.ToUpper(hex.EncodeToString(id)) | ||
} | ||
} | ||
return string(id) | ||
} | ||
|
||
func nodeEncoder(id []byte, depth int, isLeaf bool) string { | ||
prefix := fmt.Sprintf("-%d ", depth) | ||
if isLeaf { | ||
prefix = fmt.Sprintf("*%d ", depth) | ||
} | ||
if len(id) == 0 { | ||
return fmt.Sprintf("%s<nil>", prefix) | ||
} | ||
return fmt.Sprintf("%s%s", prefix, parseWeaveKey(id)) | ||
} |
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