prysm-pulse/beacon-chain/sync/validate_proposer_slashing.go
Raul Jordan 0326be86b5 Apply Patch Rules to Use EthereumAPIs Generated Protos in Prysm (#4112)
* starting on patch
* finish determining all required patches
* properly redefine the patch rules
* new patch
* rem double semicolon
* fix patch file
* Merge branch 'master' of github.com:prysmaticlabs/prysm into deprecate-eth-protos
* building the deps
* test target passes using ethereumapis
* compile gateway
* attempting to build everything
* e2e use ethereumapis
* more fixes for slasher
* other item
* getting closer to compiling slasher
* build slasher package
* Merge branch 'master' into deprecate-eth-protos
* Merge branch 'master' into deprecate-eth-protos
* fix benches
* lint gazelle
* Merge branch 'deprecate-eth-protos' of github.com:prysmaticlabs/prysm into deprecate-eth-protos
* proper gateway
* lint
* Merge branch 'master' into deprecate-eth-protos
* fix build
* Merge branch 'deprecate-eth-protos' of github.com:prysmaticlabs/prysm into deprecate-eth-protos
* use swag
* resolve
* ignore change
* include new patch changes
* fix test
* builds
* fix e2e
* gaz
2019-11-27 05:08:18 +00:00

85 lines
2.6 KiB
Go

package sync
import (
"context"
"github.com/gogo/protobuf/proto"
"github.com/karlseguin/ccache"
"github.com/pkg/errors"
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/beacon-chain/p2p"
"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.
func (r *RegularSync) validateProposerSlashing(ctx context.Context, msg proto.Message, p p2p.Broadcaster, fromSelf bool) (bool, error) {
// The head state will be too far away to validate any slashing.
if r.initialSync.Syncing() {
return false, nil
}
slashing, ok := msg.(*ethpb.ProposerSlashing)
if !ok {
return false, nil
}
cacheKey, err := propSlashingCacheKey(slashing)
if err != nil {
return false, errors.Wrapf(err, "could not hash proposer slashing")
}
invalidKey := invalid + cacheKey
if seenProposerSlashings.Get(invalidKey) != nil {
return false, errors.New("previously seen invalid proposer slashing received")
}
if seenProposerSlashings.Get(cacheKey) != nil {
return false, nil
}
// 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, errors.Wrap(err, "Could not get head state")
}
slashSlot := slashing.Header_1.Slot
if s.Slot < slashSlot {
if ctx.Err() != nil {
return false, errors.Wrapf(ctx.Err(),
"Failed to advance state to slot %d to process proposer slashing", slashSlot)
}
var err error
s, err = state.ProcessSlots(ctx, s, slashSlot)
if err != nil {
return false, errors.Wrapf(err, "Failed to advance state to slot %d", slashSlot)
}
}
if err := blocks.VerifyProposerSlashing(s, slashing); err != nil {
seenProposerSlashings.Set(invalidKey, true /*value*/, oneYear /*TTL*/)
return false, errors.Wrap(err, "Received invalid proposer slashing")
}
seenProposerSlashings.Set(cacheKey, true /*value*/, oneYear /*TTL*/)
if fromSelf {
return false, nil
}
if err := p.Broadcast(ctx, slashing); err != nil {
log.WithError(err).Error("Failed to propagate proposer slashing")
}
return true, nil
}