2020-07-03 19:54:42 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-10-15 19:35:31 +00:00
|
|
|
"bytes"
|
2020-07-03 19:54:42 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/blockutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
var failedPreBlockSignLocalErr = "attempted to sign a double proposal, block rejected by local protection"
|
|
|
|
var failedPreBlockSignExternalErr = "attempted a double proposal, block rejected by remote slashing protection"
|
|
|
|
var failedPostBlockSignErr = "made a double proposal, considered slashable by remote slashing protection"
|
|
|
|
|
|
|
|
func (v *validator) preBlockSignValidations(ctx context.Context, pubKey [48]byte, block *ethpb.BeaconBlock) error {
|
|
|
|
fmtKey := fmt.Sprintf("%#x", pubKey[:])
|
2020-10-15 19:35:31 +00:00
|
|
|
signingRoot, err := v.db.ProposalHistoryForSlot(ctx, pubKey[:], block.Slot)
|
2020-10-15 14:52:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if v.emitAccountMetrics {
|
|
|
|
ValidatorProposeFailVec.WithLabelValues(fmtKey).Inc()
|
2020-07-03 19:54:42 +00:00
|
|
|
}
|
2020-10-15 14:52:45 +00:00
|
|
|
return errors.Wrap(err, "failed to get proposal history")
|
|
|
|
}
|
|
|
|
// If the bit for the current slot is marked, do not propose.
|
2020-10-15 19:35:31 +00:00
|
|
|
if !bytes.Equal(signingRoot, params.BeaconConfig().ZeroHash[:]) {
|
2020-10-15 14:52:45 +00:00
|
|
|
if v.emitAccountMetrics {
|
|
|
|
ValidatorProposeFailVec.WithLabelValues(fmtKey).Inc()
|
2020-07-03 19:54:42 +00:00
|
|
|
}
|
2020-10-15 14:52:45 +00:00
|
|
|
return errors.New(failedPreBlockSignLocalErr)
|
2020-07-03 19:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if featureconfig.Get().SlasherProtection && v.protector != nil {
|
|
|
|
blockHdr, err := blockutil.BeaconBlockHeaderFromBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get block header from block")
|
|
|
|
}
|
|
|
|
if !v.protector.CheckBlockSafety(ctx, blockHdr) {
|
|
|
|
if v.emitAccountMetrics {
|
|
|
|
ValidatorProposeFailVecSlasher.WithLabelValues(fmtKey).Inc()
|
|
|
|
}
|
|
|
|
return errors.New(failedPreBlockSignExternalErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:35:31 +00:00
|
|
|
func (v *validator) postBlockSignUpdate(ctx context.Context, pubKey [48]byte, block *ethpb.SignedBeaconBlock, domain *ethpb.DomainResponse) error {
|
2020-07-03 19:54:42 +00:00
|
|
|
fmtKey := fmt.Sprintf("%#x", pubKey[:])
|
|
|
|
if featureconfig.Get().SlasherProtection && v.protector != nil {
|
|
|
|
sbh, err := blockutil.SignedBeaconBlockHeaderFromBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get block header from block")
|
|
|
|
}
|
2020-09-11 14:53:53 +00:00
|
|
|
valid, err := v.protector.CommitBlock(ctx, sbh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !valid {
|
2020-07-03 19:54:42 +00:00
|
|
|
if v.emitAccountMetrics {
|
|
|
|
ValidatorProposeFailVecSlasher.WithLabelValues(fmtKey).Inc()
|
|
|
|
}
|
|
|
|
return fmt.Errorf(failedPostBlockSignErr)
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 19:35:31 +00:00
|
|
|
signingRoot, err := helpers.ComputeSigningRoot(block.Block, domain.SignatureDomain)
|
2020-10-15 14:52:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if v.emitAccountMetrics {
|
|
|
|
ValidatorProposeFailVec.WithLabelValues(fmtKey).Inc()
|
2020-07-03 19:54:42 +00:00
|
|
|
}
|
2020-10-15 19:35:31 +00:00
|
|
|
return errors.Wrap(err, "failed to compute signing root for block")
|
2020-10-15 14:52:45 +00:00
|
|
|
}
|
2020-10-15 19:35:31 +00:00
|
|
|
if err := v.db.SaveProposalHistoryForSlot(ctx, pubKey[:], block.Block.Slot, signingRoot[:]); err != nil {
|
2020-10-15 14:52:45 +00:00
|
|
|
if v.emitAccountMetrics {
|
|
|
|
ValidatorProposeFailVec.WithLabelValues(fmtKey).Inc()
|
2020-07-03 19:54:42 +00:00
|
|
|
}
|
2020-10-15 14:52:45 +00:00
|
|
|
return errors.Wrap(err, "failed to save updated proposal history")
|
2020-07-03 19:54:42 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|