[HAR-5] leader won't start consensus if not enough validators

Signed-off-by: Leo Chen <leo@harmony.one>
pull/88/head
Leo Chen 6 years ago
parent 8a1353c35e
commit 676785fa36
  1. 4
      benchmark.go
  2. 5
      consensus/consensus.go
  3. 14
      consensus/consensus_leader.go

@ -92,6 +92,9 @@ func main() {
idcPort := flag.String("idc_port", "8080", "port of the identity chain")
peerDisvoery := flag.Bool("peer_discovery", false, "Enable Peer Discovery")
// Leader needs to have a minimal number of peers to start consensus
minPeers := flag.Int("min_peers", 100, "Minimal number of Peers in shard")
flag.Parse()
if *versionFlag {
@ -165,6 +168,7 @@ func main() {
// Consensus object.
consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader)
consensus.MinPeers = *minPeers
// Start Profiler for leader if profile argument is on
if role == "leader" && (*profile || *metricsReportURL != "") {

@ -39,6 +39,11 @@ type Consensus struct {
// However, we have assumed uint16 in consensus/consensus_leader.go:136
// we won't change it now
validators map[uint16]p2p.Peer
// Minimal number of peers in the shard
// If the number of validators is less than minPeers, the consensus won't start
MinPeers int
// Leader
leader p2p.Peer
// Public keys of the committee including leader and validators

@ -28,6 +28,13 @@ func (consensus *Consensus) WaitForNewBlock(blockChannel chan blockchain.Block)
consensus.Log.Debug("Waiting for block", "consensus", consensus)
for { // keep waiting for new blocks
newBlock := <-blockChannel
if !consensus.HasEnoughValidators() {
consensus.Log.Debug("Not enough validators", "# Validators", len(consensus.validators))
time.Sleep(500 * time.Millisecond)
continue
}
// TODO: think about potential race condition
startTime = time.Now()
consensus.Log.Debug("STARTING CONSENSUS", "consensus", consensus, "startTime", startTime)
@ -452,3 +459,10 @@ func (consensus *Consensus) reportMetrics(block blockchain.Block) {
}
profiler.LogMetrics(metrics)
}
func (consensus *Consensus) HasEnoughValidators() bool {
if len(consensus.validators) < consensus.MinPeers {
return false
}
return true
}

Loading…
Cancel
Save