prysm-pulse/beacon-chain/sync/validate_proposer_slashing.go
Preston Van Loon 49a0d3caf0
Refactor dependencies, make Prysm "go gettable" (#6053)
* Fix a few deps to work with go.mod, check in generated files

* Update Gossipsub to 1.1 (#5998)

* update libs

* add new validators

* add new deps

* new set of deps

* tls

* further fix gossip update

* get everything to build

* clean up

* gaz

* fix build

* fix all tests

* add deps to images

* imports

Co-authored-by: rauljordan <raul@prysmaticlabs.com>

* Beacon chain builds with go build

* fix bazel

* fix dep

* lint

* Add github action for testing go

* on PR for any branch

* fix libp2p test failure

* Fix TestProcessBlock_PassesProcessingConditions by updating the proposer index in test

* Revert "Fix TestProcessBlock_PassesProcessingConditions by updating the proposer index in test"

This reverts commit 43676894ab01f03fe90a9b8ee3ecfbc2ec1ec4e4.

* Compute and set proposer index instead of hard code

* Add back go mod/sum, fix deps

* go build ./...

* Temporarily skip two tests

* Fix kafka confluent patch

* Fix kafka confluent patch

* fix kafka build

* fix kafka

* Add info in DEPENDENCIES. Added a stub link for Why Bazel? until https://github.com/prysmaticlabs/documentation/issues/138

* Update fuzz ssz files as well

* Update fuzz ssz files as well

* getting closer

* rollback rules_go and gazelle

* fix gogo protobuf

* install librdkafka-dev as part of github actions

* Update kafka to a recent version where librkafkfa is not required for go modules

* clarify comment

* fix kafka build

* disable go tests

* comment

* Fix geth dependencies for end to end

* rename word

* lint

* fix docker

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: rauljordan <raul@prysmaticlabs.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-05-31 14:44:34 +08:00

90 lines
2.7 KiB
Go

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) pubsub.ValidationResult {
// Validation runs on publish (not just subscriptions), so we should approve any message from
// ourselves.
if pid == r.p2p.PeerID() {
return pubsub.ValidationAccept
}
// The head state will be too far away to validate any slashing.
if r.initialSync.Syncing() {
return pubsub.ValidationIgnore
}
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 pubsub.ValidationReject
}
slashing, ok := m.(*ethpb.ProposerSlashing)
if !ok {
return pubsub.ValidationReject
}
if slashing.Header_1 == nil || slashing.Header_1.Header == nil {
return pubsub.ValidationReject
}
if r.hasSeenProposerSlashingIndex(slashing.Header_1.Header.ProposerIndex) {
return pubsub.ValidationIgnore
}
// Retrieve head state, advance state to the epoch slot used specified in slashing message.
s, err := r.chain.HeadState(ctx)
if err != nil {
return pubsub.ValidationIgnore
}
slashSlot := slashing.Header_1.Header.Slot
if s.Slot() < slashSlot {
if ctx.Err() != nil {
return pubsub.ValidationIgnore
}
var err error
s, err = state.ProcessSlots(ctx, s, slashSlot)
if err != nil {
return pubsub.ValidationIgnore
}
}
if err := blocks.VerifyProposerSlashing(s, slashing); err != nil {
return pubsub.ValidationReject
}
msg.ValidatorData = slashing // Used in downstream subscriber
return pubsub.ValidationAccept
}
// Returns true if the node has already received a valid proposer slashing received for the proposer with index
func (r *Service) hasSeenProposerSlashingIndex(i uint64) bool {
r.seenProposerSlashingLock.RLock()
defer r.seenProposerSlashingLock.RUnlock()
_, seen := r.seenProposerSlashingCache.Get(i)
return seen
}
// Set proposer slashing index in proposer slashing cache.
func (r *Service) setProposerSlashingIndexSeen(i uint64) {
r.seenProposerSlashingLock.Lock()
defer r.seenProposerSlashingLock.Unlock()
r.seenProposerSlashingCache.Add(i, true)
}