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