github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/iavl/nodedb_orphan.go (about)

     1  package iavl
     2  
     3  import (
     4  	"github.com/tendermint/go-amino"
     5  )
     6  
     7  func (ndb *nodeDB) enqueueOrphanTask(version int64, orphans []*Node, rootHash []byte, persist bool) {
     8  	ndb.addOrphanItem(version, rootHash)
     9  
    10  	task := func() {
    11  		ndb.mtx.Lock()
    12  		if !persist {
    13  			ndb.saveNewOrphans(version, orphans, false)
    14  		}
    15  		ndb.oi.removeOldOrphans(version)
    16  		ndb.mtx.Unlock()
    17  
    18  		ndb.oi.enqueueResult(version)
    19  		ndb.uncacheNodeRontine(orphans)
    20  	}
    21  
    22  	ndb.oi.enqueueTask(task)
    23  }
    24  
    25  func (ndb *nodeDB) addOrphanItem(version int64, rootHash []byte) {
    26  	ndb.mtx.Lock()
    27  	defer ndb.mtx.Unlock()
    28  	ndb.oi.addOrphanItem(version, rootHash)
    29  }
    30  
    31  func (ndb *nodeDB) saveNewOrphans(version int64, orphans []*Node, lock bool) {
    32  	if orphans == nil {
    33  		return
    34  	}
    35  
    36  	version--
    37  	ndb.log(IavlDebug, "saving orphan node to OrphanCache", "size", len(orphans))
    38  	ndb.state.increasOrphanCount(len(orphans))
    39  
    40  	if lock {
    41  		ndb.mtx.Lock()
    42  		defer ndb.mtx.Unlock()
    43  	}
    44  
    45  	ndb.oi.feedOrphansMap(version, orphans)
    46  	for _, node := range orphans {
    47  		ndb.oi.feedOrphanNodeCache(node)
    48  		delete(ndb.prePersistNodeCache, amino.BytesToStr(node.hash))
    49  		node.leftNode = nil
    50  		node.rightNode = nil
    51  	}
    52  }
    53  
    54  func (ndb *nodeDB) sanityCheckHandleOrphansResult(version int64) {
    55  	ndb.oi.wait4Result(version)
    56  }
    57  
    58  func (ndb *nodeDB) findRootHash(version int64) (res []byte, found bool) {
    59  	ndb.mtx.RLock()
    60  	defer ndb.mtx.RUnlock()
    61  	return ndb.oi.findRootHash(version)
    62  }
    63  //
    64  //func (ndb *nodeDB) orphanTask(version int64, orphans []*Node, rootHash []byte, persist bool) {
    65  //	ndb.addOrphanItem(version, rootHash)
    66  //	ndb.mtx.Lock()
    67  //
    68  //	go func(ndb *nodeDB, version int64, orphans []*Node, persist bool) {
    69  //		if persist {
    70  //			ndb.oi.removeOldOrphans(version)
    71  //			ndb.mtx.Unlock()
    72  //		} else {
    73  //			ndb.saveNewOrphans(version, orphans, false)
    74  //			ndb.oi.removeOldOrphans(version)
    75  //			ndb.mtx.Unlock()
    76  //		}
    77  //
    78  //		ndb.oi.enqueueResult(version)
    79  //		ndb.uncacheNodeRontine(orphans)
    80  //	}(ndb, version, orphans, persist)
    81  //}