github.com/turingchain2020/turingchain@v1.1.21/util/calccommit.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  //GetMostCommit 获取commits中最多的一个,数量和string
     8  func GetMostCommit(commits [][]byte) (int, string) {
     9  	stats := make(map[string]int)
    10  	n := len(commits)
    11  	for i := 0; i < n; i++ {
    12  		if _, ok := stats[string(commits[i])]; ok {
    13  			stats[string(commits[i])]++
    14  		} else {
    15  			stats[string(commits[i])] = 1
    16  		}
    17  	}
    18  	most := -1
    19  	var key string
    20  	for k, v := range stats {
    21  		if v > most {
    22  			most = v
    23  			key = k
    24  		}
    25  	}
    26  	return most, key
    27  }
    28  
    29  //IsCommitDone 判断是否most 超过total*2/3
    30  func IsCommitDone(total, most int) bool {
    31  	return 3*most > 2*total
    32  }