-
Notifications
You must be signed in to change notification settings - Fork 962
/
process_slashings.go
95 lines (88 loc) · 3.25 KB
/
process_slashings.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package slasher
import (
"context"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
)
// Verifies attester slashings, logs them, and submits them to the slashing operations pool
// in the beacon node if they pass validation.
func (s *Service) processAttesterSlashings(ctx context.Context, slashings []*ethpb.AttesterSlashing) error {
var beaconState state.BeaconState
var err error
if len(slashings) > 0 {
beaconState, err = s.serviceCfg.HeadStateFetcher.HeadState(ctx)
if err != nil {
return err
}
}
for _, sl := range slashings {
if err := s.verifyAttSignature(ctx, sl.Attestation_1); err != nil {
log.WithError(err).WithField("a", sl.Attestation_1).Warn(
"Invalid signature for attestation in detected slashing offense",
)
continue
}
if err := s.verifyAttSignature(ctx, sl.Attestation_2); err != nil {
log.WithError(err).WithField("b", sl.Attestation_2).Warn(
"Invalid signature for attestation in detected slashing offense",
)
continue
}
// Log the slashing event and insert into the beacon node's operations pool.
logAttesterSlashing(sl)
if err := s.serviceCfg.SlashingPoolInserter.InsertAttesterSlashing(
ctx, beaconState, sl,
); err != nil {
log.WithError(err).Error("Could not insert attester slashing into operations pool")
}
}
return nil
}
// Verifies proposer slashings, logs them, and submits them to the slashing operations pool
// in the beacon node if they pass validation.
func (s *Service) processProposerSlashings(ctx context.Context, slashings []*ethpb.ProposerSlashing) error {
var beaconState state.BeaconState
var err error
if len(slashings) > 0 {
beaconState, err = s.serviceCfg.HeadStateFetcher.HeadState(ctx)
if err != nil {
return err
}
}
for _, sl := range slashings {
if err := s.verifyBlockSignature(ctx, sl.Header_1); err != nil {
log.WithError(err).WithField("a", sl.Header_1).Warn(
"Invalid signature for block header in detected slashing offense",
)
continue
}
if err := s.verifyBlockSignature(ctx, sl.Header_2); err != nil {
log.WithError(err).WithField("b", sl.Header_2).Warn(
"Invalid signature for block header in detected slashing offense",
)
continue
}
// Log the slashing event and insert into the beacon node's operations pool.
logProposerSlashing(sl)
if err := s.serviceCfg.SlashingPoolInserter.InsertProposerSlashing(ctx, beaconState, sl); err != nil {
log.WithError(err).Error("Could not insert proposer slashing into operations pool")
}
}
return nil
}
func (s *Service) verifyBlockSignature(ctx context.Context, header *ethpb.SignedBeaconBlockHeader) error {
parentState, err := s.serviceCfg.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(header.Header.ParentRoot))
if err != nil {
return err
}
return blocks.VerifyBlockHeaderSignature(parentState, header)
}
func (s *Service) verifyAttSignature(ctx context.Context, att *ethpb.IndexedAttestation) error {
preState, err := s.serviceCfg.AttestationStateFetcher.AttestationTargetState(ctx, att.Data.Target)
if err != nil {
return err
}
return blocks.VerifyIndexedAttestation(ctx, preState, att)
}