Skip to content

Commit

Permalink
Refactor on_attestation return signature (#8310)
Browse files Browse the repository at this point in the history
* Update func on_attestation's return signature

* Add return
  • Loading branch information
terencechain authored Jan 21, 2021
1 parent 9cc1438 commit 2586be2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
18 changes: 9 additions & 9 deletions beacon-chain/blockchain/process_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ var ErrTargetRootNotInDB = errors.New("target root does not exist in db")
//
// # Update latest messages for attesting indices
// update_latest_messages(store, indexed_attestation.attesting_indices, attestation)
func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) ([]uint64, error) {
func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) error {
ctx, span := trace.StartSpan(ctx, "blockChain.onAttestation")
defer span.End()

if err := helpers.ValidateNilAttestation(a); err != nil {
return nil, err
return err
}
if err := helpers.ValidateSlotTargetEpoch(a.Data); err != nil {
return nil, err
return err
}
tgt := stateTrie.CopyCheckpoint(a.Data.Target)

Expand All @@ -59,33 +59,33 @@ func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) ([]ui
// save it to the cache.
baseState, err := s.getAttPreState(ctx, tgt)
if err != nil {
return nil, err
return err
}

genesisTime := baseState.GenesisTime()

// Verify attestation target is from current epoch or previous epoch.
if err := s.verifyAttTargetEpoch(ctx, genesisTime, uint64(timeutils.Now().Unix()), tgt); err != nil {
return nil, err
return err
}

// Verify attestation beacon block is known and not from the future.
if err := s.verifyBeaconBlock(ctx, a.Data); err != nil {
return nil, errors.Wrap(err, "could not verify attestation beacon block")
return errors.Wrap(err, "could not verify attestation beacon block")
}

// Note that LMG GHOST and FFG consistency check is ignored because it was performed in sync's validation pipeline:
// validate_aggregate_proof.go and validate_beacon_attestation.go

// Verify attestations can only affect the fork choice of subsequent slots.
if err := helpers.VerifySlotTime(genesisTime, a.Data.Slot+1, params.BeaconNetworkConfig().MaximumGossipClockDisparity); err != nil {
return nil, err
return err
}

// Use the target state to validate attestation and calculate the committees.
indexedAtt, err := s.verifyAttestationIndices(ctx, baseState, a)
if err != nil {
return nil, err
return err
}

// Note that signature verification is ignored here because it was performed in sync's validation pipeline:
Expand All @@ -95,5 +95,5 @@ func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) ([]ui
// 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)

return indexedAtt.AttestingIndices, nil
return nil
}
8 changes: 2 additions & 6 deletions beacon-chain/blockchain/process_attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestStore_OnAttestation_ErrorConditions(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := service.onAttestation(ctx, tt.a)
err := service.onAttestation(ctx, tt.a)
if tt.wantedErr != "" {
assert.ErrorContains(t, tt.wantedErr, err)
} else {
Expand Down Expand Up @@ -146,11 +146,7 @@ func TestStore_OnAttestation_Ok(t *testing.T) {
require.NoError(t, err)
require.NoError(t, service.beaconDB.SaveState(ctx, copied, tRoot))
require.NoError(t, service.forkChoiceStore.ProcessBlock(ctx, 0, tRoot, tRoot, tRoot, 1, 1))
indices, err := service.onAttestation(ctx, att[0])
require.NoError(t, err)
wanted, err := helpers.BeaconCommitteeFromState(copied, 0, 0)
require.NoError(t, err)
require.DeepEqual(t, wanted, indices)
require.NoError(t, service.onAttestation(ctx, att[0]))
}

func TestStore_SaveCheckpointState(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/blockchain/receive_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func (s *Service) ReceiveAttestationNoPubsub(ctx context.Context, att *ethpb.Att
ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveAttestationNoPubsub")
defer span.End()

_, err := s.onAttestation(ctx, att)
if err != nil {
if err := s.onAttestation(ctx, att); err != nil {
return errors.Wrap(err, "could not process attestation")
}

Expand Down

0 comments on commit 2586be2

Please sign in to comment.