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