Skip to content

Commit

Permalink
Remove DB check for receiving p2p slashings (#5892)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
0xKiwi committed May 18, 2020
1 parent 5bb9278 commit 5d8b5a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
33 changes: 14 additions & 19 deletions beacon-chain/sync/subscriber_handlers.go
Expand Up @@ -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 {
Expand All @@ -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
}
7 changes: 1 addition & 6 deletions beacon-chain/sync/subscriber_test.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 5d8b5a3

Please sign in to comment.