github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/consensus.go (about)

     1  package sdk
     2  
     3  import "sync"
     4  
     5  type Consensus struct {
     6  	*sync.RWMutex
     7  	consensus       int // Total successful and valid response from blobbers
     8  	consensusThresh int // Consensus threshold percentage
     9  	fullconsensus   int // Total number of blobbers in allocation
    10  }
    11  
    12  // Done increase consensus by 1
    13  func (c *Consensus) Done() {
    14  	c.Lock()
    15  	c.consensus++
    16  	c.Unlock()
    17  }
    18  
    19  // Reset reset consensus to 0
    20  func (c *Consensus) Reset() {
    21  	c.Lock()
    22  	c.consensus = 0
    23  	c.Unlock()
    24  }
    25  
    26  func (c *Consensus) Init(threshConsensus, fullConsensus int) {
    27  	c.Lock()
    28  	c.consensusThresh = threshConsensus
    29  	c.fullconsensus = fullConsensus
    30  	c.Unlock()
    31  }
    32  
    33  func (c *Consensus) getConsensus() int {
    34  	c.RLock()
    35  	defer c.RUnlock()
    36  	return c.consensus
    37  }
    38  
    39  func (c *Consensus) isConsensusOk() bool {
    40  	c.RLock()
    41  	defer c.RUnlock()
    42  
    43  	return c.getConsensus() >= c.consensusThresh
    44  }