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