forked from osmosis-labs/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
170 lines (146 loc) · 3.52 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package iavl
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
)
// PrintTree prints the whole tree in an indented form.
func PrintTree(tree *ImmutableTree) {
ndb, root := tree.ndb, tree.root
printNode(ndb, root, 0)
}
func printNode(ndb *nodeDB, node *Node, indent int) {
indentPrefix := ""
for i := 0; i < indent; i++ {
indentPrefix += " "
}
if node == nil {
fmt.Printf("%s<nil>\n", indentPrefix)
return
}
if node.rightNode != nil {
printNode(ndb, node.rightNode, indent+1)
} else if node.rightHash != nil {
rightNode := ndb.GetNode(node.rightHash)
printNode(ndb, rightNode, indent+1)
}
hash := node._hash()
fmt.Printf("%sh:%X\n", indentPrefix, hash)
if node.isLeaf() {
fmt.Printf("%s%X:%X (%v)\n", indentPrefix, node.key, node.value, node.height)
}
if node.leftNode != nil {
printNode(ndb, node.leftNode, indent+1)
} else if node.leftHash != nil {
leftNode := ndb.GetNode(node.leftHash)
printNode(ndb, leftNode, indent+1)
}
}
func maxInt8(a, b int8) int8 {
if a > b {
return a
}
return b
}
func cp(bz []byte) (ret []byte) {
ret = make([]byte, len(bz))
copy(ret, bz)
return ret
}
// Returns a slice of the same length (big endian)
// except incremented by one.
// Appends 0x00 if bz is all 0xFF.
// CONTRACT: len(bz) > 0
func cpIncr(bz []byte) (ret []byte) {
ret = cp(bz)
for i := len(bz) - 1; i >= 0; i-- {
if ret[i] < byte(0xFF) {
ret[i]++
return
}
ret[i] = byte(0x00)
if i == 0 {
// here, the original bz is all 0xFF, so we keep the original and append 0x00
// instead of returning all 0x00
ret = cp(bz)
return append(ret, 0x00)
}
}
return []byte{0x00}
}
type byteslices [][]byte
func (bz byteslices) Len() int {
return len(bz)
}
func (bz byteslices) Less(i, j int) bool {
switch bytes.Compare(bz[i], bz[j]) {
case -1:
return true
case 0, 1:
return false
default:
panic("should not happen")
}
}
func (bz byteslices) Swap(i, j int) {
bz[j], bz[i] = bz[i], bz[j]
}
func sortByteSlices(src [][]byte) [][]byte {
bzz := byteslices(src)
sort.Sort(bzz)
return bzz
}
// Colors: ------------------------------------------------
const (
ANSIReset = "\x1b[0m"
ANSIBright = "\x1b[1m"
ANSIFgGreen = "\x1b[32m"
ANSIFgBlue = "\x1b[34m"
ANSIFgCyan = "\x1b[36m"
)
// color the string s with color 'color'
// unless s is already colored
func treat(s string, color string) string {
if len(s) > 2 && s[:2] == "\x1b[" {
return s
}
return color + s + ANSIReset
}
func treatAll(color string, args ...interface{}) string {
parts := make([]string, 0, len(args))
for _, arg := range args {
parts = append(parts, treat(fmt.Sprintf("%v", arg), color))
}
return strings.Join(parts, "")
}
func Green(args ...interface{}) string {
return treatAll(ANSIFgGreen, args...)
}
func Blue(args ...interface{}) string {
return treatAll(ANSIFgBlue, args...)
}
func Cyan(args ...interface{}) string {
return treatAll(ANSIFgCyan, args...)
}
// ColoredBytes takes in the byte that you would like to show as a string and byte
// and will display them in a human readable format.
// If the environment variable TENDERMINT_IAVL_COLORS_ON is set to a non-empty string then different colors will be used for bytes and strings.
func ColoredBytes(data []byte, textColor, bytesColor func(...interface{}) string) string {
colors := os.Getenv("TENDERMINT_IAVL_COLORS_ON")
if colors == "" {
for _, b := range data {
return string(b)
}
}
s := ""
for _, b := range data {
if 0x21 <= b && b < 0x7F {
s += textColor(string(b))
} else {
s += bytesColor(fmt.Sprintf("%02X", b))
}
}
return s
}