SubscribeCommitteeSubnets fields length check (#6359)

* SubscribeCommitteeSubnets fields length check

A case where `len(req.Slots) != len(req.CommitteeIds)` but `len(req.CommitteeIds) == len(req.IsAggregator)`
should also throw an error. Without this fix only 2 length inequalities will throw the error.

Greetings from PwC Switzerland
* Added diff length SubscribeCommitteeSubnets test
* Merge branch 'master' into patch-1
This commit is contained in:
Anton
2020-06-23 16:50:46 +02:00
committed by GitHub
parent 078c157f56
commit fe6cf9f33b
2 changed files with 40 additions and 1 deletions

View File

@@ -194,7 +194,7 @@ func (vs *Server) SubscribeCommitteeSubnets(ctx context.Context, req *ethpb.Comm
ctx, span := trace.StartSpan(ctx, "AttesterServer.SubscribeCommitteeSubnets")
defer span.End()
if len(req.Slots) != len(req.CommitteeIds) && len(req.CommitteeIds) != len(req.IsAggregator) {
if len(req.Slots) != len(req.CommitteeIds) || len(req.CommitteeIds) != len(req.IsAggregator) {
return nil, status.Error(codes.InvalidArgument, "request fields are not the same length")
}
if len(req.Slots) == 0 {

View File

@@ -659,6 +659,45 @@ func TestServer_SubscribeCommitteeSubnets_NoSlots(t *testing.T) {
}
}
func TestServer_SubscribeCommitteeSubnets_DifferentLengthSlots(t *testing.T) {
db := dbutil.SetupDB(t)
// fixed seed
s := rand.NewSource(10)
randGen := rand.New(s)
attesterServer := &Server{
HeadFetcher: &mock.ChainService{},
P2P: &mockp2p.MockBroadcaster{},
BeaconDB: db,
AttestationCache: cache.NewAttestationCache(),
AttPool: attestations.NewPool(),
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
}
var slots []uint64
var comIdxs []uint64
var isAggregator []bool
for i := uint64(100); i < 200; i++ {
slots = append(slots, i)
comIdxs = append(comIdxs, uint64(randGen.Int63n(64)))
boolVal := randGen.Uint64()%2 == 0
isAggregator = append(isAggregator, boolVal)
}
slots = append(slots, 321)
_, err := attesterServer.SubscribeCommitteeSubnets(context.Background(), &ethpb.CommitteeSubnetsSubscribeRequest{
Slots: slots,
CommitteeIds: comIdxs,
IsAggregator: isAggregator,
})
if err == nil || !strings.Contains(err.Error(), "request fields are not the same length") {
t.Fatalf("Expected request fields are not the same length error, received: %v", err)
}
}
func TestServer_SubscribeCommitteeSubnets_MultipleSlots(t *testing.T) {
db := dbutil.SetupDB(t)
// fixed seed