mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
Add search for most profitable sync contribution (#9121)
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
parent
cd3a2e87fe
commit
4fb3e05124
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user