Refactor on_attestation return signature (#8310)

* Update func on_attestation's return signature

* Add return
This commit is contained in:
terence tsao 2021-01-21 10:38:51 -08:00 committed by GitHub
parent 9cc1438ea9
commit 2586be29ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 17 deletions

View File

@ -39,15 +39,15 @@ var ErrTargetRootNotInDB = errors.New("target root does not exist in db")
// //
// # Update latest messages for attesting indices // # Update latest messages for attesting indices
// update_latest_messages(store, indexed_attestation.attesting_indices, attestation) // 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") ctx, span := trace.StartSpan(ctx, "blockChain.onAttestation")
defer span.End() defer span.End()
if err := helpers.ValidateNilAttestation(a); err != nil { if err := helpers.ValidateNilAttestation(a); err != nil {
return nil, err return err
} }
if err := helpers.ValidateSlotTargetEpoch(a.Data); err != nil { if err := helpers.ValidateSlotTargetEpoch(a.Data); err != nil {
return nil, err return err
} }
tgt := stateTrie.CopyCheckpoint(a.Data.Target) tgt := stateTrie.CopyCheckpoint(a.Data.Target)
@ -59,19 +59,19 @@ func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) ([]ui
// save it to the cache. // save it to the cache.
baseState, err := s.getAttPreState(ctx, tgt) baseState, err := s.getAttPreState(ctx, tgt)
if err != nil { if err != nil {
return nil, err return err
} }
genesisTime := baseState.GenesisTime() genesisTime := baseState.GenesisTime()
// Verify attestation target is from current epoch or previous epoch. // Verify attestation target is from current epoch or previous epoch.
if err := s.verifyAttTargetEpoch(ctx, genesisTime, uint64(timeutils.Now().Unix()), tgt); err != nil { 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. // Verify attestation beacon block is known and not from the future.
if err := s.verifyBeaconBlock(ctx, a.Data); err != nil { 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: // Note that LMG GHOST and FFG consistency check is ignored because it was performed in sync's validation pipeline:
@ -79,13 +79,13 @@ func (s *Service) onAttestation(ctx context.Context, a *ethpb.Attestation) ([]ui
// Verify attestations can only affect the fork choice of subsequent slots. // 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 { 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. // Use the target state to validate attestation and calculate the committees.
indexedAtt, err := s.verifyAttestationIndices(ctx, baseState, a) indexedAtt, err := s.verifyAttestationIndices(ctx, baseState, a)
if err != nil { if err != nil {
return nil, err return err
} }
// Note that signature verification is ignored here because it was performed in sync's validation pipeline: // Note that signature verification is ignored here because it was performed in sync's validation pipeline:
@ -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. // 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) s.forkChoiceStore.ProcessAttestation(ctx, indexedAtt.AttestingIndices, bytesutil.ToBytes32(a.Data.BeaconBlockRoot), a.Data.Target.Epoch)
return indexedAtt.AttestingIndices, nil return nil
} }

View File

@ -114,7 +114,7 @@ func TestStore_OnAttestation_ErrorConditions(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
_, err := service.onAttestation(ctx, tt.a) err := service.onAttestation(ctx, tt.a)
if tt.wantedErr != "" { if tt.wantedErr != "" {
assert.ErrorContains(t, tt.wantedErr, err) assert.ErrorContains(t, tt.wantedErr, err)
} else { } else {
@ -146,11 +146,7 @@ func TestStore_OnAttestation_Ok(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, service.beaconDB.SaveState(ctx, copied, tRoot)) require.NoError(t, service.beaconDB.SaveState(ctx, copied, tRoot))
require.NoError(t, service.forkChoiceStore.ProcessBlock(ctx, 0, tRoot, tRoot, tRoot, 1, 1)) require.NoError(t, service.forkChoiceStore.ProcessBlock(ctx, 0, tRoot, tRoot, tRoot, 1, 1))
indices, err := service.onAttestation(ctx, att[0]) require.NoError(t, 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)
} }
func TestStore_SaveCheckpointState(t *testing.T) { func TestStore_SaveCheckpointState(t *testing.T) {

View File

@ -35,8 +35,7 @@ func (s *Service) ReceiveAttestationNoPubsub(ctx context.Context, att *ethpb.Att
ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveAttestationNoPubsub") ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveAttestationNoPubsub")
defer span.End() defer span.End()
_, err := s.onAttestation(ctx, att) if err := s.onAttestation(ctx, att); err != nil {
if err != nil {
return errors.Wrap(err, "could not process attestation") return errors.Wrap(err, "could not process attestation")
} }