github.com/ethereum/go-ethereum@v1.16.1/trie/sync.go (about)

     1  // Copyright 2015 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
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"sync"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/hexutil"
    26  	"github.com/ethereum/go-ethereum/common/prque"
    27  	"github.com/ethereum/go-ethereum/core/rawdb"
    28  	"github.com/ethereum/go-ethereum/core/types"
    29  	"github.com/ethereum/go-ethereum/crypto"
    30  	"github.com/ethereum/go-ethereum/ethdb"
    31  	"github.com/ethereum/go-ethereum/log"
    32  	"github.com/ethereum/go-ethereum/metrics"
    33  )
    34  
    35  // ErrNotRequested is returned by the trie sync when it's requested to process a
    36  // node it did not request.
    37  var ErrNotRequested = errors.New("not requested")
    38  
    39  // ErrAlreadyProcessed is returned by the trie sync when it's requested to process a
    40  // node it already processed previously.
    41  var ErrAlreadyProcessed = errors.New("already processed")
    42  
    43  // maxFetchesPerDepth is the maximum number of pending trie nodes per depth. The
    44  // role of this value is to limit the number of trie nodes that get expanded in
    45  // memory if the node was configured with a significant number of peers.
    46  const maxFetchesPerDepth = 16384
    47  
    48  var (
    49  	// deletionGauge is the metric to track how many trie node deletions
    50  	// are performed in total during the sync process.
    51  	deletionGauge = metrics.NewRegisteredGauge("trie/sync/delete", nil)
    52  
    53  	// lookupGauge is the metric to track how many trie node lookups are
    54  	// performed to determine if node needs to be deleted.
    55  	lookupGauge = metrics.NewRegisteredGauge("trie/sync/lookup", nil)
    56  
    57  	// accountNodeSyncedGauge is the metric to track how many account trie
    58  	// node are written during the sync.
    59  	accountNodeSyncedGauge = metrics.NewRegisteredGauge("trie/sync/nodes/account", nil)
    60  
    61  	// storageNodeSyncedGauge is the metric to track how many account trie
    62  	// node are written during the sync.
    63  	storageNodeSyncedGauge = metrics.NewRegisteredGauge("trie/sync/nodes/storage", nil)
    64  
    65  	// codeSyncedGauge is the metric to track how many contract codes are
    66  	// written during the sync.
    67  	codeSyncedGauge = metrics.NewRegisteredGauge("trie/sync/codes", nil)
    68  )
    69  
    70  // SyncPath is a path tuple identifying a particular trie node either in a single
    71  // trie (account) or a layered trie (account -> storage).
    72  //
    73  // Content wise the tuple either has 1 element if it addresses a node in a single
    74  // trie or 2 elements if it addresses a node in a stacked trie.
    75  //
    76  // To support aiming arbitrary trie nodes, the path needs to support odd nibble
    77  // lengths. To avoid transferring expanded hex form over the network, the last
    78  // part of the tuple (which needs to index into the middle of a trie) is compact
    79  // encoded. In case of a 2-tuple, the first item is always 32 bytes so that is
    80  // simple binary encoded.
    81  //
    82  // Examples:
    83  //   - Path 0x9  -> {0x19}
    84  //   - Path 0x99 -> {0x0099}
    85  //   - Path 0x01234567890123456789012345678901012345678901234567890123456789019  -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x19}
    86  //   - Path 0x012345678901234567890123456789010123456789012345678901234567890199 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x0099}
    87  type SyncPath [][]byte
    88  
    89  // NewSyncPath converts an expanded trie path from nibble form into a compact
    90  // version that can be sent over the network.
    91  func NewSyncPath(path []byte) SyncPath {
    92  	// If the hash is from the account trie, append a single item, if it
    93  	// is from a storage trie, append a tuple. Note, the length 64 is
    94  	// clashing between account leaf and storage root. It's fine though
    95  	// because having a trie node at 64 depth means a hash collision was
    96  	// found and we're long dead.
    97  	if len(path) < 64 {
    98  		return SyncPath{hexToCompact(path)}
    99  	}
   100  	return SyncPath{hexToKeybytes(path[:64]), hexToCompact(path[64:])}
   101  }
   102  
   103  // LeafCallback is a callback type invoked when a trie operation reaches a leaf
   104  // node.
   105  //
   106  // The keys is a path tuple identifying a particular trie node either in a single
   107  // trie (account) or a layered trie (account -> storage). Each key in the tuple
   108  // is in the raw format(32 bytes).
   109  //
   110  // The path is a composite hexary path identifying the trie node. All the key
   111  // bytes are converted to the hexary nibbles and composited with the parent path
   112  // if the trie node is in a layered trie.
   113  //
   114  // It's used by state sync and commit to allow handling external references
   115  // between account and storage tries. And also it's used in the state healing
   116  // for extracting the raw states(leaf nodes) with corresponding paths.
   117  type LeafCallback func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error
   118  
   119  // nodeRequest represents a scheduled or already in-flight trie node retrieval request.
   120  type nodeRequest struct {
   121  	hash common.Hash // Hash of the trie node to retrieve
   122  	path []byte      // Merkle path leading to this node for prioritization
   123  	data []byte      // Data content of the node, cached until all subtrees complete
   124  
   125  	parent   *nodeRequest // Parent state node referencing this entry
   126  	deps     int          // Number of dependencies before allowed to commit this node
   127  	callback LeafCallback // Callback to invoke if a leaf node it reached on this branch
   128  }
   129  
   130  // codeRequest represents a scheduled or already in-flight bytecode retrieval request.
   131  type codeRequest struct {
   132  	hash    common.Hash    // Hash of the contract bytecode to retrieve
   133  	path    []byte         // Merkle path leading to this node for prioritization
   134  	data    []byte         // Data content of the node, cached until all subtrees complete
   135  	parents []*nodeRequest // Parent state nodes referencing this entry (notify all upon completion)
   136  }
   137  
   138  // NodeSyncResult is a response with requested trie node along with its node path.
   139  type NodeSyncResult struct {
   140  	Path string // Path of the originally unknown trie node
   141  	Data []byte // Data content of the retrieved trie node
   142  }
   143  
   144  // CodeSyncResult is a response with requested bytecode along with its hash.
   145  type CodeSyncResult struct {
   146  	Hash common.Hash // Hash the originally unknown bytecode
   147  	Data []byte      // Data content of the retrieved bytecode
   148  }
   149  
   150  // nodeOp represents an operation upon the trie node. It can either represent a
   151  // deletion to the specific node or a node write for persisting retrieved node.
   152  type nodeOp struct {
   153  	del   bool        // flag if op stands for a delete operation
   154  	owner common.Hash // identifier of the trie (empty for account trie)
   155  	path  []byte      // path from the root to the specified node.
   156  	blob  []byte      // the content of the node (nil for deletion)
   157  	hash  common.Hash // hash of the node content (empty for node deletion)
   158  }
   159  
   160  // valid checks whether the node operation is valid.
   161  func (op *nodeOp) valid() bool {
   162  	if op.del && len(op.blob) != 0 {
   163  		return false
   164  	}
   165  	if !op.del && len(op.blob) == 0 {
   166  		return false
   167  	}
   168  	return true
   169  }
   170  
   171  // string returns the node operation in string representation.
   172  func (op *nodeOp) string() string {
   173  	var node string
   174  	if op.owner == (common.Hash{}) {
   175  		node = fmt.Sprintf("node: (%v)", op.path)
   176  	} else {
   177  		node = fmt.Sprintf("node: (%x-%v)", op.owner, op.path)
   178  	}
   179  	var blobHex string
   180  	if len(op.blob) == 0 {
   181  		blobHex = "nil"
   182  	} else {
   183  		blobHex = hexutil.Encode(op.blob)
   184  	}
   185  	if op.del {
   186  		return fmt.Sprintf("del %s %s %s", node, blobHex, op.hash.Hex())
   187  	}
   188  	return fmt.Sprintf("write %s %s %s", node, blobHex, op.hash.Hex())
   189  }
   190  
   191  // syncMemBatch is an in-memory buffer of successfully downloaded but not yet
   192  // persisted data items.
   193  type syncMemBatch struct {
   194  	scheme string                 // State scheme identifier
   195  	codes  map[common.Hash][]byte // In-memory batch of recently completed codes
   196  	nodes  []nodeOp               // In-memory batch of recently completed/deleted nodes
   197  	size   uint64                 // Estimated batch-size of in-memory data.
   198  }
   199  
   200  // newSyncMemBatch allocates a new memory-buffer for not-yet persisted trie nodes.
   201  func newSyncMemBatch(scheme string) *syncMemBatch {
   202  	return &syncMemBatch{
   203  		scheme: scheme,
   204  		codes:  make(map[common.Hash][]byte),
   205  	}
   206  }
   207  
   208  // hasCode reports the contract code with specific hash is already cached.
   209  func (batch *syncMemBatch) hasCode(hash common.Hash) bool {
   210  	_, ok := batch.codes[hash]
   211  	return ok
   212  }
   213  
   214  // addCode caches a contract code database write operation.
   215  func (batch *syncMemBatch) addCode(hash common.Hash, code []byte) {
   216  	batch.codes[hash] = code
   217  	batch.size += common.HashLength + uint64(len(code))
   218  }
   219  
   220  // addNode caches a node database write operation.
   221  func (batch *syncMemBatch) addNode(owner common.Hash, path []byte, blob []byte, hash common.Hash) {
   222  	if batch.scheme == rawdb.PathScheme {
   223  		if owner == (common.Hash{}) {
   224  			batch.size += uint64(len(path) + len(blob))
   225  		} else {
   226  			batch.size += common.HashLength + uint64(len(path)+len(blob))
   227  		}
   228  	} else {
   229  		batch.size += common.HashLength + uint64(len(blob))
   230  	}
   231  	batch.nodes = append(batch.nodes, nodeOp{
   232  		owner: owner,
   233  		path:  path,
   234  		blob:  blob,
   235  		hash:  hash,
   236  	})
   237  }
   238  
   239  // delNode caches a node database delete operation.
   240  func (batch *syncMemBatch) delNode(owner common.Hash, path []byte) {
   241  	if batch.scheme != rawdb.PathScheme {
   242  		log.Error("Unexpected node deletion", "owner", owner, "path", path, "scheme", batch.scheme)
   243  		return // deletion is not supported in hash mode.
   244  	}
   245  	if owner == (common.Hash{}) {
   246  		batch.size += uint64(len(path))
   247  	} else {
   248  		batch.size += common.HashLength + uint64(len(path))
   249  	}
   250  	batch.nodes = append(batch.nodes, nodeOp{
   251  		del:   true,
   252  		owner: owner,
   253  		path:  path,
   254  	})
   255  }
   256  
   257  // Sync is the main state trie synchronisation scheduler, which provides yet
   258  // unknown trie hashes to retrieve, accepts node data associated with said hashes
   259  // and reconstructs the trie step by step until all is done.
   260  type Sync struct {
   261  	scheme   string                       // Node scheme descriptor used in database.
   262  	database ethdb.KeyValueReader         // Persistent database to check for existing entries
   263  	membatch *syncMemBatch                // Memory buffer to avoid frequent database writes
   264  	nodeReqs map[string]*nodeRequest      // Pending requests pertaining to a trie node path
   265  	codeReqs map[common.Hash]*codeRequest // Pending requests pertaining to a code hash
   266  	queue    *prque.Prque[int64, any]     // Priority queue with the pending requests
   267  	fetches  map[int]int                  // Number of active fetches per trie node depth
   268  }
   269  
   270  // NewSync creates a new trie data download scheduler.
   271  func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallback, scheme string) *Sync {
   272  	ts := &Sync{
   273  		scheme:   scheme,
   274  		database: database,
   275  		membatch: newSyncMemBatch(scheme),
   276  		nodeReqs: make(map[string]*nodeRequest),
   277  		codeReqs: make(map[common.Hash]*codeRequest),
   278  		queue:    prque.New[int64, any](nil), // Ugh, can contain both string and hash, whyyy
   279  		fetches:  make(map[int]int),
   280  	}
   281  	ts.AddSubTrie(root, nil, common.Hash{}, nil, callback)
   282  	return ts
   283  }
   284  
   285  // AddSubTrie registers a new trie to the sync code, rooted at the designated
   286  // parent for completion tracking. The given path is a unique node path in
   287  // hex format and contain all the parent path if it's layered trie node.
   288  func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, parentPath []byte, callback LeafCallback) {
   289  	if root == types.EmptyRootHash {
   290  		return
   291  	}
   292  	owner, inner := ResolvePath(path)
   293  	exist, inconsistent := s.hasNode(owner, inner, root)
   294  	if exist {
   295  		// The entire subtrie is already present in the database.
   296  		return
   297  	} else if inconsistent {
   298  		// There is a pre-existing node with the wrong hash in DB, remove it.
   299  		s.membatch.delNode(owner, inner)
   300  	}
   301  	// Assemble the new sub-trie sync request
   302  	req := &nodeRequest{
   303  		hash:     root,
   304  		path:     path,
   305  		callback: callback,
   306  	}
   307  	// If this sub-trie has a designated parent, link them together
   308  	if parent != (common.Hash{}) {
   309  		ancestor := s.nodeReqs[string(parentPath)]
   310  		if ancestor == nil {
   311  			panic(fmt.Sprintf("sub-trie ancestor not found: %x", parent))
   312  		}
   313  		ancestor.deps++
   314  		req.parent = ancestor
   315  	}
   316  	s.scheduleNodeRequest(req)
   317  }
   318  
   319  // AddCodeEntry schedules the direct retrieval of a contract code that should not
   320  // be interpreted as a trie node, but rather accepted and stored into the database
   321  // as is.
   322  func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash, parentPath []byte) {
   323  	// Short circuit if the entry is empty or already known
   324  	if hash == types.EmptyCodeHash {
   325  		return
   326  	}
   327  	if s.membatch.hasCode(hash) {
   328  		return
   329  	}
   330  	// If database says duplicate, the blob is present for sure.
   331  	// Note we only check the existence with new code scheme, snap
   332  	// sync is expected to run with a fresh new node. Even there
   333  	// exists the code with legacy format, fetch and store with
   334  	// new scheme anyway.
   335  	if rawdb.HasCodeWithPrefix(s.database, hash) {
   336  		return
   337  	}
   338  	// Assemble the new sub-trie sync request
   339  	req := &codeRequest{
   340  		path: path,
   341  		hash: hash,
   342  	}
   343  	// If this sub-trie has a designated parent, link them together
   344  	if parent != (common.Hash{}) {
   345  		ancestor := s.nodeReqs[string(parentPath)] // the parent of codereq can ONLY be nodereq
   346  		if ancestor == nil {
   347  			panic(fmt.Sprintf("raw-entry ancestor not found: %x", parent))
   348  		}
   349  		ancestor.deps++
   350  		req.parents = append(req.parents, ancestor)
   351  	}
   352  	s.scheduleCodeRequest(req)
   353  }
   354  
   355  // Missing retrieves the known missing nodes from the trie for retrieval. To aid
   356  // both eth/6x style fast sync and snap/1x style state sync, the paths of trie
   357  // nodes are returned too, as well as separate hash list for codes.
   358  func (s *Sync) Missing(max int) ([]string, []common.Hash, []common.Hash) {
   359  	var (
   360  		nodePaths  []string
   361  		nodeHashes []common.Hash
   362  		codeHashes []common.Hash
   363  	)
   364  	for !s.queue.Empty() && (max == 0 || len(nodeHashes)+len(codeHashes) < max) {
   365  		// Retrieve the next item in line
   366  		item, prio := s.queue.Peek()
   367  
   368  		// If we have too many already-pending tasks for this depth, throttle
   369  		depth := int(prio >> 56)
   370  		if s.fetches[depth] > maxFetchesPerDepth {
   371  			break
   372  		}
   373  		// Item is allowed to be scheduled, add it to the task list
   374  		s.queue.Pop()
   375  		s.fetches[depth]++
   376  
   377  		switch item := item.(type) {
   378  		case common.Hash:
   379  			codeHashes = append(codeHashes, item)
   380  		case string:
   381  			req, ok := s.nodeReqs[item]
   382  			if !ok {
   383  				log.Error("Missing node request", "path", item)
   384  				continue // System very wrong, shouldn't happen
   385  			}
   386  			nodePaths = append(nodePaths, item)
   387  			nodeHashes = append(nodeHashes, req.hash)
   388  		}
   389  	}
   390  	return nodePaths, nodeHashes, codeHashes
   391  }
   392  
   393  // ProcessCode injects the received data for requested item. Note it can
   394  // happen that the single response commits two pending requests(e.g.
   395  // there are two requests one for code and one for node but the hash
   396  // is same). In this case the second response for the same hash will
   397  // be treated as "non-requested" item or "already-processed" item but
   398  // there is no downside.
   399  func (s *Sync) ProcessCode(result CodeSyncResult) error {
   400  	// If the code was not requested or it's already processed, bail out
   401  	req := s.codeReqs[result.Hash]
   402  	if req == nil {
   403  		return ErrNotRequested
   404  	}
   405  	if req.data != nil {
   406  		return ErrAlreadyProcessed
   407  	}
   408  	req.data = result.Data
   409  	return s.commitCodeRequest(req)
   410  }
   411  
   412  // ProcessNode injects the received data for requested item. Note it can
   413  // happen that the single response commits two pending requests(e.g.
   414  // there are two requests one for code and one for node but the hash
   415  // is same). In this case the second response for the same hash will
   416  // be treated as "non-requested" item or "already-processed" item but
   417  // there is no downside.
   418  func (s *Sync) ProcessNode(result NodeSyncResult) error {
   419  	// If the trie node was not requested or it's already processed, bail out
   420  	req := s.nodeReqs[result.Path]
   421  	if req == nil {
   422  		return ErrNotRequested
   423  	}
   424  	if req.data != nil {
   425  		return ErrAlreadyProcessed
   426  	}
   427  	// Decode the node data content and update the request
   428  	node, err := decodeNode(req.hash.Bytes(), result.Data)
   429  	if err != nil {
   430  		return err
   431  	}
   432  	req.data = result.Data
   433  
   434  	// Create and schedule a request for all the children nodes
   435  	requests, err := s.children(req, node)
   436  	if err != nil {
   437  		return err
   438  	}
   439  	if len(requests) == 0 && req.deps == 0 {
   440  		s.commitNodeRequest(req)
   441  	} else {
   442  		req.deps += len(requests)
   443  		for _, child := range requests {
   444  			s.scheduleNodeRequest(child)
   445  		}
   446  	}
   447  	return nil
   448  }
   449  
   450  // Commit flushes the data stored in the internal membatch out to persistent
   451  // storage, returning any occurred error. The whole data set will be flushed
   452  // in an atomic database batch.
   453  func (s *Sync) Commit(dbw ethdb.Batch) error {
   454  	// Flush the pending node writes into database batch.
   455  	var (
   456  		account int
   457  		storage int
   458  	)
   459  	for _, op := range s.membatch.nodes {
   460  		if !op.valid() {
   461  			return fmt.Errorf("invalid op, %s", op.string())
   462  		}
   463  		if op.del {
   464  			// node deletion is only supported in path mode.
   465  			if op.owner == (common.Hash{}) {
   466  				rawdb.DeleteAccountTrieNode(dbw, op.path)
   467  			} else {
   468  				rawdb.DeleteStorageTrieNode(dbw, op.owner, op.path)
   469  			}
   470  			deletionGauge.Inc(1)
   471  		} else {
   472  			if op.owner == (common.Hash{}) {
   473  				account += 1
   474  			} else {
   475  				storage += 1
   476  			}
   477  			rawdb.WriteTrieNode(dbw, op.owner, op.path, op.hash, op.blob, s.scheme)
   478  		}
   479  	}
   480  	accountNodeSyncedGauge.Inc(int64(account))
   481  	storageNodeSyncedGauge.Inc(int64(storage))
   482  
   483  	// Flush the pending code writes into database batch.
   484  	for hash, value := range s.membatch.codes {
   485  		rawdb.WriteCode(dbw, hash, value)
   486  	}
   487  	codeSyncedGauge.Inc(int64(len(s.membatch.codes)))
   488  
   489  	s.membatch = newSyncMemBatch(s.scheme) // reset the batch
   490  	return nil
   491  }
   492  
   493  // MemSize returns an estimated size (in bytes) of the data held in the membatch.
   494  func (s *Sync) MemSize() uint64 {
   495  	return s.membatch.size
   496  }
   497  
   498  // Pending returns the number of state entries currently pending for download.
   499  func (s *Sync) Pending() int {
   500  	return len(s.nodeReqs) + len(s.codeReqs)
   501  }
   502  
   503  // scheduleNodeRequest inserts a new state retrieval request into the fetch queue. If there
   504  // is already a pending request for this node, the new request will be discarded
   505  // and only a parent reference added to the old one.
   506  func (s *Sync) scheduleNodeRequest(req *nodeRequest) {
   507  	s.nodeReqs[string(req.path)] = req
   508  
   509  	// Schedule the request for future retrieval. This queue is shared
   510  	// by both node requests and code requests.
   511  	prio := int64(len(req.path)) << 56 // depth >= 128 will never happen, storage leaves will be included in their parents
   512  	for i := 0; i < 14 && i < len(req.path); i++ {
   513  		prio |= int64(15-req.path[i]) << (52 - i*4) // 15-nibble => lexicographic order
   514  	}
   515  	s.queue.Push(string(req.path), prio)
   516  }
   517  
   518  // scheduleCodeRequest inserts a new state retrieval request into the fetch queue. If there
   519  // is already a pending request for this node, the new request will be discarded
   520  // and only a parent reference added to the old one.
   521  func (s *Sync) scheduleCodeRequest(req *codeRequest) {
   522  	// If we're already requesting this node, add a new reference and stop
   523  	if old, ok := s.codeReqs[req.hash]; ok {
   524  		old.parents = append(old.parents, req.parents...)
   525  		return
   526  	}
   527  	s.codeReqs[req.hash] = req
   528  
   529  	// Schedule the request for future retrieval. This queue is shared
   530  	// by both node requests and code requests.
   531  	prio := int64(len(req.path)) << 56 // depth >= 128 will never happen, storage leaves will be included in their parents
   532  	for i := 0; i < 14 && i < len(req.path); i++ {
   533  		prio |= int64(15-req.path[i]) << (52 - i*4) // 15-nibble => lexicographic order
   534  	}
   535  	s.queue.Push(req.hash, prio)
   536  }
   537  
   538  // children retrieves all the missing children of a state trie entry for future
   539  // retrieval scheduling.
   540  func (s *Sync) children(req *nodeRequest, object node) ([]*nodeRequest, error) {
   541  	// Gather all the children of the node, irrelevant whether known or not
   542  	type childNode struct {
   543  		path []byte
   544  		node node
   545  	}
   546  	var children []childNode
   547  
   548  	switch node := (object).(type) {
   549  	case *shortNode:
   550  		key := node.Key
   551  		if hasTerm(key) {
   552  			key = key[:len(key)-1]
   553  		}
   554  		children = []childNode{{
   555  			node: node.Val,
   556  			path: append(append([]byte(nil), req.path...), key...),
   557  		}}
   558  		// Mark all internal nodes between shortNode and its **in disk**
   559  		// child as invalid. This is essential in the case of path mode
   560  		// scheme; otherwise, state healing might overwrite existing child
   561  		// nodes silently while leaving a dangling parent node within the
   562  		// range of this internal path on disk and the persistent state
   563  		// ends up with a very weird situation that nodes on the same path
   564  		// are not inconsistent while they all present in disk. This property
   565  		// would break the guarantee for state healing.
   566  		//
   567  		// While it's possible for this shortNode to overwrite a previously
   568  		// existing full node, the other branches of the fullNode can be
   569  		// retained as they are not accessible with the new shortNode, and
   570  		// also the whole sub-trie is still untouched and complete.
   571  		//
   572  		// This step is only necessary for path mode, as there is no deletion
   573  		// in hash mode at all.
   574  		if _, ok := node.Val.(hashNode); ok && s.scheme == rawdb.PathScheme {
   575  			owner, inner := ResolvePath(req.path)
   576  			for i := 1; i < len(key); i++ {
   577  				// While checking for a non-existent item in Pebble can be less efficient
   578  				// without a bloom filter, the relatively low frequency of lookups makes
   579  				// the performance impact negligible.
   580  				var exists bool
   581  				if owner == (common.Hash{}) {
   582  					exists = rawdb.HasAccountTrieNode(s.database, append(inner, key[:i]...))
   583  				} else {
   584  					exists = rawdb.HasStorageTrieNode(s.database, owner, append(inner, key[:i]...))
   585  				}
   586  				if exists {
   587  					s.membatch.delNode(owner, append(inner, key[:i]...))
   588  					log.Debug("Detected dangling node", "owner", owner, "path", append(inner, key[:i]...))
   589  				}
   590  			}
   591  			lookupGauge.Inc(int64(len(key) - 1))
   592  		}
   593  	case *fullNode:
   594  		for i := 0; i < 17; i++ {
   595  			if node.Children[i] != nil {
   596  				children = append(children, childNode{
   597  					node: node.Children[i],
   598  					path: append(append([]byte(nil), req.path...), byte(i)),
   599  				})
   600  			}
   601  		}
   602  	default:
   603  		panic(fmt.Sprintf("unknown node: %+v", node))
   604  	}
   605  	// Iterate over the children, and request all unknown ones
   606  	var (
   607  		missing = make(chan *nodeRequest, len(children))
   608  		pending sync.WaitGroup
   609  		batchMu sync.Mutex
   610  	)
   611  	for _, child := range children {
   612  		// Notify any external watcher of a new key/value node
   613  		if req.callback != nil {
   614  			if node, ok := (child.node).(valueNode); ok {
   615  				var paths [][]byte
   616  				if len(child.path) == 2*common.HashLength {
   617  					paths = append(paths, hexToKeybytes(child.path))
   618  				} else if len(child.path) == 4*common.HashLength {
   619  					paths = append(paths, hexToKeybytes(child.path[:2*common.HashLength]))
   620  					paths = append(paths, hexToKeybytes(child.path[2*common.HashLength:]))
   621  				}
   622  				if err := req.callback(paths, child.path, node, req.hash, req.path); err != nil {
   623  					return nil, err
   624  				}
   625  			}
   626  		}
   627  		// If the child references another node, resolve or schedule.
   628  		// We check all children concurrently.
   629  		if node, ok := (child.node).(hashNode); ok {
   630  			path := child.path
   631  			hash := common.BytesToHash(node)
   632  			pending.Add(1)
   633  			go func() {
   634  				defer pending.Done()
   635  				owner, inner := ResolvePath(path)
   636  				exist, inconsistent := s.hasNode(owner, inner, hash)
   637  				if exist {
   638  					return
   639  				} else if inconsistent {
   640  					// There is a pre-existing node with the wrong hash in DB, remove it.
   641  					batchMu.Lock()
   642  					s.membatch.delNode(owner, inner)
   643  					batchMu.Unlock()
   644  				}
   645  				// Locally unknown node, schedule for retrieval
   646  				missing <- &nodeRequest{
   647  					path:     path,
   648  					hash:     hash,
   649  					parent:   req,
   650  					callback: req.callback,
   651  				}
   652  			}()
   653  		}
   654  	}
   655  	pending.Wait()
   656  
   657  	requests := make([]*nodeRequest, 0, len(children))
   658  	for done := false; !done; {
   659  		select {
   660  		case miss := <-missing:
   661  			requests = append(requests, miss)
   662  		default:
   663  			done = true
   664  		}
   665  	}
   666  	return requests, nil
   667  }
   668  
   669  // commitNodeRequest finalizes a retrieval request and stores it into the membatch. If any
   670  // of the referencing parent requests complete due to this commit, they are also
   671  // committed themselves.
   672  func (s *Sync) commitNodeRequest(req *nodeRequest) error {
   673  	// Write the node content to the membatch
   674  	owner, path := ResolvePath(req.path)
   675  	s.membatch.addNode(owner, path, req.data, req.hash)
   676  
   677  	// Removed the completed node request
   678  	delete(s.nodeReqs, string(req.path))
   679  	s.fetches[len(req.path)]--
   680  
   681  	// Check parent for completion
   682  	if req.parent != nil {
   683  		req.parent.deps--
   684  		if req.parent.deps == 0 {
   685  			if err := s.commitNodeRequest(req.parent); err != nil {
   686  				return err
   687  			}
   688  		}
   689  	}
   690  	return nil
   691  }
   692  
   693  // commitCodeRequest finalizes a retrieval request and stores it into the membatch. If any
   694  // of the referencing parent requests complete due to this commit, they are also
   695  // committed themselves.
   696  func (s *Sync) commitCodeRequest(req *codeRequest) error {
   697  	// Write the node content to the membatch
   698  	s.membatch.addCode(req.hash, req.data)
   699  
   700  	// Removed the completed code request
   701  	delete(s.codeReqs, req.hash)
   702  	s.fetches[len(req.path)]--
   703  
   704  	// Check all parents for completion
   705  	for _, parent := range req.parents {
   706  		parent.deps--
   707  		if parent.deps == 0 {
   708  			if err := s.commitNodeRequest(parent); err != nil {
   709  				return err
   710  			}
   711  		}
   712  	}
   713  	return nil
   714  }
   715  
   716  // hasNode reports whether the specified trie node is present in the database.
   717  // 'exists' is true when the node exists in the database and matches the given root
   718  // hash. The 'inconsistent' return value is true when the node exists but does not
   719  // match the expected hash.
   720  func (s *Sync) hasNode(owner common.Hash, path []byte, hash common.Hash) (exists bool, inconsistent bool) {
   721  	// If node is running with hash scheme, check the presence with node hash.
   722  	if s.scheme == rawdb.HashScheme {
   723  		return rawdb.HasLegacyTrieNode(s.database, hash), false
   724  	}
   725  	// If node is running with path scheme, check the presence with node path.
   726  	var blob []byte
   727  	if owner == (common.Hash{}) {
   728  		blob = rawdb.ReadAccountTrieNode(s.database, path)
   729  	} else {
   730  		blob = rawdb.ReadStorageTrieNode(s.database, owner, path)
   731  	}
   732  	exists = hash == crypto.Keccak256Hash(blob)
   733  	inconsistent = !exists && len(blob) != 0
   734  	return exists, inconsistent
   735  }
   736  
   737  // ResolvePath resolves the provided composite node path by separating the
   738  // path in account trie if it's existent.
   739  func ResolvePath(path []byte) (common.Hash, []byte) {
   740  	var owner common.Hash
   741  	if len(path) >= 2*common.HashLength {
   742  		owner = common.BytesToHash(hexToKeybytes(path[:2*common.HashLength]))
   743  		path = path[2*common.HashLength:]
   744  	}
   745  	return owner, path
   746  }