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