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