github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/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  	"errors"
    23  	"fmt"
    24  
    25  	"github.com/tacshi/go-ethereum/common"
    26  	"github.com/tacshi/go-ethereum/core/types"
    27  	"github.com/tacshi/go-ethereum/log"
    28  )
    29  
    30  // Trie is a Merkle Patricia Trie. Use New to create a trie that sits on
    31  // top of a database. Whenever trie performs a commit operation, the generated
    32  // nodes will be gathered and returned in a set. Once the trie is committed,
    33  // it's not usable anymore. Callers have to re-create the trie with new root
    34  // based on the updated trie database.
    35  //
    36  // Trie is not safe for concurrent use.
    37  type Trie struct {
    38  	root  node
    39  	owner common.Hash
    40  
    41  	// Keep track of the number leaves which have been inserted since the last
    42  	// hashing operation. This number will not directly map to the number of
    43  	// actually unhashed nodes.
    44  	unhashed int
    45  
    46  	// reader is the handler trie can retrieve nodes from.
    47  	reader *trieReader
    48  
    49  	// tracer is the tool to track the trie changes.
    50  	// It will be reset after each commit operation.
    51  	tracer *tracer
    52  }
    53  
    54  // newFlag returns the cache flag value for a newly created node.
    55  func (t *Trie) newFlag() nodeFlag {
    56  	return nodeFlag{dirty: true}
    57  }
    58  
    59  // Copy returns a copy of Trie.
    60  func (t *Trie) Copy() *Trie {
    61  	return &Trie{
    62  		root:     t.root,
    63  		owner:    t.owner,
    64  		unhashed: t.unhashed,
    65  		reader:   t.reader,
    66  		tracer:   t.tracer.copy(),
    67  	}
    68  }
    69  
    70  // New creates the trie instance with provided trie id and the read-only
    71  // database. The state specified by trie id must be available, otherwise
    72  // an error will be returned. The trie root specified by trie id can be
    73  // zero hash or the sha3 hash of an empty string, then trie is initially
    74  // empty, otherwise, the root node must be present in database or returns
    75  // a MissingNodeError if not.
    76  func New(id *ID, db NodeReader) (*Trie, error) {
    77  	reader, err := newTrieReader(id.StateRoot, id.Owner, db)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	trie := &Trie{
    82  		owner:  id.Owner,
    83  		reader: reader,
    84  		//tracer: newTracer(),
    85  	}
    86  	if id.Root != (common.Hash{}) && id.Root != types.EmptyRootHash {
    87  		rootnode, err := trie.resolveAndTrack(id.Root[:], nil)
    88  		if err != nil {
    89  			return nil, err
    90  		}
    91  		trie.root = rootnode
    92  	}
    93  	return trie, nil
    94  }
    95  
    96  // NewEmpty is a shortcut to create empty tree. It's mostly used in tests.
    97  func NewEmpty(db *Database) *Trie {
    98  	tr, _ := New(TrieID(common.Hash{}), db)
    99  	return tr
   100  }
   101  
   102  // NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at
   103  // the key after the given start key.
   104  func (t *Trie) NodeIterator(start []byte) NodeIterator {
   105  	return newNodeIterator(t, start)
   106  }
   107  
   108  // Get returns the value for key stored in the trie.
   109  // The value bytes must not be modified by the caller.
   110  func (t *Trie) Get(key []byte) []byte {
   111  	res, err := t.TryGet(key)
   112  	if err != nil {
   113  		log.Error("Unhandled trie error in Trie.Get", "err", err)
   114  	}
   115  	return res
   116  }
   117  
   118  // TryGet returns the value for key stored in the trie.
   119  // The value bytes must not be modified by the caller.
   120  // If a node was not found in the database, a MissingNodeError is returned.
   121  func (t *Trie) TryGet(key []byte) ([]byte, error) {
   122  	value, newroot, didResolve, err := t.tryGet(t.root, keybytesToHex(key), 0)
   123  	if err == nil && didResolve {
   124  		t.root = newroot
   125  	}
   126  	return value, err
   127  }
   128  
   129  func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) {
   130  	switch n := (origNode).(type) {
   131  	case nil:
   132  		return nil, nil, false, nil
   133  	case valueNode:
   134  		return n, n, false, nil
   135  	case *shortNode:
   136  		if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) {
   137  			// key not found in trie
   138  			return nil, n, false, nil
   139  		}
   140  		value, newnode, didResolve, err = t.tryGet(n.Val, key, pos+len(n.Key))
   141  		if err == nil && didResolve {
   142  			n = n.copy()
   143  			n.Val = newnode
   144  		}
   145  		return value, n, didResolve, err
   146  	case *fullNode:
   147  		value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1)
   148  		if err == nil && didResolve {
   149  			n = n.copy()
   150  			n.Children[key[pos]] = newnode
   151  		}
   152  		return value, n, didResolve, err
   153  	case hashNode:
   154  		child, err := t.resolveAndTrack(n, key[:pos])
   155  		if err != nil {
   156  			return nil, n, true, err
   157  		}
   158  		value, newnode, _, err := t.tryGet(child, key, pos)
   159  		return value, newnode, true, err
   160  	default:
   161  		panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
   162  	}
   163  }
   164  
   165  // TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not
   166  // possible to use keybyte-encoding as the path might contain odd nibbles.
   167  func (t *Trie) TryGetNode(path []byte) ([]byte, int, error) {
   168  	item, newroot, resolved, err := t.tryGetNode(t.root, compactToHex(path), 0)
   169  	if err != nil {
   170  		return nil, resolved, err
   171  	}
   172  	if resolved > 0 {
   173  		t.root = newroot
   174  	}
   175  	if item == nil {
   176  		return nil, resolved, nil
   177  	}
   178  	return item, resolved, err
   179  }
   180  
   181  func (t *Trie) tryGetNode(origNode node, path []byte, pos int) (item []byte, newnode node, resolved int, err error) {
   182  	// If non-existent path requested, abort
   183  	if origNode == nil {
   184  		return nil, nil, 0, nil
   185  	}
   186  	// If we reached the requested path, return the current node
   187  	if pos >= len(path) {
   188  		// Although we most probably have the original node expanded, encoding
   189  		// that into consensus form can be nasty (needs to cascade down) and
   190  		// time consuming. Instead, just pull the hash up from disk directly.
   191  		var hash hashNode
   192  		if node, ok := origNode.(hashNode); ok {
   193  			hash = node
   194  		} else {
   195  			hash, _ = origNode.cache()
   196  		}
   197  		if hash == nil {
   198  			return nil, origNode, 0, errors.New("non-consensus node")
   199  		}
   200  		blob, err := t.reader.nodeBlob(path, common.BytesToHash(hash))
   201  		return blob, origNode, 1, err
   202  	}
   203  	// Path still needs to be traversed, descend into children
   204  	switch n := (origNode).(type) {
   205  	case valueNode:
   206  		// Path prematurely ended, abort
   207  		return nil, nil, 0, nil
   208  
   209  	case *shortNode:
   210  		if len(path)-pos < len(n.Key) || !bytes.Equal(n.Key, path[pos:pos+len(n.Key)]) {
   211  			// Path branches off from short node
   212  			return nil, n, 0, nil
   213  		}
   214  		item, newnode, resolved, err = t.tryGetNode(n.Val, path, pos+len(n.Key))
   215  		if err == nil && resolved > 0 {
   216  			n = n.copy()
   217  			n.Val = newnode
   218  		}
   219  		return item, n, resolved, err
   220  
   221  	case *fullNode:
   222  		item, newnode, resolved, err = t.tryGetNode(n.Children[path[pos]], path, pos+1)
   223  		if err == nil && resolved > 0 {
   224  			n = n.copy()
   225  			n.Children[path[pos]] = newnode
   226  		}
   227  		return item, n, resolved, err
   228  
   229  	case hashNode:
   230  		child, err := t.resolveAndTrack(n, path[:pos])
   231  		if err != nil {
   232  			return nil, n, 1, err
   233  		}
   234  		item, newnode, resolved, err := t.tryGetNode(child, path, pos)
   235  		return item, newnode, resolved + 1, err
   236  
   237  	default:
   238  		panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
   239  	}
   240  }
   241  
   242  // Update associates key with value in the trie. Subsequent calls to
   243  // Get will return value. If value has length zero, any existing value
   244  // is deleted from the trie and calls to Get will return nil.
   245  //
   246  // The value bytes must not be modified by the caller while they are
   247  // stored in the trie.
   248  func (t *Trie) Update(key, value []byte) {
   249  	if err := t.TryUpdate(key, value); err != nil {
   250  		log.Error("Unhandled trie error in Trie.Update", "err", err)
   251  	}
   252  }
   253  
   254  // TryUpdate associates key with value in the trie. Subsequent calls to
   255  // Get will return value. If value has length zero, any existing value
   256  // is deleted from the trie and calls to Get will return nil.
   257  //
   258  // The value bytes must not be modified by the caller while they are
   259  // stored in the trie.
   260  //
   261  // If a node was not found in the database, a MissingNodeError is returned.
   262  func (t *Trie) TryUpdate(key, value []byte) error {
   263  	return t.tryUpdate(key, value)
   264  }
   265  
   266  // tryUpdate expects an RLP-encoded value and performs the core function
   267  // for TryUpdate and TryUpdateAccount.
   268  func (t *Trie) tryUpdate(key, value []byte) error {
   269  	t.unhashed++
   270  	k := keybytesToHex(key)
   271  	if len(value) != 0 {
   272  		_, n, err := t.insert(t.root, nil, k, valueNode(value))
   273  		if err != nil {
   274  			return err
   275  		}
   276  		t.root = n
   277  	} else {
   278  		_, n, err := t.delete(t.root, nil, k)
   279  		if err != nil {
   280  			return err
   281  		}
   282  		t.root = n
   283  	}
   284  	return nil
   285  }
   286  
   287  func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
   288  	if len(key) == 0 {
   289  		if v, ok := n.(valueNode); ok {
   290  			return !bytes.Equal(v, value.(valueNode)), value, nil
   291  		}
   292  		return true, value, nil
   293  	}
   294  	switch n := n.(type) {
   295  	case *shortNode:
   296  		matchlen := prefixLen(key, n.Key)
   297  		// If the whole key matches, keep this short node as is
   298  		// and only update the value.
   299  		if matchlen == len(n.Key) {
   300  			dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value)
   301  			if !dirty || err != nil {
   302  				return false, n, err
   303  			}
   304  			return true, &shortNode{n.Key, nn, t.newFlag()}, nil
   305  		}
   306  		// Otherwise branch out at the index where they differ.
   307  		branch := &fullNode{flags: t.newFlag()}
   308  		var err error
   309  		_, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val)
   310  		if err != nil {
   311  			return false, nil, err
   312  		}
   313  		_, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value)
   314  		if err != nil {
   315  			return false, nil, err
   316  		}
   317  		// Replace this shortNode with the branch if it occurs at index 0.
   318  		if matchlen == 0 {
   319  			return true, branch, nil
   320  		}
   321  		// New branch node is created as a child of the original short node.
   322  		// Track the newly inserted node in the tracer. The node identifier
   323  		// passed is the path from the root node.
   324  		t.tracer.onInsert(append(prefix, key[:matchlen]...))
   325  
   326  		// Replace it with a short node leading up to the branch.
   327  		return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil
   328  
   329  	case *fullNode:
   330  		dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value)
   331  		if !dirty || err != nil {
   332  			return false, n, err
   333  		}
   334  		n = n.copy()
   335  		n.flags = t.newFlag()
   336  		n.Children[key[0]] = nn
   337  		return true, n, nil
   338  
   339  	case nil:
   340  		// New short node is created and track it in the tracer. The node identifier
   341  		// passed is the path from the root node. Note the valueNode won't be tracked
   342  		// since it's always embedded in its parent.
   343  		t.tracer.onInsert(prefix)
   344  
   345  		return true, &shortNode{key, value, t.newFlag()}, nil
   346  
   347  	case hashNode:
   348  		// We've hit a part of the trie that isn't loaded yet. Load
   349  		// the node and insert into it. This leaves all child nodes on
   350  		// the path to the value in the trie.
   351  		rn, err := t.resolveAndTrack(n, prefix)
   352  		if err != nil {
   353  			return false, nil, err
   354  		}
   355  		dirty, nn, err := t.insert(rn, prefix, key, value)
   356  		if !dirty || err != nil {
   357  			return false, rn, err
   358  		}
   359  		return true, nn, nil
   360  
   361  	default:
   362  		panic(fmt.Sprintf("%T: invalid node: %v", n, n))
   363  	}
   364  }
   365  
   366  // Delete removes any existing value for key from the trie.
   367  func (t *Trie) Delete(key []byte) {
   368  	if err := t.TryDelete(key); err != nil {
   369  		log.Error("Unhandled trie error in Trie.Delete", "err", err)
   370  	}
   371  }
   372  
   373  // TryDelete removes any existing value for key from the trie.
   374  // If a node was not found in the database, a MissingNodeError is returned.
   375  func (t *Trie) TryDelete(key []byte) error {
   376  	t.unhashed++
   377  	k := keybytesToHex(key)
   378  	_, n, err := t.delete(t.root, nil, k)
   379  	if err != nil {
   380  		return err
   381  	}
   382  	t.root = n
   383  	return nil
   384  }
   385  
   386  // delete returns the new root of the trie with key deleted.
   387  // It reduces the trie to minimal form by simplifying
   388  // nodes on the way up after deleting recursively.
   389  func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
   390  	switch n := n.(type) {
   391  	case *shortNode:
   392  		matchlen := prefixLen(key, n.Key)
   393  		if matchlen < len(n.Key) {
   394  			return false, n, nil // don't replace n on mismatch
   395  		}
   396  		if matchlen == len(key) {
   397  			// The matched short node is deleted entirely and track
   398  			// it in the deletion set. The same the valueNode doesn't
   399  			// need to be tracked at all since it's always embedded.
   400  			t.tracer.onDelete(prefix)
   401  
   402  			return true, nil, nil // remove n entirely for whole matches
   403  		}
   404  		// The key is longer than n.Key. Remove the remaining suffix
   405  		// from the subtrie. Child can never be nil here since the
   406  		// subtrie must contain at least two other values with keys
   407  		// longer than n.Key.
   408  		dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):])
   409  		if !dirty || err != nil {
   410  			return false, n, err
   411  		}
   412  		switch child := child.(type) {
   413  		case *shortNode:
   414  			// The child shortNode is merged into its parent, track
   415  			// is deleted as well.
   416  			t.tracer.onDelete(append(prefix, n.Key...))
   417  
   418  			// Deleting from the subtrie reduced it to another
   419  			// short node. Merge the nodes to avoid creating a
   420  			// shortNode{..., shortNode{...}}. Use concat (which
   421  			// always creates a new slice) instead of append to
   422  			// avoid modifying n.Key since it might be shared with
   423  			// other nodes.
   424  			return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil
   425  		default:
   426  			return true, &shortNode{n.Key, child, t.newFlag()}, nil
   427  		}
   428  
   429  	case *fullNode:
   430  		dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:])
   431  		if !dirty || err != nil {
   432  			return false, n, err
   433  		}
   434  		n = n.copy()
   435  		n.flags = t.newFlag()
   436  		n.Children[key[0]] = nn
   437  
   438  		// Because n is a full node, it must've contained at least two children
   439  		// before the delete operation. If the new child value is non-nil, n still
   440  		// has at least two children after the deletion, and cannot be reduced to
   441  		// a short node.
   442  		if nn != nil {
   443  			return true, n, nil
   444  		}
   445  		// Reduction:
   446  		// Check how many non-nil entries are left after deleting and
   447  		// reduce the full node to a short node if only one entry is
   448  		// left. Since n must've contained at least two children
   449  		// before deletion (otherwise it would not be a full node) n
   450  		// can never be reduced to nil.
   451  		//
   452  		// When the loop is done, pos contains the index of the single
   453  		// value that is left in n or -2 if n contains at least two
   454  		// values.
   455  		pos := -1
   456  		for i, cld := range &n.Children {
   457  			if cld != nil {
   458  				if pos == -1 {
   459  					pos = i
   460  				} else {
   461  					pos = -2
   462  					break
   463  				}
   464  			}
   465  		}
   466  		if pos >= 0 {
   467  			if pos != 16 {
   468  				// If the remaining entry is a short node, it replaces
   469  				// n and its key gets the missing nibble tacked to the
   470  				// front. This avoids creating an invalid
   471  				// shortNode{..., shortNode{...}}.  Since the entry
   472  				// might not be loaded yet, resolve it just for this
   473  				// check.
   474  				cnode, err := t.resolve(n.Children[pos], append(prefix, byte(pos)))
   475  				if err != nil {
   476  					return false, nil, err
   477  				}
   478  				if cnode, ok := cnode.(*shortNode); ok {
   479  					// Replace the entire full node with the short node.
   480  					// Mark the original short node as deleted since the
   481  					// value is embedded into the parent now.
   482  					t.tracer.onDelete(append(prefix, byte(pos)))
   483  
   484  					k := append([]byte{byte(pos)}, cnode.Key...)
   485  					return true, &shortNode{k, cnode.Val, t.newFlag()}, nil
   486  				}
   487  			}
   488  			// Otherwise, n is replaced by a one-nibble short node
   489  			// containing the child.
   490  			return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil
   491  		}
   492  		// n still contains at least two values and cannot be reduced.
   493  		return true, n, nil
   494  
   495  	case valueNode:
   496  		return true, nil, nil
   497  
   498  	case nil:
   499  		return false, nil, nil
   500  
   501  	case hashNode:
   502  		// We've hit a part of the trie that isn't loaded yet. Load
   503  		// the node and delete from it. This leaves all child nodes on
   504  		// the path to the value in the trie.
   505  		rn, err := t.resolveAndTrack(n, prefix)
   506  		if err != nil {
   507  			return false, nil, err
   508  		}
   509  		dirty, nn, err := t.delete(rn, prefix, key)
   510  		if !dirty || err != nil {
   511  			return false, rn, err
   512  		}
   513  		return true, nn, nil
   514  
   515  	default:
   516  		panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key))
   517  	}
   518  }
   519  
   520  func concat(s1 []byte, s2 ...byte) []byte {
   521  	r := make([]byte, len(s1)+len(s2))
   522  	copy(r, s1)
   523  	copy(r[len(s1):], s2)
   524  	return r
   525  }
   526  
   527  func (t *Trie) resolve(n node, prefix []byte) (node, error) {
   528  	if n, ok := n.(hashNode); ok {
   529  		return t.resolveAndTrack(n, prefix)
   530  	}
   531  	return n, nil
   532  }
   533  
   534  // resolveAndTrack loads node from the underlying store with the given node hash
   535  // and path prefix and also tracks the loaded node blob in tracer treated as the
   536  // node's original value. The rlp-encoded blob is preferred to be loaded from
   537  // database because it's easy to decode node while complex to encode node to blob.
   538  func (t *Trie) resolveAndTrack(n hashNode, prefix []byte) (node, error) {
   539  	blob, err := t.reader.nodeBlob(prefix, common.BytesToHash(n))
   540  	if err != nil {
   541  		return nil, err
   542  	}
   543  	t.tracer.onRead(prefix, blob)
   544  	return mustDecodeNode(n, blob), nil
   545  }
   546  
   547  // Hash returns the root hash of the trie. It does not write to the
   548  // database and can be used even if the trie doesn't have one.
   549  func (t *Trie) Hash() common.Hash {
   550  	hash, cached, _ := t.hashRoot()
   551  	t.root = cached
   552  	return common.BytesToHash(hash.(hashNode))
   553  }
   554  
   555  // Commit collects all dirty nodes in the trie and replaces them with the
   556  // corresponding node hash. All collected nodes (including dirty leaves if
   557  // collectLeaf is true) will be encapsulated into a nodeset for return.
   558  // The returned nodeset can be nil if the trie is clean (nothing to commit).
   559  // Once the trie is committed, it's not usable anymore. A new trie must
   560  // be created with new root and updated trie database for following usage
   561  func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet) {
   562  	defer t.tracer.reset()
   563  
   564  	// Trie is empty and can be classified into two types of situations:
   565  	// - The trie was empty and no update happens
   566  	// - The trie was non-empty and all nodes are dropped
   567  	if t.root == nil {
   568  		// Wrap tracked deletions as the return
   569  		set := NewNodeSet(t.owner)
   570  		t.tracer.markDeletions(set)
   571  		return types.EmptyRootHash, set
   572  	}
   573  	// Derive the hash for all dirty nodes first. We hold the assumption
   574  	// in the following procedure that all nodes are hashed.
   575  	rootHash := t.Hash()
   576  
   577  	// Do a quick check if we really need to commit. This can happen e.g.
   578  	// if we load a trie for reading storage values, but don't write to it.
   579  	if hashedNode, dirty := t.root.cache(); !dirty {
   580  		// Replace the root node with the origin hash in order to
   581  		// ensure all resolved nodes are dropped after the commit.
   582  		t.root = hashedNode
   583  		return rootHash, nil
   584  	}
   585  	h := newCommitter(t.owner, t.tracer, collectLeaf)
   586  	newRoot, nodes := h.Commit(t.root)
   587  	t.root = newRoot
   588  	return rootHash, nodes
   589  }
   590  
   591  // hashRoot calculates the root hash of the given trie
   592  func (t *Trie) hashRoot() (node, node, error) {
   593  	if t.root == nil {
   594  		return hashNode(types.EmptyRootHash.Bytes()), nil, nil
   595  	}
   596  	// If the number of changes is below 100, we let one thread handle it
   597  	h := newHasher(t.unhashed >= 100)
   598  	defer returnHasherToPool(h)
   599  	hashed, cached := h.hash(t.root, true)
   600  	t.unhashed = 0
   601  	return hashed, cached, nil
   602  }
   603  
   604  // Reset drops the referenced root node and cleans all internal state.
   605  func (t *Trie) Reset() {
   606  	t.root = nil
   607  	t.owner = common.Hash{}
   608  	t.unhashed = 0
   609  	t.tracer.reset()
   610  }