github.com/prysmaticlabs/prysm@v1.4.4/shared/aggregation/attestations/naive.go (about)

     1  package attestations
     2  
     3  import ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     4  
     5  // NaiveAttestationAggregation aggregates naively, without any complex algorithms or optimizations.
     6  // Note: this is currently a naive implementation to the order of O(mn^2).
     7  func NaiveAttestationAggregation(atts []*ethpb.Attestation) ([]*ethpb.Attestation, error) {
     8  	if len(atts) <= 1 {
     9  		return atts, nil
    10  	}
    11  
    12  	// Naive aggregation. O(n^2) time.
    13  	for i, a := range atts {
    14  		if i >= len(atts) {
    15  			break
    16  		}
    17  		for j := i + 1; j < len(atts); j++ {
    18  			b := atts[j]
    19  			if o, err := a.AggregationBits.Overlaps(b.AggregationBits); err != nil {
    20  				return nil, err
    21  			} else if !o {
    22  				var err error
    23  				a, err = AggregatePair(a, b)
    24  				if err != nil {
    25  					return nil, err
    26  				}
    27  				// Delete b
    28  				atts = append(atts[:j], atts[j+1:]...)
    29  				j--
    30  				atts[i] = a
    31  			}
    32  		}
    33  	}
    34  
    35  	// Naive deduplication of identical aggregations. O(n^2) time.
    36  	for i, a := range atts {
    37  		for j := i + 1; j < len(atts); j++ {
    38  			b := atts[j]
    39  
    40  			if a.AggregationBits.Len() != b.AggregationBits.Len() {
    41  				continue
    42  			}
    43  
    44  			if c, err := a.AggregationBits.Contains(b.AggregationBits); err != nil {
    45  				return nil, err
    46  			} else if c {
    47  				// If b is fully contained in a, then b can be removed.
    48  				atts = append(atts[:j], atts[j+1:]...)
    49  				j--
    50  			} else if c, err := b.AggregationBits.Contains(a.AggregationBits); err != nil {
    51  				return nil, err
    52  			} else if c {
    53  				// if a is fully contained in b, then a can be removed.
    54  				atts = append(atts[:i], atts[i+1:]...)
    55  				break // Stop the inner loop, advance a.
    56  			}
    57  		}
    58  	}
    59  
    60  	return atts, nil
    61  }