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