diff --git a/benchmark.go b/benchmark.go index 5ae7a2cab..35322cc1d 100644 --- a/benchmark.go +++ b/benchmark.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 != "") { diff --git a/consensus/consensus.go b/consensus/consensus.go index 8acfa034f..bd5136685 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -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 diff --git a/consensus/consensus_leader.go b/consensus/consensus_leader.go index 8e4957639..7df244509 100644 --- a/consensus/consensus_leader.go +++ b/consensus/consensus_leader.go @@ -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 +}