github.com/luckypickle/go-ethereum-vet@v1.14.2/trie/trie.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 implements Merkle Patricia Tries. 18 package trie 19 20 import ( 21 "bytes" 22 "fmt" 23 24 "github.com/luckypickle/go-ethereum-vet/common" 25 "github.com/luckypickle/go-ethereum-vet/crypto" 26 "github.com/luckypickle/go-ethereum-vet/log" 27 "github.com/luckypickle/go-ethereum-vet/metrics" 28 ) 29 30 var ( 31 // emptyRoot is the known root hash of an empty trie. 32 emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") 33 34 // emptyState is the known hash of an empty state trie entry. 35 emptyState = crypto.Keccak256Hash(nil) 36 ) 37 38 var ( 39 cacheMissCounter = metrics.NewRegisteredCounter("trie/cachemiss", nil) 40 cacheUnloadCounter = metrics.NewRegisteredCounter("trie/cacheunload", nil) 41 ) 42 43 // CacheMisses retrieves a global counter measuring the number of cache misses 44 // the trie had since process startup. This isn't useful for anything apart from 45 // trie debugging purposes. 46 func CacheMisses() int64 { 47 return cacheMissCounter.Count() 48 } 49 50 // CacheUnloads retrieves a global counter measuring the number of cache unloads 51 // the trie did since process startup. This isn't useful for anything apart from 52 // trie debugging purposes. 53 func CacheUnloads() int64 { 54 return cacheUnloadCounter.Count() 55 } 56 57 // LeafCallback is a callback type invoked when a trie operation reaches a leaf 58 // node. It's used by state sync and commit to allow handling external references 59 // between account and storage tries. 60 type LeafCallback func(leaf []byte, parent common.Hash) error 61 62 // Trie is a Merkle Patricia Trie. 63 // The zero value is an empty trie with no database. 64 // Use New to create a trie that sits on top of a database. 65 // 66 // Trie is not safe for concurrent use. 67 type Trie struct { 68 db *Database 69 root node 70 originalRoot common.Hash 71 72 // Cache generation values. 73 // cachegen increases by one with each commit operation. 74 // new nodes are tagged with the current generation and unloaded 75 // when their generation is older than than cachegen-cachelimit. 76 cachegen, cachelimit uint16 77 } 78 79 // SetCacheLimit sets the number of 'cache generations' to keep. 80 // A cache generation is created by a call to Commit. 81 func (t *Trie) SetCacheLimit(l uint16) { 82 t.cachelimit = l 83 } 84 85 // newFlag returns the cache flag value for a newly created node. 86 func (t *Trie) newFlag() nodeFlag { 87 return nodeFlag{dirty: true, gen: t.cachegen} 88 } 89 90 // New creates a trie with an existing root node from db. 91 // 92 // If root is the zero hash or the sha3 hash of an empty string, the 93 // trie is initially empty and does not require a database. Otherwise, 94 // New will panic if db is nil and returns a MissingNodeError if root does 95 // not exist in the database. Accessing the trie loads nodes from db on demand. 96 func New(root common.Hash, db *Database) (*Trie, error) { 97 if db == nil { 98 panic("trie.New called without a database") 99 } 100 trie := &Trie{ 101 db: db, 102 originalRoot: root, 103 } 104 if root != (common.Hash{}) && root != emptyRoot { 105 rootnode, err := trie.resolveHash(root[:], nil) 106 if err != nil { 107 return nil, err 108 } 109 trie.root = rootnode 110 } 111 return trie, nil 112 } 113 114 // NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at 115 // the key after the given start key. 116 func (t *Trie) NodeIterator(start []byte) NodeIterator { 117 return newNodeIterator(t, start) 118 } 119 120 // Get returns the value for key stored in the trie. 121 // The value bytes must not be modified by the caller. 122 func (t *Trie) Get(key []byte) []byte { 123 res, err := t.TryGet(key) 124 if err != nil { 125 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 126 } 127 return res 128 } 129 130 // TryGet returns the value for key stored in the trie. 131 // The value bytes must not be modified by the caller. 132 // If a node was not found in the database, a MissingNodeError is returned. 133 func (t *Trie) TryGet(key []byte) ([]byte, error) { 134 key = keybytesToHex(key) 135 value, newroot, didResolve, err := t.tryGet(t.root, key, 0) 136 if err == nil && didResolve { 137 t.root = newroot 138 } 139 return value, err 140 } 141 142 func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) { 143 switch n := (origNode).(type) { 144 case nil: 145 return nil, nil, false, nil 146 case valueNode: 147 return n, n, false, nil 148 case *shortNode: 149 if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) { 150 // key not found in trie 151 return nil, n, false, nil 152 } 153 value, newnode, didResolve, err = t.tryGet(n.Val, key, pos+len(n.Key)) 154 if err == nil && didResolve { 155 n = n.copy() 156 n.Val = newnode 157 n.flags.gen = t.cachegen 158 } 159 return value, n, didResolve, err 160 case *fullNode: 161 value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1) 162 if err == nil && didResolve { 163 n = n.copy() 164 n.flags.gen = t.cachegen 165 n.Children[key[pos]] = newnode 166 } 167 return value, n, didResolve, err 168 case hashNode: 169 child, err := t.resolveHash(n, key[:pos]) 170 if err != nil { 171 return nil, n, true, err 172 } 173 value, newnode, _, err := t.tryGet(child, key, pos) 174 return value, newnode, true, err 175 default: 176 panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode)) 177 } 178 } 179 180 // Update associates key with value in the trie. Subsequent calls to 181 // Get will return value. If value has length zero, any existing value 182 // is deleted from the trie and calls to Get will return nil. 183 // 184 // The value bytes must not be modified by the caller while they are 185 // stored in the trie. 186 func (t *Trie) Update(key, value []byte) { 187 if err := t.TryUpdate(key, value); err != nil { 188 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 189 } 190 } 191 192 // TryUpdate associates key with value in the trie. Subsequent calls to 193 // Get will return value. If value has length zero, any existing value 194 // is deleted from the trie and calls to Get will return nil. 195 // 196 // The value bytes must not be modified by the caller while they are 197 // stored in the trie. 198 // 199 // If a node was not found in the database, a MissingNodeError is returned. 200 func (t *Trie) TryUpdate(key, value []byte) error { 201 k := keybytesToHex(key) 202 if len(value) != 0 { 203 _, n, err := t.insert(t.root, nil, k, valueNode(value)) 204 if err != nil { 205 return err 206 } 207 t.root = n 208 } else { 209 _, n, err := t.delete(t.root, nil, k) 210 if err != nil { 211 return err 212 } 213 t.root = n 214 } 215 return nil 216 } 217 218 func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) { 219 if len(key) == 0 { 220 if v, ok := n.(valueNode); ok { 221 return !bytes.Equal(v, value.(valueNode)), value, nil 222 } 223 return true, value, nil 224 } 225 switch n := n.(type) { 226 case *shortNode: 227 matchlen := prefixLen(key, n.Key) 228 // If the whole key matches, keep this short node as is 229 // and only update the value. 230 if matchlen == len(n.Key) { 231 dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value) 232 if !dirty || err != nil { 233 return false, n, err 234 } 235 return true, &shortNode{n.Key, nn, t.newFlag()}, nil 236 } 237 // Otherwise branch out at the index where they differ. 238 branch := &fullNode{flags: t.newFlag()} 239 var err error 240 _, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val) 241 if err != nil { 242 return false, nil, err 243 } 244 _, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value) 245 if err != nil { 246 return false, nil, err 247 } 248 // Replace this shortNode with the branch if it occurs at index 0. 249 if matchlen == 0 { 250 return true, branch, nil 251 } 252 // Otherwise, replace it with a short node leading up to the branch. 253 return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil 254 255 case *fullNode: 256 dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value) 257 if !dirty || err != nil { 258 return false, n, err 259 } 260 n = n.copy() 261 n.flags = t.newFlag() 262 n.Children[key[0]] = nn 263 return true, n, nil 264 265 case nil: 266 return true, &shortNode{key, value, t.newFlag()}, nil 267 268 case hashNode: 269 // We've hit a part of the trie that isn't loaded yet. Load 270 // the node and insert into it. This leaves all child nodes on 271 // the path to the value in the trie. 272 rn, err := t.resolveHash(n, prefix) 273 if err != nil { 274 return false, nil, err 275 } 276 dirty, nn, err := t.insert(rn, prefix, key, value) 277 if !dirty || err != nil { 278 return false, rn, err 279 } 280 return true, nn, nil 281 282 default: 283 panic(fmt.Sprintf("%T: invalid node: %v", n, n)) 284 } 285 } 286 287 // Delete removes any existing value for key from the trie. 288 func (t *Trie) Delete(key []byte) { 289 if err := t.TryDelete(key); err != nil { 290 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 291 } 292 } 293 294 // TryDelete removes any existing value for key from the trie. 295 // If a node was not found in the database, a MissingNodeError is returned. 296 func (t *Trie) TryDelete(key []byte) error { 297 k := keybytesToHex(key) 298 _, n, err := t.delete(t.root, nil, k) 299 if err != nil { 300 return err 301 } 302 t.root = n 303 return nil 304 } 305 306 // delete returns the new root of the trie with key deleted. 307 // It reduces the trie to minimal form by simplifying 308 // nodes on the way up after deleting recursively. 309 func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { 310 switch n := n.(type) { 311 case *shortNode: 312 matchlen := prefixLen(key, n.Key) 313 if matchlen < len(n.Key) { 314 return false, n, nil // don't replace n on mismatch 315 } 316 if matchlen == len(key) { 317 return true, nil, nil // remove n entirely for whole matches 318 } 319 // The key is longer than n.Key. Remove the remaining suffix 320 // from the subtrie. Child can never be nil here since the 321 // subtrie must contain at least two other values with keys 322 // longer than n.Key. 323 dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):]) 324 if !dirty || err != nil { 325 return false, n, err 326 } 327 switch child := child.(type) { 328 case *shortNode: 329 // Deleting from the subtrie reduced it to another 330 // short node. Merge the nodes to avoid creating a 331 // shortNode{..., shortNode{...}}. Use concat (which 332 // always creates a new slice) instead of append to 333 // avoid modifying n.Key since it might be shared with 334 // other nodes. 335 return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil 336 default: 337 return true, &shortNode{n.Key, child, t.newFlag()}, nil 338 } 339 340 case *fullNode: 341 dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:]) 342 if !dirty || err != nil { 343 return false, n, err 344 } 345 n = n.copy() 346 n.flags = t.newFlag() 347 n.Children[key[0]] = nn 348 349 // Check how many non-nil entries are left after deleting and 350 // reduce the full node to a short node if only one entry is 351 // left. Since n must've contained at least two children 352 // before deletion (otherwise it would not be a full node) n 353 // can never be reduced to nil. 354 // 355 // When the loop is done, pos contains the index of the single 356 // value that is left in n or -2 if n contains at least two 357 // values. 358 pos := -1 359 for i, cld := range &n.Children { 360 if cld != nil { 361 if pos == -1 { 362 pos = i 363 } else { 364 pos = -2 365 break 366 } 367 } 368 } 369 if pos >= 0 { 370 if pos != 16 { 371 // If the remaining entry is a short node, it replaces 372 // n and its key gets the missing nibble tacked to the 373 // front. This avoids creating an invalid 374 // shortNode{..., shortNode{...}}. Since the entry 375 // might not be loaded yet, resolve it just for this 376 // check. 377 cnode, err := t.resolve(n.Children[pos], prefix) 378 if err != nil { 379 return false, nil, err 380 } 381 if cnode, ok := cnode.(*shortNode); ok { 382 k := append([]byte{byte(pos)}, cnode.Key...) 383 return true, &shortNode{k, cnode.Val, t.newFlag()}, nil 384 } 385 } 386 // Otherwise, n is replaced by a one-nibble short node 387 // containing the child. 388 return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil 389 } 390 // n still contains at least two values and cannot be reduced. 391 return true, n, nil 392 393 case valueNode: 394 return true, nil, nil 395 396 case nil: 397 return false, nil, nil 398 399 case hashNode: 400 // We've hit a part of the trie that isn't loaded yet. Load 401 // the node and delete from it. This leaves all child nodes on 402 // the path to the value in the trie. 403 rn, err := t.resolveHash(n, prefix) 404 if err != nil { 405 return false, nil, err 406 } 407 dirty, nn, err := t.delete(rn, prefix, key) 408 if !dirty || err != nil { 409 return false, rn, err 410 } 411 return true, nn, nil 412 413 default: 414 panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key)) 415 } 416 } 417 418 func concat(s1 []byte, s2 ...byte) []byte { 419 r := make([]byte, len(s1)+len(s2)) 420 copy(r, s1) 421 copy(r[len(s1):], s2) 422 return r 423 } 424 425 func (t *Trie) resolve(n node, prefix []byte) (node, error) { 426 if n, ok := n.(hashNode); ok { 427 return t.resolveHash(n, prefix) 428 } 429 return n, nil 430 } 431 432 func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) { 433 cacheMissCounter.Inc(1) 434 435 hash := common.BytesToHash(n) 436 if node := t.db.node(hash, t.cachegen); node != nil { 437 return node, nil 438 } 439 return nil, &MissingNodeError{NodeHash: hash, Path: prefix} 440 } 441 442 // Root returns the root hash of the trie. 443 // Deprecated: use Hash instead. 444 func (t *Trie) Root() []byte { return t.Hash().Bytes() } 445 446 // Hash returns the root hash of the trie. It does not write to the 447 // database and can be used even if the trie doesn't have one. 448 func (t *Trie) Hash() common.Hash { 449 hash, cached, _ := t.hashRoot(nil, nil) 450 t.root = cached 451 return common.BytesToHash(hash.(hashNode)) 452 } 453 454 // Commit writes all nodes to the trie's memory database, tracking the internal 455 // and external (for account tries) references. 456 func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) { 457 if t.db == nil { 458 panic("commit called on trie with nil database") 459 } 460 hash, cached, err := t.hashRoot(t.db, onleaf) 461 if err != nil { 462 return common.Hash{}, err 463 } 464 t.root = cached 465 t.cachegen++ 466 return common.BytesToHash(hash.(hashNode)), nil 467 } 468 469 func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node, error) { 470 if t.root == nil { 471 return hashNode(emptyRoot.Bytes()), nil, nil 472 } 473 h := newHasher(t.cachegen, t.cachelimit, onleaf) 474 defer returnHasherToPool(h) 475 return h.hash(t.root, db, true) 476 }