2019-08-22 14:31:56 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/karlseguin/ccache"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2019-08-23 17:48:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2019-08-22 14:31:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// seenProposerSlashings represents a cache of all the seen slashings
|
|
|
|
var seenProposerSlashings = ccache.New(ccache.Configure())
|
|
|
|
|
|
|
|
func propSlashingCacheKey(slashing *ethpb.ProposerSlashing) (string, error) {
|
|
|
|
hash, err := hashutil.HashProto(slashing)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(hash[:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clients who receive a proposer slashing on this topic MUST validate the conditions within VerifyProposerSlashing before
|
|
|
|
// forwarding it across the network.
|
2019-09-04 00:22:15 +00:00
|
|
|
func (r *RegularSync) validateProposerSlashing(ctx context.Context, msg proto.Message, p p2p.Broadcaster, fromSelf bool) bool {
|
2019-09-20 17:54:32 +00:00
|
|
|
// The head state will be too far away to validate any slashing.
|
|
|
|
if r.initialSync.Syncing() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-22 14:31:56 +00:00
|
|
|
slashing, ok := msg.(*ethpb.ProposerSlashing)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cacheKey, err := propSlashingCacheKey(slashing)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Warn("could not hash proposer slashing")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidKey := invalid + cacheKey
|
|
|
|
if seenProposerSlashings.Get(invalidKey) != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if seenProposerSlashings.Get(cacheKey) != nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-08-23 17:48:40 +00:00
|
|
|
|
|
|
|
// Retrieve head state, advance state to the epoch slot used specified in slashing message.
|
|
|
|
s := r.chain.HeadState()
|
|
|
|
slashSlot := slashing.Header_1.Slot
|
|
|
|
if s.Slot < slashSlot {
|
2019-09-20 17:54:32 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
log.WithError(ctx.Err()).Errorf("Failed to advance state to slot %d to process proposer slashing", slashSlot)
|
|
|
|
return false
|
|
|
|
}
|
2019-08-23 17:48:40 +00:00
|
|
|
var err error
|
|
|
|
s, err = state.ProcessSlots(ctx, s, slashSlot)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("Failed to advance state to slot %d", slashSlot)
|
|
|
|
return false
|
|
|
|
}
|
2019-08-22 14:31:56 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
if err := blocks.VerifyProposerSlashing(s, slashing); err != nil {
|
2019-08-22 14:31:56 +00:00
|
|
|
log.WithError(err).Warn("Received invalid proposer slashing")
|
|
|
|
seenProposerSlashings.Set(invalidKey, true /*value*/, oneYear /*TTL*/)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
seenProposerSlashings.Set(cacheKey, true /*value*/, oneYear /*TTL*/)
|
|
|
|
|
2019-09-04 00:22:15 +00:00
|
|
|
if fromSelf {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-22 14:31:56 +00:00
|
|
|
if err := p.Broadcast(ctx, slashing); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to propagate proposer slashing")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|