github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/trie/trie.go (about)

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