Skip to content

Commit 0370eb1

Browse files
committed
feat(iaviewer): Add "data-full" and "hash" commands
1 parent fae8119 commit 0370eb1

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

cmd/iaviewer/main.go

+26-10
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ const (
2222
)
2323

2424
var cmds = map[string]bool{
25-
"data": true,
26-
"shape": true,
27-
"versions": true,
25+
"data": true,
26+
"data-full": true,
27+
"hash": true,
28+
"shape": true,
29+
"versions": true,
2830
}
2931

3032
func main() {
3133
args := os.Args[1:]
3234
if len(args) < 3 || len(args) > 4 || !cmds[args[0]] {
3335
fmt.Fprintln(os.Stderr, strings.TrimSpace(`
34-
Usage: iaviewer <data|shape|versions> <leveldb dir> <prefix> [version number]
36+
Usage: iaviewer <data|data-full|hash|shape|versions> <leveldb dir> <prefix> [version number]
3537
<prefix> is the prefix of db, and the iavl tree of different modules in cosmos-sdk uses
3638
different <prefix> to identify, just like "s/k:gov/" represents the prefix of gov module
3739
`))
@@ -60,9 +62,15 @@ different <prefix> to identify, just like "s/k:gov/" represents the prefix of go
6062
assertNoError(err, "Error reading target version")
6163
fmt.Printf("Got version: %d\n", tree.Version())
6264

65+
fullValues := false
6366
switch args[0] {
67+
case "data-full":
68+
fullValues = true
69+
fallthrough
6470
case "data":
65-
PrintKeys(tree)
71+
PrintKeys(tree, fullValues)
72+
fallthrough
73+
case "hash":
6674
hash := tree.Hash()
6775
fmt.Printf("Hash: %X\n", hash)
6876
fmt.Printf("Size: %X\n", tree.Size())
@@ -150,12 +158,20 @@ func ReadTree(dir string, prefix []byte) (tree *iavl.MutableTree, latestVersion
150158
return tree, latestVersion, err
151159
}
152160

153-
func PrintKeys(tree *iavl.ImmutableTree) {
154-
fmt.Println("Printing all keys with hashed values (to detect diff)")
161+
func PrintKeys(tree *iavl.ImmutableTree, fullValues bool) {
162+
valuesLabel := "hashed values"
163+
valueToString := func(value []byte) string {
164+
return fmt.Sprintf("%X", sha256.Sum256(value))
165+
}
166+
if fullValues {
167+
valuesLabel = "values"
168+
valueToString = encodeData
169+
}
170+
fmt.Printf("Printing all keys with %s (to detect diff)\n", valuesLabel)
155171
tree.Iterate(func(key []byte, value []byte) bool {
156-
printKey := parseWeaveKey(key)
157-
digest := sha256.Sum256(value)
158-
fmt.Printf(" %s\n %X\n", printKey, digest)
172+
keyStr := parseWeaveKey(key)
173+
valueStr := valueToString(value)
174+
fmt.Printf(" %s\n %s\n", keyStr, valueStr)
159175
return false
160176
})
161177
}

0 commit comments

Comments
 (0)