github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/consensus/cbft/roundcache.go (about)

     1  package cbft
     2  
     3  import (
     4  	"github.com/PlatONnetwork/PlatON-Go/common"
     5  	"math/big"
     6  	"github.com/PlatONnetwork/PlatON-Go/p2p/discover"
     7  	"github.com/PlatONnetwork/PlatON-Go/log"
     8  )
     9  
    10  type roundCache map[uint64]map[common.Hash]*nodeCache
    11  
    12  type nodeCache struct {
    13  	former            *pposRound 	// the previous round of witnesses nodeId
    14  	current           *pposRound 	// the current round of witnesses nodeId
    15  	next              *pposRound 	// the next round of witnesses nodeId
    16  }
    17  
    18  type pposRound struct {
    19  	nodeIds []discover.NodeID
    20  	nodes 	[]*discover.Node
    21  	start *big.Int
    22  	end   *big.Int
    23  }
    24  
    25  
    26  func (r roundCache) getFormerRound(blockNumber *big.Int, blockHash common.Hash) *pposRound {
    27  	num := blockNumber.Uint64()
    28  	if round, ok := r[num]; ok {
    29  		if node, has := round[blockHash]; has {
    30  			if nil != node.former {
    31  				return node.former
    32  			}
    33  		}
    34  	}
    35  	return nil
    36  }
    37  
    38  
    39  func (r roundCache) getCurrentRound (blockNumber *big.Int, blockHash common.Hash) *pposRound {
    40  	num := blockNumber.Uint64()
    41  	if round, ok := r[num]; ok {
    42  		if node, has := round[blockHash]; has {
    43  			if nil != node.current {
    44  				return node.current
    45  			}
    46  		}
    47  	}
    48  	return nil
    49  }
    50  
    51  func (r roundCache) getNextRound (blockNumber *big.Int, blockHash common.Hash) *pposRound {
    52  	num := blockNumber.Uint64()
    53  	if round, ok := r[num]; ok {
    54  		if node, has := round[blockHash]; has {
    55  			if nil != node.next {
    56  				return node.next
    57  			}
    58  		}
    59  	}
    60  	return nil
    61  }
    62  
    63  func (r roundCache) getNodeCache (blockNumber *big.Int, blockHash common.Hash) *nodeCache {
    64  	num := blockNumber.Uint64()
    65  	if round, ok := r[num]; ok {
    66  		if node, has := round[blockHash]; has {
    67  			return node
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  
    74  func (r roundCache) setNodeCache (blockNumber *big.Int, blockHash common.Hash, cache *nodeCache) {
    75  	num := blockNumber.Uint64()
    76  	var node map[common.Hash]*nodeCache
    77  	if _, ok := r[num]; ok {
    78  		node = r[num]
    79  	}else {
    80  		node = make(map[common.Hash]*nodeCache)
    81  	}
    82  	if _, ok := node[blockHash]; !ok {
    83  		node[blockHash] = cache
    84  		r[num] = node
    85  	}
    86  	// delete index out of BaseIrrCount
    87  	// But doesn't delete genesis NodeCache
    88  	baseIrrCountNum := num - common.PPosNodeCacheSize
    89  	if baseIrrCountNum > 0 && baseIrrCountNum < num {
    90  		delete(r, baseIrrCountNum)
    91  	}
    92  
    93  	log.Debug("Setting NodeCache", "blocknumber", blockNumber.String(), "blockHash", blockHash)
    94  }
    95