github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/trie/trie.go (about)

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