func (s *State) findWinningRoot(shardCommittee ShardAndCommittee, c *config.Config) (*chainhash.Hash, *AttestationData, error) {
	// find all possible hashes for the winning root
	possibleHashes := map[chainhash.Hash]AttestationData{}
	for _, a := range s.CurrentEpochAttestations {
		if a.Data.Shard != shardCommittee.Shard {
			continue
		}
		possibleHashes[a.Data.ShardBlockHash] = a.Data
	}
	for _, a := range s.PreviousEpochAttestations {
		if a.Data.Shard != shardCommittee.Shard {
			continue
		}
		possibleHashes[a.Data.ShardBlockHash] = a.Data
	}

	if len(possibleHashes) == 0 {
		return nil, nil, nil
	}

	// find the top balance
	topBalance := uint64(0)
	topHash := chainhash.Hash{}

	for b := range possibleHashes {
		validatorIndices, err := s.getAttestingValidatorIndices(shardCommittee, b)
		if err != nil {
			return nil, nil, err
		}

		sumBalance := s.GetTotalBalance(validatorIndices, c)

		if sumBalance > topBalance {
			topHash = b
			topBalance = sumBalance
		}

		if sumBalance == topBalance {
			// in case of a tie, favor the lower hash
			if bytes.Compare(topHash[:], b[:]) > 0 {
				topHash = b
			}
		}
	}
	if topBalance == 0 {
		return nil, nil, nil
	}
	data := possibleHashes[topHash]

	return &topHash, &data, nil
}
// findWinningRoot finds the shard block hash with the most balance voting for it.
findWinningRoot finds the shard block hash with the most balance voting for it.
// findWinningRoot finds the shard block hash with the most balance voting for it .
func (s *State) findWinningRoot(shardCommittee ShardAndCommittee, c *config.Config) (*chainhash.Hash, *AttestationData, error) {
	possibleHashes := map[chainhash.Hash]AttestationData{}
	for _, a := range s.CurrentEpochAttestations {
		if a.Data.Shard != shardCommittee.Shard {
			continue
		}
		possibleHashes[a.Data.ShardBlockHash] = a.Data
	}
	for _, a := range s.PreviousEpochAttestations {
		if a.Data.Shard != shardCommittee.Shard {
			continue
		}
		possibleHashes[a.Data.ShardBlockHash] = a.Data
	}
	if len(possibleHashes) == 0 {
		return nil, nil, nil
	}
	topBalance := uint64(0)
	topHash := chainhash.Hash{}
	for b := range possibleHashes {
		validatorIndices, err := s.getAttestingValidatorIndices(shardCommittee, b)
		if err != nil {
			return nil, nil, err
		}
		sumBalance := s.GetTotalBalance(validatorIndices, c)
		if sumBalance > topBalance {
			topHash = b
			topBalance = sumBalance
		}
		if sumBalance == topBalance {
			if bytes.Compare(topHash[:], b[:]) > 0 {
				topHash = b
			}
		}
	}
	if topBalance == 0 {
		return nil, nil, nil
	}
	data := possibleHashes[topHash]
	return &topHash, &data, nil
}
func ( s * State ) findWinningRoot ( shardCommittee ShardAndCommittee , c * config . Config ) ( * chainhash . Hash , * AttestationData , error ) { possibleHashes := map [ chainhash . Hash ] AttestationData { } for _ , a := range s . CurrentEpochAttestations { if a . Data . Shard != shardCommittee . Shard { continue } possibleHashes [ a . Data . ShardBlockHash ] = a . Data } for _ , a := range s . PreviousEpochAttestations { if a . Data . Shard != shardCommittee . Shard { continue } possibleHashes [ a . Data . ShardBlockHash ] = a . Data } if len ( possibleHashes ) == 0 { return nil , nil , nil } topBalance := uint64 ( 0 ) topHash := chainhash . Hash { } for b := range possibleHashes { validatorIndices , err := s . getAttestingValidatorIndices ( shardCommittee , b ) if err != nil { return nil , nil , err } sumBalance := s . GetTotalBalance ( validatorIndices , c ) if sumBalance > topBalance { topHash = b topBalance = sumBalance } if sumBalance == topBalance { if bytes . Compare ( topHash [ : ] , b [ : ] ) > 0 { topHash = b } } } if topBalance == 0 { return nil , nil , nil } data := possibleHashes [ topHash ] return & topHash , & data , nil }
findWinningRoot finds the shard block hash with the most balance voting for it.
findWinningRoot finds the shard block hash with the most balance voting for it .
// find all possible hashes for the winning root // find the top balance // in case of a tie, favor the lower hash