mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 07:58:22 -05:00
Remove proposer_sync_aggregate.go (#9231)
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
@@ -10,7 +10,6 @@ go_library(
|
||||
"log.go",
|
||||
"proposer.go",
|
||||
"proposer_attestations.go",
|
||||
"proposer_sync_aggregate.go",
|
||||
"server.go",
|
||||
"status.go",
|
||||
],
|
||||
@@ -42,7 +41,6 @@ go_library(
|
||||
"//proto/eth/v1alpha1:go_default_library",
|
||||
"//proto/eth/v1alpha1/wrapper:go_default_library",
|
||||
"//proto/interfaces:go_default_library",
|
||||
"//proto/prysm/v2:go_default_library",
|
||||
"//shared/aggregation:go_default_library",
|
||||
"//shared/aggregation/attestations:go_default_library",
|
||||
"//shared/bls:go_default_library",
|
||||
@@ -79,7 +77,6 @@ go_test(
|
||||
"attester_test.go",
|
||||
"exit_test.go",
|
||||
"proposer_attestations_test.go",
|
||||
"proposer_sync_aggregate_test.go",
|
||||
"proposer_test.go",
|
||||
"server_test.go",
|
||||
"status_test.go",
|
||||
@@ -111,7 +108,6 @@ go_test(
|
||||
"//proto/eth/v1:go_default_library",
|
||||
"//proto/eth/v1alpha1:go_default_library",
|
||||
"//proto/eth/v1alpha1/wrapper:go_default_library",
|
||||
"//proto/prysm/v2:go_default_library",
|
||||
"//shared/aggregation/attestations:go_default_library",
|
||||
"//shared/attestationutil:go_default_library",
|
||||
"//shared/bls:go_default_library",
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
eth "github.com/prysmaticlabs/prysm/proto/prysm/v2"
|
||||
)
|
||||
|
||||
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 c, err := a.AggregationBits.Contains(b.AggregationBits); err != nil {
|
||||
return nil, err
|
||||
} else if c {
|
||||
// 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 c, err := b.AggregationBits.Contains(a.GetAggregationBits()); err != nil {
|
||||
return nil, err
|
||||
} else if c {
|
||||
// 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]
|
||||
for _, c := range cs[1:] {
|
||||
if c.AggregationBits.Count() > mostProfitable.AggregationBits.Count() {
|
||||
mostProfitable = c
|
||||
}
|
||||
}
|
||||
return mostProfitable
|
||||
}
|
||||
@@ -1,392 +0,0 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/go-bitfield"
|
||||
v2 "github.com/prysmaticlabs/prysm/proto/prysm/v2"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
||||
)
|
||||
|
||||
func TestProposerSyncContributions_FilterByBlockRoot(t *testing.T) {
|
||||
rootA := [32]byte{'a'}
|
||||
rootB := [32]byte{'b'}
|
||||
tests := []struct {
|
||||
name string
|
||||
cs proposerSyncContributions
|
||||
want proposerSyncContributions
|
||||
}{
|
||||
{
|
||||
name: "empty list",
|
||||
cs: proposerSyncContributions{},
|
||||
want: proposerSyncContributions{},
|
||||
},
|
||||
{
|
||||
name: "single item, not found",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.NewBitvector128()},
|
||||
},
|
||||
want: proposerSyncContributions{},
|
||||
},
|
||||
{
|
||||
name: "single item with filter, found",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:], Slot: 0},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootB[:], Slot: 1},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:]},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple items with filter, found",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:], Slot: 0},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootB[:], Slot: 1},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:], Slot: 2},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootB[:], Slot: 3},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:], Slot: 0},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:], Slot: 2},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cs := tt.cs.filterByBlockRoot(rootA)
|
||||
assert.DeepEqual(t, tt.want, cs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProposerSyncContributions_FilterBySubcommitteeID(t *testing.T) {
|
||||
rootA := [32]byte{'a'}
|
||||
rootB := [32]byte{'b'}
|
||||
tests := []struct {
|
||||
name string
|
||||
cs proposerSyncContributions
|
||||
want proposerSyncContributions
|
||||
}{
|
||||
{
|
||||
name: "empty list",
|
||||
cs: proposerSyncContributions{},
|
||||
want: proposerSyncContributions{},
|
||||
},
|
||||
{
|
||||
name: "single item, not found",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.NewBitvector128(), SubcommitteeIndex: 1},
|
||||
},
|
||||
want: proposerSyncContributions{},
|
||||
},
|
||||
{
|
||||
name: "single item with filter",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:], SubcommitteeIndex: 0},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootB[:], SubcommitteeIndex: 1},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:]},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple items with filter",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:], SubcommitteeIndex: 0},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootB[:], SubcommitteeIndex: 1},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootB[:], SubcommitteeIndex: 0},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootB[:], SubcommitteeIndex: 2},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootA[:], SubcommitteeIndex: 0},
|
||||
&v2.SyncCommitteeContribution{BlockRoot: rootB[:], SubcommitteeIndex: 0},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cs := tt.cs.filterBySubIndex(0)
|
||||
assert.DeepEqual(t, tt.want, cs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProposerSyncContributions_Dedup(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cs proposerSyncContributions
|
||||
want proposerSyncContributions
|
||||
}{
|
||||
{
|
||||
name: "nil list",
|
||||
cs: nil,
|
||||
want: proposerSyncContributions(nil),
|
||||
},
|
||||
{
|
||||
name: "empty list",
|
||||
cs: proposerSyncContributions{},
|
||||
want: proposerSyncContributions{},
|
||||
},
|
||||
{
|
||||
name: "single item",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.NewBitvector128()},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.NewBitvector128()},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "two items no duplicates",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10111110, 0x01}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01111111, 0x01}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01111111, 0x01}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10111110, 0x01}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "two items with duplicates",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0xba, 0x01}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0xba, 0x01}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0xba, 0x01}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sorted no duplicates",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00101011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10100000, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00010000, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00101011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10100000, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00010000, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sorted with duplicates",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000001, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "all equal",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "unsorted no duplicates",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00100010, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10100101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00010000, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10100101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00100010, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00010000, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "unsorted with duplicates",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10100101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10100101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000001, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10100101, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no proper subset (same root)",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10000001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00011001, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00011001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b10000001, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "proper subset (same root)",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no proper subset (different index)",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000101, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b10000001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b00011001, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b00011001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b10000001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000101, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "proper subset (different index 1)",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b00000011, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00000001, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b01101101, 0b1}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "proper subset (different index 2)",
|
||||
cs: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b00001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
},
|
||||
want: proposerSyncContributions{
|
||||
&v2.SyncCommitteeContribution{SubcommitteeIndex: 1, AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
&v2.SyncCommitteeContribution{AggregationBits: bitfield.Bitvector128{0b11001111, 0b1}},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cs, err := tt.cs.dedup()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
sort.Slice(cs, func(i, j int) bool {
|
||||
if cs[i].AggregationBits.Count() == cs[j].AggregationBits.Count() {
|
||||
if cs[i].SubcommitteeIndex == cs[j].SubcommitteeIndex {
|
||||
return bytes.Compare(cs[i].AggregationBits, cs[j].AggregationBits) <= 0
|
||||
}
|
||||
return cs[i].SubcommitteeIndex > cs[j].SubcommitteeIndex
|
||||
}
|
||||
return cs[i].AggregationBits.Count() > cs[j].AggregationBits.Count()
|
||||
})
|
||||
assert.DeepEqual(t, tt.want, cs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user