Add search for most profitable sync contribution (#9121)

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
terence tsao 2021-06-29 10:26:01 -07:00 committed by GitHub
parent cd3a2e87fe
commit 4fb3e05124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 0 deletions

View File

@ -70,3 +70,18 @@ func (cs proposerSyncContributions) dedup() proposerSyncContributions {
}
return uniqContributions
}
// 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]
for _, c := range cs[1:] {
if c.AggregationBits.Count() > mostProfitable.AggregationBits.Count() {
mostProfitable = c
}
}
return mostProfitable
}

View File

@ -340,3 +340,50 @@ func TestProposerSyncContributions_Dedup(t *testing.T) {
})
}
}
func TestProposerSyncContributions_MostProfitable(t *testing.T) {
tests := []struct {
name string
cs proposerSyncContributions
want *v2.SyncCommitteeContribution
}{
{
name: "Same item",
cs: proposerSyncContributions{
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01}},
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01}},
},
want: &v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01}},
},
{
name: "Same item again",
cs: proposerSyncContributions{
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01}},
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10}},
},
want: &v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01}},
},
{
name: "most profitable at the start",
cs: proposerSyncContributions{
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b0101}},
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b0100}},
},
want: &v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b0101}},
},
{
name: "most profitable at the end",
cs: proposerSyncContributions{
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b0101}},
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b0111}},
},
want: &v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b0111}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cs := tt.cs.mostProfitable()
assert.DeepEqual(t, tt.want, cs)
})
}
}