2019-08-22 05:34:25 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-08-22 05:34:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2019-08-23 17:48:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-12-20 03:18:08 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/traceutil"
|
2019-12-03 19:15:01 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-08-22 05:34:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Clients who receive an attester slashing on this topic MUST validate the conditions within VerifyAttesterSlashing before
|
|
|
|
// forwarding it across the network.
|
2019-12-20 03:18:08 +00:00
|
|
|
func (r *Service) validateAttesterSlashing(ctx context.Context, pid peer.ID, msg *pubsub.Message) bool {
|
|
|
|
// Validation runs on publish (not just subscriptions), so we should approve any message from
|
|
|
|
// ourselves.
|
|
|
|
if pid == r.p2p.PeerID() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
// The head state will be too far away to validate any slashing.
|
|
|
|
if r.initialSync.Syncing() {
|
2019-12-20 03:18:08 +00:00
|
|
|
return false
|
2019-09-20 17:54:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 19:15:01 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "sync.validateAttesterSlashing")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
m, err := r.decodePubsubMessage(msg)
|
2019-08-22 05:34:25 +00:00
|
|
|
if err != nil {
|
2019-12-20 03:18:08 +00:00
|
|
|
log.WithError(err).Error("Failed to decode message")
|
|
|
|
traceutil.AnnotateError(span, err)
|
|
|
|
return false
|
2019-08-22 05:34:25 +00:00
|
|
|
}
|
2019-12-20 03:18:08 +00:00
|
|
|
slashing, ok := m.(*ethpb.AttesterSlashing)
|
|
|
|
if !ok {
|
|
|
|
return false
|
2019-08-22 05:34:25 +00:00
|
|
|
}
|
2019-08-23 17:48:40 +00:00
|
|
|
|
|
|
|
// Retrieve head state, advance state to the epoch slot used specified in slashing message.
|
2019-11-19 16:12:50 +00:00
|
|
|
s, err := r.chain.HeadState(ctx)
|
|
|
|
if err != nil {
|
2019-12-20 03:18:08 +00:00
|
|
|
return false
|
2019-11-19 16:12:50 +00:00
|
|
|
}
|
2019-08-23 17:48:40 +00:00
|
|
|
slashSlot := slashing.Attestation_1.Data.Target.Epoch * params.BeaconConfig().SlotsPerEpoch
|
|
|
|
if s.Slot < slashSlot {
|
2019-09-20 17:54:32 +00:00
|
|
|
if ctx.Err() != nil {
|
2019-12-20 03:18:08 +00:00
|
|
|
return false
|
2019-09-20 17:54:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
var err error
|
|
|
|
s, err = state.ProcessSlots(ctx, s, slashSlot)
|
|
|
|
if err != nil {
|
2019-12-20 03:18:08 +00:00
|
|
|
return false
|
2019-08-23 17:48:40 +00:00
|
|
|
}
|
2019-08-22 05:34:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 22:42:17 +00:00
|
|
|
if err := blocks.VerifyAttesterSlashing(ctx, s, slashing); err != nil {
|
2019-12-20 03:18:08 +00:00
|
|
|
return false
|
2019-09-04 00:22:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
msg.ValidatorData = slashing // Used in downstream subscriber
|
|
|
|
return true
|
2019-08-22 05:34:25 +00:00
|
|
|
}
|