/
proposer_sync_aggregate.go
94 lines (86 loc) · 2.95 KB
/
proposer_sync_aggregate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package validator
import (
"bytes"
eth "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
)
type proposerSyncContributions []*eth.SyncCommitteeContribution
// filterByBlockRoot separates sync aggregate list into a valid group.
// The valid group contains the input block root.
func (cs proposerSyncContributions) filterByBlockRoot(r [32]byte) proposerSyncContributions {
matchedSyncContributions := make([]*eth.SyncCommitteeContribution, 0, len(cs))
for _, c := range cs {
if bytes.Equal(c.BlockRoot, r[:]) {
matchedSyncContributions = append(matchedSyncContributions, c)
}
}
return matchedSyncContributions
}
// filterBySubIndex separates sync aggregate list into a valid group.
// The valid group contains the matching sub committee index.
func (cs proposerSyncContributions) filterBySubIndex(i uint64) proposerSyncContributions {
matchedSyncContributions := make([]*eth.SyncCommitteeContribution, 0, len(cs))
for _, c := range cs {
if c.SubcommitteeIndex == i {
matchedSyncContributions = append(matchedSyncContributions, c)
}
}
return matchedSyncContributions
}
// dedup removes duplicate sync contributions (ones with the same bits set on).
// Important: not only exact duplicates are removed, but proper subsets are removed too
// (their known bits are redundant and are already contained in their supersets).
func (cs proposerSyncContributions) dedup() (proposerSyncContributions, error) {
if len(cs) < 2 {
return cs, nil
}
contributionsBySubIdx := make(map[uint64][]*eth.SyncCommitteeContribution, len(cs))
for _, c := range cs {
contributionsBySubIdx[c.SubcommitteeIndex] = append(contributionsBySubIdx[c.SubcommitteeIndex], c)
}
uniqContributions := make([]*eth.SyncCommitteeContribution, 0, len(cs))
for _, cs := range contributionsBySubIdx {
for i := 0; i < len(cs); i++ {
a := cs[i]
for j := i + 1; j < len(cs); j++ {
b := cs[j]
if aContainsB, err := a.AggregationBits.Contains(b.AggregationBits); err != nil {
return nil, err
} else if aContainsB {
// a contains b, b is redundant.
cs[j] = cs[len(cs)-1]
cs[len(cs)-1] = nil
cs = cs[:len(cs)-1]
j--
} else if bContainsA, err := b.AggregationBits.Contains(a.GetAggregationBits()); err != nil {
return nil, err
} else if bContainsA {
// b contains a, a is redundant.
cs[i] = cs[len(cs)-1]
cs[len(cs)-1] = nil
cs = cs[:len(cs)-1]
i--
break
}
}
}
uniqContributions = append(uniqContributions, cs...)
}
return uniqContributions, nil
}
// mostProfitable returns the most profitable sync contribution, the one with the most
// votes (ie. aggregation bits count)
func (cs proposerSyncContributions) mostProfitable() *eth.SyncCommitteeContribution {
if len(cs) == 0 {
return nil
}
mostProfitable := cs[0]
if len(cs) < 2 {
return mostProfitable
}
for _, c := range cs[1:] {
if c.AggregationBits.Count() > mostProfitable.AggregationBits.Count() {
mostProfitable = c
}
}
return mostProfitable
}