github.com/klaytn/klaytn@v1.10.2/storage/statedb/trie.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from trie/trie.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package statedb
    22  
    23  import (
    24  	"bytes"
    25  	"errors"
    26  	"fmt"
    27  
    28  	"github.com/klaytn/klaytn/common"
    29  	"github.com/klaytn/klaytn/crypto"
    30  )
    31  
    32  var (
    33  	// emptyRoot is the known root hash of an empty trie.
    34  	emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
    35  
    36  	// emptyState is the known hash of an empty state trie entry.
    37  	emptyState = crypto.Keccak256Hash(nil)
    38  )
    39  
    40  // LeafCallback is a callback type invoked when a trie operation reaches a leaf
    41  // node.
    42  //
    43  // The paths is a path tuple identifying a particular trie node either in a single
    44  // trie (account) or a layered trie (account -> storage). Each path in the tuple
    45  // is in the raw format(32 bytes).
    46  //
    47  // The hexpath is a composite hexary path identifying the trie node. All the key
    48  // bytes are converted to the hexary nibbles and composited with the parent path
    49  // if the trie node is in a layered trie.
    50  //
    51  // It's used by state sync and commit to allow handling external references
    52  // between account and storage tries. And also it's used in the state healing
    53  // for extracting the raw states(leaf nodes) with corresponding paths.
    54  type LeafCallback func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash, parentDepth int) error
    55  
    56  // Trie is a Merkle Patricia Trie.
    57  // The zero value is an empty trie with no database.
    58  // Use NewTrie to create a trie that sits on top of a database.
    59  //
    60  // Trie is not safe for concurrent use.
    61  type Trie struct {
    62  	db           *Database
    63  	root         node
    64  	originalRoot common.Hash
    65  	prefetching  bool
    66  }
    67  
    68  // newFlag returns the cache flag value for a newly created node.
    69  func (t *Trie) newFlag() nodeFlag {
    70  	return nodeFlag{dirty: true}
    71  }
    72  
    73  // NewTrie creates a trie with an existing root node from db.
    74  //
    75  // If root is the zero hash or the sha3 hash of an empty string, the
    76  // trie is initially empty and does not require a database. Otherwise,
    77  // NewTrie will panic if db is nil and returns a MissingNodeError if root does
    78  // not exist in the database. Accessing the trie loads nodes from db on demand.
    79  func NewTrie(root common.Hash, db *Database) (*Trie, error) {
    80  	if db == nil {
    81  		panic("statedb.NewTrie called without a database")
    82  	}
    83  	trie := &Trie{
    84  		db:           db,
    85  		originalRoot: root,
    86  	}
    87  	if (root != common.Hash{}) && root != emptyRoot {
    88  		rootnode, err := trie.resolveHash(root[:], nil)
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  		trie.root = rootnode
    93  	}
    94  	return trie, nil
    95  }
    96  
    97  func NewTrieForPrefetching(root common.Hash, db *Database) (*Trie, error) {
    98  	trie, err := NewTrie(root, db)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	trie.prefetching = true
   103  	return trie, err
   104  }
   105  
   106  // NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at
   107  // the key after the given start key.
   108  func (t *Trie) NodeIterator(start []byte) NodeIterator {
   109  	return newNodeIterator(t, start)
   110  }
   111  
   112  // Get returns the value for key stored in the trie.
   113  // The value bytes must not be modified by the caller.
   114  func (t *Trie) Get(key []byte) []byte {
   115  	res, err := t.TryGet(key)
   116  	if err != nil {
   117  		logger.Error("Unhandled trie error in Trie.Get", "err", err)
   118  	}
   119  	return res
   120  }
   121  
   122  // TryGet returns the value for key stored in the trie.
   123  // The value bytes must not be modified by the caller.
   124  // If a node was not found in the database, a MissingNodeError is returned.
   125  func (t *Trie) TryGet(key []byte) ([]byte, error) {
   126  	key = keybytesToHex(key)
   127  	value, newroot, didResolve, err := t.tryGet(t.root, key, 0)
   128  	if err == nil && didResolve {
   129  		t.root = newroot
   130  	}
   131  	return value, err
   132  }
   133  
   134  func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) {
   135  	switch n := (origNode).(type) {
   136  	case nil:
   137  		return nil, nil, false, nil
   138  	case valueNode:
   139  		return n, n, false, nil
   140  	case *shortNode:
   141  		if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) {
   142  			// key not found in trie
   143  			return nil, n, false, nil
   144  		}
   145  		value, newnode, didResolve, err = t.tryGet(n.Val, key, pos+len(n.Key))
   146  		if err == nil && didResolve {
   147  			n = n.copy()
   148  			n.Val = newnode
   149  		}
   150  		return value, n, didResolve, err
   151  	case *fullNode:
   152  		value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1)
   153  		if err == nil && didResolve {
   154  			n = n.copy()
   155  			n.Children[key[pos]] = newnode
   156  		}
   157  		return value, n, didResolve, err
   158  	case hashNode:
   159  		child, err := t.resolveHash(n, key[:pos])
   160  		if err != nil {
   161  			return nil, n, true, err
   162  		}
   163  		value, newnode, _, err := t.tryGet(child, key, pos)
   164  		return value, newnode, true, err
   165  	default:
   166  		panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
   167  	}
   168  }
   169  
   170  // TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not
   171  // possible to use keybyte-encoding as the path might contain odd nibbles.
   172  func (t *Trie) TryGetNode(path []byte) ([]byte, int, error) {
   173  	item, newroot, resolved, err := t.tryGetNode(t.root, compactToHex(path), 0)
   174  	if err != nil {
   175  		return nil, resolved, err
   176  	}
   177  	if resolved > 0 {
   178  		t.root = newroot
   179  	}
   180  	if item == nil {
   181  		return nil, resolved, nil
   182  	}
   183  	return item, resolved, err
   184  }
   185  
   186  func (t *Trie) tryGetNode(origNode node, path []byte, pos int) (item []byte, newnode node, resolved int, err error) {
   187  	// If non-existent path requested, abort
   188  	if origNode == nil {
   189  		return nil, nil, 0, nil
   190  	}
   191  	// If we reached the requested path, return the current node
   192  	if pos >= len(path) {
   193  		// Although we most probably have the original node expanded, encoding
   194  		// that into consensus form can be nasty (needs to cascade down) and
   195  		// time consuming. Instead, just pull the hash up from disk directly.
   196  		var hash hashNode
   197  		if node, ok := origNode.(hashNode); ok {
   198  			hash = node
   199  		} else {
   200  			hash, _ = origNode.cache()
   201  		}
   202  		if hash == nil {
   203  			return nil, origNode, 0, errors.New("non-consensus node")
   204  		}
   205  		blob, err := t.db.Node(common.BytesToHash(hash))
   206  		return blob, origNode, 1, err
   207  	}
   208  	// Path still needs to be traversed, descend into children
   209  	switch n := (origNode).(type) {
   210  	case valueNode:
   211  		// Path prematurely ended, abort
   212  		return nil, nil, 0, nil
   213  
   214  	case *shortNode:
   215  		if len(path)-pos < len(n.Key) || !bytes.Equal(n.Key, path[pos:pos+len(n.Key)]) {
   216  			// Path branches off from short node
   217  			return nil, n, 0, nil
   218  		}
   219  		item, newnode, resolved, err = t.tryGetNode(n.Val, path, pos+len(n.Key))
   220  		if err == nil && resolved > 0 {
   221  			n = n.copy()
   222  			n.Val = newnode
   223  		}
   224  		return item, n, resolved, err
   225  
   226  	case *fullNode:
   227  		item, newnode, resolved, err = t.tryGetNode(n.Children[path[pos]], path, pos+1)
   228  		if err == nil && resolved > 0 {
   229  			n = n.copy()
   230  			n.Children[path[pos]] = newnode
   231  		}
   232  		return item, n, resolved, err
   233  
   234  	case hashNode:
   235  		child, err := t.resolveHash(n, path[:pos])
   236  		if err != nil {
   237  			return nil, n, 1, err
   238  		}
   239  		item, newnode, resolved, err := t.tryGetNode(child, path, pos)
   240  		return item, newnode, resolved + 1, err
   241  
   242  	default:
   243  		panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
   244  	}
   245  }
   246  
   247  // Update associates key with value in the trie. Subsequent calls to
   248  // Get will return value. If value has length zero, any existing value
   249  // is deleted from the trie and calls to Get will return nil.
   250  //
   251  // The value bytes must not be modified by the caller while they are
   252  // stored in the trie.
   253  func (t *Trie) Update(key, value []byte) {
   254  	if err := t.TryUpdate(key, value); err != nil {
   255  		logger.Error("Unhandled trie error in Trie.Update", "err", err)
   256  	}
   257  }
   258  
   259  // TryUpdate associates key with value in the trie. Subsequent calls to
   260  // Get will return value. If value has length zero, any existing value
   261  // is deleted from the trie and calls to Get will return nil.
   262  //
   263  // The value bytes must not be modified by the caller while they are
   264  // stored in the trie.
   265  //
   266  // If a node was not found in the database, a MissingNodeError is returned.
   267  func (t *Trie) TryUpdate(key, value []byte) error {
   268  	hexKey := keybytesToHex(key)
   269  	return t.TryUpdateWithHexKey(hexKey, value)
   270  }
   271  
   272  // TryUpdateWithHexKey uses pre-generated hexKey.
   273  // It is both called from TryUpdate and SecureTrie.TryUpdateWithKeys.
   274  func (t *Trie) TryUpdateWithHexKey(hexKey, value []byte) error {
   275  	if len(value) != 0 {
   276  		_, n, err := t.insert(t.root, nil, hexKey, valueNode(value))
   277  		if err != nil {
   278  			return err
   279  		}
   280  		t.root = n
   281  	} else {
   282  		_, n, err := t.delete(t.root, nil, hexKey)
   283  		if err != nil {
   284  			return err
   285  		}
   286  		t.root = n
   287  	}
   288  	return nil
   289  }
   290  
   291  func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
   292  	if len(key) == 0 {
   293  		if v, ok := n.(valueNode); ok {
   294  			return !bytes.Equal(v, value.(valueNode)), value, nil
   295  		}
   296  		return true, value, nil
   297  	}
   298  	switch n := n.(type) {
   299  	case *shortNode:
   300  		matchlen := prefixLen(key, n.Key)
   301  		// If the whole key matches, keep this short node as is
   302  		// and only update the value.
   303  		if matchlen == len(n.Key) {
   304  			dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value)
   305  			if !dirty || err != nil {
   306  				return false, n, err
   307  			}
   308  			return true, &shortNode{n.Key, nn, t.newFlag()}, nil
   309  		}
   310  		// Otherwise branch out at the index where they differ.
   311  		branch := &fullNode{flags: t.newFlag()}
   312  		var err error
   313  		_, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val)
   314  		if err != nil {
   315  			return false, nil, err
   316  		}
   317  		_, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value)
   318  		if err != nil {
   319  			return false, nil, err
   320  		}
   321  		// Replace this shortNode with the branch if it occurs at index 0.
   322  		if matchlen == 0 {
   323  			return true, branch, nil
   324  		}
   325  		// Otherwise, replace it with a short node leading up to the branch.
   326  		return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil
   327  
   328  	case *fullNode:
   329  		dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value)
   330  		if !dirty || err != nil {
   331  			return false, n, err
   332  		}
   333  		n = n.copy()
   334  		n.flags = t.newFlag()
   335  		n.Children[key[0]] = nn
   336  		return true, n, nil
   337  
   338  	case nil:
   339  		return true, &shortNode{key, value, t.newFlag()}, nil
   340  
   341  	case hashNode:
   342  		// We've hit a part of the trie that isn't loaded yet. Load
   343  		// the node and insert into it. This leaves all child nodes on
   344  		// the path to the value in the trie.
   345  		rn, err := t.resolveHash(n, prefix)
   346  		if err != nil {
   347  			return false, nil, err
   348  		}
   349  		dirty, nn, err := t.insert(rn, prefix, key, value)
   350  		if !dirty || err != nil {
   351  			return false, rn, err
   352  		}
   353  		return true, nn, nil
   354  
   355  	default:
   356  		panic(fmt.Sprintf("%T: invalid node: %v", n, n))
   357  	}
   358  }
   359  
   360  // Delete removes any existing value for key from the trie.
   361  func (t *Trie) Delete(key []byte) {
   362  	if err := t.TryDelete(key); err != nil {
   363  		logger.Error("Unhandled trie error in Trie.Delete", "err", err)
   364  	}
   365  }
   366  
   367  // TryDelete removes any existing value for key from the trie.
   368  // If a node was not found in the database, a MissingNodeError is returned.
   369  func (t *Trie) TryDelete(key []byte) error {
   370  	k := keybytesToHex(key)
   371  	_, n, err := t.delete(t.root, nil, k)
   372  	if err != nil {
   373  		return err
   374  	}
   375  	t.root = n
   376  	return nil
   377  }
   378  
   379  // delete returns the new root of the trie with key deleted.
   380  // It reduces the trie to minimal form by simplifying
   381  // nodes on the way up after deleting recursively.
   382  func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
   383  	switch n := n.(type) {
   384  	case *shortNode:
   385  		matchlen := prefixLen(key, n.Key)
   386  		if matchlen < len(n.Key) {
   387  			return false, n, nil // don't replace n on mismatch
   388  		}
   389  		if matchlen == len(key) {
   390  			return true, nil, nil // remove n entirely for whole matches
   391  		}
   392  		// The key is longer than n.Key. Remove the remaining suffix
   393  		// from the subtrie. Child can never be nil here since the
   394  		// subtrie must contain at least two other values with keys
   395  		// longer than n.Key.
   396  		dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):])
   397  		if !dirty || err != nil {
   398  			return false, n, err
   399  		}
   400  		switch child := child.(type) {
   401  		case *shortNode:
   402  			// Deleting from the subtrie reduced it to another
   403  			// short node. Merge the nodes to avoid creating a
   404  			// shortNode{..., shortNode{...}}. Use concat (which
   405  			// always creates a new slice) instead of append to
   406  			// avoid modifying n.Key since it might be shared with
   407  			// other nodes.
   408  			return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil
   409  		default:
   410  			return true, &shortNode{n.Key, child, t.newFlag()}, nil
   411  		}
   412  
   413  	case *fullNode:
   414  		dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:])
   415  		if !dirty || err != nil {
   416  			return false, n, err
   417  		}
   418  		n = n.copy()
   419  		n.flags = t.newFlag()
   420  		n.Children[key[0]] = nn
   421  
   422  		// Check how many non-nil entries are left after deleting and
   423  		// reduce the full node to a short node if only one entry is
   424  		// left. Since n must've contained at least two children
   425  		// before deletion (otherwise it would not be a full node) n
   426  		// can never be reduced to nil.
   427  		//
   428  		// When the loop is done, pos contains the index of the single
   429  		// value that is left in n or -2 if n contains at least two
   430  		// values.
   431  		pos := -1
   432  		for i, cld := range &n.Children {
   433  			if cld != nil {
   434  				if pos == -1 {
   435  					pos = i
   436  				} else {
   437  					pos = -2
   438  					break
   439  				}
   440  			}
   441  		}
   442  		if pos >= 0 {
   443  			if pos != 16 {
   444  				// If the remaining entry is a short node, it replaces
   445  				// n and its key gets the missing nibble tacked to the
   446  				// front. This avoids creating an invalid
   447  				// shortNode{..., shortNode{...}}.  Since the entry
   448  				// might not be loaded yet, resolve it just for this
   449  				// check.
   450  				cnode, err := t.resolve(n.Children[pos], prefix)
   451  				if err != nil {
   452  					return false, nil, err
   453  				}
   454  				if cnode, ok := cnode.(*shortNode); ok {
   455  					k := append([]byte{byte(pos)}, cnode.Key...)
   456  					return true, &shortNode{k, cnode.Val, t.newFlag()}, nil
   457  				}
   458  			}
   459  			// Otherwise, n is replaced by a one-nibble short node
   460  			// containing the child.
   461  			return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil
   462  		}
   463  		// n still contains at least two values and cannot be reduced.
   464  		return true, n, nil
   465  
   466  	case valueNode:
   467  		return true, nil, nil
   468  
   469  	case nil:
   470  		return false, nil, nil
   471  
   472  	case hashNode:
   473  		// We've hit a part of the trie that isn't loaded yet. Load
   474  		// the node and delete from it. This leaves all child nodes on
   475  		// the path to the value in the trie.
   476  		rn, err := t.resolveHash(n, prefix)
   477  		if err != nil {
   478  			return false, nil, err
   479  		}
   480  		dirty, nn, err := t.delete(rn, prefix, key)
   481  		if !dirty || err != nil {
   482  			return false, rn, err
   483  		}
   484  		return true, nn, nil
   485  
   486  	default:
   487  		panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key))
   488  	}
   489  }
   490  
   491  func concat(s1 []byte, s2 ...byte) []byte {
   492  	r := make([]byte, len(s1)+len(s2))
   493  	copy(r, s1)
   494  	copy(r[len(s1):], s2)
   495  	return r
   496  }
   497  
   498  func (t *Trie) resolve(n node, prefix []byte) (node, error) {
   499  	if n, ok := n.(hashNode); ok {
   500  		return t.resolveHash(n, prefix)
   501  	}
   502  	return n, nil
   503  }
   504  
   505  func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) {
   506  	hash := common.BytesToHash(n)
   507  	node, fromDB := t.db.node(hash)
   508  	if t.prefetching && fromDB {
   509  		memcacheCleanPrefetchMissMeter.Mark(1)
   510  	}
   511  	if node != nil {
   512  		return node, nil
   513  	}
   514  	return nil, &MissingNodeError{NodeHash: hash, Path: prefix}
   515  }
   516  
   517  // Hash returns the root hash of the trie. It does not write to the
   518  // database and can be used even if the trie doesn't have one.
   519  func (t *Trie) Hash() common.Hash {
   520  	hash, cached := t.hashRoot(nil, nil)
   521  	t.root = cached
   522  	return common.BytesToHash(hash.(hashNode))
   523  }
   524  
   525  // Commit writes all nodes to the trie's memory database, tracking the internal
   526  // and external (for account tries) references.
   527  func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
   528  	if t.db == nil {
   529  		panic("commit called on trie with nil database")
   530  	}
   531  	hash, cached := t.hashRoot(t.db, onleaf)
   532  	t.root = cached
   533  	return common.BytesToHash(hash.(hashNode)), nil
   534  }
   535  
   536  func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node) {
   537  	if t.root == nil {
   538  		return hashNode(emptyRoot.Bytes()), nil
   539  	}
   540  	h := newHasher(onleaf)
   541  	defer returnHasherToPool(h)
   542  	return h.hashRoot(t.root, db, true)
   543  }
   544  
   545  func GetHashAndHexKey(key []byte) ([]byte, []byte) {
   546  	var hashKeyBuf [common.HashLength]byte
   547  	h := newHasher(nil)
   548  	h.sha.Reset()
   549  	h.sha.Write(key)
   550  	hashKey := h.sha.Sum(hashKeyBuf[:0])
   551  	returnHasherToPool(h)
   552  	hexKey := keybytesToHex(hashKey)
   553  	return hashKey, hexKey
   554  }