From 5d8b5a3c3064e53939276b5c2ea1b983c8675c8c Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 18 May 2020 03:36:37 -0400 Subject: [PATCH] Remove DB check for receiving p2p slashings (#5892) * Removes DB check for receiving p2p slashings * Fix bad logic * Merge refs/heads/master into p2p-remove-db-check * Whoops * Merge branch 'p2p-remove-db-check' of github.com:prysmaticlabs/prysm into p2p-remove-db-check --- beacon-chain/sync/subscriber_handlers.go | 33 ++++++++++-------------- beacon-chain/sync/subscriber_test.go | 7 +---- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/beacon-chain/sync/subscriber_handlers.go b/beacon-chain/sync/subscriber_handlers.go index 55cf14f2dc7..cc54f0ade6a 100644 --- a/beacon-chain/sync/subscriber_handlers.go +++ b/beacon-chain/sync/subscriber_handlers.go @@ -7,8 +7,6 @@ import ( "github.com/gogo/protobuf/proto" "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" - "github.com/prysmaticlabs/prysm/shared/bytesutil" ) func (r *Service) voluntaryExitSubscriber(ctx context.Context, msg proto.Message) error { @@ -31,46 +29,43 @@ func (r *Service) voluntaryExitSubscriber(ctx context.Context, msg proto.Message } func (r *Service) attesterSlashingSubscriber(ctx context.Context, msg proto.Message) error { - as, ok := msg.(*ethpb.AttesterSlashing) + aSlashing, ok := msg.(*ethpb.AttesterSlashing) if !ok { return fmt.Errorf("wrong type, expected: *ethpb.AttesterSlashing got: %T", msg) } // Do some nil checks to prevent easy DoS'ing of this handler. - if as != nil && as.Attestation_1 != nil && as.Attestation_1.Data != nil { - s, err := r.db.State(ctx, bytesutil.ToBytes32(as.Attestation_1.Data.BeaconBlockRoot)) + aSlashing1IsNil := aSlashing == nil || aSlashing.Attestation_1 == nil || aSlashing.Attestation_1.AttestingIndices == nil + aSlashing2IsNil := aSlashing == nil || aSlashing.Attestation_2 == nil || aSlashing.Attestation_2.AttestingIndices == nil + if !aSlashing1IsNil && !aSlashing2IsNil { + headState, err := r.chain.HeadState(ctx) if err != nil { return err } - if s == nil { - return fmt.Errorf("no state found for block root %#x", as.Attestation_1.Data.BeaconBlockRoot) - } - if err := r.slashingPool.InsertAttesterSlashing(ctx, s, as); err != nil { + if err := r.slashingPool.InsertAttesterSlashing(ctx, headState, aSlashing); err != nil { return errors.Wrap(err, "could not insert attester slashing into pool") } - r.setAttesterSlashingIndicesSeen(as.Attestation_1.AttestingIndices, as.Attestation_2.AttestingIndices) + r.setAttesterSlashingIndicesSeen(aSlashing.Attestation_1.AttestingIndices, aSlashing.Attestation_2.AttestingIndices) } return nil } func (r *Service) proposerSlashingSubscriber(ctx context.Context, msg proto.Message) error { - ps, ok := msg.(*ethpb.ProposerSlashing) + pSlashing, ok := msg.(*ethpb.ProposerSlashing) if !ok { return fmt.Errorf("wrong type, expected: *ethpb.ProposerSlashing got: %T", msg) } // Do some nil checks to prevent easy DoS'ing of this handler. - if ps.Header_1 != nil && ps.Header_1.Header != nil { - root, err := stateutil.BlockHeaderRoot(ps.Header_1.Header) - s, err := r.db.State(ctx, root) + header1IsNil := pSlashing == nil || pSlashing.Header_1 == nil || pSlashing.Header_1.Header == nil + header2IsNil := pSlashing == nil || pSlashing.Header_2 == nil || pSlashing.Header_2.Header == nil + if !header1IsNil && !header2IsNil { + headState, err := r.chain.HeadState(ctx) if err != nil { return err } - if s == nil { - return fmt.Errorf("no state found for block root %#x", root) - } - if err := r.slashingPool.InsertProposerSlashing(ctx, s, ps); err != nil { + if err := r.slashingPool.InsertProposerSlashing(ctx, headState, pSlashing); err != nil { return errors.Wrap(err, "could not insert proposer slashing into pool") } - r.setProposerSlashingIndexSeen(ps.Header_1.Header.ProposerIndex) + r.setProposerSlashingIndexSeen(pSlashing.Header_1.Header.ProposerIndex) } return nil } diff --git a/beacon-chain/sync/subscriber_test.go b/beacon-chain/sync/subscriber_test.go index 1045661a5e4..226267bb186 100644 --- a/beacon-chain/sync/subscriber_test.go +++ b/beacon-chain/sync/subscriber_test.go @@ -19,7 +19,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/operations/slashings" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -175,10 +174,6 @@ func TestSubscribe_ReceivesProposerSlashing(t *testing.T) { if err != nil { t.Fatalf("Error generating proposer slashing") } - root, err := stateutil.BlockHeaderRoot(proposerSlashing.Header_1.Header) - if err := r.db.SaveState(ctx, beaconState, root); err != nil { - t.Fatal(err) - } p2p.Digest, err = r.forkDigest() if err != nil { t.Fatal(err) @@ -190,7 +185,7 @@ func TestSubscribe_ReceivesProposerSlashing(t *testing.T) { } ps := r.slashingPool.PendingProposerSlashings(ctx, beaconState) if len(ps) != 1 { - t.Errorf("Expected proposer slashing: %v to be added to slashing pool. got: %v", proposerSlashing, ps[0]) + t.Errorf("Expected proposer slashing: %v to be added to slashing pool. got: %v", proposerSlashing, ps) } }