github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/parallel_trie/node.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package trie 18 19 import ( 20 "fmt" 21 "io" 22 "strings" 23 24 "github.com/bigzoro/my_simplechain/common" 25 "github.com/bigzoro/my_simplechain/rlp" 26 ) 27 28 var indices = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "[17]"} 29 30 type node interface { 31 fstring(string) string 32 cache() (hashNode, bool) 33 } 34 35 type ( 36 fullNode struct { 37 Children [17]node // Actual trie node data to encode/decode (needs custom encoder) 38 flags nodeFlag 39 } 40 shortNode struct { 41 Key []byte 42 Val node 43 flags nodeFlag 44 } 45 hashNode []byte 46 valueNode []byte 47 ) 48 49 // nilValueNode is used when collapsing internal trie nodes for hashing, since 50 // unset children need to serialize correctly. 51 var nilValueNode = valueNode(nil) 52 53 // EncodeRLP encodes a full node into the consensus RLP format. 54 func (n *fullNode) EncodeRLP(w io.Writer) error { 55 var nodes [17]node 56 57 for i, child := range &n.Children { 58 if child != nil { 59 nodes[i] = child 60 } else { 61 nodes[i] = nilValueNode 62 } 63 } 64 return rlp.Encode(w, nodes) 65 } 66 67 func (n *fullNode) copy() *fullNode { copy := *n; return © } 68 func (n *shortNode) copy() *shortNode { copy := *n; return © } 69 70 // nodeFlag contains caching-related metadata about a node. 71 type nodeFlag struct { 72 hash *hashNode // cached hash of the node (may be nil) 73 gen uint16 // cache generation counter 74 dirty *bool // whether the node has changes that must be written to the database 75 } 76 77 func (n *fullNode) cache() (hashNode, bool) { return *n.flags.hash, *n.flags.dirty } 78 func (n *shortNode) cache() (hashNode, bool) { return *n.flags.hash, *n.flags.dirty } 79 func (n hashNode) cache() (hashNode, bool) { return nil, true } 80 func (n valueNode) cache() (hashNode, bool) { return nil, true } 81 82 // Pretty printing. 83 func (n *fullNode) String() string { return n.fstring("") } 84 func (n *shortNode) String() string { return n.fstring("") } 85 func (n hashNode) String() string { return n.fstring("") } 86 func (n valueNode) String() string { return n.fstring("") } 87 88 func (n *fullNode) fstring(ind string) string { 89 resp := fmt.Sprintf("[\n%s ", ind) 90 for i, node := range &n.Children { 91 if node == nil { 92 resp += fmt.Sprintf("%s: <nil> ", indices[i]) 93 } else { 94 resp += fmt.Sprintf("%s: %v", indices[i], node.fstring(ind+" ")) 95 } 96 } 97 return resp + fmt.Sprintf("\n%s] ", ind) 98 } 99 func (n *shortNode) fstring(ind string) string { 100 return fmt.Sprintf("{%x: %v} ", n.Key, n.Val.fstring(ind+" ")) 101 } 102 func (n hashNode) fstring(ind string) string { 103 return fmt.Sprintf("<%x> ", []byte(n)) 104 } 105 func (n valueNode) fstring(ind string) string { 106 return fmt.Sprintf("%x ", []byte(n)) 107 } 108 109 func mustDecodeNode(hash, buf []byte) node { 110 n, err := decodeNode(hash, buf) 111 if err != nil { 112 panic(fmt.Sprintf("node %x: %v", hash, err)) 113 } 114 return n 115 } 116 117 // decodeNode parses the RLP encoding of a trie node. 118 func decodeNode(hash, buf []byte) (node, error) { 119 if len(buf) == 0 { 120 return nil, io.ErrUnexpectedEOF 121 } 122 elems, _, err := rlp.SplitList(buf) 123 if err != nil { 124 return nil, fmt.Errorf("decode error: %v", err) 125 } 126 switch c, _ := rlp.CountValues(elems); c { 127 case 2: 128 n, err := decodeShort(hash, elems) 129 return n, wrapError(err, "short") 130 case 17: 131 n, err := decodeFull(hash, elems) 132 return n, wrapError(err, "full") 133 default: 134 return nil, fmt.Errorf("invalid number of list elements: %v", c) 135 } 136 } 137 138 func decodeShort(hash, elems []byte) (node, error) { 139 kbuf, rest, err := rlp.SplitString(elems) 140 if err != nil { 141 return nil, err 142 } 143 dirty := false 144 h := hashNode(hash) 145 flag := nodeFlag{hash: &h, dirty: &dirty} 146 key := compactToHex(kbuf) 147 if hasTerm(key) { 148 // value node 149 val, _, err := rlp.SplitString(rest) 150 if err != nil { 151 return nil, fmt.Errorf("invalid value node: %v", err) 152 } 153 return &shortNode{key, append(valueNode{}, val...), flag}, nil 154 } 155 r, _, err := decodeRef(rest) 156 if err != nil { 157 return nil, wrapError(err, "val") 158 } 159 return &shortNode{key, r, flag}, nil 160 } 161 162 func decodeFull(hash, elems []byte) (*fullNode, error) { 163 dirty := false 164 h := hashNode(hash) 165 n := &fullNode{flags: nodeFlag{hash: &h, dirty: &dirty}} 166 for i := 0; i < 16; i++ { 167 cld, rest, err := decodeRef(elems) 168 if err != nil { 169 return n, wrapError(err, fmt.Sprintf("[%d]", i)) 170 } 171 n.Children[i], elems = cld, rest 172 } 173 val, _, err := rlp.SplitString(elems) 174 if err != nil { 175 return n, err 176 } 177 if len(val) > 0 { 178 n.Children[16] = append(valueNode{}, val...) 179 } 180 return n, nil 181 } 182 183 const hashLen = len(common.Hash{}) 184 185 func decodeRef(buf []byte) (node, []byte, error) { 186 kind, val, rest, err := rlp.Split(buf) 187 if err != nil { 188 return nil, buf, err 189 } 190 switch { 191 case kind == rlp.List: 192 // 'embedded' node reference. The encoding must be smaller 193 // than a hash in order to be valid. 194 if size := len(buf) - len(rest); size > hashLen { 195 err := fmt.Errorf("oversized embedded node (size is %d bytes, want size < %d)", size, hashLen) 196 return nil, buf, err 197 } 198 n, err := decodeNode(nil, buf) 199 return n, rest, err 200 case kind == rlp.String && len(val) == 0: 201 // empty node 202 return nil, rest, nil 203 case kind == rlp.String && len(val) == 32: 204 return append(hashNode{}, val...), rest, nil 205 default: 206 return nil, nil, fmt.Errorf("invalid RLP string size %d (want 0 or 32)", len(val)) 207 } 208 } 209 210 // wraps a decoding error with information about the path to the 211 // invalid child node (for debugging encoding issues). 212 type decodeError struct { 213 what error 214 stack []string 215 } 216 217 func wrapError(err error, ctx string) error { 218 if err == nil { 219 return nil 220 } 221 if decErr, ok := err.(*decodeError); ok { 222 decErr.stack = append(decErr.stack, ctx) 223 return decErr 224 } 225 return &decodeError{err, []string{ctx}} 226 } 227 228 func (err *decodeError) Error() string { 229 return fmt.Sprintf("%v (decode path: %s)", err.what, strings.Join(err.stack, "<-")) 230 }