Skip to content

Commit

Permalink
nil attestation tests added (#5457)
Browse files Browse the repository at this point in the history
* nil attestation tests added
  • Loading branch information
farazdagi authored Apr 16, 2020
1 parent c70103b commit 075d29f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
16 changes: 10 additions & 6 deletions beacon-chain/blockchain/process_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) ([]ui
ctx, span := trace.StartSpan(ctx, "blockchain.onAttestation")
defer span.End()

if a == nil {
return nil, errors.New("nil attestation")
}
if a.Data == nil {
return nil, errors.New("nil attestation.Data field")
}
if a.Data.Target == nil {
return nil, errors.New("nil attestation.Data.Target field")
}

tgt := stateTrie.CopyCheckpoint(a.Data.Target)
tgtSlot := helpers.StartSlot(tgt.Epoch)

Expand Down Expand Up @@ -120,12 +130,6 @@ func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) ([]ui
if indexedAtt.AttestingIndices == nil {
return nil, errors.New("nil attesting indices")
}
if a.Data == nil {
return nil, errors.New("nil att data")
}
if a.Data.Target == nil {
return nil, errors.New("nil att target")
}

// Update forkchoice store with the new attestation for updating weight.
s.forkChoiceStore.ProcessAttestation(ctx, indexedAtt.AttestingIndices, bytesutil.ToBytes32(a.Data.BeaconBlockRoot), a.Data.Target.Epoch)
Expand Down
23 changes: 22 additions & 1 deletion beacon-chain/blockchain/process_attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,34 @@ func TestStore_OnAttestation(t *testing.T) {
wantErr: true,
wantErrString: "does not match current epoch",
},
{
name: "process nil field (a.Target) in attestation",
a: nil,
s: &pb.BeaconState{},
wantErr: true,
wantErrString: "nil attestation",
},
{
name: "process nil field (a.Data) in attestation",
a: &ethpb.Attestation{},
s: &pb.BeaconState{},
wantErr: true,
wantErrString: "nil attestation.Data field",
},
{
name: "process nil field (a.Target) in attestation",
a: &ethpb.Attestation{Data: &ethpb.AttestationData{}},
s: &pb.BeaconState{},
wantErr: true,
wantErrString: "nil attestation.Data.Target field",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := service.onAttestation(ctx, tt.a)
if tt.wantErr {
if !strings.Contains(err.Error(), tt.wantErrString) {
if err == nil || !strings.Contains(err.Error(), tt.wantErrString) {
t.Errorf("Store.onAttestation() error = %v, wantErr = %v", err, tt.wantErrString)
}
} else {
Expand Down

0 comments on commit 075d29f

Please sign in to comment.