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