prysm-pulse/beacon-chain/sync/subscriber_handlers.go
shayzluf 035eaffd9d
handle slashing from p2p (#5047)
* handle slashing from p2p

* gaz

* remove unneeded check

* add tests

* gaz  goimports

* text update

* Apply suggestions from code review

* add proto.equal

* fix test

* add context to call

* fix state bug found by terence

* fix tests add error type handling

* nil checks

* nil head state check

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2020-03-13 16:47:27 -05:00

64 lines
1.9 KiB
Go

package sync
import (
"context"
"fmt"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
)
func (r *Service) voluntaryExitSubscriber(ctx context.Context, msg proto.Message) error {
ve, ok := msg.(*ethpb.SignedVoluntaryExit)
if !ok {
return fmt.Errorf("wrong type, expected: *ethpb.SignedVoluntaryExit got: %T", msg)
}
s, err := r.chain.HeadState(ctx)
if err != nil {
return err
}
r.exitPool.InsertVoluntaryExit(ctx, s, ve)
return nil
}
func (r *Service) attesterSlashingSubscriber(ctx context.Context, msg proto.Message) error {
as, 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))
if err != nil {
return err
}
if s == nil {
return fmt.Errorf("no state found for block root %#x", as.Attestation_1.Data.BeaconBlockRoot)
}
return r.slashingPool.InsertAttesterSlashing(ctx, s, as)
}
return nil
}
func (r *Service) proposerSlashingSubscriber(ctx context.Context, msg proto.Message) error {
ps, 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 := ssz.HashTreeRoot(ps.Header_1.Header)
s, err := r.db.State(ctx, root)
if err != nil {
return err
}
if s == nil {
return fmt.Errorf("no state found for block root %#x", root)
}
return r.slashingPool.InsertProposerSlashing(ctx, s, ps)
}
return nil
}