forked from prysmaticlabs/prysm
/
validate_proposer_slashing.go
67 lines (57 loc) · 1.75 KB
/
validate_proposer_slashing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package sync
import (
"context"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/shared/traceutil"
"go.opencensus.io/trace"
)
// Clients who receive a proposer slashing on this topic MUST validate the conditions within VerifyProposerSlashing before
// forwarding it across the network.
func (r *Service) validateProposerSlashing(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
}
// The head state will be too far away to validate any slashing.
if r.initialSync.Syncing() {
return false
}
ctx, span := trace.StartSpan(ctx, "sync.validateProposerSlashing")
defer span.End()
m, err := r.decodePubsubMessage(msg)
if err != nil {
log.WithError(err).Error("Failed to decode message")
traceutil.AnnotateError(span, err)
return false
}
slashing, ok := m.(*ethpb.ProposerSlashing)
if !ok {
return false
}
// Retrieve head state, advance state to the epoch slot used specified in slashing message.
s, err := r.chain.HeadState(ctx)
if err != nil {
return false
}
slashSlot := slashing.Header_1.Header.Slot
if s.Slot() < slashSlot {
if ctx.Err() != nil {
return false
}
var err error
s, err = state.ProcessSlots(ctx, s, slashSlot)
if err != nil {
return false
}
}
if err := blocks.VerifyProposerSlashing(s, slashing); err != nil {
return false
}
msg.ValidatorData = slashing // Used in downstream subscriber
return true
}