diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 95e9e558a..e6db77c03 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -127,7 +127,7 @@ func (s *Service) onBlock(ctx context.Context, signed interfaces.SignedBeaconBlo if err := s.insertBlockAndAttestationsToForkChoiceStore(ctx, signed.Block(), blockRoot, postState); err != nil { return errors.Wrapf(err, "could not insert block %d to fork choice store", signed.Block().Slot()) } - s.insertSlashingsToForkChoiceStore(ctx, signed.Block()) + s.insertSlashingsToForkChoiceStore(ctx, signed.Block().Body().AttesterSlashings()) if isValidPayload { if err := s.cfg.ForkChoiceStore.SetOptimisticToValid(ctx, blockRoot); err != nil { return errors.Wrap(err, "could not set optimistic block to valid") @@ -576,8 +576,7 @@ func (s *Service) insertBlockToForkChoiceStore(ctx context.Context, blk interfac // Inserts attester slashing indices to fork choice store. // To call this function, it's caller's responsibility to ensure the slashing object is valid. -func (s *Service) insertSlashingsToForkChoiceStore(ctx context.Context, blk interfaces.BeaconBlock) { - slashings := blk.Body().AttesterSlashings() +func (s *Service) insertSlashingsToForkChoiceStore(ctx context.Context, slashings []*ethpb.AttesterSlashing) { for _, slashing := range slashings { indices := blocks.SlashableAttesterIndices(slashing) for _, index := range indices { diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index 60ce80cc0..9134933a6 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -1885,5 +1885,5 @@ func TestService_insertSlashingsToForkChoiceStore(t *testing.T) { b.Block.Body.AttesterSlashings = slashings wb, err := wrapper.WrappedSignedBeaconBlock(b) require.NoError(t, err) - service.insertSlashingsToForkChoiceStore(ctx, wb.Block()) + service.insertSlashingsToForkChoiceStore(ctx, wb.Block().Body().AttesterSlashings()) } diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 85e2530ac..fd2dcb52c 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/prysm/consensus-types/interfaces" types "github.com/prysmaticlabs/prysm/consensus-types/primitives" "github.com/prysmaticlabs/prysm/monitoring/tracing" + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/time" "github.com/prysmaticlabs/prysm/time/slots" "go.opencensus.io/trace" @@ -24,6 +25,11 @@ type BlockReceiver interface { HasBlock(ctx context.Context, root [32]byte) bool } +// SlashingReceiver interface defines the methods of chain service for receiving validated slashing over the wire. +type SlashingReceiver interface { + ReceiveAttesterSlashing(ctx context.Context, slashings *ethpb.AttesterSlashing) +} + // ReceiveBlock is a function that defines the the operations (minus pubsub) // that are performed on blocks that is received from regular sync service. The operations consists of: // 1. Validate block, apply state transition and update check points @@ -133,6 +139,11 @@ func (s *Service) HasBlock(ctx context.Context, root [32]byte) bool { return s.hasBlockInInitSyncOrDB(ctx, root) } +// ReceiveAttesterSlashing receives an attester slashing and inserts it to forkchoice +func (s *Service) ReceiveAttesterSlashing(ctx context.Context, slashing *ethpb.AttesterSlashing) { + s.insertSlashingsToForkChoiceStore(ctx, []*ethpb.AttesterSlashing{slashing}) +} + func (s *Service) handlePostBlockOperations(b interfaces.BeaconBlock) error { // Delete the processed block attestations from attestation pool. if err := s.deletePoolAtts(b.Body().Attestations()); err != nil { diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index 38c71ae51..5518e9016 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -450,3 +450,7 @@ func (s *ChainService) IsOptimistic(_ context.Context) (bool, error) { func (s *ChainService) IsOptimisticForRoot(_ context.Context, _ [32]byte) (bool, error) { return s.Optimistic, nil } + +// ReceiveAttesterSlashing mocks the same method in the chain service. +func (s *ChainService) ReceiveAttesterSlashing(context.Context, *ethpb.AttesterSlashing) { +} diff --git a/beacon-chain/sync/service.go b/beacon-chain/sync/service.go index 8fc5bb9e0..6fafec4da 100644 --- a/beacon-chain/sync/service.go +++ b/beacon-chain/sync/service.go @@ -95,6 +95,7 @@ type blockchainService interface { blockchain.TimeFetcher blockchain.GenesisFetcher blockchain.CanonicalFetcher + blockchain.SlashingReceiver } // Service is responsible for handling all run time p2p related operations as the diff --git a/beacon-chain/sync/validate_attester_slashing.go b/beacon-chain/sync/validate_attester_slashing.go index faae937e4..97c530710 100644 --- a/beacon-chain/sync/validate_attester_slashing.go +++ b/beacon-chain/sync/validate_attester_slashing.go @@ -64,6 +64,8 @@ func (s *Service) validateAttesterSlashing(ctx context.Context, pid peer.ID, msg return pubsub.ValidationReject, err } + s.cfg.chain.ReceiveAttesterSlashing(ctx, slashing) + msg.ValidatorData = slashing // Used in downstream subscriber return pubsub.ValidationAccept, nil }