github.com/klaytn/klaytn@v1.12.1/storage/statedb/stacktrie.go (about)

     1  // Modifications Copyright 2021 The klaytn Authors
     2  // Copyright 2020 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/stacktrie.go (2021/11/23).
    19  // Modified and improved for the klaytn development.
    20  
    21  package statedb
    22  
    23  import (
    24  	"bufio"
    25  	"bytes"
    26  	"encoding/gob"
    27  	"errors"
    28  	"fmt"
    29  	"io"
    30  	"sync"
    31  
    32  	"github.com/klaytn/klaytn/common"
    33  	"github.com/klaytn/klaytn/rlp"
    34  	"github.com/klaytn/klaytn/storage/database"
    35  )
    36  
    37  var ErrCommitDisabled = errors.New("no database for committing")
    38  
    39  var stPool = sync.Pool{
    40  	New: func() interface{} {
    41  		return NewStackTrie(nil)
    42  	},
    43  }
    44  
    45  func stackTrieFromPool(db database.DBManager) *StackTrie {
    46  	st := stPool.Get().(*StackTrie)
    47  	st.db = db
    48  	return st
    49  }
    50  
    51  func returnToPool(st *StackTrie) {
    52  	st.Reset()
    53  	stPool.Put(st)
    54  }
    55  
    56  // StackTrie is a trie implementation that expects keys to be inserted
    57  // in order. Once it determines that a subtree will no longer be inserted
    58  // into, it will hash it and free up the memory it uses.
    59  type StackTrie struct {
    60  	nodeType  uint8              // node type (as in branch, ext, leaf)
    61  	val       []byte             // value contained by this node if it's a leaf
    62  	key       []byte             // key chunk covered by this (full|ext) node
    63  	keyOffset int                // offset of the key chunk inside a full key
    64  	children  [16]*StackTrie     // list of children (for fullnodes and exts)
    65  	db        database.DBManager // Pointer to the commit db, can be nil
    66  }
    67  
    68  // NewStackTrie allocates and initializes an empty trie.
    69  func NewStackTrie(db database.DBManager) *StackTrie {
    70  	return &StackTrie{
    71  		nodeType: emptyNode,
    72  		db:       db,
    73  	}
    74  }
    75  
    76  // NewFromBinary initialises a serialized stacktrie with the given db.
    77  func NewFromBinary(data []byte, db database.DBManager) (*StackTrie, error) {
    78  	var st StackTrie
    79  	if err := st.UnmarshalBinary(data); err != nil {
    80  		return nil, err
    81  	}
    82  	// If a database is used, we need to recursively add it to every child
    83  	if db != nil {
    84  		st.setDb(db)
    85  	}
    86  	return &st, nil
    87  }
    88  
    89  // MarshalBinary implements encoding.BinaryMarshaler
    90  func (st *StackTrie) MarshalBinary() (data []byte, err error) {
    91  	var (
    92  		b bytes.Buffer
    93  		w = bufio.NewWriter(&b)
    94  	)
    95  	if err := gob.NewEncoder(w).Encode(struct {
    96  		Nodetype  uint8
    97  		KeyOffset uint8
    98  		Val       []byte
    99  		Key       []byte
   100  	}{
   101  		st.nodeType,
   102  		uint8(st.keyOffset),
   103  		st.val,
   104  		st.key,
   105  	}); err != nil {
   106  		return nil, err
   107  	}
   108  	for _, child := range st.children {
   109  		if child == nil {
   110  			w.WriteByte(0)
   111  			continue
   112  		}
   113  		w.WriteByte(1)
   114  		if childData, err := child.MarshalBinary(); err != nil {
   115  			return nil, err
   116  		} else {
   117  			w.Write(childData)
   118  		}
   119  	}
   120  	w.Flush()
   121  	return b.Bytes(), nil
   122  }
   123  
   124  // UnmarshalBinary implements encoding.BinaryUnmarshaler
   125  func (st *StackTrie) UnmarshalBinary(data []byte) error {
   126  	r := bytes.NewReader(data)
   127  	return st.unmarshalBinary(r)
   128  }
   129  
   130  func (st *StackTrie) unmarshalBinary(r io.Reader) error {
   131  	var dec struct {
   132  		Nodetype  uint8
   133  		KeyOffset uint8
   134  		Val       []byte
   135  		Key       []byte
   136  	}
   137  	gob.NewDecoder(r).Decode(&dec)
   138  	st.nodeType = dec.Nodetype
   139  	st.val = dec.Val
   140  	st.key = dec.Key
   141  	st.keyOffset = int(dec.KeyOffset)
   142  
   143  	hasChild := make([]byte, 1)
   144  	for i := range st.children {
   145  		if _, err := r.Read(hasChild); err != nil {
   146  			return err
   147  		} else if hasChild[0] == 0 {
   148  			continue
   149  		}
   150  		var child StackTrie
   151  		child.unmarshalBinary(r)
   152  		st.children[i] = &child
   153  	}
   154  	return nil
   155  }
   156  
   157  func (st *StackTrie) setDb(db database.DBManager) {
   158  	st.db = db
   159  	for _, child := range st.children {
   160  		if child != nil {
   161  			child.setDb(db)
   162  		}
   163  	}
   164  }
   165  
   166  func newLeaf(ko int, key, val []byte, db database.DBManager) *StackTrie {
   167  	st := stackTrieFromPool(db)
   168  	st.nodeType = leafNode
   169  	st.keyOffset = ko
   170  	st.key = append(st.key, key[ko:]...)
   171  	st.val = val
   172  	return st
   173  }
   174  
   175  func newExt(ko int, key []byte, child *StackTrie, db database.DBManager) *StackTrie {
   176  	st := stackTrieFromPool(db)
   177  	st.nodeType = extNode
   178  	st.keyOffset = ko
   179  	st.key = append(st.key, key[ko:]...)
   180  	st.children[0] = child
   181  	return st
   182  }
   183  
   184  // List all values that StackTrie#nodeType can hold
   185  const (
   186  	emptyNode = iota
   187  	branchNode
   188  	extNode
   189  	leafNode
   190  	hashedNode
   191  )
   192  
   193  // TryUpdate inserts a (key, value) pair into the stack trie
   194  func (st *StackTrie) TryUpdate(key, value []byte) error {
   195  	k := keybytesToHex(key)
   196  	if len(value) == 0 {
   197  		panic("deletion not supported")
   198  	}
   199  	st.insert(k[:len(k)-1], value)
   200  	return nil
   201  }
   202  
   203  func (st *StackTrie) Update(key, value []byte) {
   204  	if err := st.TryUpdate(key, value); err != nil {
   205  		logger.Error(fmt.Sprintf("Unhandled trie error: %v", err))
   206  	}
   207  }
   208  
   209  func (st *StackTrie) Reset() {
   210  	st.db = nil
   211  	st.key = st.key[:0]
   212  	st.val = nil
   213  	for i := range st.children {
   214  		st.children[i] = nil
   215  	}
   216  	st.nodeType = emptyNode
   217  	st.keyOffset = 0
   218  }
   219  
   220  // Helper function that, given a full key, determines the index
   221  // at which the chunk pointed by st.keyOffset is different from
   222  // the same chunk in the full key.
   223  func (st *StackTrie) getDiffIndex(key []byte) int {
   224  	diffindex := 0
   225  	for ; diffindex < len(st.key) && st.key[diffindex] == key[st.keyOffset+diffindex]; diffindex++ {
   226  	}
   227  	return diffindex
   228  }
   229  
   230  // Helper function to that inserts a (key, value) pair into
   231  // the trie.
   232  func (st *StackTrie) insert(key, value []byte) {
   233  	switch st.nodeType {
   234  	case branchNode: /* Branch */
   235  		idx := int(key[st.keyOffset])
   236  		// Unresolve elder siblings
   237  		for i := idx - 1; i >= 0; i-- {
   238  			if st.children[i] != nil {
   239  				if st.children[i].nodeType != hashedNode {
   240  					st.children[i].hash()
   241  				}
   242  				break
   243  			}
   244  		}
   245  		// Add new child
   246  		if st.children[idx] == nil {
   247  			st.children[idx] = stackTrieFromPool(st.db)
   248  			st.children[idx].keyOffset = st.keyOffset + 1
   249  		}
   250  		st.children[idx].insert(key, value)
   251  	case extNode: /* Ext */
   252  		// Compare both key chunks and see where they differ
   253  		diffidx := st.getDiffIndex(key)
   254  
   255  		// Check if chunks are identical. If so, recurse into
   256  		// the child node. Otherwise, the key has to be split
   257  		// into 1) an optional common prefix, 2) the fullnode
   258  		// representing the two differing path, and 3) a leaf
   259  		// for each of the differentiated subtrees.
   260  		if diffidx == len(st.key) {
   261  			// Ext key and key segment are identical, recurse into
   262  			// the child node.
   263  			st.children[0].insert(key, value)
   264  			return
   265  		}
   266  		// Save the original part. Depending if the break is
   267  		// at the extension's last byte or not, create an
   268  		// intermediate extension or use the extension's child
   269  		// node directly.
   270  		var n *StackTrie
   271  		if diffidx < len(st.key)-1 {
   272  			n = newExt(diffidx+1, st.key, st.children[0], st.db)
   273  		} else {
   274  			// Break on the last byte, no need to insert
   275  			// an extension node: reuse the current node
   276  			n = st.children[0]
   277  		}
   278  		// Convert to hash
   279  		n.hash()
   280  		var p *StackTrie
   281  		if diffidx == 0 {
   282  			// the break is on the first byte, so
   283  			// the current node is converted into
   284  			// a branch node.
   285  			st.children[0] = nil
   286  			p = st
   287  			st.nodeType = branchNode
   288  		} else {
   289  			// the common prefix is at least one byte
   290  			// long, insert a new intermediate branch
   291  			// node.
   292  			st.children[0] = stackTrieFromPool(st.db)
   293  			st.children[0].nodeType = branchNode
   294  			st.children[0].keyOffset = st.keyOffset + diffidx
   295  			p = st.children[0]
   296  		}
   297  		// Create a leaf for the inserted part
   298  		o := newLeaf(st.keyOffset+diffidx+1, key, value, st.db)
   299  
   300  		// Insert both child leaves where they belong:
   301  		origIdx := st.key[diffidx]
   302  		newIdx := key[diffidx+st.keyOffset]
   303  		p.children[origIdx] = n
   304  		p.children[newIdx] = o
   305  		st.key = st.key[:diffidx]
   306  
   307  	case leafNode: /* Leaf */
   308  		// Compare both key chunks and see where they differ
   309  		diffidx := st.getDiffIndex(key)
   310  
   311  		// Overwriting a key isn't supported, which means that
   312  		// the current leaf is expected to be split into 1) an
   313  		// optional extension for the common prefix of these 2
   314  		// keys, 2) a fullnode selecting the path on which the
   315  		// keys differ, and 3) one leaf for the differentiated
   316  		// component of each key.
   317  		if diffidx >= len(st.key) {
   318  			panic("Trying to insert into existing key")
   319  		}
   320  
   321  		// Check if the split occurs at the first nibble of the
   322  		// chunk. In that case, no prefix extnode is necessary.
   323  		// Otherwise, create that
   324  		var p *StackTrie
   325  		if diffidx == 0 {
   326  			// Convert current leaf into a branch
   327  			st.nodeType = branchNode
   328  			p = st
   329  			st.children[0] = nil
   330  		} else {
   331  			// Convert current node into an ext,
   332  			// and insert a child branch node.
   333  			st.nodeType = extNode
   334  			st.children[0] = NewStackTrie(st.db)
   335  			st.children[0].nodeType = branchNode
   336  			st.children[0].keyOffset = st.keyOffset + diffidx
   337  			p = st.children[0]
   338  		}
   339  
   340  		// Create the two child leaves: the one containing the
   341  		// original value and the one containing the new value
   342  		// The child leave will be hashed directly in order to
   343  		// free up some memory.
   344  		origIdx := st.key[diffidx]
   345  		p.children[origIdx] = newLeaf(diffidx+1, st.key, st.val, st.db)
   346  		p.children[origIdx].hash()
   347  
   348  		newIdx := key[diffidx+st.keyOffset]
   349  		p.children[newIdx] = newLeaf(p.keyOffset+1, key, value, st.db)
   350  
   351  		// Finally, cut off the key part that has been passed
   352  		// over to the children.
   353  		st.key = st.key[:diffidx]
   354  		st.val = nil
   355  	case emptyNode: /* Empty */
   356  		st.nodeType = leafNode
   357  		st.key = key[st.keyOffset:]
   358  		st.val = value
   359  	case hashedNode:
   360  		panic("trying to insert into hash")
   361  	default:
   362  		panic("invalid type")
   363  	}
   364  }
   365  
   366  // hash() hashes the node 'st' and converts it into 'hashedNode', if possible.
   367  // Possible outcomes:
   368  // 1. The rlp-encoded value was >= 32 bytes:
   369  //  - Then the 32-byte `hash` will be accessible in `st.val`.
   370  //  - And the 'st.type' will be 'hashedNode'
   371  // 2. The rlp-encoded value was < 32 bytes
   372  //  - Then the <32 byte rlp-encoded value will be accessible in 'st.val'.
   373  //  - And the 'st.type' will be 'hashedNode' AGAIN
   374  //
   375  // This method will also:
   376  // set 'st.type' to hashedNode
   377  // clear 'st.key'
   378  func (st *StackTrie) hash() {
   379  	/* Shortcut if node is already hashed */
   380  	if st.nodeType == hashedNode {
   381  		return
   382  	}
   383  	// The 'hasher' is taken from a pool, but we don't actually
   384  	// claim an instance until all children are done with their hashing,
   385  	// and we actually need one
   386  	var h *hasher
   387  
   388  	switch st.nodeType {
   389  	case branchNode:
   390  		var nodes [17]node
   391  		for i, child := range st.children {
   392  			if child == nil {
   393  				nodes[i] = nilValueNode
   394  				continue
   395  			}
   396  			child.hash()
   397  			if len(child.val) < 32 {
   398  				nodes[i] = rawNode(child.val)
   399  			} else {
   400  				nodes[i] = hashNode(child.val)
   401  			}
   402  			st.children[i] = nil // Reclaim mem from subtree
   403  			returnToPool(child)
   404  		}
   405  		nodes[16] = nilValueNode
   406  		h = newHasher(nil)
   407  		defer returnHasherToPool(h)
   408  		h.tmp.Reset()
   409  		if err := rlp.Encode(&h.tmp, nodes); err != nil {
   410  			panic(err)
   411  		}
   412  	case extNode:
   413  		st.children[0].hash()
   414  		h = newHasher(nil)
   415  		defer returnHasherToPool(h)
   416  		h.tmp.Reset()
   417  		var valuenode node
   418  		if len(st.children[0].val) < 32 {
   419  			valuenode = rawNode(st.children[0].val)
   420  		} else {
   421  			valuenode = hashNode(st.children[0].val)
   422  		}
   423  		n := struct {
   424  			Key []byte
   425  			Val node
   426  		}{
   427  			Key: hexToCompact(st.key),
   428  			Val: valuenode,
   429  		}
   430  		if err := rlp.Encode(&h.tmp, n); err != nil {
   431  			panic(err)
   432  		}
   433  		returnToPool(st.children[0])
   434  		st.children[0] = nil // Reclaim mem from subtree
   435  	case leafNode:
   436  		h = newHasher(nil)
   437  		defer returnHasherToPool(h)
   438  		h.tmp.Reset()
   439  		st.key = append(st.key, byte(16))
   440  		sz := hexToCompactInPlace(st.key)
   441  		n := [][]byte{st.key[:sz], st.val}
   442  		if err := rlp.Encode(&h.tmp, n); err != nil {
   443  			panic(err)
   444  		}
   445  	case emptyNode:
   446  		st.val = emptyRoot.Bytes()
   447  		st.key = st.key[:0]
   448  		st.nodeType = hashedNode
   449  		return
   450  	default:
   451  		panic("Invalid node type")
   452  	}
   453  	st.key = st.key[:0]
   454  	st.nodeType = hashedNode
   455  	if len(h.tmp) < 32 {
   456  		st.val = common.CopyBytes(h.tmp)
   457  		return
   458  	}
   459  	// Write the hash to the 'val'. We allocate a new val here to not mutate
   460  	// input values
   461  	st.val = make([]byte, 32)
   462  	h.sha.Reset()
   463  	h.sha.Write(h.tmp)
   464  	h.sha.Read(st.val)
   465  	if st.db != nil {
   466  		st.db.WriteTrieNode(common.BytesToExtHash(st.val), h.tmp)
   467  	}
   468  }
   469  
   470  // Hash returns the hash of the current node
   471  func (st *StackTrie) Hash() (h common.Hash) {
   472  	st.hash()
   473  	if len(st.val) != 32 {
   474  		// If the node's RLP isn't 32 bytes long, the node will not
   475  		// be hashed, and instead contain the  rlp-encoding of the
   476  		// node. For the top level node, we need to force the hashing.
   477  		ret := make([]byte, 32)
   478  		h := newHasher(nil)
   479  		defer returnHasherToPool(h)
   480  		h.sha.Reset()
   481  		h.sha.Write(st.val)
   482  		h.sha.Read(ret)
   483  		return common.BytesToHash(ret)
   484  	}
   485  	return common.BytesToHash(st.val)
   486  }
   487  
   488  // Commit will firstly hash the entrie trie if it's still not hashed
   489  // and then commit all nodes to the associated database. Actually most
   490  // of the trie nodes MAY have been committed already. The main purpose
   491  // here is to commit the root node.
   492  //
   493  // The associated database is expected, otherwise the whole commit
   494  // functionality should be disabled.
   495  func (st *StackTrie) Commit() (common.Hash, error) {
   496  	if st.db == nil {
   497  		return common.Hash{}, ErrCommitDisabled
   498  	}
   499  	st.hash()
   500  	if len(st.val) != 32 {
   501  		// If the node's RLP isn't 32 bytes long, the node will not
   502  		// be hashed (and committed), and instead contain the  rlp-encoding of the
   503  		// node. For the top level node, we need to force the hashing+commit.
   504  		ret := make([]byte, 32)
   505  		h := newHasher(nil)
   506  		defer returnHasherToPool(h)
   507  		h.sha.Reset()
   508  		h.sha.Write(st.val)
   509  		h.sha.Read(ret)
   510  		st.db.WriteTrieNode(common.BytesToExtHash(ret), st.val)
   511  		return common.BytesToHash(ret), nil
   512  	}
   513  	return common.BytesToHash(st.val), nil
   514  }