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